KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Map JavaDoc;
41
42 import javax.jdo.PersistenceManager;
43 import javax.jdo.spi.PersistenceCapable;
44
45 import org.apache.log4j.Logger;
46
47 /**
48  * @author marco
49  */

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

59 public class RoleGroupRef implements Serializable JavaDoc
60 {
61     public static Logger LOGGER = Logger.getLogger(RoleGroupRef.class);
62     
63     /**
64      * @jdo.field persistence-modifier="persistent" primary-key="true"
65      * @jdo.column length="100"
66      */

67     private String JavaDoc authorityID;
68
69     /**
70      * @jdo.field persistence-modifier="persistent" primary-key="true"
71      * @jdo.column length="100"
72      */

73     private String JavaDoc roleGroupID;
74
75     /**
76      * @jdo.field persistence-modifier="persistent"
77      * @jdo.field-vendor-extension vendor-name="jpox" key="map-field" value="roleGroupRefs"
78      */

79     private Authority authority;
80
81     /**
82      * @jdo.field persistence-modifier="persistent"
83      * @jdo.field-vendor-extension vendor-name="jpox" key="map-field" value="roleGroupRefs"
84      */

85     private RoleGroup roleGroup;
86
87     /**
88      * key: String userID<br/>
89      * value: UserRef userRef
90      * <br/><br/>
91      * UserRef (m) - (n) RoleGroupRef
92      *
93      * @jdo.field
94      * persistence-modifier="persistent"
95      * collection-type="map"
96      * key-type="java.lang.String"
97      * value-type="UserRef"
98      *
99      * @jdo.join
100      *
101      * @jdo.map-vendor-extension vendor-name="jpox" key="key-field" value="userID"
102      */

103     private Map JavaDoc userRefs = new HashMap JavaDoc();
104     
105     public RoleGroupRef() { }
106     
107     public RoleGroupRef(Authority _authority, RoleGroup _roleGroup)
108     {
109         if (_authority == null)
110             throw new NullPointerException JavaDoc("authority must not be null!");
111         
112         if (_roleGroup == null)
113             throw new NullPointerException JavaDoc("roleGroup must not be null!");
114         
115         this.authorityID = _authority.getAuthorityID();
116         this.roleGroupID = _roleGroup.getRoleGroupID();
117         this.authority = _authority;
118         this.roleGroup = _roleGroup;
119     }
120
121     /**
122      * @return Returns the authorityID.
123      */

124     public String JavaDoc getAuthorityID() {
125         return authorityID;
126     }
127     /**
128      * @return Returns the roleGroupID.
129      */

130     public String JavaDoc getRoleGroupID() {
131         return roleGroupID;
132     }
133     /**
134      * @return Returns the authority.
135      */

136     public Authority getAuthority() {
137         return authority;
138     }
139     /**
140      * @return Returns the roleGroup.
141      */

142     public RoleGroup getRoleGroup() {
143         return roleGroup;
144     }
145     
146     /**
147      * This method is called by the User if this roleGroup is removed there.
148      * It does not update the RoleRefs, because User.removeRoleGroup(...) does
149      * that already.
150      *
151      * @param user
152      */

153     protected void _removeUserRef(UserRef userRef)
154     {
155         if (userRefs.remove(userRef.getUserID()) == null)
156             LOGGER.warn("_removeUserRef("+userRef+"): user did not exist in map!");
157     }
158
159     /**
160      * This method is called by the UserRef if this roleGroupRef is added there. It
161      * does not update the RoleRefs, because UserRef.addRoleGroup(...) does that
162      * already.
163      *
164      * @param user
165      */

166     protected void _addUserRef(UserRef userRef)
167     {
168         String JavaDoc userID = userRef.getUserID();
169         if (userRefs.containsKey(userID)) {
170             LOGGER.warn("_addUserRef("+userRef+"): userRef already exists in map!");
171             return;
172         }
173         userRefs.put(userID, userRef);
174     }
175
176     /**
177      * This method makes sure, all userRefs get a direct link to the role via a roleRef.
178      *
179      * @param role
180      */

181     protected void _addRole(Role role)
182     {
183         // Add this role to all UserRefs of this RoleGroupRef
184
for (Iterator JavaDoc it = getUserRefs().iterator(); it.hasNext(); ) {
185             UserRef userRef = (UserRef)it.next();
186             userRef._addRole(role, 1);
187         } // for (Iterator it = users.iterator(); it.hasNext(); ) {
188
}
189
190     /**
191      * This method makes sure, all direct links to the role via a roleRef will be removed in all userRefs.
192      *
193      * @param role
194      */

195     protected void _removeRole(Role role)
196     {
197         // Add this role to all UserRefs of this RoleGroupRef
198
for (Iterator JavaDoc it = getUserRefs().iterator(); it.hasNext(); ) {
199             UserRef userRef = (UserRef)it.next();
200             userRef._removeRole(role, 1);
201         } // for (Iterator it = users.iterator(); it.hasNext(); ) {
202
}
203
204     public Collection JavaDoc getUserRefs()
205     {
206         return userRefs.values();
207     }
208     
209     public static int INCLUDE_NONE = 0;
210     public static int INCLUDE_AUTHORITY = 0x1;
211     public static int INCLUDE_ROLEGROUP = 0x2;
212     public static int INCLUDE_USERREFS = 0x4;
213     public static int INCLUDE_ALL = Integer.MAX_VALUE;
214     
215     public void makeTransient(int includeMask)
216     {
217         PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager();
218         if (pm == null)
219             return;
220         
221         pm.retrieve(this);
222         
223         if ((INCLUDE_AUTHORITY & includeMask) != 0)
224             getAuthority().makeTransient(Authority.INCLUDE_NONE);
225         
226         if ((INCLUDE_ROLEGROUP & includeMask) != 0)
227             getRoleGroup().makeTransient(RoleGroup.INCLUDE_NONE);
228         
229         Map JavaDoc tmpUserRefs = null;
230         
231         if ((INCLUDE_USERREFS & includeMask) != 0) {
232             tmpUserRefs = new HashMap JavaDoc();
233             for (Iterator JavaDoc it = getUserRefs().iterator(); it.hasNext(); ) {
234                 UserRef userRef = (UserRef)it.next();
235                 userRef.makeTransient(UserRef.INCLUDE_NONE);
236                 tmpUserRefs.put(userRef.getUserID(), userRef);
237             }
238         }
239
240         pm.makeTransient(this);
241         
242         if ((INCLUDE_AUTHORITY & includeMask) == 0)
243             authority = null;
244         
245         if ((INCLUDE_ROLEGROUP & includeMask) == 0)
246             roleGroup = null;
247
248         userRefs = tmpUserRefs;
249     }
250
251 }
252
Popular Tags