Subversion Repositories general

Rev

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