Subversion Repositories general

Rev

Rev 1221 | 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
 
1041 dev 3
import java.util.Collection;
4
import java.util.ArrayList;
5
import java.util.Iterator;
6
import java.util.Map;
7
import java.util.Comparator;
8
 
9
import ak.hostadmiral.util.ConfigInit;
1020 dev 10
import ak.hostadmiral.util.CollectionInfo;
924 dev 11
import ak.hostadmiral.util.ModelException;
12
import ak.hostadmiral.util.ModelSecurityException;
1041 dev 13
import ak.hostadmiral.core.model.store.MailboxStore;
919 dev 14
 
15
public class MailboxManager
949 dev 16
	implements
1041 dev 17
		ConfigInit,
949 dev 18
		UserBeforeDeleteListener,
1014 dev 19
		UserDeletingListener,
949 dev 20
		SystemUserBeforeDeleteListener,
1014 dev 21
		SystemUserDeletingListener,
22
		InetDomainBeforeDeleteListener,
23
		InetDomainDeletingListener
919 dev 24
{
1041 dev 25
	private MailboxStore store;
1046 dev 26
	private Class[]      passwordStores;
949 dev 27
 
1062 dev 28
	private Collection validateListeners     = new ArrayList();
1011 dev 29
	private Collection createdListeners      = new ArrayList();
30
	private Collection modifiedListeners     = new ArrayList();
949 dev 31
	private Collection beforeDeleteListeners = new ArrayList();
1014 dev 32
	private Collection deletingListeners     = new ArrayList();
1011 dev 33
	private Collection deletedListeners      = new ArrayList();
949 dev 34
 
1041 dev 35
	public MailboxManager()
36
		throws ModelException
919 dev 37
	{
949 dev 38
		UserManager.getInstance().addBeforeDeleteListener(this);
1221 dev 39
		UserManager.getInstance().addDeletingListener(this);
949 dev 40
		SystemUserManager.getInstance().addBeforeDeleteListener(this);
1221 dev 41
		SystemUserManager.getInstance().addDeletingListener(this);
949 dev 42
		InetDomainManager.getInstance().addBeforeDeleteListener(this);
1221 dev 43
		InetDomainManager.getInstance().addDeletingListener(this);
919 dev 44
	}
45
 
46
	public Mailbox create(User editor)
47
		throws ModelException
48
	{
49
		if(!allowedToCreate(editor)) throw new ModelSecurityException();
50
 
1015 dev 51
		Mailbox mailbox = new Mailbox();
1046 dev 52
        setMailboxPasswordStores(mailbox);
1015 dev 53
 
54
		return mailbox;
919 dev 55
	}
56
 
57
	public boolean allowedToCreate(User editor)
58
		throws ModelException
59
	{
921 dev 60
		return Mailbox.allowedToCreate(this, editor);
919 dev 61
	}
62
 
63
	public Mailbox get(User editor, Long id)
64
		throws ModelException
65
	{
1041 dev 66
		Mailbox mailbox = store.get(id);
919 dev 67
 
68
		if(!mailbox.viewableBy(editor))
69
			throw new ModelSecurityException();
70
 
71
		return mailbox;
72
	}
73
 
926 dev 74
	public boolean loginExists(User editor, Mailbox mailbox, String login)
75
		throws ModelException
76
	{
77
		if(mailbox.getDomain() == null)
78
			throw new ModelException("Cannot check unique login for mailbox without domain");
79
 
1041 dev 80
		return store.loginExists(mailbox, login);
926 dev 81
	}
82
 
1041 dev 83
	public Mailbox findForLogin(User editor, String login)
919 dev 84
		throws ModelException
85
	{
1041 dev 86
		Mailbox mailbox = store.findForLogin(login);
919 dev 87
 
1041 dev 88
		if(!mailbox.viewableBy(editor))
89
			throw new ModelSecurityException();
90
 
91
		return mailbox;
919 dev 92
	}
93
 
94
	public void save(User editor, Mailbox mailbox)
95
		throws ModelException
96
	{
1062 dev 97
		// security check
919 dev 98
		if(!mailbox.editableBy(editor))
99
			throw new ModelSecurityException();
100
 
1062 dev 101
		//mailbox.setModUser(editor); // FIXME
102
 
1011 dev 103
        boolean isNew = mailbox.isNew();
1062 dev 104
        Mailbox oldMailbox = mailbox.getOrigin();
105
        if(oldMailbox == null) oldMailbox = mailbox;
1011 dev 106
 
1062 dev 107
		// validate
108
    	for(Iterator i = validateListeners.iterator(); i.hasNext(); ) {
109
    		MailboxValidateListener listener = (MailboxValidateListener)i.next();
110
			listener.mailboxValidate(editor, mailbox, oldMailbox);
111
    	}
919 dev 112
 
1041 dev 113
		store.save(mailbox);
1011 dev 114
 
115
        // inform listeners
116
        if(isNew) {
117
        	for(Iterator i = createdListeners.iterator(); i.hasNext(); ) {
118
        		MailboxCreatedListener listener = (MailboxCreatedListener)i.next();
119
    			listener.mailboxCreated(editor, mailbox);
120
        	}
121
        }
122
        else {
123
        	for(Iterator i = modifiedListeners.iterator(); i.hasNext(); ) {
124
        		MailboxModifiedListener listener = (MailboxModifiedListener)i.next();
125
    			listener.mailboxModified(editor, mailbox, oldMailbox);
126
        	}
127
        }
1045 dev 128
 
129
        // reset backup
130
        mailbox.resetOrigin();
919 dev 131
	}
132
 
1062 dev 133
    public void addValidateListener(MailboxValidateListener listener)
134
    {
135
    	validateListeners.add(listener);
136
    }
137
 
138
    public void removeValidateListener(MailboxValidateListener listener)
139
    {
140
    	validateListeners.remove(listener);
141
    }
142
 
1011 dev 143
    public void addCreatedListener(MailboxCreatedListener listener)
144
    {
145
    	createdListeners.add(listener);
146
    }
147
 
148
    public void removeCreatedListener(MailboxCreatedListener listener)
149
    {
150
    	createdListeners.remove(listener);
151
    }
152
 
153
    public void addModifiedListener(MailboxModifiedListener listener)
154
    {
155
    	modifiedListeners.add(listener);
156
    }
157
 
158
    public void removeModifiedListener(MailboxModifiedListener listener)
159
    {
160
    	modifiedListeners.remove(listener);
161
    }
162
 
949 dev 163
    public void addBeforeDeleteListener(MailboxBeforeDeleteListener listener)
164
    {
165
    	beforeDeleteListeners.add(listener);
166
    }
167
 
168
    public void removeBeforeDeleteListener(MailboxBeforeDeleteListener listener)
169
    {
170
    	beforeDeleteListeners.remove(listener);
171
    }
172
 
1014 dev 173
    public void addDeletingListener(MailboxDeletingListener listener)
174
    {
175
    	deletingListeners.add(listener);
176
    }
177
 
178
    public void removeDeletingListener(MailboxDeletingListener listener)
179
    {
180
    	deletingListeners.remove(listener);
181
    }
182
 
1011 dev 183
    public void addDeletedListener(MailboxDeletedListener listener)
184
    {
185
    	deletedListeners.add(listener);
186
    }
187
 
188
    public void removeDeletedListener(MailboxDeletedListener listener)
189
    {
190
    	deletedListeners.remove(listener);
191
    }
192
 
949 dev 193
    public Collection beforeDelete(User editor, Mailbox mailbox, Collection known)
194
		throws ModelException
195
    {
196
    	Collection cascade = new ArrayList();
197
 
198
    	for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
199
    		MailboxBeforeDeleteListener listener = (MailboxBeforeDeleteListener)i.next();
200
			Collection subcascade = listener.mailboxBeforeDelete(editor, mailbox, known);
201
    		if(subcascade != null)
202
    			cascade.addAll(subcascade);
203
    	}
204
 
205
    	return cascade;
206
    }
