Rev 1010 | Rev 1014 | 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; |
||
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( |
||
950 | 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 | |||
1011 | dev | 47 | private Collection createdListeners = new ArrayList(); |
48 | private Collection modifiedListeners = new ArrayList(); |
||
949 | dev | 49 | private Collection beforeDeleteListeners = new ArrayList(); |
1011 | dev | 50 | private Collection deletedListeners = new ArrayList(); |
949 | dev | 51 | |
919 | dev | 52 | private MailboxManager() |
53 | { |
||
949 | dev | 54 | UserManager.getInstance().addBeforeDeleteListener(this); |
55 | SystemUserManager.getInstance().addBeforeDeleteListener(this); |
||
56 | InetDomainManager.getInstance().addBeforeDeleteListener(this); |
||
919 | dev | 57 | } |
58 | |||
59 | public Mailbox create(User editor) |
||
60 | throws ModelException |
||
61 | { |
||
62 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
||
63 | |||
64 | return new Mailbox(); |
||
65 | } |
||
66 | |||
67 | public boolean allowedToCreate(User editor) |
||
68 | throws ModelException |
||
69 | { |
||
921 | dev | 70 | return Mailbox.allowedToCreate(this, editor); |
919 | dev | 71 | } |
72 | |||
73 | public Mailbox get(User editor, Long id) |
||
74 | throws ModelException |
||
75 | { |
||
76 | Mailbox mailbox; |
||
77 | |||
78 | try { |
||
79 | mailbox = (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id); |
||
80 | } |
||
81 | catch(HibernateException ex) |
||
82 | { |
||
83 | throw new ModelException(ex); |
||
84 | } |
||
85 | |||
86 | if(!mailbox.viewableBy(editor)) |
||
87 | throw new ModelSecurityException(); |
||
88 | |||
89 | return mailbox; |
||
90 | } |
||
91 | |||
926 | dev | 92 | public boolean loginExists(User editor, Mailbox mailbox, String login) |
93 | throws ModelException |
||
94 | { |
||
95 | if(mailbox.getDomain() == null) |
||
96 | throw new ModelException("Cannot check unique login for mailbox without domain"); |
||
97 | |||
98 | try { |
||
99 | if(mailbox.getId() == null) |
||
100 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
101 | "select count(*) from Mailbox where login = ? and domain = ?", |
||
102 | new Object[] { login, mailbox.getDomain() }, |
||
103 | new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } ) |
||
104 | .next()).intValue() > 0; |
||
105 | else |
||
106 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
107 | "select count(*) from Mailbox b where login = ? and domain = ? and b != ?", |
||
108 | new Object[] { login, mailbox.getDomain(), mailbox }, |
||
109 | new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class), |
||
110 | Hibernate.entity(Mailbox.class) } ) |
||
111 | .next()).intValue() > 0; |
||
112 | } |
||
113 | catch(HibernateException ex) |
||
114 | { |
||
115 | throw new ModelException(ex); |
||
116 | } |
||
117 | } |
||
118 | |||
919 | dev | 119 | protected Mailbox findForLogin(String login) |
120 | throws ModelException |
||
121 | { |
||
122 | try { |
||
123 | List list = HibernateUtil.currentSession().find( |
||
124 | "from Mailbox where login=?", login, Hibernate.STRING); |
||
125 | |||
126 | if(list.size() == 0) |
||
127 | return null; |
||
128 | else |
||
129 | return (Mailbox)list.get(0); |
||
130 | } |
||
131 | catch(HibernateException ex) |
||
132 | { |
||
133 | throw new ModelException(ex); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public void save(User editor, Mailbox mailbox) |
||
138 | throws ModelException |
||
139 | { |
||
140 | if(!mailbox.editableBy(editor)) |
||
141 | throw new ModelSecurityException(); |
||
142 | |||
1011 | dev | 143 | boolean isNew = mailbox.isNew(); |
144 | |||
1010 | dev | 145 | //mailbox.setModUser(editor); // FIXME |
919 | dev | 146 | |
147 | try { |
||
148 | HibernateUtil.currentSession().saveOrUpdate(mailbox); |
||
149 | } |
||
150 | catch(HibernateException ex) |
||
151 | { |
||
152 | throw new ModelException(ex); |
||
153 | } |
||
1011 | dev | 154 | |
155 | // inform listeners |
||
156 | if(isNew) { |
||
157 | for(Iterator i = createdListeners.iterator(); i.hasNext(); ) { |
||
158 | MailboxCreatedListener listener = (MailboxCreatedListener)i.next(); |
||
159 | listener.mailboxCreated(editor, mailbox); |
||
160 | } |
||
161 | } |
||
162 | else { |
||
163 | Mailbox oldMailbox = mailbox.getOrigin(); |
||
164 | if(oldMailbox == null) oldMailbox = mailbox; |
||
165 | for(Iterator i = modifiedListeners.iterator(); i.hasNext(); ) { |
||
166 | MailboxModifiedListener listener = (MailboxModifiedListener)i.next(); |
||
167 | listener.mailboxModified(editor, mailbox, oldMailbox); |
||
168 | } |
||
169 | } |
||
919 | dev | 170 | } |
171 | |||
1011 | dev | 172 | public void addCreatedListener(MailboxCreatedListener listener) |
173 | { |
||
174 | createdListeners.add(listener); |
||
175 | } |
||
176 | |||
177 | public void removeCreatedListener(MailboxCreatedListener listener) |
||
178 | { |
||
179 | createdListeners.remove(listener); |
||
180 | } |
||
181 | |||
182 | public void addModifiedListener(MailboxModifiedListener listener) |
||
183 | { |
||
184 | modifiedListeners.add(listener); |
||
185 | } |
||
186 | |||
187 | public void removeModifiedListener(MailboxModifiedListener listener) |
||
188 | { |
||
189 | modifiedListeners.remove(listener); |
||
190 | } |
||
191 | |||
949 | dev | 192 | public void addBeforeDeleteListener(MailboxBeforeDeleteListener listener) |
193 | { |
||
194 | beforeDeleteListeners.add(listener); |
||
195 | } |
||
196 | |||
197 | public void removeBeforeDeleteListener(MailboxBeforeDeleteListener listener) |
||
198 | { |
||
199 | beforeDeleteListeners.remove(listener); |
||
200 | } |
||
201 | |||
1011 | dev | 202 | public void addDeletedListener(MailboxDeletedListener listener) |
203 | { |
||
204 | deletedListeners.add(listener); |
||
205 | } |
||
206 | |||
207 | public void removeDeletedListener(MailboxDeletedListener listener) |
||
208 | { |
||
209 | deletedListeners.remove(listener); |
||
210 | } |
||
211 | |||
949 | dev | 212 | public Collection beforeDelete(User editor, Mailbox mailbox, Collection known) |
213 | throws ModelException |
||
214 | { |
||
215 | Collection cascade = new ArrayList(); |
||
216 | |||
217 | for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) { |
||
218 | MailboxBeforeDeleteListener listener = (MailboxBeforeDeleteListener)i.next(); |
||
219 | Collection subcascade = listener.mailboxBeforeDelete(editor, mailbox, known); |
||
220 | if(subcascade != null) |
||
221 | cascade.addAll(subcascade); |
||
222 | } |
||
223 | |||
224 | return cascade; |
||
225 | } |
||
226 | |||
919 | dev | 227 | public void delete(User editor, Mailbox mailbox) |
228 | throws ModelException |
||
229 | { |
||
1011 | dev | 230 | // check rights |
919 | dev | 231 | if(!mailbox.deleteableBy(editor)) |
232 | throw new ModelSecurityException(); |
||
233 | |||
1011 | dev | 234 | // backup copy |
235 | Mailbox oldMailbox = new Mailbox(mailbox); |
||
236 | |||
237 | // delete it |
||
919 | dev | 238 | try { |
239 | HibernateUtil.currentSession().delete(mailbox); |
||
240 | } |
||
241 | catch(HibernateException ex) |
||
242 | { |
||
243 | throw new ModelException(ex); |
||
244 | } |
||
1011 | dev | 245 | |
246 | // inform listeners |
||
247 | for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) { |
||
248 | MailboxDeletedListener listener = (MailboxDeletedListener)i.next(); |
||
249 | listener.mailboxDeleted(editor, oldMailbox); |
||
250 | } |
||
919 | dev | 251 | } |
252 | |||
253 | public Collection listMailboxes(User editor) |
||
254 | throws ModelException |
||
255 | { |
||
256 | try { |
||
257 | if(editor.isSuperuser()) |
||
258 | return HibernateUtil.currentSession().find("from Mailbox"); |
||
259 | else |
||
260 | return HibernateUtil.currentSession().find( |
||
261 | "select mb from Mailbox mb left join mb.domain as d" |
||
262 | + " where d.owner=? or mb.owner=?", |
||
263 | new Object[] { editor, editor }, |
||
264 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } ); |
||
265 | } |
||
266 | catch(HibernateException ex) |
||
267 | { |
||
268 | throw new ModelException(ex); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | public boolean areMailboxesAvailable(User editor) |
||
273 | throws ModelException |
||
274 | { |
||
275 | try { |
||
276 | if(editor.isSuperuser() |
||
277 | || InetDomainManager.getInstance().areInetDomainsAvailable(editor)) |
||
278 | { |
||
279 | return true; |
||
280 | } |
||
281 | else { |
||
282 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
283 | "select count(*) from Mailbox mb left join mb.domain as d" |
||
284 | + " where d.owner=? or mb.owner=?", |
||
285 | new Object[] { editor, editor }, |
||
286 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) }) |
||
287 | .next()).intValue() > 0; |
||
288 | } |
||
289 | } |
||
290 | catch(HibernateException ex) |
||
291 | { |
||
292 | throw new ModelException(ex); |
||
293 | } |
||
294 | } |
||
295 | |||
949 | dev | 296 | public Collection userBeforeDelete(User editor, User user, Collection known) |
297 | throws ModelException |
||
298 | { |
||
299 | Collection mailboxes; |
||
919 | dev | 300 | |
949 | dev | 301 | try { |
302 | mailboxes = HibernateUtil.currentSession().find( |
||
303 | "from Mailbox where owner = ?", |
||
304 | user, Hibernate.entity(User.class) ); |
||
305 | } |
||
306 | catch(HibernateException ex) |
||
307 | { |
||
308 | throw new ModelException(ex); |
||
309 | } |
||
310 | |||
311 | return iterateBeforeDelete(editor, mailboxes, known); |
||
312 | } |
||
313 | |||
314 | public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known) |
||
315 | throws ModelException |
||
919 | dev | 316 | { |
949 | dev | 317 | Collection mailboxes; |
919 | dev | 318 | |
949 | dev | 319 | try { |
320 | mailboxes = HibernateUtil.currentSession().find( |
||
321 | "from Mailbox where domain = ?", |
||
322 | domain, Hibernate.entity(InetDomain.class) ); |
||
323 | } |
||
324 | catch(HibernateException ex) |
||
325 | { |
||
326 | throw new ModelException(ex); |
||
327 | } |
||
328 | |||
329 | return iterateBeforeDelete(editor, mailboxes, known); |
||
330 | } |
||
331 | |||
332 | public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known) |
||
333 | throws ModelException |
||
334 | { |
||
335 | Collection mailboxes; |
||
336 | |||
337 | try { |
||
338 | mailboxes = HibernateUtil.currentSession().find( |
||
339 | "from Mailbox where systemUser = ?", |
||
340 | user, Hibernate.entity(SystemUser.class) ); |
||
341 | } |
||
342 | catch(HibernateException ex) |
||
343 | { |
||
344 | throw new ModelException(ex); |
||
345 | } |
||
346 | |||
347 | return iterateBeforeDelete(editor, mailboxes, known); |
||
348 | } |
||
349 | |||
350 | private Collection iterateBeforeDelete(User editor, Collection mailboxes, Collection known) |
||
351 | throws ModelException |
||
352 | { |
||
353 | Collection cascade = new ArrayList(); |
||
354 | for(Iterator i = mailboxes.iterator(); i.hasNext(); ) { |
||
355 | Mailbox mailbox = (Mailbox)i.next(); |
||
356 | if(mailbox.viewableBy(editor)) { |
||
357 | if(mailbox.deleteableBy(editor)) |
||
358 | cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.DELETE, |
||
359 | this.beforeDelete(editor, mailbox, known))); |
||
360 | else |
||
361 | cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.FORBIDDEN, |
||
362 | null)); |
||
363 | } |
||
364 | else { |
||
365 | cascade.add(new CascadeDeleteElement(Mailbox.createLimitedCopy(mailbox), |
||
366 | CascadeDeleteElement.FORBIDDEN, null)); |
||
367 | } |
||
368 | } |
||
369 | |||
370 | return cascade; |
||
919 | dev | 371 | } |
372 | |||
373 | public static final Comparator LOGIN_COMPARATOR = new LoginComparator(); |
||
374 | |||
375 | private static class LoginComparator |
||
376 | implements Comparator |
||
377 | { |
||
378 | public int compare(Object o1, Object o2) |
||
379 | { |
||
380 | if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox)) |
||
381 | throw new ClassCastException("not a Mailbox"); |
||
382 | |||
383 | Mailbox a1 = (Mailbox)o1; |
||
384 | Mailbox a2 = (Mailbox)o2; |
||
385 | |||
386 | if(a1 == null && a2 == null) |
||
387 | return 0; |
||
388 | else if(a1 == null && a2 != null) |
||
389 | return -1; |
||
390 | else if(a1 != null && a2 == null) |
||
391 | return 1; |
||
392 | else |
||
393 | return a1.getLogin().compareToIgnoreCase(a2.getLogin()); |
||
394 | } |
||
395 | |||
396 | public boolean equals(Object obj) |
||
397 | { |
||
398 | return (obj instanceof LoginComparator); |
||
399 | } |
||
400 | } |
||
401 | } |