Subversion Repositories general

Rev

Rev 961 | Rev 1011 | 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 MailAliasManager
11
{
12
	private static boolean registered = false;
13
	protected static void register()
14
	{
15
		synchronized(MailAliasManager.class) {
16
			if(registered) return;
17
 
18
			registered = true;
19
			try {
20
				HibernateUtil.getConfiguration().addResource(
950 dev 21
					"ak/hostadmiral/core/model/MailAlias.hbm.xml");
919 dev 22
			}
23
			catch(Exception ex) {
24
				ex.printStackTrace();
25
				throw new RuntimeException(ex.getMessage());
26
			}
27
		}
28
	}
29
 
30
	static {
31
		register();
32
	}
33
 
34
	private MailAliasManager()
35
	{
36
	}
37
 
38
	public MailAlias create(User editor)
39
		throws ModelException
40
	{
41
		if(!allowedToCreate(editor)) throw new ModelSecurityException();
42
 
43
		MailAlias alias = new MailAlias();
44
		alias.setDestinations(new ArrayList());
45
		return alias;
46
	}
47
 
48
	public boolean allowedToCreate(User editor)
49
		throws ModelException
50
	{
921 dev 51
		return MailAlias.allowedToCreate(this, editor);
919 dev 52
	}
53
 
54
	public MailAlias get(User editor, Long id)
55
		throws ModelException
56
	{
57
		MailAlias alias;
58
 
59
		try {
60
			alias = (MailAlias)HibernateUtil.currentSession().load(MailAlias.class, id);
61
		}
62
		catch(HibernateException ex)
63
		{
64
			throw new ModelException(ex);
65
		}
66
 
67
		if(!alias.viewableBy(editor))
68
			throw new ModelSecurityException();
69
 
70
		return alias;
71
	}
72
 
926 dev 73
	public boolean addressExists(User editor, MailAlias alias, String address)
74
		throws ModelException
75
	{
76
		if(alias.getDomain() == null)
77
			throw new ModelException("Cannot check unique address for mail alias without domain");
78
 
79
		try {
80
			if(alias.getId() == null)
81
				return ((Integer)HibernateUtil.currentSession().iterate(
82
					"select count(*) from MailAlias where address = ? and domain = ?",
83
					new Object[] { address, alias.getDomain() },
84
					new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
85
					.next()).intValue() > 0;
86
			else
87
				return ((Integer)HibernateUtil.currentSession().iterate(
88
					"select count(*) from MailAlias a where address = ? and domain = ? and a != ?",
89
					new Object[] { address, alias.getDomain(), alias },
90
					new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class),
91
						Hibernate.entity(MailAlias.class) } )
92
					.next()).intValue() > 0;
93
		}
94
		catch(HibernateException ex)
95
		{
96
			throw new ModelException(ex);
97
		}
98
	}
99
 
919 dev 100
	protected MailAlias findForName(String name)
101
		throws ModelException
102
	{
103
		try {
104
			List list = HibernateUtil.currentSession().find(
105
				"from MailAlias where name=?", name, Hibernate.STRING);
106
 
107
			if(list.size() == 0)
108
				return null;
109
			else
110
				return (MailAlias)list.get(0);
111
		}
112
		catch(HibernateException ex)
113
		{
114
			throw new ModelException(ex);
115
		}
116
	}
117
 
118
	public void save(User editor, MailAlias mailAlias)
119
		throws ModelException
120
	{
121
		if(!mailAlias.editableBy(editor))
122
			throw new ModelSecurityException();
123
 
1010 dev 124
		//mailAlias.setModUser(editor);  // FIXME
919 dev 125
 
126
		try {
127
			HibernateUtil.currentSession().saveOrUpdate(mailAlias);
128
		}
129
		catch(HibernateException ex)
130
		{
131
			throw new ModelException(ex);
132
		}
133
	}
134
 
135
	public void delete(User editor, MailAlias mailAlias)
136
		throws ModelException
137
	{
138
		if(!mailAlias.deleteableBy(editor))
139
			throw new ModelSecurityException();
140
 
141
		try {
142
			HibernateUtil.currentSession().delete(mailAlias);
143
		}
144
		catch(HibernateException ex)
145
		{
146
			throw new ModelException(ex);
147
		}
148
	}
149
 
150
	public Collection listMailAliases(User editor)
151
		throws ModelException
152
	{
153
		try {
154
			if(editor.isSuperuser())
155
				return HibernateUtil.currentSession().find("from MailAlias");
156
			else
157
				return HibernateUtil.currentSession().find(
158
					"select a from MailAlias a left join a.domain as d"
159
					+ " where d.owner=? or a.owner=?",
160
					new Object[] { editor, editor },
161
					new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
162
		}
163
		catch(HibernateException ex)
164
		{
165
			throw new ModelException(ex);
166
		}
167
	}
168
 
169
	public boolean areMailAliasesAvailable(User editor)
170
		throws ModelException
171
	{
172
		try {
173
			if(editor.isSuperuser()
174
				|| InetDomainManager.getInstance().areInetDomainsAvailable(editor))
175
			{
176
				return true;
177
			}
178
			else {
179
				return ((Integer)HibernateUtil.currentSession().iterate(
180
					"select count(*) from MailAlias a left join a.domain as d"
181
					+ " where d.owner=? or a.owner=?",
182
					new Object[] { editor, editor },
183
					new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) })
184
					.next()).intValue() > 0;
185
			}
186
		}
187
		catch(HibernateException ex)
188
		{
189
			throw new ModelException(ex);
190
		}
191
	}
192
 
193
	private static MailAliasManager mailAliasManager = null;
194
 
195
	public static MailAliasManager getInstance()
196
	{
197
		if(mailAliasManager == null)
198
			mailAliasManager = new MailAliasManager();
199
 
200
		return mailAliasManager;
201
	}
202
 
203
	public static final Comparator ADDRESS_COMPARATOR = new AddressComparator();
204
 
205
	private static class AddressComparator
206
		implements Comparator
207
	{
208
		public int compare(Object o1, Object o2)
209
		{
210
			if(!(o1 instanceof MailAlias) || !(o2 instanceof MailAlias))
211
				throw new ClassCastException("not a MailAlias");
212
 
213
		    MailAlias a1 = (MailAlias)o1;
214
		    MailAlias a2 = (MailAlias)o2;
215
 
216
		    if(a1 == null && a2 == null)
217
		    	return 0;
218
		    else if(a1 == null && a2 != null)
219
		    	return -1;
220
		    else if(a1 != null && a2 == null)
221
		    	return 1;
222
		    else
223
		    	return a1.getAddress().compareToIgnoreCase(a2.getAddress());
224
		}
225
 
226
		public boolean equals(Object obj)
227
		{
228
			return (obj instanceof AddressComparator);
229
		}
230
	}
231
}