Subversion Repositories general

Rev

Rev 1044 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1041 dev 1
package ak.hostadmiral.core.model.store.hibernate;
2
 
3
import java.util.Collection;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.HashMap;
7
 
8
import net.sf.hibernate.Hibernate;
9
import net.sf.hibernate.HibernateException;
10
import net.sf.hibernate.type.Type;
11
 
12
import ak.hostadmiral.util.CollectionInfo;
13
import ak.hostadmiral.util.ModelStoreException;
1042 dev 14
import ak.hostadmiral.util.hibernate.HibernateUtil;
1041 dev 15
import ak.hostadmiral.core.model.User;
16
import ak.hostadmiral.core.model.InetDomain;
1221 dev 17
import ak.hostadmiral.core.model.Mailbox;
1041 dev 18
import ak.hostadmiral.core.model.MailAlias;
19
import ak.hostadmiral.core.model.MailAliasManager;
20
import ak.hostadmiral.core.model.store.MailAliasStore;
21
 
22
public class MailAliasHibernate
23
	implements MailAliasStore
24
{
25
	public MailAliasHibernate()
26
		throws ModelStoreException
27
	{
28
		initSortKeys();
29
		register();
30
	}
31
 
32
	public MailAlias get(Long id)
33
		throws ModelStoreException
34
	{
35
		try {
36
			return (MailAlias)HibernateUtil.currentSession().load(MailAlias.class, id);
37
		}
38
		catch(HibernateException ex)
39
		{
40
			throw new ModelStoreException(ex);
41
		}
42
	}
43
 
44
	public boolean addressExists(MailAlias alias, String address)
45
		throws ModelStoreException
46
	{
47
		try {
48
			if(alias.getId() == null)
49
				return ((Integer)HibernateUtil.currentSession().iterate(
50
					"select count(*) from MailAlias where address = ? and domain = ?",
51
					new Object[] { address, alias.getDomain() },
52
					new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
53
					.next()).intValue() > 0;
54
			else
55
				return ((Integer)HibernateUtil.currentSession().iterate(
56
					"select count(*) from MailAlias a where address = ? and domain = ? and a != ?",
57
					new Object[] { address, alias.getDomain(), alias },
58
					new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class),
59
						Hibernate.entity(MailAlias.class) } )
60
					.next()).intValue() > 0;
61
		}
62
		catch(HibernateException ex)
63
		{
64
			throw new ModelStoreException(ex);
65
		}
66
	}
67
 
68
	public MailAlias findForName(String name)
69
		throws ModelStoreException
70
	{
71
		try {
72
			List list = HibernateUtil.currentSession().find(
73
				"select a from MailAlias a left join fetch a.domain"
74
				+ " left join fetch a.owner where a.name=?", name, Hibernate.STRING);
75
 
76
			if(list.size() == 0)
77
				return null;
78
			else
79
				return (MailAlias)list.get(0);
80
		}
81
		catch(HibernateException ex)
82
		{
83
			throw new ModelStoreException(ex);
84
		}
85
	}
86
 
87
	public void save(MailAlias mailAlias)
88
		throws ModelStoreException
89
	{
90
		try {
91
			HibernateUtil.currentSession().saveOrUpdate(mailAlias);
92
		}
93
		catch(HibernateException ex)
94
		{
95
			throw new ModelStoreException(ex);
96
		}
97
	}
98
 
99
	public void delete(MailAlias mailAlias)
100
		throws ModelStoreException
101
	{
102
		try {
103
			HibernateUtil.currentSession().delete(mailAlias);
104
		}
105
		catch(HibernateException ex)
106
		{
107
			throw new ModelStoreException(ex);
108
		}
109
	}
110
 
111
	public Collection listAllMailAliases(CollectionInfo info, int rowsPerPage, int pageNumber,
112
			Integer[] sortingKeys)
113
		throws ModelStoreException
