| 1 30 31 34 package com.nightlabs.ipanema.security; 35 36 import java.io.Serializable ; 37 import java.util.Collection ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.Map ; 41 42 import javax.jdo.PersistenceManager; 43 import javax.jdo.spi.PersistenceCapable; 44 45 import org.apache.log4j.Logger; 46 47 50 51 59 public class RoleGroupRef implements Serializable  60 { 61 public static Logger LOGGER = Logger.getLogger(RoleGroupRef.class); 62 63 67 private String authorityID; 68 69 73 private String roleGroupID; 74 75 79 private Authority authority; 80 81 85 private RoleGroup roleGroup; 86 87 103 private Map userRefs = new HashMap (); 104 105 public RoleGroupRef() { } 106 107 public RoleGroupRef(Authority _authority, RoleGroup _roleGroup) 108 { 109 if (_authority == null) 110 throw new NullPointerException ("authority must not be null!"); 111 112 if (_roleGroup == null) 113 throw new NullPointerException ("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 124 public String getAuthorityID() { 125 return authorityID; 126 } 127 130 public String getRoleGroupID() { 131 return roleGroupID; 132 } 133 136 public Authority getAuthority() { 137 return authority; 138 } 139 142 public RoleGroup getRoleGroup() { 143 return roleGroup; 144 } 145 146 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 166 protected void _addUserRef(UserRef userRef) 167 { 168 String 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 181 protected void _addRole(Role role) 182 { 183 for (Iterator it = getUserRefs().iterator(); it.hasNext(); ) { 185 UserRef userRef = (UserRef)it.next(); 186 userRef._addRole(role, 1); 187 } } 189 190 195 protected void _removeRole(Role role) 196 { 197 for (Iterator it = getUserRefs().iterator(); it.hasNext(); ) { 199 UserRef userRef = (UserRef)it.next(); 200 userRef._removeRole(role, 1); 201 } } 203 204 public Collection 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 tmpUserRefs = null; 230 231 if ((INCLUDE_USERREFS & includeMask) != 0) { 232 tmpUserRefs = new HashMap (); 233 for (Iterator 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 |