207
 
919 dev 208
	public void delete(User editor, Mailbox mailbox)
209
		throws ModelException
210
	{
1011 dev 211
	    // check rights
919 dev 212
		if(!mailbox.deleteableBy(editor))
213
			throw new ModelSecurityException();
214
 
1014 dev 215
        // inform deleting listeners
216
    	for(Iterator i = deletingListeners.iterator(); i.hasNext(); ) {
217
    		MailboxDeletingListener listener = (MailboxDeletingListener)i.next();
218
			listener.mailboxDeleting(editor, mailbox);
219
    	}
220
 
1011 dev 221
        // backup copy
222
        Mailbox oldMailbox = new Mailbox(mailbox);
223
 
224
        // delete it
1041 dev 225
		store.delete(mailbox);
1011 dev 226
 
1014 dev 227
        // inform deleted listeners
1011 dev 228
    	for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
229
    		MailboxDeletedListener listener = (MailboxDeletedListener)i.next();
230
			listener.mailboxDeleted(editor, oldMailbox);
231
    	}
919 dev 232
	}
233
 
234
	public Collection listMailboxes(User editor)
235
		throws ModelException
236
	{
1020 dev 237
		return listMailboxes(null, 0, 0, null, editor);
238
	}
239
 
1027 dev 240
	public Collection listMailboxes(CollectionInfo info, int rowsPerPage, int pageNumber,
1020 dev 241
			Integer[] sortingKeys, User editor)
