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 com.nightlabs.jdo.BaseObjectID; 49 50 53 54 62 public class Authority implements Serializable 63 { 64 public static AuthoritySearchResult searchAuthorities ( 65 PersistenceManager pm, 66 String searchStr, boolean exact, int itemsPerPage, int pageIndex) 67 throws SecurityException 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 filter = new StringBuffer (); 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 c = (Collection )query.execute(searchStr); 94 int itemsFound = c.size(); 95 Iterator it = c.iterator(); 96 List items = new ArrayList (); 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 } return new AuthoritySearchResult(itemsFound, itemsPerPage, pageIndex, items); 113 } 114 115 public UserRefSearchResult searchUserRefs( 116 String searchStr, boolean exact, int itemsPerPage, int pageIndex) 117 throws SecurityException 118 { 119 PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager(); 120 if (pm == null) 121 throw new IllegalStateException ("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 filter = new StringBuffer (); 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 c = (Collection )query.execute(authorityID, searchStr); 150 int itemsFound = c.size(); 151 Iterator it = c.iterator(); 152 List items = new ArrayList (); 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 } return new UserRefSearchResult(itemsFound, itemsPerPage, pageIndex, items); 169 } 170 171 172 public RoleGroupRefSearchResult searchRoleGroupRefs( 173 String searchStr, boolean exact, int itemsPerPage, int pageIndex) 174 throws SecurityException 175 { 176 PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager(); 177 if (pm == null) 178 throw new IllegalStateException ("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 filter = new StringBuffer (); 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 c = (Collection )query.execute(authorityID, searchStr); 207 int itemsFound = c.size(); 208 Iterator it = c.iterator(); 209 List items = new ArrayList (); 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 } return new RoleGroupRefSearchResult(itemsFound, itemsPerPage, pageIndex, items); 226 } 227 228 232 public static String ORGANISATION_AUTHORITYID = "_Organisation_"; 233 234 238 private String authorityID; 239 240 256 private Map names = new HashMap (); 257 258 262 263 279 private Map descriptions = new HashMap (); 280 281 297 private Map userRefs = new HashMap (); 298 299 316 private Map roleGroupRefs = new HashMap (); 317 318 public Authority() { } 319 320 public Authority(String _authorityID) 321 { 322 if (!BaseObjectID.isValidIDString(_authorityID)) 323 throw new IllegalArgumentException ("authorityID \""+_authorityID+"\" is not a valid id!"); 324 this.authorityID = _authorityID; 325 } 326 327 328 331 public String getAuthorityID() { 332 return authorityID; 333 } 334 335 336 339 public String getName() { 340 return (String )names.get(EMPTYSTRING); 341 } 342 343 protected static String EMPTYSTRING = ""; 344 345 348 public String getName(String languageID) { 349 if (languageID == null) 350 languageID = EMPTYSTRING; 351 String res = (String )names.get(languageID); 352 if (res == null) 353 res = (String )names.get(EMPTYSTRING); 354 return res; 355 } 356 359 public void setName(String languageID, String name) { 360 if (languageID == null) 361 languageID = EMPTYSTRING; 362 names.put(languageID, name); 363 } 364 365 368 public String getDescription() { 369 return (String )descriptions.get(EMPTYSTRING); 370 } 371 374 public String getDescription(String languageID) { 375 if (languageID == null) 376 languageID = EMPTYSTRING; 377 String res = (String )descriptions.get(languageID); 378 if (res == null) 379 res = (String )descriptions.get(EMPTYSTRING); 380 return res; 381 } 382 385 public void setDescription(String languageID, String 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 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 userID) 420 { 421 return (UserRef)userRefs.get(userID); 422 } 423 424 425 public void destroyUserRef(String userID) 426 { 427 _destroyUserRef(userID, true); 428 } 429 430 public void _destroyUserRef(String 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 for (Iterator 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); } 454 } 455 456 public void destroyRoleGroupRef(String roleGroupID) 457 { 458 RoleGroupRef roleGroupRef = (RoleGroupRef)roleGroupRefs.get(roleGroupID); 459 if (roleGroupRef != null) { 460 for (Iterator 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); } 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 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 tmpNames = new HashMap (); 498 for (Iterator it = names.entrySet().iterator(); it.hasNext(); ) { 499 Map.Entry me = (Map.Entry )it.next(); 500 tmpNames.put(me.getKey(), me.getValue()); 501 } 502 503 Map tmpDescriptions = new HashMap (); 504 for (Iterator it = descriptions.entrySet().iterator(); it.hasNext(); ) { 505 Map.Entry me = (Map.Entry )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 |