Subversion Repositories general

Rev

Rev 1024 | Rev 1051 | 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
 
1015 dev 3
import java.util.Iterator;
4
import java.util.Collection;
5
import java.util.HashSet;
6
 
924 dev 7
import ak.hostadmiral.util.ModelException;
8
import ak.hostadmiral.util.ModelSecurityException;
919 dev 9
 
10
/**
11
 *
12
 * @hibernate.class table="mailboxes"
13
 */
14
public class Mailbox
15
	extends GeneralModelObject
16
{
17
	private String     login;
1015 dev 18
	private Collection passwords; // Collection(PasswordStore)
919 dev 19
	private InetDomain domain;
20
	private User       owner;
21
	private Boolean    virusCheck;
22
	private Boolean    spamCheck;
23
	private SystemUser systemUser;
1011 dev 24
	private Mailbox    origin;  // save original object state before any changes
919 dev 25
 
26
	protected Mailbox()
27
	{
28
	}
29
 
1011 dev 30
	protected Mailbox(Mailbox origin)
31
	{
32
		super(origin);
33
		this.login      = origin.login;
1015 dev 34
 
35
		if(origin.passwords == null)
36
	        this.passwords = null;
37
		else
38
	        this.passwords = new HashSet(origin.passwords);
39
 
1011 dev 40
		this.domain     = origin.domain;
41
		this.owner      = origin.owner;
42
		this.virusCheck = origin.virusCheck;
43
		this.spamCheck  = origin.spamCheck;
44
		this.systemUser = origin.systemUser;
45
	}
46
 
47
    protected Mailbox getOrigin()
48
    {
49
        return origin;
50
    }
51
 
52
    protected void backupMe()
53
    {
54
        if(origin == null)
55
            origin = new Mailbox(this);
56
    }
57
 
1045 dev 58
    protected void resetOrigin()
59
    {
60
    	origin = null;
61
    }
62
 
919 dev 63
	/**
64
	 *
65
	 * @hibernate.property
66
	 */
67
	public String getLogin()
68
	{
69
		return login;
70
	}
71
 
921 dev 72
	protected void setLogin(String login)
919 dev 73
	{
74
		this.login = login;
75
	}
76
 
921 dev 77
	public void setLogin(User editor, String login)
78
		throws ModelException
79
	{
80
		if(!editableBy(editor))
81
			throw new ModelSecurityException();
82
 
1011 dev 83
        backupMe();
921 dev 84
		this.login = login;
85
	}
86
 
1015 dev 87
	protected void addPasswordStore(PasswordStore ps)
919 dev 88
	{
1015 dev 89
		if(passwords == null) passwords = new HashSet();
90
		passwords.add(ps);
919 dev 91
	}
92
 
921 dev 93
	public void setPassword(User editor, String password)
94
		throws ModelException
919 dev 95
	{
1015 dev 96
		if(!editableBy(editor))
97
			throw new ModelSecurityException();
98
 
919 dev 99
		if(password == null)
100
			throw new NullPointerException("Null password");
101
 
1011 dev 102
        backupMe();
1015 dev 103
 
104
		for(Iterator i = passwords.iterator(); i.hasNext(); ) {
105
			Object o = i.next();
106
			if(!(o instanceof PasswordStore))
107
				throw new ModelException("It's not a password store");
108
			((PasswordStore)o).setNewPassword(password);
109
		}
919 dev 110
	}
111
 
1024 dev 112
	/**
113
	 *
114
	 * @hibernate.set                    cascade="all" lazy="true"
115
	 * @hibernate.collection-key         column="obj"
116
	 * @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.PasswordStoreAbstract"
117
	 */
118
	protected Collection getPasswords()
119
	{
120
		return passwords;
121
	}
122
 
1015 dev 123
	protected void setPasswords(Collection passwords)
124
	{
125
		this.passwords = passwords;
126
	}
127
 
919 dev 128
	/**
129
	 *
130
	 * @hibernate.many-to-one
131
	 */
132
	public InetDomain getDomain()
133
	{
134
		return domain;
135
	}
136
 
921 dev 137
	protected void setDomain(InetDomain domain)
919 dev 138
	{
139
		this.domain = domain;
140
	}
141
 
921 dev 142
	public void setDomain(User editor, InetDomain domain)
143
		throws ModelException
144
	{
145
		if(!editableBy(editor))
146
			throw new ModelSecurityException();
147
 
1011 dev 148
        backupMe();
921 dev 149
		this.domain = domain;
150
	}
151
 
919 dev 152
	/**
153
	 *
154
	 * @hibernate.many-to-one
155
	 */
156
	public User getOwner()
157
	{
158
		return owner;
159
	}
160
 
921 dev 161
	protected void setOwner(User owner)
919 dev 162
	{
163
		this.owner = owner;
164
	}
165
 
921 dev 166
	public void setOwner(User editor, User owner)
167
		throws ModelException
168
	{
169
		if(!editableBy(editor))
170
			throw new ModelSecurityException();
171
 
1011 dev 172
        backupMe();
921 dev 173
		this.owner = owner;
174
	}
175
 
919 dev 176
	/**
177
	 *
178
	 * @hibernate.property
179
	 */
180
	public Boolean getVirusCheck()
181
	{
182
		return virusCheck;
183
	}
184
 
921 dev 185
	protected void setVirusCheck(Boolean virusCheck)
919 dev 186
	{
187
		this.virusCheck = virusCheck;
188
	}
189
 
921 dev 190
	public void setVirusCheck(User editor, Boolean virusCheck)
191
		throws ModelException
192
	{
193
		if(!editableBy(editor))
194
			throw new ModelSecurityException();
195
 
1011 dev 196
        backupMe();
921 dev 197
		this.virusCheck = virusCheck;
198
	}
199
 
919 dev 200
	/**
201
	 *
202
	 * @hibernate.property
203
	 */
204
	public Boolean getSpamCheck()
205
	{
206
		return spamCheck;
207
	}
208
 
921 dev 209
	protected void setSpamCheck(Boolean spamCheck)
919 dev 210
	{
211
		this.spamCheck = spamCheck;
212
	}
213
 
921 dev 214
	public void setSpamCheck(User editor, Boolean spamCheck)
215
		throws ModelException
216
	{
217
		if(!editableBy(editor))
218
			throw new ModelSecurityException();
219
 
1011 dev 220
        backupMe();
921 dev 221
		this.spamCheck = spamCheck;
222
	}
223
 
919 dev 224
	/**
225
	 *
226
	 * @hibernate.many-to-one
227
	 */
228
	public SystemUser getSystemUser()
229
	{
230
		return systemUser;
231
	}
232
 
921 dev 233
	protected void setSystemUser(SystemUser systemUser)
919 dev 234
	{
235
		this.systemUser = systemUser;
236
	}
237
 
921 dev 238
	public void setSystemUser(User editor, SystemUser systemUser)
239
		throws ModelException
240
	{
241
		if(!editableBy(editor))
242
			throw new ModelSecurityException();
243
 
1011 dev 244
        backupMe();
921 dev 245
		this.systemUser = systemUser;
246
	}
247
 
919 dev 248
	public String getTypeKey()
249
	{
924 dev 250
		return ak.hostadmiral.core.CoreResources.TYPE_MAILBOX;
919 dev 251
	}
252
 
253
	public String getIdentKey()
254
	{
924 dev 255
		return ak.hostadmiral.core.CoreResources.IDENT_MAILBOX;
919 dev 256
	}
257
 
258
	public Object[] getIdentParams()
259
	{
260
		return new Object[] { getLogin(), getDomain().getName() };
261
	}
262
 
263
	public boolean viewableBy(User user)
264
	{
265
		return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
266
	}
267
 
268
	public boolean editableBy(User user)
269
	{
1010 dev 270
		return user.isSuperuser()
271
			|| (domain == null) // just created
272
			|| user.equals(domain.getOwner());
919 dev 273
	}
274
 
275
	public boolean deleteableBy(User user)
276
	{
277
		return user.isSuperuser() || user.equals(domain.getOwner());
278
	}
921 dev 279
 
280
	protected static boolean allowedToCreate(MailboxManager manager, User editor)
281
		throws ModelException
282
	{
283
		return editor.isSuperuser()
284
			|| InetDomainManager.getInstance().areInetDomainsAvailable(editor);
285
	}
949 dev 286
 
287
	protected static Mailbox createLimitedCopy(Mailbox origin)
288
	{
1011 dev 289
		Mailbox m = new Mailbox();
290
		m.setLogin(origin.getLogin());
291
		m.setDomain(origin.getDomain());
292
		m.setOwner(origin.getOwner());
293
		return m;
949 dev 294
	}
919 dev 295
}