Rev 1015 | Rev 1018 | Go to most recent revision | Details | Compare with Previous | 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; |
||
1016 | dev | 6 | import ak.hostadmiral.util.CollectionUtils; |
924 | dev | 7 | import ak.hostadmiral.util.HibernateUtil; |
8 | import ak.hostadmiral.util.ModelException; |
||
9 | import ak.hostadmiral.util.ModelSecurityException; |
||
919 | dev | 10 | |
11 | public class MailboxManager |
||
949 | dev | 12 | implements |
13 | UserBeforeDeleteListener, |
||
1014 | dev | 14 | UserDeletingListener, |
949 | dev | 15 | SystemUserBeforeDeleteListener, |
1014 | dev | 16 | SystemUserDeletingListener, |
17 | InetDomainBeforeDeleteListener, |
||
18 | InetDomainDeletingListener |
||
919 | dev | 19 | { |
949 | dev | 20 | private static MailboxManager mailboxManager = null; |
919 | dev | 21 | private static boolean registered = false; |
949 | dev | 22 | |
23 | public static MailboxManager getInstance() |
||
24 | { |
||
25 | return mailboxManager; |
||
26 | } |
||
27 | |||
919 | dev | 28 | protected static void register() |
29 | { |
||
30 | synchronized(MailboxManager.class) { |
||
31 | if(registered) return; |
||
32 | |||
33 | registered = true; |
||
34 | try { |
||
35 | HibernateUtil.getConfiguration().addResource( |
||
950 | dev | 36 | "ak/hostadmiral/core/model/Mailbox.hbm.xml"); |
949 | dev | 37 | |
38 | mailboxManager = new MailboxManager(); |
||
919 | dev | 39 | } |
40 | catch(Exception ex) { |
||
41 | ex.printStackTrace(); |
||
42 | throw new RuntimeException(ex.getMessage()); |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | |||
47 | static { |
||
48 | register(); |
||
49 | } |
||
50 | |||
1011 | dev | 51 | private Collection createdListeners = new ArrayList(); |
52 | private Collection modifiedListeners = new ArrayList(); |
||
949 | dev | 53 | private Collection beforeDeleteListeners = new ArrayList(); |
1014 | dev | 54 | private Collection deletingListeners = new ArrayList(); |
1011 | dev | 55 | private Collection deletedListeners = new ArrayList(); |
949 | dev | 56 | |
919 | dev | 57 | private MailboxManager() |
58 | { |
||
949 | dev | 59 | UserManager.getInstance().addBeforeDeleteListener(this); |
60 | SystemUserManager.getInstance().addBeforeDeleteListener(this); |
||
61 | InetDomainManager.getInstance().addBeforeDeleteListener(this); |
||
919 | dev | 62 | } |
63 | |||
64 | public Mailbox create(User editor) |
||
65 | throws ModelException |
||
66 | { |
||
67 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
||
68 | |||
1015 | dev | 69 | Mailbox mailbox = new Mailbox(); |
70 | |||
71 | // FIXME: make this configurable |
||
72 | mailbox.addPasswordStore(new PasswordStoreMd5()); |
||
73 | mailbox.addPasswordStore(new PasswordStoreCrypt()); |
||
74 | mailbox.addPasswordStore(new PasswordStorePlain()); |
||
75 | |||
76 | return mailbox; |
||
919 | dev | 77 | } |
78 | |||
79 | public boolean allowedToCreate(User editor) |
||
80 | throws ModelException |
||
81 | { |
||
921 | dev | 82 | return Mailbox.allowedToCreate(this, editor); |
919 | dev | 83 | } |
84 | |||
85 | public Mailbox get(User editor, Long id) |
||
86 | throws ModelException |
||
87 | { |
||
88 | Mailbox mailbox; |
||
89 | |||
90 | try { |
||
91 | mailbox = (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id); |
||
92 | } |
||
93 | catch(HibernateException ex) |
||
94 | { |
||
95 | throw new ModelException(ex); |
||
96 | } |
||
97 | |||
98 | if(!mailbox.viewableBy(editor)) |
||
99 | throw new ModelSecurityException(); |
||
100 | |||
101 | return mailbox; |
||
102 | } |
||
103 | |||
926 | dev | 104 | public boolean loginExists(User editor, Mailbox mailbox, String login) |
105 | throws ModelException |
||
106 | { |
||
107 | if(mailbox.getDomain() == null) |
||
108 | throw new ModelException("Cannot check unique login for mailbox without domain"); |
||
109 | |||
110 | try { |
||
111 | if(mailbox.getId() == null) |
||
112 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
113 | "select count(*) from Mailbox where login = ? and domain = ?", |
||
114 | new Object[] { login, mailbox.getDomain() }, |
||
115 | new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } ) |
||
116 | .next()).intValue() > 0; |
||
117 | else |
||
118 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
119 | "select count(*) from Mailbox b where login = ? and domain = ? and b != ?", |
||
120 | new Object[] { login, mailbox.getDomain(), mailbox }, |
||
121 | new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class), |
||
122 | Hibernate.entity(Mailbox.class) } ) |
||
123 | .next()).intValue() > 0; |
||
124 | } |
||
125 | catch(HibernateException ex) |
||
126 | { |
||
127 | throw new ModelException(ex); |
||
128 | } |
||
129 | } |
||
130 | |||
919 | dev | 131 | protected Mailbox findForLogin(String login) |
132 | throws ModelException |
||
133 | { |
||
134 | try { |
||
135 | List list = HibernateUtil.currentSession().find( |
||
136 | "from Mailbox where login=?", login, Hibernate.STRING); |
||
137 | |||
138 | if(list.size() == 0) |
||
139 | return null; |
||
140 | else |
||
141 | return (Mailbox)list.get(0); |
||
142 | } |
||
143 | catch(HibernateException ex) |
||
144 | { |
||
145 | throw new ModelException(ex); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | public void save(User editor, Mailbox mailbox) |
||
150 | throws ModelException |
||
151 | { |
||
152 | if(!mailbox.editableBy(editor)) |
||
153 | throw new ModelSecurityException(); |
||
154 | |||
1011 | dev | 155 | boolean isNew = mailbox.isNew(); |
156 | |||
1010 | dev | 157 | //mailbox.setModUser(editor); // FIXME |
919 | dev | 158 | |
159 | try { |
||
160 | HibernateUtil.currentSession().saveOrUpdate(mailbox); |
||
161 | } |
||
162 | catch(HibernateException ex) |
||
163 | { |
||
164 | throw new ModelException(ex); |
||
165 | } |
||
1011 | dev | 166 | |
167 | // inform listeners |
||
168 | if(isNew) { |
||
169 | for(Iterator i = createdListeners.iterator(); i.hasNext(); ) { |
||
170 | MailboxCreatedListener listener = (MailboxCreatedListener)i.next(); |
||
171 | listener.mailboxCreated(editor, mailbox); |
||
172 | } |
||
173 | } |
||
174 | else { |
||
175 | Mailbox oldMailbox = mailbox.getOrigin(); |
||
176 | if(oldMailbox == null) oldMailbox = mailbox; |
||
177 | for(Iterator i = modifiedListeners.iterator(); i.hasNext(); ) { |
||
178 | MailboxModifiedListener listener = (MailboxModifiedListener)i.next(); |
||
179 | listener.mailboxModified(editor, mailbox, oldMailbox); |
||
180 | } |
||
181 | } |
||
919 | dev | 182 | } |
183 | |||
1011 | dev | 184 | public void addCreatedListener(MailboxCreatedListener listener) |
185 | { |
||
186 | createdListeners.add(listener); |
||
187 | } |
||
188 | |||
189 | public void removeCreatedListener(MailboxCreatedListener listener) |
||
190 | { |
||
191 | createdListeners.remove(listener); |
||
192 | } |
||
193 | |||
194 | public void addModifiedListener(MailboxModifiedListener listener) |
||
195 | { |
||
196 | modifiedListeners.add(listener); |
||
197 | } |
||
198 | |||
199 | public void removeModifiedListener(MailboxModifiedListener listener) |
||
200 | { |
||
201 | modifiedListeners.remove(listener); |
||
202 | } |
||
203 | |||
949 | dev | 204 | public void addBeforeDeleteListener(MailboxBeforeDeleteListener listener) |
205 | { |
||
206 | beforeDeleteListeners.add(listener); |
||
207 | } |
||
208 | |||
209 | public void removeBeforeDeleteListener(MailboxBeforeDeleteListener listener) |
||
210 | { |
||
211 | beforeDeleteListeners.remove(listener); |
||
212 | } |
||
213 | |||
1014 | dev | 214 | public void addDeletingListener(MailboxDeletingListener listener) |
215 | { |
||
216 | deletingListeners.add(listener); |
||
217 | } |
||
218 | |||
219 | public void removeDeletingListener(MailboxDeletingListener listener) |
||
220 | { |
||
221 | deletingListeners.remove(listener); |
||
222 | } |
||
223 | |||
1011 | dev | 224 | public void addDeletedListener(MailboxDeletedListener listener) |
225 | { |
||
226 | deletedListeners.add(listener); |
||
227 | } |
||
228 | |||
229 | public void removeDeletedListener(MailboxDeletedListener listener) |
||
230 | { |
||
231 | deletedListeners.remove(listener); |
||
232 | } |
||
233 | |||
949 | dev | 234 | public Collection beforeDelete(User editor, Mailbox mailbox, Collection known) |
235 | throws ModelException |
||
236 | { |
||
237 | Collection cascade = new ArrayList(); |
||
238 | |||
239 | for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) { |
||
240 | MailboxBeforeDeleteListener listener = (MailboxBeforeDeleteListener)i.next(); |
||
241 | Collection subcascade = listener.mailboxBeforeDelete(editor, mailbox, known); |
||
242 | if(subcascade != null) |
||
243 | cascade.addAll(subcascade); |
||
244 | } |
||
245 | |||
246 | return cascade; |
||
247 | } |
||
248 | |||
919 | dev | 249 | public void delete(User editor, Mailbox mailbox) |
250 | throws ModelException |
||
251 | { |
||
1011 | dev | 252 | // check rights |
919 | dev | 253 | if(!mailbox.deleteableBy(editor)) |
254 | throw new ModelSecurityException(); |
||
255 | |||
1014 | dev | 256 | // inform deleting listeners |
257 | for(Iterator i = deletingListeners.iterator(); i.hasNext(); ) { |
||
258 | MailboxDeletingListener listener = (MailboxDeletingListener)i.next(); |
||
259 | listener.mailboxDeleting(editor, mailbox); |
||
260 | } |
||
261 | |||
1011 | dev | 262 | // backup copy |
263 | Mailbox oldMailbox = new Mailbox(mailbox); |
||
264 | |||
265 | // delete it |
||
919 | dev | 266 | try { |
267 | HibernateUtil.currentSession().delete(mailbox); |
||
268 | } |
||
269 | catch(HibernateException ex) |
||
270 | { |
||
271 | throw new ModelException(ex); |
||
272 | } |
||
1011 | dev | 273 | |
1014 | dev | 274 | // inform deleted listeners |
1011 | dev | 275 | for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) { |
276 | MailboxDeletedListener listener = (MailboxDeletedListener)i.next(); |
||
277 | listener.mailboxDeleted(editor, oldMailbox); |
||
278 | } |
||
919 | dev | 279 | } |
280 | |||
281 | public Collection listMailboxes(User editor) |
||
282 | throws ModelException |
||
283 | { |
||
284 | try { |
||
285 | if(editor.isSuperuser()) |
||
1016 | dev | 286 | return HibernateUtil.currentSession().find( |
287 | "select mb from Mailbox mb left join fetch mb.domain as d" |
||
288 | + " left join fetch mb.owner"); |
||
919 | dev | 289 | else |
1016 | dev | 290 | // FIXME: any problems for big lists or by pages? |
291 | return CollectionUtils.addUnique( |
||
292 | HibernateUtil.currentSession().find( |
||
293 | "select mb from Mailbox mb left join fetch mb.domain as d" |
||
294 | + " left join fetch mb.owner" |
||
295 | + " where mb.owner=?", |
||
296 | new Object[] { editor }, new Type[] { Hibernate.entity(User.class) } ), |
||
297 | HibernateUtil.currentSession().find( |
||
298 | " select mb from Mailbox mb left join fetch mb.domain as d" |
||
299 | + " left join fetch mb.owner" |
||
300 | + " where d.owner=?", |
||
301 | new Object[] { editor }, new Type[] { Hibernate.entity(User.class) } )); |
||
919 | dev | 302 | } |
303 | catch(HibernateException ex) |
||
304 | { |
||
305 | throw new ModelException(ex); |
||
306 | } |
||
307 | } |
||
308 | |||
309 | public boolean areMailboxesAvailable(User editor) |
||
310 | throws ModelException |
||
311 | { |
||
312 | try { |
||
313 | if(editor.isSuperuser() |
||
314 | || InetDomainManager.getInstance().areInetDomainsAvailable(editor)) |
||
315 | { |
||
316 | return true; |
||
317 | } |
||
318 | else { |
||
319 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
320 | "select count(*) from Mailbox mb left join mb.domain as d" |
||
321 | + " where d.owner=? or mb.owner=?", |
||
322 | new Object[] { editor, editor }, |
||
323 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) }) |
||
324 | .next()).intValue() > 0; |
||
325 | } |
||
326 | } |
||
327 | catch(HibernateException ex) |
||
328 | { |
||
329 | throw new ModelException(ex); |
||
330 | } |
||
331 | } |
||
332 | |||
949 | dev | 333 | public Collection userBeforeDelete(User editor, User user, Collection known) |
334 | throws ModelException |
||
335 | { |
||
336 | Collection mailboxes; |
||
919 | dev | 337 | |
949 | dev | 338 | try { |
339 | mailboxes = HibernateUtil.currentSession().find( |
||
340 | "from Mailbox where owner = ?", |
||
341 | user, Hibernate.entity(User.class) ); |
||
342 | } |
||
343 | catch(HibernateException ex) |
||
344 | { |
||
345 | throw new ModelException(ex); |
||
346 | } |
||
347 | |||
348 | return iterateBeforeDelete(editor, mailboxes, known); |
||
349 | } |
||
350 | |||
1014 | dev | 351 | public void userDeleting(User editor, User user) |
352 | throws ModelException |
||
353 | { |
||
354 | Collection mailboxes; |
||
355 | |||
356 | try { |
||
357 | mailboxes = HibernateUtil.currentSession().find( |
||
358 | "from Mailbox where owner = ?", |
||
359 | user, Hibernate.entity(User.class) ); |
||
360 | } |
||
361 | catch(HibernateException ex) |
||
362 | { |
||
363 | throw new ModelException(ex); |
||
364 | } |
||
365 | |||
366 | for(Iterator i = mailboxes.iterator(); i.hasNext(); ) { |
||
367 | delete(editor, (Mailbox)i.next()); |
||
368 | } |
||
369 | } |
||
370 | |||
949 | dev | 371 | public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known) |
372 | throws ModelException |
||
919 | dev | 373 | { |
949 | dev | 374 | Collection mailboxes; |
919 | dev | 375 | |
949 | dev | 376 | try { |
377 | mailboxes = HibernateUtil.currentSession().find( |
||
378 | "from Mailbox where domain = ?", |
||
379 | domain, Hibernate.entity(InetDomain.class) ); |
||
380 | } |
||
381 | catch(HibernateException ex) |
||
382 | { |
||
383 | throw new ModelException(ex); |
||
384 | } |
||
385 | |||
386 | return iterateBeforeDelete(editor, mailboxes, known); |
||
387 | } |
||
388 | |||
1014 | dev | 389 | public void inetDomainDeleting(User editor, InetDomain domain) |
390 | throws ModelException |
||
391 | { |
||
392 | Collection mailboxes; |
||
393 | |||
394 | try { |
||
395 | mailboxes = HibernateUtil.currentSession().find( |
||
396 | "from Mailbox where domain = ?", |
||
397 | domain, Hibernate.entity(InetDomain.class) ); |
||
398 | } |
||
399 | catch(HibernateException ex) |
||
400 | { |
||
401 | throw new ModelException(ex); |
||
402 | } |
||
403 | |||
404 | for(Iterator i = mailboxes.iterator(); i.hasNext(); ) { |
||
405 | delete(editor, (Mailbox)i.next()); |
||
406 | } |
||
407 | } |
||
408 | |||
949 | dev | 409 | public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known) |
410 | throws ModelException |
||
411 | { |
||
412 | Collection mailboxes; |
||
413 | |||
414 | try { |
||
415 | mailboxes = HibernateUtil.currentSession().find( |
||
416 | "from Mailbox where systemUser = ?", |
||
417 | user, Hibernate.entity(SystemUser.class) ); |
||
418 | } |
||
419 | catch(HibernateException ex) |
||
420 | { |
||
421 | throw new ModelException(ex); |
||
422 | } |
||
423 | |||
424 | return iterateBeforeDelete(editor, mailboxes, known); |
||
425 | } |
||
426 | |||
1014 | dev | 427 | public void systemUserDeleting(User editor, SystemUser user) |
428 | throws ModelException |
||
429 | { |
||
430 | Collection mailboxes; |
||
431 | |||
432 | try { |
||
433 | mailboxes = HibernateUtil.currentSession().find( |
||
434 | "from Mailbox where systemUser = ?", |
||
435 | user, Hibernate.entity(SystemUser.class) ); |
||
436 | } |
||
437 | catch(HibernateException ex) |
||
438 | { |
||
439 | throw new ModelException(ex); |
||
440 | } |
||
441 | |||
442 | for(Iterator i = mailboxes.iterator(); i.hasNext(); ) { |
||
443 | delete(editor, (Mailbox)i.next()); |
||
444 | } |
||
445 | } |
||
446 | |||
949 | dev | 447 | private Collection iterateBeforeDelete(User editor, Collection mailboxes, Collection known) |
448 | throws ModelException |
||
449 | { |
||
450 | Collection cascade = new ArrayList(); |
||
451 | for(Iterator i = mailboxes.iterator(); i.hasNext(); ) { |
||
452 | Mailbox mailbox = (Mailbox)i.next(); |
||
453 | if(mailbox.viewableBy(editor)) { |
||
454 | if(mailbox.deleteableBy(editor)) |
||
455 | cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.DELETE, |
||
456 | this.beforeDelete(editor, mailbox, known))); |
||
457 | else |
||
458 | cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.FORBIDDEN, |
||
459 | null)); |
||
460 | } |
||
461 | else { |
||
462 | cascade.add(new CascadeDeleteElement(Mailbox.createLimitedCopy(mailbox), |
||
463 | CascadeDeleteElement.FORBIDDEN, null)); |
||
464 | } |
||
465 | } |
||
466 | |||
467 | return cascade; |
||
919 | dev | 468 | } |
469 | |||
470 | public static final Comparator LOGIN_COMPARATOR = new LoginComparator(); |
||
471 | |||
472 | private static class LoginComparator |
||
473 | implements Comparator |
||
474 | { |
||
475 | public int compare(Object o1, Object o2) |
||
476 | { |
||
477 | if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox)) |
||
478 | throw new ClassCastException("not a Mailbox"); |
||
479 | |||
480 | Mailbox a1 = (Mailbox)o1; |
||
481 | Mailbox a2 = (Mailbox)o2; |
||
482 | |||
483 | if(a1 == null && a2 == null) |
||
484 | return 0; |
||
485 | else if(a1 == null && a2 != null) |
||
486 | return -1; |
||
487 | else if(a1 != null && a2 == null) |
||
488 | return 1; |
||
489 | else |
||
490 | return a1.getLogin().compareToIgnoreCase(a2.getLogin()); |
||
491 | } |
||
492 | |||
493 | public boolean equals(Object obj) |
||
494 | { |
||
495 | return (obj instanceof LoginComparator); |
||
496 | } |
||
497 | } |
||
498 | } |