242
		throws ModelException
243
	{
1041 dev 244
		if(editor.isSuperuser())
245
			return store.listAllMailboxes(info, rowsPerPage, pageNumber, sortingKeys);
246
		else
247
			return store.listMailboxes(info, rowsPerPage, pageNumber, sortingKeys, editor);
919 dev 248
	}
249
 
250
	public boolean areMailboxesAvailable(User editor)
251
		throws ModelException
252
	{
1073 dev 253
		if(editor.isSuperuser() || InetDomainManager.getInstance().areInetDomainsAvailable(editor))
1041 dev 254
			return true;
255
		else
256
			return store.countMailboxesAvailable(editor) > 0;
919 dev 257
	}
258
 
949 dev 259
	public Collection userBeforeDelete(User editor, User user, Collection known)
260
		throws ModelException
261
	{
1041 dev 262
        Collection mailboxes = store.listOwnMailboxes(user);
919 dev 263
 
1222 dev 264
		return iterateBeforeDelete(editor, mailboxes, known, CascadeDeleteElement.DELETE);
949 dev 265
    }
266
 
1014 dev 267
	public void userDeleting(User editor, User user)
268
		throws ModelException
269
	{
1041 dev 270
        Collection mailboxes = store.listOwnMailboxes(user);
1014 dev 271
 
272
		for(Iterator i = mailboxes.iterator(); i.hasNext(); ) {
273
			delete(editor, (Mailbox)i.next());
274
		}
275
    }
276
 
949 dev 277
	public Collection inetDomainBeforeDelete(User editor, InetDomain domain, Collection known)
278
		throws ModelException
919 dev 279
	{
1041 dev 280
        Collection mailboxes = store.listMailboxesForDomain(domain);
919 dev 281
 
1222 dev 282
		return iterateBeforeDelete(editor, mailboxes, known, CascadeDeleteElement.DELETE);
949 dev 283
    }
284
 
1014 dev 285
	public void inetDomainDeleting(User editor, InetDomain domain)
286
		throws ModelException
287
	{
1041 dev 288
        Collection mailboxes = store.listMailboxesForDomain(domain);
1014 dev 289
 
290
		for(Iterator i = mailboxes.iterator(); i.hasNext(); ) {
291
			delete(editor, (Mailbox)i.next());
292
		}
293
    }
294
 
949 dev 295
	public Collection systemUserBeforeDelete(User editor, SystemUser user, Collection known)
296
		throws ModelException
297
	{
1041 dev 298
        Collection mailboxes = store.listMailboxesForSystemUser(user);
949 dev 299
 
1222 dev 300
		return iterateBeforeDelete(editor, mailboxes, known, CascadeDeleteElement.CHANGE);
949 dev 301
    }
302
 
1014 dev 303
	public void systemUserDeleting(User editor, SystemUser user)
304
		throws ModelException
305
	{
1041 dev 306
        Collection mailboxes = store.listMailboxesForSystemUser(user);
1014 dev 307
 
308
		for(Iterator i = mailboxes.iterator(); i.hasNext(); ) {
1222 dev 309
			Mailbox mailbox = (Mailbox)i.next();
310
			mailbox.setSystemUser(null);
311
			save(editor, mailbox);
1014 dev 312
		}
313
    }
314
 
1222 dev 315
	private Collection iterateBeforeDelete(User editor, Collection mailboxes, Collection known, int action)
949 dev 316
		throws ModelException
