Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
924 | dev | 1 | package ak.hostadmiral.core.model; |
919 | dev | 2 | |
3 | import java.util.*; |
||
4 | import net.sf.hibernate.*; |
||
924 | dev | 5 | import ak.hostadmiral.util.HibernateUtil; |
6 | import ak.hostadmiral.util.ModelException; |
||
7 | import ak.hostadmiral.util.ModelSecurityException; |
||
919 | dev | 8 | |
9 | public class MailAliasDestinationManager |
||
10 | { |
||
11 | private static boolean registered = false; |
||
12 | protected static void register() |
||
13 | { |
||
14 | synchronized(MailAliasDestinationManager.class) { |
||
15 | if(registered) return; |
||
16 | |||
17 | registered = true; |
||
18 | try { |
||
19 | HibernateUtil.getConfiguration().addResource( |
||
924 | dev | 20 | "/ak/hostadmiral/core/model/MailAliasDestination.hbm.xml"); |
919 | dev | 21 | } |
22 | catch(Exception ex) { |
||
23 | ex.printStackTrace(); |
||
24 | throw new RuntimeException(ex.getMessage()); |
||
25 | } |
||
26 | } |
||
27 | } |
||
28 | |||
29 | static { |
||
30 | register(); |
||
31 | } |
||
32 | |||
33 | private MailAliasDestinationManager() |
||
34 | { |
||
35 | } |
||
36 | |||
37 | public MailAliasDestination create(User editor) |
||
38 | throws ModelException |
||
39 | { |
||
40 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
||
41 | |||
42 | return new MailAliasDestination(); |
||
43 | } |
||
44 | |||
45 | public boolean allowedToCreate(User editor) |
||
46 | throws ModelException |
||
47 | { |
||
922 | dev | 48 | return MailAliasDestination.allowedToCreate(this, editor); |
919 | dev | 49 | } |
50 | |||
51 | public MailAliasDestination get(User editor, Long id) |
||
52 | throws ModelException |
||
53 | { |
||
54 | MailAliasDestination dest; |
||
55 | |||
56 | try { |
||
57 | dest = (MailAliasDestination)HibernateUtil.currentSession() |
||
58 | .load(MailAliasDestination.class, id); |
||
59 | } |
||
60 | catch(HibernateException ex) |
||
61 | { |
||
62 | throw new ModelException(ex); |
||
63 | } |
||
64 | |||
65 | if(!dest.viewableBy(editor)) |
||
66 | throw new ModelSecurityException(); |
||
67 | |||
68 | return dest; |
||
69 | } |
||
70 | |||
71 | public void save(User editor, MailAliasDestination mailAliasDestination) |
||
72 | throws ModelException |
||
73 | { |
||
74 | if(!mailAliasDestination.editableBy(editor)) |
||
75 | throw new ModelSecurityException(); |
||
76 | |||
77 | mailAliasDestination.setModUser(editor); |
||
923 | dev | 78 | // FIXME: the mod_user is not set when changing a destination as element of collection |
919 | dev | 79 | |
80 | try { |
||
81 | HibernateUtil.currentSession().saveOrUpdate(mailAliasDestination); |
||
82 | } |
||
83 | catch(HibernateException ex) |
||
84 | { |
||
85 | throw new ModelException(ex); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public void delete(User editor, MailAliasDestination mailAliasDestination) |
||
90 | throws ModelException |
||
91 | { |
||
92 | if(!mailAliasDestination.deleteableBy(editor)) |
||
93 | throw new ModelSecurityException(); |
||
94 | |||
95 | try { |
||
96 | HibernateUtil.currentSession().delete(mailAliasDestination); |
||
97 | } |
||
98 | catch(HibernateException ex) |
||
99 | { |
||
100 | throw new ModelException(ex); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | public Collection listMailAliasesDestination(MailAlias alias) |
||
105 | throws ModelException |
||
106 | { |
||
107 | try { |
||
108 | return HibernateUtil.currentSession().find( |
||
109 | "from MailAliasDestination where alias=?", |
||
110 | alias, Hibernate.entity(MailAlias.class)); |
||
111 | } |
||
112 | catch(HibernateException ex) |
||
113 | { |
||
114 | throw new ModelException(ex); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | public boolean areMailAliasesDestinationsAvailable(User editor) |
||
119 | throws ModelException |
||
120 | { |
||
121 | return true; |
||
122 | } |
||
123 | |||
124 | private static MailAliasDestinationManager mailAliasDestinationManager = null; |
||
125 | |||
126 | public static MailAliasDestinationManager getInstance() |
||
127 | { |
||
128 | if(mailAliasDestinationManager == null) |
||
129 | mailAliasDestinationManager = new MailAliasDestinationManager(); |
||
130 | |||
131 | return mailAliasDestinationManager; |
||
132 | } |
||
133 | |||
134 | public static final Comparator EMAIL_COMPARATOR = new EmailComparator(); |
||
135 | |||
136 | private static class EmailComparator |
||
137 | implements Comparator |
||
138 | { |
||
139 | public int compare(Object o1, Object o2) |
||
140 | { |
||
141 | if(!(o1 instanceof MailAliasDestination) || !(o2 instanceof MailAliasDestination)) |
||
142 | throw new ClassCastException("not a MailAliasDestination"); |
||
143 | |||
144 | MailAliasDestination a1 = (MailAliasDestination)o1; |
||
145 | MailAliasDestination a2 = (MailAliasDestination)o2; |
||
146 | |||
147 | if(a1 == null && a2 == null) |
||
148 | return 0; |
||
149 | else if(a1 == null && a2 != null) |
||
150 | return -1; |
||
151 | else if(a1 != null && a2 == null) |
||
152 | return 1; |
||
153 | else |
||
154 | return a1.getEmail().compareToIgnoreCase(a2.getEmail()); |
||
155 | } |
||
156 | |||
157 | public boolean equals(Object obj) |
||
158 | { |
||
159 | return (obj instanceof EmailComparator); |
||
160 | } |
||
161 | } |
||
162 | } |