1 30 31 34 package com.nightlabs.ipanema.security; 35 36 import java.io.Serializable ; 37 import java.util.ArrayList ; 38 import java.util.Collection ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Map ; 43 44 import javax.jdo.PersistenceManager; 45 import javax.jdo.Query; 46 import javax.jdo.spi.PersistenceCapable; 47 48 import org.apache.log4j.Logger; 49 50 import com.nightlabs.jdo.BaseObjectID; 51 52 53 56 57 65 public class RoleGroup implements Serializable 66 { 67 public static final Logger LOGGER = Logger.getLogger(RoleGroup.class); 68 69 public static RoleGroupSearchResult searchRoleGroups ( 70 PersistenceManager pm, 71 String searchStr, boolean exact, int itemsPerPage, int pageIndex) 72 throws SecurityException 73 { 74 if ("".equals(searchStr)) 75 searchStr = null; 76 77 if (itemsPerPage <= 0) { 78 itemsPerPage = Integer.MAX_VALUE; 79 pageIndex = 0; 80 } 81 82 if (pageIndex < 0) 83 pageIndex = 0; 84 85 Query query = pm.newQuery(pm.getExtent(RoleGroup.class, true)); 86 query.declareImports("import java.lang.String"); 87 query.declareParameters("String searchStr"); 88 StringBuffer filter = new StringBuffer (); 89 if (searchStr != null) { 90 searchStr = searchStr.toLowerCase(); 91 if (exact) 92 filter.append("this.roleGroupID.toLowerCase() == searchStr"); 93 else 94 filter.append("this.roleGroupID.toLowerCase().indexOf(searchStr) >= 0"); 95 } 96 query.setFilter(filter.toString()); 97 query.setOrdering("this.roleGroupID ascending"); 98 Collection c = (Collection )query.execute(searchStr); 99 int itemsFound = c.size(); 100 Iterator it = c.iterator(); 101 List items = new ArrayList (); 102 int idx = 0; 103 int firstIdx = 0; int lastIdx = Integer.MAX_VALUE; 104 if (pageIndex >= 0) 105 firstIdx = itemsPerPage * pageIndex; 106 lastIdx = firstIdx + itemsPerPage - 1; 107 108 while (it.hasNext()) { 109 RoleGroup roleGroup = (RoleGroup)it.next(); 110 if (idx >= firstIdx) 111 items.add(roleGroup); 112 113 ++idx; 114 if (idx > lastIdx) 115 break; 116 } return new RoleGroupSearchResult(itemsFound, itemsPerPage, pageIndex, items); 118 } 119 120 124 private String roleGroupID; 125 126 143 private Map names = new HashMap (); 144 145 162 private Map descriptions = new HashMap (); 163 164 180 private Map roles = new HashMap (); 181 182 198 private Map roleGroupRefs = new HashMap (); 199 200 public RoleGroup() { } 201 202 public RoleGroup(String _roleGroupID) 203 { 204 if (!BaseObjectID.isValidIDString(_roleGroupID)) 205 throw new IllegalArgumentException ("roleID \""+_roleGroupID+"\" is not a valid id!"); 206 this.roleGroupID = _roleGroupID; 207 } 208 209 212 public String getRoleGroupID() { 213 return roleGroupID; 214 } 215 216 219 public String getName() { 220 return (String )names.get(EMPTYSTRING); 221 } 222 223 protected static String EMPTYSTRING = ""; 224 225 228 public String getName(String languageID) { 229 if (languageID == null) 230 languageID = EMPTYSTRING; 231 String res = (String )names.get(languageID); 232 if (res == null) 233 res = (String )names.get(EMPTYSTRING); 234 return res; 235 } 236 239 public void setName(String languageID, String name) { 240 if (languageID == null) 241 languageID = EMPTYSTRING; 242 names.put(languageID, name); 243 } 244 245 248 public String getDescription() { 249 return (String )descriptions.get(EMPTYSTRING); 250 } 251 254 public String getDescription(String languageID) { 255 if (languageID == null) 256 languageID = EMPTYSTRING; 257 String res = (String )descriptions.get(languageID); 258 if (res == null) 259 res = (String )descriptions.get(EMPTYSTRING); 260 return res; 261 } 262 265 public void setDescription(String languageID, String description) { 266 if (languageID == null) 267 languageID = EMPTYSTRING; 268 descriptions.put(languageID, description); 269 } 270 271 274 public Collection getRoles() { 275 return roles.values(); 276 } 277 284 287 public Collection getRoleGroupRefs() { 288 return roleGroupRefs.values(); 289 } 290 291 296 public Role getRole(String roleID) 297 { 298 return (Role)roles.get(roleID); 299 } 300 301 311 328 341 public void addRole(Role role) 342 { 343 String roleID = role.getRoleID(); 344 LOGGER.debug("addRole(...): before: if (roles.containsKey(roleID))"); 345 if (roles.containsKey(roleID)) 346 return; 347 LOGGER.debug("addRole(...): before: roles.put(roleID, role);"); 348 roles.put(roleID, role); 349 LOGGER.debug("addRole(...): before: role._addRoleGroup(this);"); 350 role._addRoleGroup(this); 351 352 LOGGER.debug("addRole(...): before: for (Iterator it = getRoleGroupRefs().iterator(); it.hasNext(); ) {"); 354 for (Iterator it = getRoleGroupRefs().iterator(); it.hasNext(); ) { 355 RoleGroupRef roleGroupRef = (RoleGroupRef)it.next(); 356 roleGroupRef._addRole(role); 357 } 358 359 } 365 366 public void removeRole(Role role) 367 { 368 String roleID = role.getRoleID(); 369 if (!roles.containsKey(roleID)) 370 return; 371 role._removeRoleGroup(this); 372 373 for (Iterator it = getRoleGroupRefs().iterator(); it.hasNext(); ) { 375 RoleGroupRef roleGroupRef = (RoleGroupRef)it.next(); 376 roleGroupRef._removeRole(role); 377 } 378 } 384 385 public boolean containsRole(Role role) 386 { 387 return roles.containsKey(role.getRoleID()); 388 } 389 390 public boolean containsRole(String roleID) 391 { 392 return roles.containsKey(roleID); 393 } 394 395 protected void _addRoleGroupRef(RoleGroupRef roleGroupRef) 396 { 397 if (roleGroupRef == null) 398 throw new NullPointerException ("roleGroupRef must not be null!"); 399 400 if (!this.roleGroupID.equals(roleGroupRef.getRoleGroupID())) 401 throw new IllegalArgumentException ("roleGroupRef.roleGroupID invalid!"); 402 403 roleGroupRefs.put(roleGroupRef.getAuthorityID(), roleGroupRef); 404 } 405 406 protected void _removeRoleGroupRef(String authorityID) 407 { 408 roleGroupRefs.remove(authorityID); 409 } 410 411 public RoleGroupRef getRoleGroupRef(String authorityID) 412 { 413 return (RoleGroupRef)roleGroupRefs.get(authorityID); 414 } 415 416 public static final int INCLUDE_NONE = 0; 417 public static final int INCLUDE_ROLES = 0x2; 419 420 public void makeTransient(int includeMask) 421 { 422 PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager(); 423 if (pm == null) 424 return; 425 426 pm.retrieve(this); 427 428 439 Map tmpRoles = new HashMap (); 440 if ((includeMask & INCLUDE_ROLES) != 0) { 441 int roleIncludeMask = 0; 442 443 for (Iterator it = getRoles().iterator(); it.hasNext(); ) { 444 Role role = (Role)it.next(); 445 role.makeTransient(roleIncludeMask); 446 tmpRoles.put(role.getRoleID(), role); 447 } 448 } 449 450 pm.makeTransient(this); 451 452 roles = tmpRoles; 454 } 455 456 } 457 | Popular Tags |