Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
904 | dev | 1 | package ak.hostcaptain.core.model; |
899 | dev | 2 | |
3 | import java.util.*; |
||
4 | import net.sf.hibernate.*; |
||
904 | dev | 5 | import ak.hostcaptain.util.HibernateUtil; |
6 | import ak.hostcaptain.util.ModelException; |
||
914 | dev | 7 | import ak.hostcaptain.util.ModelSecurityException; |
899 | dev | 8 | |
9 | public class SystemUserManager |
||
10 | { |
||
11 | private static boolean registered = false; |
||
12 | protected static void register() |
||
13 | { |
||
14 | synchronized(SystemUserManager.class) { |
||
15 | if(registered) return; |
||
16 | |||
17 | registered = true; |
||
18 | try { |
||
19 | HibernateUtil.getConfiguration().addResource( |
||
904 | dev | 20 | "/ak/hostcaptain/core/model/SystemUser.hbm.xml"); |
899 | dev | 21 | } |
22 | catch(Exception ex) { |
||
23 | ex.printStackTrace(); |
||
24 | throw new RuntimeException(ex.getMessage()); |
||
25 | } |
||
26 | } |
||
27 | } |
||
28 | |||
29 | static { |
||
30 | register(); |
||
31 | } |
||
32 | |||
33 | private SystemUserManager() |
||
34 | { |
||
35 | } |
||
36 | |||
914 | dev | 37 | public SystemUser create(User editor) |
38 | throws ModelException |
||
899 | dev | 39 | { |
914 | dev | 40 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
41 | |||
899 | dev | 42 | return new SystemUser(); |
43 | } |
||
44 | |||
914 | dev | 45 | public boolean allowedToCreate(User editor) |
899 | dev | 46 | throws ModelException |
47 | { |
||
914 | dev | 48 | return editor.isSuperuser(); |
49 | } |
||
50 | |||
51 | public SystemUser get(User editor, Long id) |
||
52 | throws ModelException |
||
53 | { |
||
54 | SystemUser user; |
||
55 | |||
899 | dev | 56 | try { |
914 | dev | 57 | user = (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id); |
899 | dev | 58 | } |
59 | catch(HibernateException ex) |
||
60 | { |
||
61 | throw new ModelException(ex); |
||
62 | } |
||
914 | dev | 63 | |
64 | if(!user.viewableBy(editor)) |
||
65 | throw new ModelSecurityException(); |
||
66 | |||
67 | return user; |
||
899 | dev | 68 | } |
69 | |||
914 | dev | 70 | protected SystemUser findForName(String name) |
899 | dev | 71 | throws ModelException |
72 | { |
||
73 | try { |
||
74 | List list = HibernateUtil.currentSession().find( |
||
75 | "from SystemUser where name=?", name, Hibernate.STRING); |
||
76 | |||
77 | if(list.size() == 0) |
||
78 | return null; |
||
79 | else |
||
80 | return (SystemUser)list.get(0); |
||
81 | } |
||
82 | catch(HibernateException ex) |
||
83 | { |
||
84 | throw new ModelException(ex); |
||
85 | } |
||
86 | } |
||
87 | |||
914 | dev | 88 | protected SystemUser findForUid(Integer uid) |
899 | dev | 89 | throws ModelException |
90 | { |
||
91 | try { |
||
92 | List list = HibernateUtil.currentSession().find( |
||
93 | "from SystemUser where uid=?", uid, Hibernate.INTEGER); |
||
94 | |||
95 | if(list.size() == 0) |
||
96 | return null; |
||
97 | else |
||
98 | return (SystemUser)list.get(0); |
||
99 | } |
||
100 | catch(HibernateException ex) |
||
101 | { |
||
102 | throw new ModelException(ex); |
||
103 | } |
||
104 | } |
||
105 | |||
914 | dev | 106 | public void save(User editor, SystemUser systemUser) |
899 | dev | 107 | throws ModelException |
108 | { |
||
914 | dev | 109 | if(!systemUser.editableBy(editor)) |
110 | throw new ModelSecurityException(); |
||
111 | |||
899 | dev | 112 | try { |
113 | HibernateUtil.currentSession().saveOrUpdate(systemUser); |
||
114 | } |
||
115 | catch(HibernateException ex) |
||
116 | { |
||
117 | throw new ModelException(ex); |
||
118 | } |
||
119 | } |
||
120 | |||
914 | dev | 121 | public void delete(User editor, SystemUser systemUser) |
899 | dev | 122 | throws ModelException |
123 | { |
||
914 | dev | 124 | if(!systemUser.deleteableBy(editor)) |
125 | throw new ModelSecurityException(); |
||
126 | |||
899 | dev | 127 | try { |
128 | HibernateUtil.currentSession().delete(systemUser); |
||
129 | } |
||
130 | catch(HibernateException ex) |
||
131 | { |
||
132 | throw new ModelException(ex); |
||
133 | } |
||
134 | } |
||
135 | |||
914 | dev | 136 | public Collection listSystemUsers(User editor) |
899 | dev | 137 | throws ModelException |
138 | { |
||
139 | try { |
||
914 | dev | 140 | if(editor.isSuperuser()) |
141 | return HibernateUtil.currentSession().find("from SystemUser"); |
||
142 | else |
||
143 | return HibernateUtil.currentSession().find( |
||
144 | "select u from SystemUser u left join u.owner o where o is null or o=?", |
||
145 | editor, Hibernate.entity(User.class)); |
||
899 | dev | 146 | } |
147 | catch(HibernateException ex) |
||
148 | { |
||
149 | throw new ModelException(ex); |
||
150 | } |
||
151 | } |
||
152 | |||
914 | dev | 153 | public boolean areSystemUsersAvailable(User editor) |
154 | throws ModelException |
||
155 | { |
||
156 | try { |
||
157 | if(editor.isSuperuser()) |
||
158 | return true; |
||
159 | else |
||
160 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
161 | "select count(*) from SystemUser u left join u.owner o where o is null or o=?", |
||
162 | editor, Hibernate.entity(User.class)).next()).intValue() > 0; |
||
163 | } |
||
164 | catch(HibernateException ex) |
||
165 | { |
||
166 | throw new ModelException(ex); |
||
167 | } |
||
168 | } |
||
169 | |||
899 | dev | 170 | private static SystemUserManager systemUserManager = null; |
171 | |||
172 | public static SystemUserManager getInstance() |
||
173 | { |
||
174 | if(systemUserManager == null) |
||
175 | systemUserManager = new SystemUserManager(); |
||
176 | |||
177 | return systemUserManager; |
||
178 | } |
||
179 | |||
180 | public static final Comparator UID_COMPARATOR = new UidComparator(); |
||
181 | public static final Comparator NAME_COMPARATOR = new NameComparator(); |
||
182 | |||
183 | private static class UidComparator |
||
184 | implements Comparator |
||
185 | { |
||
186 | public int compare(Object o1, Object o2) |
||
187 | { |
||
188 | if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser)) |
||
189 | throw new ClassCastException("not a SystemUser"); |
||
190 | |||
191 | SystemUser a1 = (SystemUser)o1; |
||
192 | SystemUser a2 = (SystemUser)o2; |
||
193 | |||
194 | if(a1 == null && a2 == null) |
||
195 | return 0; |
||
196 | else if(a1 == null && a2 != null) |
||
197 | return -1; |
||
198 | else if(a1 != null && a2 == null) |
||
199 | return 1; |
||
200 | else |
||
201 | return a1.getUid().compareTo(a2.getUid()); |
||
202 | } |
||
203 | |||
204 | public boolean equals(Object obj) |
||
205 | { |
||
206 | return (obj instanceof UidComparator); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | private static class NameComparator |
||
211 | implements Comparator |
||
212 | { |
||
213 | public int compare(Object o1, Object o2) |
||
214 | { |
||
215 | if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser)) |
||
216 | throw new ClassCastException("not a SystemUser"); |
||
217 | |||
218 | SystemUser a1 = (SystemUser)o1; |
||
219 | SystemUser a2 = (SystemUser)o2; |
||
220 | |||
221 | if(a1 == null && a2 == null) |
||
222 | return 0; |
||
223 | else if(a1 == null && a2 != null) |
||
224 | return -1; |
||
225 | else if(a1 != null && a2 == null) |
||
226 | return 1; |
||
227 | else |
||
228 | return a1.getName().compareToIgnoreCase(a2.getName()); |
||
229 | } |
||
230 | |||
231 | public boolean equals(Object obj) |
||
232 | { |
||
233 | return (obj instanceof NameComparator); |
||
234 | } |
||
235 | } |
||
236 | } |