Subversion Repositories general

Rev

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