Subversion Repositories general

Rev

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