317
	{
318
    	Collection cascade = new ArrayList();
319
		for(Iterator i = mailboxes.iterator(); i.hasNext(); ) {
320
			Mailbox mailbox = (Mailbox)i.next();
1058 dev 321
			if(known.contains(mailbox)) continue;
322
 
323
			known.add(mailbox);
949 dev 324
            if(mailbox.viewableBy(editor)) {
1222 dev 325
				if(action == CascadeDeleteElement.CHANGE && mailbox.editableBy(editor))
326
					cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.CHANGE, null));
327
				else if(action == CascadeDeleteElement.DELETE && mailbox.deleteableBy(editor))
949 dev 328
					cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.DELETE,
329
						this.beforeDelete(editor, mailbox, known)));
330
				else
331
					cascade.add(new CascadeDeleteElement(mailbox, CascadeDeleteElement.FORBIDDEN,
332
						null));
333
			}
334
			else {
335
				cascade.add(new CascadeDeleteElement(Mailbox.createLimitedCopy(mailbox),
336
					CascadeDeleteElement.FORBIDDEN, null));
337
			}
338
		}
339
 
340
    	return cascade;
919 dev 341
	}
342
 
1046 dev 343
	protected void setMailboxPasswordStores(Mailbox mailbox)
344
		throws ModelException
345
	{
346
		if(passwordStores == null) return;
347
 
348
   		try {
349
			for(int i = 0; i < passwordStores.length; i++)
350
				mailbox.addPasswordStore((PasswordStore)passwordStores[i].newInstance());
351
        }
352
        catch(Exception ex) {
353
        	throw new ModelException(ex);
354
        }
355
	}
356
 
1020 dev 357
	public static final Integer SORT_LOGIN  = new Integer(1);
358
	public static final Integer SORT_DOMAIN = new Integer(2);
919 dev 359
 
1020 dev 360
	public static final Comparator LOGIN_COMPARATOR  = new LoginComparator();
361
	public static final Comparator DOMAIN_COMPARATOR = new DomainComparator();
362
 
919 dev 363
	private static class LoginComparator
364
		implements Comparator
365
	{
366
		public int compare(Object o1, Object o2)
367
		{
368
			if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox))
369
				throw new ClassCastException("not a Mailbox");
370
 
371
		    Mailbox a1 = (Mailbox)o1;
372
		    Mailbox a2 = (Mailbox)o2;
373
 
374
		    if(a1 == null && a2 == null)
375
		    	return 0;
376
		    else if(a1 == null && a2 != null)
377
		    	return -1;
378
		    else if(a1 != null && a2 == null)
379
		    	return 1;
380
		    else
381
		    	return a1.getLogin().compareToIgnoreCase(a2.getLogin());
382
		}
383
 
384
		public boolean equals(Object obj)
385
		{
386
			return (obj instanceof LoginComparator);
387
		}
388
	}
1020 dev 389
 
390
	private static class DomainComparator
391
		implements Comparator
392
	{
393
		public int compare(Object o1, Object o2)
394
		{
395
			if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox))
396
				throw new ClassCastException("not a Mailbox");
397
 
398
		    Mailbox a1 = (Mailbox)o1;
399
		    Mailbox a2 = (Mailbox)o2;
400
 
401
		    if(a1 == null && a2 == null)
402
		    	return 0;
403
		    else if(a1 == null && a2 != null)
404
		    	return -1;
405
		    else if(a1 != null && a2 == null)
406
		    	return 1;
407
		    else
408
		    	return a1.getDomain().getName().compareToIgnoreCase(a2.getDomain().getName());
409
		}
410
 
411
		public boolean equals(Object obj)
412
		{
413
			return (obj instanceof DomainComparator);
414
		}
415
	}
1041 dev 416
 
417
	public void init(Map params)
418
		throws ModelException
419
	{
420
		try {
421
    		mailboxManager = this;
422
 
1046 dev 423
			Class c = Class.forName(((String[])params.get("store"))[0]);
1041 dev 424
			store = (MailboxStore)c.newInstance();
1046 dev 425
 
426
			String[] passwordStoreNames = (String[])params.get("passwordStore");
427
			if(passwordStoreNames != null) {
428
				passwordStores = new Class[passwordStoreNames.length];
429
				for(int i = 0; i < passwordStoreNames.length; i++) {
430
					passwordStores[i] = Class.forName(passwordStoreNames[i]);
431
				}
432
			}
1041 dev 433
        }
434
        catch(Exception ex) {
435
        	throw new ModelException(ex);
436
        }
437
	}
438
 
439
	private static MailboxManager mailboxManager = null;
440
 
441
	public static MailboxManager getInstance()
442
	{
443
		return mailboxManager;
444
	}
919 dev 445
}