Rev 1042 | 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 { |
||
1044 | dev | 181 | List countlist = HibernateUtil.sqlQuery( |
182 | "select count(*) from (" |
||
183 | + " select a.id from mailaliases a" |
||
184 | + " where a.owner=?" |
||
185 | + " union" |
||
186 | + " select a.id from mailaliases a" |
||
187 | + " left join domains as d on a.domain = d.id" |
||
188 | + " where d.owner=?" |
||
189 | + ") as count_table", |
||
190 | new Object[] { user.getId(), user.getId() }); |
||
191 | |||
192 | return ((Long)countlist.get(0)).intValue(); |
||
1041 | dev | 193 | } |
194 | catch(HibernateException ex) |
||
195 | { |
||
196 | throw new ModelStoreException(ex); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | public Collection listOwnMailAliases(User user) |
||
201 | throws ModelStoreException |
||
202 | { |
||
203 | try { |
||
204 | return HibernateUtil.currentSession().find( |
||
205 | "select a from MailAlias a left join fetch a.domain where a.owner = ?", |
||
206 | user, Hibernate.entity(User.class) ); |
||
207 | } |
||
208 | catch(HibernateException ex) |
||
209 | { |
||
210 | throw new ModelStoreException(ex); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | public Collection listMailAliasesForDomain(InetDomain domain) |
||
215 | throws ModelStoreException |
||
216 | { |
||
217 | try { |
||
218 | return HibernateUtil.currentSession().find( |
||
219 | "select a from MailAlias a left join fetch a.owner where a.domain = ?", |
||
220 | domain, Hibernate.entity(InetDomain.class) ); |
||
221 | } |
||
222 | catch(HibernateException ex) |
||
223 | { |
||
224 | throw new ModelStoreException(ex); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | protected static Map sortKeys = new HashMap(); |
||
229 | protected static Map sortKeysSql = new HashMap(); |
||
230 | private static boolean sortKeysInitialized = false; |
||
231 | |||
232 | private static void initSortKeys() |
||
233 | { |
||
234 | if(!sortKeysInitialized) { |
||
235 | sortKeys.put(MailAliasManager.SORT_ADDRESS, "a.address"); |
||
236 | sortKeys.put(MailAliasManager.SORT_DOMAIN, "d.name"); |
||
237 | sortKeysSql.put(MailAliasManager.SORT_ADDRESS, "address0_"); |
||
238 | sortKeysSql.put(MailAliasManager.SORT_DOMAIN, "name1_"); |
||
239 | sortKeysInitialized = true; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | private static boolean registered = false; |
||
244 | protected static void register() |
||
245 | throws ModelStoreException |
||
246 | { |
||
247 | synchronized(MailAliasHibernate.class) { |
||
248 | if(registered) return; |
||
249 | |||
250 | registered = true; |
||
251 | try { |
||
252 | HibernateUtil.getConfiguration().addResource( |
||
253 | "ak/hostadmiral/core/model/MailAlias.hbm.xml"); |
||
254 | } |
||
255 | catch(Exception ex) { |
||
256 | throw new ModelStoreException(ex); |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | } |