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