Rev 1058 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
924 | dev | 1 | package ak.hostadmiral.core.model; |
919 | dev | 2 | |
1041 | dev | 3 | import java.util.Collection; |
4 | import java.util.ArrayList; |
||
5 | import java.util.Iterator; |
||
6 | import java.util.Map; |
||
7 | import java.util.Comparator; |
||
8 | |||
9 | import ak.hostadmiral.util.ConfigInit; |
||
1028 | dev | 10 | import ak.hostadmiral.util.CollectionInfo; |
924 | dev | 11 | import ak.hostadmiral.util.ModelException; |
12 | import ak.hostadmiral.util.ModelSecurityException; |
||
1041 | dev | 13 | import ak.hostadmiral.core.model.store.InetDomainStore; |
919 | dev | 14 | |
15 | public class InetDomainManager |
||
1014 | dev | 16 | implements |
1041 | dev | 17 | ConfigInit, |
1014 | dev | 18 | UserBeforeDeleteListener, |
19 | UserDeletingListener |
||
919 | dev | 20 | { |
1041 | dev | 21 | private InetDomainStore store; |
946 | dev | 22 | |
1062 | dev | 23 | private Collection validateListeners = new ArrayList(); |
1011 | dev | 24 | private Collection createdListeners = new ArrayList(); |
25 | private Collection modifiedListeners = new ArrayList(); |
||
949 | dev | 26 | private Collection beforeDeleteListeners = new ArrayList(); |
1014 | dev | 27 | private Collection deletingListeners = new ArrayList(); |
1011 | dev | 28 | private Collection deletedListeners = new ArrayList(); |
949 | dev | 29 | |
1041 | dev | 30 | public InetDomainManager() |
31 | throws ModelException |
||
919 | dev | 32 | { |
946 | dev | 33 | UserManager.getInstance().addBeforeDeleteListener(this); |
1014 | dev | 34 | UserManager.getInstance().addDeletingListener(this); |
919 | dev | 35 | } |
36 | |||
37 | public InetDomain create(User editor) |
||
38 | throws ModelException |
||
39 | { |
||
40 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
||
41 | |||
42 | return new InetDomain(); |
||
43 | } |
||
44 | |||
45 | public boolean allowedToCreate(User editor) |
||
46 | throws ModelException |
||
47 | { |
||
921 | dev | 48 | return InetDomain.allowedToCreate(this, editor); |
919 | dev | 49 | } |
50 | |||
51 | public InetDomain get(User editor, Long id) |
||
52 | throws ModelException |
||
53 | { |
||
1041 | dev | 54 | InetDomain domain = store.get(id); |
919 | dev | 55 | |
56 | if(!domain.viewableBy(editor)) |
||
57 | throw new ModelSecurityException(); |
||
58 | |||
59 | return domain; |
||
60 | } |
||
61 | |||
923 | dev | 62 | public boolean nameExists(User editor, InetDomain domain, String name) |
63 | throws ModelException |
||
64 | { |
||
1041 | dev | 65 | return store.nameExists(domain, name); |
923 | dev | 66 | } |
67 | |||
1041 | dev | 68 | public InetDomain findForName(User editor, String name) |
919 | dev | 69 | throws ModelException |
70 | { |
||
1041 | dev | 71 | InetDomain domain = store.findForName(name); |
919 | dev | 72 | |
1041 | dev | 73 | if(domain != null && !domain.viewableBy(editor)) |
74 | throw new ModelSecurityException(); |
||
75 | |||
76 | return domain; |
||
919 | dev | 77 | } |
78 | |||
946 | dev | 79 | public void save(User editor, InetDomain domain) |
919 | dev | 80 | throws ModelException |
81 | { |
||
1062 | dev | 82 | // security check |
946 | dev | 83 | if(!domain.editableBy(editor)) |
919 | dev | 84 | throw new ModelSecurityException(); |
85 | |||
1062 | dev | 86 | //domain.setModUser(editor); // FIXME |
87 | |||
1011 | dev | 88 | boolean isNew = domain.isNew(); |
1062 | dev | 89 | InetDomain oldDomain = domain.getOrigin(); |
90 | if(oldDomain == null) oldDomain = domain; |
||
1011 | dev | 91 | |
1062 | dev | 92 | // validate |
93 | for(Iterator i = validateListeners.iterator(); i.hasNext(); ) { |
||
94 | InetDomainValidateListener listener = (InetDomainValidateListener)i.next(); |
||
95 | listener.inetDomainValidate(editor, domain, oldDomain); |
||
96 | } |
||
919 | dev | 97 | |
1041 | dev | 98 | store.save(domain); |
1011 | dev | 99 | |
100 | // inform listeners |
||
101 | if(isNew) { |
||
102 | for(Iterator i = createdListeners.iterator(); i.hasNext(); ) { |
||
103 | InetDomainCreatedListener listener = (InetDomainCreatedListener)i.next(); |
||
104 | listener.inetDomainCreated(editor, domain); |
||
105 | } |
||
106 | } |
||
107 | else { |
||
108 | for(Iterator i = modifiedListeners.iterator(); i.hasNext(); ) { |
||
109 | InetDomainModifiedListener listener = (InetDomainModifiedListener)i.next(); |
||
110 | listener.inetDomainModified(editor, domain, oldDomain); |
||
111 | } |
||
112 | } |
||
1045 | dev | 113 | |
114 | // reset backup |
||
115 | domain.resetOrigin(); |
||
919 | dev | 116 | } |
117 | |||
1062 | dev | 118 | public void addValidateListener(InetDomainValidateListener listener) |
119 | { |
||
120 | validateListeners.add(listener); |
||
121 | } |
||
122 | |||
123 | public void removeValidateListener(InetDomainValidateListener listener) |
||
124 | { |
||
125 | validateListeners.remove(listener); |
||
126 | } |
||
127 | |||
1011 | dev | 128 | public void addCreatedListener(InetDomainCreatedListener listener) |
949 | dev | 129 | { |
1011 | dev | 130 | createdListeners.add(listener); |
131 | } |
||
132 | |||
133 | public void removeCreatedListener(InetDomainCreatedListener listener) |
||
134 | { |
||
135 | createdListeners.remove(listener); |
||
136 | } |
||
137 | |||
138 | public void addModifiedListener(InetDomainModifiedListener listener) |
||
139 | { |
||
140 | modifiedListeners.add(listener); |
||
141 | } |
||
142 | |||
143 | public void removeModifiedListener(InetDomainModifiedListener listener) |
||
144 | { |
||
145 | modifiedListeners.remove(listener); |
||
146 | } |
||
147 | |||
148 | public void addBeforeDeleteListener(InetDomainBeforeDeleteListener listener) |
||
149 | { |
||
949 | dev | 150 | beforeDeleteListeners.add(listener); |
151 | } |
||
152 | |||
1011 | dev | 153 | public void removeBeforeDeleteListener(InetDomainBeforeDeleteListener listener) |
949 | dev | 154 | { |
155 | beforeDeleteListeners.remove(listener); |
||
156 | } |
||
157 | |||
1014 | dev | 158 | public void addDeletingListener(InetDomainDeletingListener listener) |
159 | { |
||
160 | deletingListeners.add(listener); |
||
161 | } |
||
162 | |||
163 | public void removeDeletingListener(InetDomainDeletingListener listener) |
||
164 | { |
||
165 | deletingListeners.remove(listener); |
||
166 | } |
||
167 | |||
1011 | dev | 168 | public void addDeletedListener(InetDomainDeletedListener listener) |
169 | { |
||
170 | deletedListeners.add(listener); |
||
171 | } |
||
172 | |||
173 | public void removeDeletedListener(InetDomainDeletedListener listener) |
||
174 | { |
||
175 | deletedListeners.remove(listener); |
||
176 | } |
||
177 | |||
949 | dev | 178 | public Collection beforeDelete(User editor, InetDomain domain, Collection known) |
919 | dev | 179 | throws ModelException |
946 | dev | 180 | { |
181 | Collection cascade = new ArrayList(); |
||
949 | dev | 182 | |
946 | dev | 183 | for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) { |
184 | InetDomainBeforeDeleteListener listener = (InetDomainBeforeDeleteListener)i.next(); |
||
949 | dev | 185 | Collection subcascade = listener.inetDomainBeforeDelete(editor, domain, known); |
946 | dev | 186 | if(subcascade != null) |
187 | cascade.addAll(subcascade); |
||
188 | } |
||
949 | dev | 189 | |
946 | dev | 190 | return cascade; |
191 | } |
||
192 | |||
193 | public void delete(User editor, InetDomain domain) |
||
194 | throws ModelException |
||
919 | dev | 195 | { |
1011 | dev | 196 | // check rights |
946 | dev | 197 | if(!domain.deleteableBy(editor)) |
919 | dev | 198 | throw new ModelSecurityException(); |
199 | |||
1014 | dev | 200 | // inform deleting listeners |
201 | for(Iterator i = deletingListeners.iterator(); i.hasNext(); ) { |
||
202 | InetDomainDeletingListener listener = (InetDomainDeletingListener)i.next(); |
||
203 | listener.inetDomainDeleting(editor, domain); |
||
204 | } |
||
205 | |||
1011 | dev | 206 | // backup copy |
207 | InetDomain oldDomain = new InetDomain(domain); |
||
208 | |||
209 | // delete it |
||
1041 | dev | 210 | store.delete(domain); |
919 | dev | 211 | |
1014 | dev | 212 | // inform deleted listeners |
1011 | dev | 213 | for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) { |
214 | InetDomainDeletedListener listener = (InetDomainDeletedListener)i.next(); |
||
215 | listener.inetDomainDeleted(editor, oldDomain); |
||
216 | } |
||
919 | dev | 217 | } |
218 | |||
219 | public Collection listInetDomains(User editor) |
||
220 | throws ModelException |
||
221 | { |
||
1028 | dev | 222 | return listInetDomains(null, 0, 0, null, editor); |
223 | } |
||
224 | |||
225 | public Collection listInetDomains(CollectionInfo info, int rowsPerPage, int pageNumber, |
||
226 | Integer[] sortingKeys, User editor) |
||
227 | throws ModelException |
||
228 | { |
||
1041 | dev | 229 | if(editor.isSuperuser()) |
230 | return store.listAllInetDomains(info, rowsPerPage, pageNumber, sortingKeys); |
||
231 | else |
||
232 | return store.listInetDomains(info, rowsPerPage, pageNumber, sortingKeys, editor); |
||
919 | dev | 233 | } |
234 | |||
235 | public boolean areInetDomainsAvailable(User editor) |
||
236 | throws ModelException |
||
237 | { |
||
1041 | dev | 238 | if(editor.isSuperuser()) |
239 | return true; |
||
240 | else |
||
241 | return store.countInetDomainsAvailable(editor) > 0; |
||
919 | dev | 242 | } |
243 | |||
949 | dev | 244 | public Collection userBeforeDelete(User editor, User user, Collection known) |
946 | dev | 245 | throws ModelException |
919 | dev | 246 | { |
1041 | dev | 247 | Collection domains = store.listOwnInetDomains(user); |
919 | dev | 248 | |
946 | dev | 249 | Collection cascade = new ArrayList(); |
250 | for(Iterator i = domains.iterator(); i.hasNext(); ) { |
||
251 | InetDomain d = (InetDomain)i.next(); |
||
1058 | dev | 252 | if(known.contains(d)) continue; |
253 | |||
254 | known.add(d); |
||
946 | dev | 255 | if(d.viewableBy(editor)) { |
256 | if(d.deleteableBy(editor)) |
||
257 | cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.DELETE, |
||
949 | dev | 258 | this.beforeDelete(editor, d, known))); |
946 | dev | 259 | else |
260 | cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.FORBIDDEN, null)); |
||
261 | } |
||
262 | else { |
||
263 | cascade.add(new CascadeDeleteElement(InetDomain.createLimitedCopy(d), |
||
264 | CascadeDeleteElement.FORBIDDEN, null)); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | return cascade; |
||
919 | dev | 269 | } |
270 | |||
1014 | dev | 271 | public void userDeleting(User editor, User user) |
272 | throws ModelException |
||
273 | { |
||
1041 | dev | 274 | Collection domains = store.listOwnInetDomains(user); |
1014 | dev | 275 | |
276 | for(Iterator i = domains.iterator(); i.hasNext(); ) { |
||
277 | delete(editor, (InetDomain)i.next()); |
||
278 | } |
||
279 | } |
||
280 | |||
1028 | dev | 281 | public static final Integer SORT_NAME = new Integer(1); |
282 | |||
919 | dev | 283 | public static final Comparator NAME_COMPARATOR = new NameComparator(); |
284 | |||
285 | private static class NameComparator |
||
286 | implements Comparator |
||
287 | { |
||
288 | public int compare(Object o1, Object o2) |
||
289 | { |
||
290 | if(!(o1 instanceof InetDomain) || !(o2 instanceof InetDomain)) |
||
291 | throw new ClassCastException("not a InetDomain"); |
||
292 | |||
293 | InetDomain a1 = (InetDomain)o1; |
||
294 | InetDomain a2 = (InetDomain)o2; |
||
295 | |||
296 | if(a1 == null && a2 == null) |
||
297 | return 0; |
||
298 | else if(a1 == null && a2 != null) |
||
299 | return -1; |
||
300 | else if(a1 != null && a2 == null) |
||
301 | return 1; |
||
302 | else |
||
303 | return a1.getName().compareToIgnoreCase(a2.getName()); |
||
304 | } |
||
305 | |||
306 | public boolean equals(Object obj) |
||
307 | { |
||
308 | return (obj instanceof NameComparator); |
||
309 | } |
||
310 | } |
||
1041 | dev | 311 | |
312 | public void init(Map params) |
||
313 | throws ModelException |
||
314 | { |
||
315 | try { |
||
316 | inetDomainManager = this; |
||
317 | |||
1046 | dev | 318 | Class c = Class.forName(((String[])params.get("store"))[0]); |
1041 | dev | 319 | store = (InetDomainStore)c.newInstance(); |
320 | } |
||
321 | catch(Exception ex) { |
||
322 | throw new ModelException(ex); |
||
323 | } |
||
324 | } |
||
325 | |||
326 | private static InetDomainManager inetDomainManager = null; |
||
327 | |||
328 | public static InetDomainManager getInstance() |
||
329 | { |
||
330 | return inetDomainManager; |
||
331 | } |
||
919 | dev | 332 | } |