KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > security > Authority


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 30.06.2004
33  */

34 package com.nightlabs.ipanema.security;
35
36 import java.io.Serializable JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import javax.jdo.PersistenceManager;
45 import javax.jdo.Query;
46 import javax.jdo.spi.PersistenceCapable;
47
48 import com.nightlabs.jdo.BaseObjectID;
49
50 /**
51  * @author marco
52  */

53
54 /**
55  * @jdo.persistence-capable
56  * identity-type = "application"
57  * objectid-class = "com.nightlabs.ipanema.security.id.AuthorityID"
58  * detachable = "true"
59  *
60  * @jdo.inheritance strategy="new-table"
61  **/

62 public class Authority implements Serializable JavaDoc
63 {
64     public static AuthoritySearchResult searchAuthorities (
65             PersistenceManager pm,
66             String JavaDoc searchStr, boolean exact, int itemsPerPage, int pageIndex)
67         throws SecurityException JavaDoc
68     {
69         if ("".equals(searchStr))
70             searchStr = null;
71         
72         if (itemsPerPage <= 0) {
73             itemsPerPage = Integer.MAX_VALUE;
74             pageIndex = 0;
75         }
76         
77         if (pageIndex < 0)
78             pageIndex = 0;
79         
80         Query query = pm.newQuery(pm.getExtent(Authority.class, true));
81         query.declareImports("import java.lang.String");
82         query.declareParameters("String searchStr");
83         StringBuffer JavaDoc filter = new StringBuffer JavaDoc();
84         if (searchStr != null) {
85             searchStr = searchStr.toLowerCase();
86             if (exact)
87                 filter.append("this.authorityID.toLowerCase() == searchStr");
88             else
89                 filter.append("this.authorityID.toLowerCase().indexOf(searchStr) >= 0");
90         }
91         query.setFilter(filter.toString());
92         query.setOrdering("this.authorityID ascending");
93         Collection JavaDoc c = (Collection JavaDoc)query.execute(searchStr);
94         int itemsFound = c.size();
95         Iterator JavaDoc it = c.iterator();
96         List JavaDoc items = new ArrayList JavaDoc();
97         int idx = 0;
98         int firstIdx = 0; int lastIdx = Integer.MAX_VALUE;
99         if (pageIndex >= 0)
100             firstIdx = itemsPerPage * pageIndex;
101         lastIdx = firstIdx + itemsPerPage - 1;
102
103         while (it.hasNext()) {
104             Authority authority = (Authority)it.next();
105             if (idx >= firstIdx)
106                 items.add(authority);
107
108             ++idx;
109             if (idx > lastIdx)
110                 break;
111         } // while (it.hasNext()) {
112
return new AuthoritySearchResult(itemsFound, itemsPerPage, pageIndex, items);
113     }
114     
115     public UserRefSearchResult searchUserRefs(
116             String JavaDoc searchStr, boolean exact, int itemsPerPage, int pageIndex)
117         throws SecurityException JavaDoc
118     {
119         PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager();
120         if (pm == null)
121             throw new IllegalStateException JavaDoc("You cannot execute this method on a transient authority!");
122
123         if ("".equals(searchStr))
124             searchStr = null;
125         
126         if (itemsPerPage <= 0) {
127             itemsPerPage = Integer.MAX_VALUE;
128             pageIndex = 0;
129         }
130         
131         if (pageIndex < 0)
132             pageIndex = 0;
133         
134         Query query = pm.newQuery(pm.getExtent(UserRef.class, true));
135         query.declareImports("import java.lang.String");
136         query.declareParameters("String authorityID, String searchStr");
137         StringBuffer JavaDoc filter = new StringBuffer JavaDoc();
138         filter.append("this.authorityID == authorityID");
139         if (searchStr != null) {
140             searchStr = searchStr.toLowerCase();
141             filter.append(" && ");
142             if (exact)
143                 filter.append("this.userID.toLowerCase() == searchStr");
144             else
145                 filter.append("this.userID.toLowerCase().indexOf(searchStr) >= 0");
146         }
147         query.setFilter(filter.toString());
148         query.setOrdering("this.userID ascending");
149         Collection JavaDoc c = (Collection JavaDoc)query.execute(authorityID, searchStr);
150         int itemsFound = c.size();
151         Iterator JavaDoc it = c.iterator();
152         List JavaDoc items = new ArrayList JavaDoc();
153         int idx = 0;
154         int firstIdx = 0; int lastIdx = Integer.MAX_VALUE;
155         if (pageIndex >= 0)
156             firstIdx = itemsPerPage * pageIndex;
157         lastIdx = firstIdx + itemsPerPage - 1;
158
159         while (it.hasNext()) {
160             UserRef userRef = (UserRef)it.next();
161             if (idx >= firstIdx)
162                 items.add(userRef);
163
164             ++idx;
165             if (idx > lastIdx)
166                 break;
167         } // while (it.hasNext()) {
168
return new UserRefSearchResult(itemsFound, itemsPerPage, pageIndex, items);
169     }
170     
171     
172     public RoleGroupRefSearchResult searchRoleGroupRefs(
173             String JavaDoc searchStr, boolean exact, int itemsPerPage, int pageIndex)
174         throws SecurityException JavaDoc
175     {
176         PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager();
177         if (pm == null)
178             throw new IllegalStateException JavaDoc("You cannot execute this method on a transient authority!");
179
180         if ("".equals(searchStr))
181             searchStr = null;
182         
183         if (itemsPerPage <= 0) {
184             itemsPerPage = Integer.MAX_VALUE;
185             pageIndex = 0;
186         }
187         
188         if (pageIndex < 0)
189             pageIndex = 0;
190         
191         Query query = pm.newQuery(pm.getExtent(RoleGroupRef.class, true));
192         query.declareImports("import java.lang.String");
193         query.declareParameters("String authorityID, String searchStr");
194         StringBuffer JavaDoc filter = new StringBuffer JavaDoc();
195         filter.append("this.authorityID == authorityID");
196         if (searchStr != null) {
197             searchStr = searchStr.toLowerCase();
198             filter.append(" && ");
199             if (exact)
200                 filter.append("this.roleGroupID.toLowerCase() == searchStr");
201             else
202                 filter.append("this.roleGroupID.toLowerCase().indexOf(searchStr) >= 0");
203         }
204         query.setFilter(filter.toString());
205         query.setOrdering("this.roleGroupID ascending");
206         Collection JavaDoc c = (Collection JavaDoc)query.execute(authorityID, searchStr);
207         int itemsFound = c.size();
208         Iterator JavaDoc it = c.iterator();
209         List JavaDoc items = new ArrayList JavaDoc();
210         int idx = 0;
211         int firstIdx = 0; int lastIdx = Integer.MAX_VALUE;
212         if (pageIndex >= 0)
213             firstIdx = itemsPerPage * pageIndex;
214         lastIdx = firstIdx + itemsPerPage - 1;
215
216         while (it.hasNext()) {
217             RoleGroupRef roleGroupRef = (RoleGroupRef)it.next();
218             if (idx >= firstIdx)
219                 items.add(roleGroupRef);
220
221             ++idx;
222             if (idx > lastIdx)
223                 break;
224         } // while (it.hasNext()) {
225
return new RoleGroupRefSearchResult(itemsFound, itemsPerPage, pageIndex, items);
226     }
227
228     /**
229      * This constant defines the authorityID that is used for representing
230      * the organisation itself.
231      */

232     public static String JavaDoc ORGANISATION_AUTHORITYID = "_Organisation_";
233
234     /**
235      * @jdo.field persistence-modifier="persistent" primary-key="true"
236      * @jdo.column length="100"
237      */

238     private String JavaDoc authorityID;
239     
240     /**
241      * key: String languageID<br/>
242      * value: String roleGroupName
243      *
244      * @jdo.field
245      * persistence-modifier="persistent"
246      * collection-type="map"
247      * key-type="java.lang.String"
248      * value-type="java.lang.String"
249      *
250      * @jdo.join
251      *
252      * @jdo.map-vendor-extension vendor-name="jpox" key="clear-on-delete" value="true"
253      * @jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 100"
254      * @jdo.map-vendor-extension vendor-name="jpox" key="value-length" value="max 255"
255      */

256     private Map JavaDoc names = new HashMap JavaDoc();
257
258     /**
259      * @jdo.field persistence-modifier="persistent"
260      * @jdo.field-vendor-extension vendor-name="jpox" key="length" value="max 255"
261      */

262     
263     /**
264      * key: String languageID<br/>
265      * value: String description
266      *
267      * @jdo.field
268      * persistence-modifier="persistent"
269      * collection-type="map"
270      * key-type="java.lang.String"
271      * value-type="java.lang.String"
272      *
273      * @jdo.join
274      *
275      * @jdo.map-vendor-extension vendor-name="jpox" key="clear-on-delete" value="true"
276      * @jdo.map-vendor-extension vendor-name="jpox" key="key-length" value="max 100"
277      * @jdo.map-vendor-extension vendor-name="jpox" key="value-length" value="max 255"
278      */

279     private Map JavaDoc descriptions = new HashMap JavaDoc();
280
281     /**
282      * key: String userID<br/>
283      * value: UserRef userRef
284      * <br/><br/>
285      * Authority (1) - (n) UserRef
286      *
287      * @jdo.field
288      * persistence-modifier="persistent"
289      * collection-type="map"
290      * key-type="java.lang.String"
291      * value-type="UserRef"
292      * mapped-by="authority"
293      *
294      * @jdo.map-vendor-extension vendor-name="jpox" key="key-field" value="userID"
295      * @jdo.map-vendor-extension vendor-name="jpox" key="clear-on-delete" value="true"
296      */

297     private Map JavaDoc userRefs = new HashMap JavaDoc();
298
299     /**
300      * key: String roleGroupID<br/>
301      * value: RoleGroupRef roleGroupRef
302      * <br/><br/>
303      * Authority (1) - (n) RoleGroupRef
304      *
305      * @jdo.field
306      * persistence-modifier="persistent"
307      * collection-type="map"
308      * key-type="java.lang.String"
309      * value-type="RoleGroupRef"
310      * mapped-by="authority"
311      *
312      * @jdo.map-vendor-extension vendor-name="jpox" key="key-field" value="roleGroupID"
313      * @!jdo.map-vendor-extension vendor-name="jpox" key="owner-field" value="authority"
314      * @jdo.map-vendor-extension vendor-name="jpox" key="clear-on-delete" value="true"
315      */

316     private Map JavaDoc roleGroupRefs = new HashMap JavaDoc();
317
318     public Authority() { }
319
320     public Authority(String JavaDoc _authorityID)
321     {
322         if (!BaseObjectID.isValidIDString(_authorityID))
323             throw new IllegalArgumentException JavaDoc("authorityID \""+_authorityID+"\" is not a valid id!");
324         this.authorityID = _authorityID;
325     }
326
327
328     /**
329      * @return Returns the authorityID.
330      */

331     public String JavaDoc getAuthorityID() {
332         return authorityID;
333     }
334     
335     
336     /**
337      * @return Returns the name.
338      */

339     public String JavaDoc getName() {
340         return (String JavaDoc)names.get(EMPTYSTRING);
341     }
342     
343     protected static String JavaDoc EMPTYSTRING = "";
344     
345     /**
346      * @return Returns the name.
347      */

348     public String JavaDoc getName(String JavaDoc languageID) {
349         if (languageID == null)
350             languageID = EMPTYSTRING;
351         String JavaDoc res = (String JavaDoc)names.get(languageID);
352         if (res == null)
353             res = (String JavaDoc)names.get(EMPTYSTRING);
354         return res;
355     }
356     /**
357      * @param name The name to set.
358      */

359     public void setName(String JavaDoc languageID, String JavaDoc name) {
360         if (languageID == null)
361             languageID = EMPTYSTRING;
362         names.put(languageID, name);
363     }
364
365     /**
366      * @return Returns the description.
367      */

368     public String JavaDoc getDescription() {
369         return (String JavaDoc)descriptions.get(EMPTYSTRING);
370     }
371     /**
372      * @return Returns the description.
373      */

374     public String JavaDoc getDescription(String JavaDoc languageID) {
375         if (languageID == null)
376             languageID = EMPTYSTRING;
377         String JavaDoc res = (String JavaDoc)descriptions.get(languageID);
378         if (res == null)
379             res = (String JavaDoc)descriptions.get(EMPTYSTRING);
380         return res;
381     }
382     /**
383      * @param description The description to set.
384      */

385     public void setDescription(String JavaDoc languageID, String JavaDoc description) {
386         if (languageID == null)
387             languageID = EMPTYSTRING;
388         descriptions.put(languageID, description);
389     }
390     
391     public UserRef createUserRef(User user)
392     {
393         UserRef userRef = _createUserRef(user, true);
394         userRef.setVisible(true);
395         return userRef;
396     }
397
398     /**
399      * This method does nothing if the UserRef already exists.
400      *
401      * @param user
402      * @param visible If the UserRef does not yet exist, whether it should be created visible.
403      * @return Returns the generated/found UserRef.
404      */

405     protected UserRef _createUserRef(User user, boolean visible)
406     {
407         UserRef userRef = (UserRef)userRefs.get(user.getUserID());
408         if (userRef == null) {
409             if (user instanceof UserGroup)
410                 userRef = new UserGroupRef(this, user, visible);
411             else
412                 userRef = new UserRef(this, user, visible);
413             user._addUserRef(userRef);
414             userRefs.put(userRef.getUserID(), userRef);
415         }
416         return userRef;
417     }
418
419     public UserRef getUserRef(String JavaDoc userID)
420     {
421         return (UserRef)userRefs.get(userID);
422     }
423
424
425     public void destroyUserRef(String JavaDoc userID)
426     {
427         _destroyUserRef(userID, true);
428     }
429
430     public void _destroyUserRef(String JavaDoc userID, boolean force)
431     {
432         UserRef userRef = (UserRef)userRefs.get(userID);
433
434         if (userRef != null) {
435             
436             if (!force && userRef.isVisible())
437                 return;
438
439             if (userRef.getUserGroupReferenceCount() > 0) {
440                 userRef.setVisible(false);
441                 return;
442             }
443
444             // It is much cleaner to manually remove all the links. Even though, some of
445
// the links are automatically removed, some are not and maybe jpox will run
446
// into trouble if sth. is deleted in the database, but not in its cache.
447
for (Iterator JavaDoc it = userRef.getRoleGroupRefs().iterator(); it.hasNext(); ) {
448                 RoleGroupRef roleGroupRef = (RoleGroupRef)it.next();
449                 userRef.removeRoleGroupRef(roleGroupRef);
450             }
451             userRef.getUser()._removeUserRef(authorityID);
452             userRefs.remove(userID); // jpox will automatically delete the userRef by this call.
453
}
454     }
455     
456     public void destroyRoleGroupRef(String JavaDoc roleGroupID)
457     {
458         RoleGroupRef roleGroupRef = (RoleGroupRef)roleGroupRefs.get(roleGroupID);
459         if (roleGroupRef != null) {
460             // It is much cleaner to manually remove all the links. Even though, some of
461
// the links are automatically removed, some are not and maybe jpox will run
462
// into trouble if sth. is deleted in the database, but not in its cache.
463
for (Iterator JavaDoc it = roleGroupRef.getUserRefs().iterator(); it.hasNext(); ) {
464                 UserRef userRef = (UserRef)it.next();
465                 userRef.removeRoleGroupRef(roleGroupRef);
466             }
467             roleGroupRef.getRoleGroup()._removeRoleGroupRef(authorityID);
468             roleGroupRefs.remove(roleGroupID); // jpox will automatically delete the roleGroupRef by this call.
469
}
470     }
471     
472     public RoleGroupRef createRoleGroupRef(RoleGroup roleGroup)
473     {
474         RoleGroupRef roleGroupRef = (RoleGroupRef)roleGroupRefs.get(roleGroup.getRoleGroupID());
475         if (roleGroupRef == null) {
476             roleGroupRef = new RoleGroupRef(this, roleGroup);
477             roleGroup._addRoleGroupRef(roleGroupRef);
478             roleGroupRefs.put(roleGroupRef.getRoleGroupID(), roleGroupRef);
479         }
480         return roleGroupRef;
481     }
482     
483     public RoleGroupRef getRoleGroupRef(String JavaDoc roleGroupID)
484     {
485         return (RoleGroupRef)roleGroupRefs.get(roleGroupID);
486     }
487
488     public static int INCLUDE_NONE = 0;
489     public static int INCLUDE_ALL = Integer.MAX_VALUE;
490
491     public void makeTransient(int includeMask)
492     {
493         PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager();
494         if (pm == null)
495             return;
496         
497         Map JavaDoc tmpNames = new HashMap JavaDoc();
498         for (Iterator JavaDoc it = names.entrySet().iterator(); it.hasNext(); ) {
499             Map.Entry JavaDoc me = (Map.Entry JavaDoc)it.next();
500             tmpNames.put(me.getKey(), me.getValue());
501         }
502         
503         Map JavaDoc tmpDescriptions = new HashMap JavaDoc();
504         for (Iterator JavaDoc it = descriptions.entrySet().iterator(); it.hasNext(); ) {
505             Map.Entry JavaDoc me = (Map.Entry JavaDoc)it.next();
506             tmpDescriptions.put(me.getKey(), me.getValue());
507         }
508         
509         pm.makeTransient(this);
510         
511         names = tmpNames;
512         descriptions = tmpDescriptions;
513     }
514     
515 }
516
Popular Tags