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.*; |
||
5 | import net.sf.hibernate.type.Type; |
||
924 | dev | 6 | import ak.hostadmiral.util.HibernateUtil; |
7 | import ak.hostadmiral.util.ModelException; |
||
8 | import ak.hostadmiral.util.ModelSecurityException; |
||
919 | dev | 9 | |
10 | public class MailboxManager |
||
949 | dev | 11 | implements |
12 | UserBeforeDeleteListener, |
||
13 | SystemUserBeforeDeleteListener, |
||
14 | InetDomainBeforeDeleteListener |
||
919 | dev | 15 | { |
949 | dev | 16 | private static MailboxManager mailboxManager = null; |
919 | dev | 17 | private static boolean registered = false; |
949 | dev | 18 | |
19 | public static MailboxManager getInstance() |
||
20 | { |
||
21 | return mailboxManager; |
||
22 | } |
||
23 | |||
919 | dev | 24 | protected static void register() |
25 | { |
||
26 | synchronized(MailboxManager.class) { |
||
27 | if(registered) return; |
||
28 | |||
29 | registered = true; |
||
30 | try { |
||
31 | HibernateUtil.getConfiguration().addResource( |
||
924 | dev | 32 | "/ak/hostadmiral/core/model/Mailbox.hbm.xml"); |
949 | dev | 33 | |
34 | mailboxManager = new MailboxManager(); |
||
919 | dev | 35 | } |
36 | catch(Exception ex) { |
||
37 | ex.printStackTrace(); |
||
38 | throw new RuntimeException(ex.getMessage()); |
||
39 | } |
||
40 | } |
||
41 | } |
||
42 | |||
43 | static { |
||
44 | register(); |
||
45 | } |
||
46 | |||
949 | dev | 47 | private Collection beforeDeleteListeners = new ArrayList(); |
48 | |||
919 | dev | 49 | private MailboxManager() |
50 | { |
||
949 | dev | 51 | UserManager.getInstance().addBeforeDeleteListener(this); |
52 | SystemUserManager.getInstance().addBeforeDeleteListener(this); |
||
53 | InetDomainManager.getInstance().addBeforeDeleteListener(this); |
||
919 | dev | 54 | } |
55 | |||
56 | public Mailbox create(User editor) |
||
57 | throws ModelException |
||
58 | { |
||
59 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
||
60 | |||
61 | return new Mailbox(); |
||
62 | } |
||
63 | |||
64 | public boolean allowedToCreate(User editor) |
||
65 | throws ModelException |
||
66 | { |
||
921 | dev | 67 | return Mailbox.allowedToCreate(this, editor); |
919 | dev | 68 | } |
69 | |||
70 | public Mailbox get(User editor, Long id) |
||
71 | throws ModelException |
||
72 | { |
||
73 | Mailbox mailbox; |
||
74 | |||
75 | try { |
||
76 | mailbox = (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id); |
||
77 | } |
||
78 | catch(HibernateException ex) |
||
79 | { |
||
80 | throw new ModelException(ex); |
||
81 | } |
||
82 | |||
83 | if(!mailbox.viewableBy(editor)) |
||
84 | throw new ModelSecurityException(); |
||
85 | |||
86 | return mailbox; |
||
87 | } |
||
88 | |||
926 | dev | 89 | public boolean loginExists(User editor, Mailbox mailbox, String login) |
90 | throws ModelException |
||
91 | { |
||
92 | if(mailbox.getDomain() == null) |
||
93 | throw new ModelException("Cannot check unique login for mailbox without domain"); |
||
94 | |||
95 | try { |
||
96 | if(mailbox.getId() == null) |
||
97 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
98 | "select count(*) from Mailbox where login = ? and domain = ?", |
||
99 | new Object[] { login, mailbox.getDomain() }, |
||
100 | new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } ) |
||
101 | .next()).intValue() > 0; |
||
102 | else |
||
103 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
104 | "select count(*) from Mailbox b where login = ? and domain = ? and b != ?", |
||
105 | new Object[] { login, mailbox.getDomain(), mailbox }, |
||
106 | new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class), |
||
107 | Hibernate.entity(Mailbox.class) } ) |
||
108 | .next()).intValue() > 0; |
||
109 | } |
||
110 | catch(HibernateException ex) |
||
111 | { |
||
112 | throw new ModelException(ex); |
||
113 | } |
||
114 | } |
||
115 | |||
919 | dev | 116 | protected Mailbox findForLogin(String login) |
117 | throws ModelException |
||
118 | { |
||
119 | try { |
||
120 | List list = HibernateUtil.currentSession().find( |
||
121 | "from Mailbox where login=?", login, Hibernate.STRING); |
||
122 | |||
123 | if(list.size() == 0) |
||
124 | return null; |
||
125 | else |
||
126 | return (Mailbox)list.get(0); |
||
127 | } |
||
128 | catch(HibernateException ex) |
||
129 | { |
||
130 | throw new ModelException(ex); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public void save(User editor, Mailbox mailbox) |
||
135 | throws ModelException |
||
136 | { |
||
137 | if(!mailbox.editableBy(editor)) |
||
138 | throw new ModelSecurityException(); |
||
139 | |||
140 | mailbox.setModUser(editor); |
||
141 | |||
142 | try { |
||
143 | HibernateUtil.currentSession().saveOrUpdate(mailbox); |
||
144 | } |
||
145 | catch(HibernateException ex) |
||
146 | { |
||
147 | throw new ModelException(ex); |
||
148 | } |
||
149 | } |
||
150 | |||
949 | dev | 151 | public void addBeforeDeleteListener(MailboxBeforeDeleteListener listener) |
152 | { |
||
153 | beforeDeleteListeners.add(listener); |
||
154 | } |
||
155 | |||
156 | public void removeBeforeDeleteListener(MailboxBeforeDeleteListener listener) |
||
157 | { |
||
158 | beforeDeleteListeners.remove(listener); |
||
159 | } |
||
160 | |||
161 | public Collection beforeDelete(User editor, Mailbox mailbox, Collection known) |
||
162 | throws ModelException |
||
163 | { |
||
164 | Collection cascade = new ArrayList(); |
||
165 | |||
166 | for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) { |
||
167 | MailboxBeforeDeleteListener listener = (MailboxBeforeDeleteListener)i.next(); |
||
168 | Collection subcascade = listener.mailboxBeforeDelete(editor, mailbox, known); |
||
169 | if(subcascade != null) |
||
170 | cascade.addAll(subcascade); |
||
171 | } |
||
172 | |||
173 | return cascade; |
||
174 | } |
||
175 | |||
919 | dev | 176 | public void delete(User editor, Mailbox mailbox) |
177 | throws ModelException |
||
178 | { |
||
179 | if(!mailbox.deleteableBy(editor)) |
||
180 | throw new ModelSecurityException(); |
||
181 | |||
182 | try { |
||
183 | HibernateUtil.currentSession().delete(mailbox); |
||
184 | } |
||
185 | catch(HibernateException ex) |
||
186 | { |
||
187 | throw new ModelException(ex); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | public Collection listMailboxes(User editor) |
||
192 | throws ModelException |
||
193 | { |
||
194 | try { |
||
195 | if(editor.isSuperuser()) |
||
196 | return HibernateUtil.currentSession().find("from Mailbox"); |
||
197 | else |
||
198 | return HibernateUtil.currentSession().find( |
||
199 | "select mb from Mailbox mb left join mb.domain as d" |
||
200 | + " where d.owner=? or mb.owner=?", |
||
201 | new Object[] { editor, editor }, |
||
202 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } ); |
||
203 | } |
||
204 | catch(HibernateException ex) |
||
205 | { |
||
206 | throw new ModelException(ex); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | public boolean areMailboxesAvailable(User editor) |
||
211 | throws ModelException |
||
212 | { |
||
213 | try { |
||
214 | if(editor.isSuperuser() |
||
215 | || InetDomainManager.getInstance().areInetDomainsAvailable(editor)) |
||
216 | { |
||
217 | return true; |
||
218 | } |
||
219 | else { |
||
220 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
221 | "select count(*) from Mailbox mb left join mb.domain as d" |
||
222 | + " where d.owner=? or mb.owner=?", |
||
223 | new Object[] { editor, editor }, |
||
224 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) }) |
||
225 | .next()).intValue() > 0; |
||
226 | } |
||
227 | } |
||
228 | catch(HibernateException ex) |
||
229 | { |
||
230 | throw new ModelException(ex); |
||
231 | } |
||
232 | } |
||
233 | |||
949 | dev | 234 | public Collection userBeforeDelete(User editor, User user, Collection known) |
235 | throws ModelException |
||
236 | { |
||
237 | Collection mailboxes; |
||
919 | dev | 238 | |
949 | dev | 239 | try { |
240 | mailboxes = HibernateUtil.currentSession().find( |
||
241 | "from Mailbox where owner = ?", |
||
242 | user, Hibernate.entity(User.class) ); |
||
243 | } |
||
244 | catch(HibernateException ex) |
||
245 | { |
||
246 | throw new ModelException(ex); |
||
247 | } |
||
248 | |||
249 | return iterateBeforeDelete(editor, mailboxes, known); |
||
250 | } |
||
251 | |||
252 | public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known) |
||
253 | throws ModelException |
||
919 | dev | 254 | { |
949 | dev | 255 | Collection mailboxes; |
919 | dev | 256 | |
949 | dev | 257 | try { |
258 | mailboxes = HibernateUtil.currentSession().find( |
||
259 | "from Mailbox where domain = ?", |
||
260 | domain, Hibernate.entity(InetDomain.class) ); |
||
261 | } |
||
262 | catch(HibernateException ex) |
||
263 | { |
||
264 | throw new ModelException(ex); |
||
265 | } |
||
266 | |||
267 | return iterateBeforeDelete(editor, mailboxes, known); |
||
268 | } |
||
269 | |||
270 | public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known) |
||
271 | throws ModelException |
||
272 | { |
||
273 | Collection mailboxes; |
||
274 | |||
275 | try { |
||
276 | mailboxes = HibernateUtil.currentSession().find( |
||
277 | "from Mailbox where systemUser = ?", |
||
278 | user, Hibernate.entity(SystemUser.class) ); |
||
279 | } |
||
280 | catch(HibernateException ex) |
||
281 | { |
||
282 | throw new ModelException(ex); |
||
283 | } |
||
284 | |||
285 | return iterateBeforeDelete(editor, mailboxes, known); |
||
286 | } |
||
287 | |||
288 | private Collection iterateBeforeDelete(User editor, Collection mailboxes, Collection known) |
||
289 | throws ModelException |
||
290 | { |
||
291 | Collection cascade = new ArrayList(); |
||
292 | for(Iterator i = mailboxes.iterator(); i.hasNext(); ) { |
||
293 | Mailbox mailbox = (Mailbox)i.next(); |
||
294 | if(mailbox.viewableBy(editor)) { |
||
295 | if(mailbox.deleteableBy(editor)) |
||
296 | cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.DELETE, |
||
297 | this.beforeDelete(editor, mailbox, known))); |
||
298 | else |
||
299 | cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.FORBIDDEN, |
||
300 | null)); |
||
301 | } |
||
302 | else { |
||
303 | cascade.add(new CascadeDeleteElement(Mailbox.createLimitedCopy(mailbox), |
||
304 | CascadeDeleteElement.FORBIDDEN, null)); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | return cascade; |
||
919 | dev | 309 | } |
310 | |||
311 | public static final Comparator LOGIN_COMPARATOR = new LoginComparator(); |
||
312 | |||
313 | private static class LoginComparator |
||
314 | implements Comparator |
||
315 | { |
||
316 | public int compare(Object o1, Object o2) |
||
317 | { |
||
318 | if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox)) |
||
319 | throw new ClassCastException("not a Mailbox"); |
||
320 | |||
321 | Mailbox a1 = (Mailbox)o1; |
||
322 | Mailbox a2 = (Mailbox)o2; |
||
323 | |||
324 | if(a1 == null && a2 == null) |
||
325 | return 0; |
||
326 | else if(a1 == null && a2 != null) |
||
327 | return -1; |
||
328 | else if(a1 != null && a2 == null) |
||
329 | return 1; |
||
330 | else |
||
331 | return a1.getLogin().compareToIgnoreCase(a2.getLogin()); |
||
332 | } |
||
333 | |||
334 | public boolean equals(Object obj) |
||
335 | { |
||
336 | return (obj instanceof LoginComparator); |
||
337 | } |
||
338 | } |
||
339 | } |