114
	{
115
		try {
116
			if(info != null) {
117
				info.init(((Integer)HibernateUtil.currentSession().iterate(
118
					"select count(*) from MailAlias").next()).intValue(),
119
					pageNumber, rowsPerPage);
120
			}
121
 
122
			return HibernateUtil.pageableList(rowsPerPage, pageNumber,
123
				"select a from MailAlias a left join fetch a.domain as d"
124
				+ " left join fetch a.owner"
125
				+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
126
		}
127
		catch(HibernateException ex)
128
		{
129
			throw new ModelStoreException(ex);
130
		}
131
	}
132
 
133
	public Collection listMailAliases(CollectionInfo info, int rowsPerPage, int pageNumber,
134
			Integer[] sortingKeys, User user)
135
		throws ModelStoreException
136
	{
137
		try {
138
			if(info != null) {
139
				List countlist = HibernateUtil.sqlQuery(
140
					"select count(*) from ("
141
					+ " select a.id from mailaliases a"
142
					+ "   where a.owner=?"
143
					+ " union"
144
					+ " select a.id from mailaliases a"
145
					+ "   left join domains as d on a.domain = d.id"
146
					+ "   where d.owner=?"
147
					+ ") as count_table",
148
					new Object[] { user.getId(), user.getId() });
149
 
150
				info.init(((Long)countlist.get(0)).intValue(),
151
					pageNumber, rowsPerPage);
152
			}
153
 
154
			return HibernateUtil.pageableListSql(rowsPerPage, pageNumber,
155
				"(select {a.*}, {d.*}, {o.*}"
156
				+ "   from      mailaliases as a"
157
				+ "   left join domains     as d on a.domain = d.id"
158
				+ "   left join users       as o on a.owner  = o.id"
159
				+ " where a.owner=?)"
160
				+ " union "
161
				+ "(select {a.*}, {d.*}, {o.*}"
162
				+ "   from      mailaliases as a"
163
				+ "   left join domains     as d on a.domain = d.id"
164
				+ "   left join users       as o on a.owner  = o.id"
165
				+ " where d.owner=?)"
166
				+ HibernateUtil.formOrderClause(sortingKeys, sortKeysSql),
167
				new String[] { "a", "d", "o" },
168
				new Class[] { MailAlias.class, InetDomain.class, User.class },
169
				new Object[] { user, user },
170
				new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) });
171
		}
172
		catch(HibernateException ex)
173
		{
174
			throw new ModelStoreException(ex);
175
		}
176
	}
177
 
178
	public int countMailAliasesAvailable(User user)
179
		throws ModelStoreException
180
	{
181
		try {
1044 dev 182
			List countlist = HibernateUtil.sqlQuery(
183
				"select count(*) from ("
184
				+ " select a.id from mailaliases a"
185
				+ "   where a.owner=?"
186
				+ " union"
187
				+ " select a.id from mailaliases a"
188
				+ "   left join domains as d on a.domain = d.id"
189
				+ "   where d.owner=?"
190
				+ ") as count_table",
191
				new Object[] { user.getId(), user.getId() });
192
 
193
			return ((Long)countlist.get(0)).intValue();
1041 dev 194
		}
195
		catch(HibernateException ex)
196
		{
197
			throw new ModelStoreException(ex);
198
		}
199
	}
200
 
201
	public Collection listOwnMailAliases(User user)
202
		throws ModelStoreException
203
	{
204
		try {
205
			return HibernateUtil.currentSession().find(
206
				"select a from MailAlias a left join fetch a.domain where a.owner = ?",
207
				user, Hibernate.entity(User.class) );
208
		}
209
		catch(HibernateException ex)
210
		{
211
			throw new ModelStoreException(ex);
212
		}
1221 dev 213
	}
1041 dev 214
 
215
	public Collection listMailAliasesForDomain(InetDomain domain)
216
		throws ModelStoreException
217
	{
218
		try {
219
			return HibernateUtil.currentSession().find(
220
				"select a from MailAlias a left join fetch a.owner where a.domain = ?",
221
				domain, Hibernate.entity(InetDomain.class) );
222
		}
223
		catch(HibernateException ex)
224
		{
225
			throw new ModelStoreException(ex);
226
		}
1221 dev 227
	}
1041 dev 228
 
1221 dev 229
	public Collection listMailAliasesForMailbox(Mailbox mailbox)
230
		throws ModelStoreException
231
	{
232
		try {
233
			return HibernateUtil.currentSession().find(
234
				"select a from MailAlias a left join fetch a.owner"
235
				+ " left join fetch a.destinations as dest where dest.mailbox = ?",
236
				mailbox, Hibernate.entity(Mailbox.class) );
237
		}
238
		catch(HibernateException ex)
239
		{
240
			throw new ModelStoreException(ex);
241
		}
242
	}
243
 
1041 dev 244
	protected static Map     sortKeys            = new HashMap();
245
	protected static Map     sortKeysSql         = new HashMap();
246
	private   static boolean sortKeysInitialized = false;
247
 
248
	private static void initSortKeys()
249
	{
250
		if(!sortKeysInitialized) {
251
			sortKeys.put(MailAliasManager.SORT_ADDRESS, "a.address");
252
			sortKeys.put(MailAliasManager.SORT_DOMAIN,  "d.name");
253
			sortKeysSql.put(MailAliasManager.SORT_ADDRESS, "address0_");
254
			sortKeysSql.put(MailAliasManager.SORT_DOMAIN,  "name1_");
255
			sortKeysInitialized = true;
256
		}
257
	}
258
 
259
	private static boolean registered = false;
260
	protected static void register()
261
		throws ModelStoreException
262
	{
263
		synchronized(MailAliasHibernate.class) {
264
			if(registered) return;
265
 
266
			registered = true;
267
			try {
268
				HibernateUtil.getConfiguration().addResource(
269
					"ak/hostadmiral/core/model/MailAlias.hbm.xml");
270
			}
271
			catch(Exception ex) {
272
				throw new ModelStoreException(ex);
273
			}
274
		}
275
	}
276
}