| 1 4 5 package org.enhydra.shark.usergroup; 6 7 import java.util.ArrayList ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Set ; 12 13 import net.sf.hibernate.Query; 14 import net.sf.hibernate.Session; 15 16 import org.enhydra.shark.api.RootException; 17 import org.enhydra.shark.api.UserTransaction; 18 import org.enhydra.shark.api.internal.usergroup.UserGroupManager; 19 import org.enhydra.shark.api.internal.working.CallbackUtilities; 20 import org.enhydra.shark.usertransaction.SharkHibernateUserTransaction; 21 import org.enhydra.shark.utilities.hibernate.HibernateUtilities; 22 23 27 public class HibernateUserGroupManager implements UserGroupManager { 28 29 private CallbackUtilities callback; 30 31 35 public void configure(CallbackUtilities cus) throws RootException { 36 if (null == cus) 37 throw new RootException("Cannot configure without call back impl."); 38 callback = cus; 39 HibernateUtilities.init(callback.getProperties()); 40 callback.debug("HibernateUserGroupManager configured"); 41 } 42 43 47 56 public void createGroup(UserTransaction t, String groupName, String description) throws RootException { 57 58 if (doesGroupExistInternal(t, groupName)) { 59 throw new RootException("Group already exists"); 60 } 61 try { 62 Session session = ((SharkHibernateUserTransaction) t).getSession(); 63 HibernateGroup group = createGroup(); 64 group.setGid(groupName); 65 group.setDescription(description); 66 session.save(group); 67 endTransaction(t, true); 68 } catch (Exception e) { 69 e.printStackTrace(); 70 if (e instanceof RootException) { 71 throw (RootException) e; 72 } 73 throw new RootException(e); 74 } 75 } 76 77 87 public void updateGroup(UserTransaction t, String groupName, String description) throws RootException { 88 try { 89 Session session = ((SharkHibernateUserTransaction) t).getSession(); 90 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 91 if (group == null) 92 return; 93 group.setDescription(description); 94 session.update(group); 95 endTransaction(t, true); 96 } catch (Exception e) { 97 e.printStackTrace(); 98 if (e instanceof RootException) { 99 throw (RootException) e; 100 } 101 throw new RootException(e); 102 } 103 } 104 105 125 public void createUser (UserTransaction t, 126 String groupName, 127 String username, 128 String password, 129 String firstName, 130 String lastName, 131 String emailAddress) throws RootException { 132 if (doesUserExistInternal(t, username)) { 133 throw new RootException("User already exists"); 134 } 135 try { 136 Session session = ((SharkHibernateUserTransaction) t).getSession(); 137 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 138 if (group == null) 139 return; 140 HibernateUser user = createUser(); 141 user.setUid(username); 142 user.setPasswd(passwordDigest(password)); 143 user.setFirstName(firstName); 144 user.setLastName(lastName); 145 user.setEmail(emailAddress); 146 user.addGroup(group); 147 group.addUser(user); 148 session.save(user); 149 session.update(group); 150 endTransaction(t, true); 151 } catch (Exception e) { 152 e.printStackTrace(); 153 if (e instanceof RootException) { 154 throw (RootException) e; 155 } 156 throw new RootException(e); 157 } 158 } 159 160 174 public void updateUser (UserTransaction t, 175 String username, 176 String firstName, 177 String lastName, 178 String emailAddress) throws RootException { 179 try { 180 Session session = ((SharkHibernateUserTransaction) t).getSession(); 181 System.out.println("username to update : " + username); 182 HibernateUser user = null; 183 if (username != null) { 184 user = (HibernateUser) session.get(HibernateUser.class, username); 185 } 186 187 if (user == null) 188 return; 189 190 user.setFirstName(firstName); 191 user.setLastName(lastName); 192 user.setEmail(emailAddress); 193 194 session.update(user); 195 196 endTransaction(t, true); 197 } catch (Exception e) { 198 e.printStackTrace(); 199 if (e instanceof RootException) { 200 throw (RootException) e; 201 } 202 throw new RootException(e); 203 } 204 } 205 206 216 public void addGroupToGroup(UserTransaction t, String groupName, String subgroupName) throws RootException { 217 try { 218 Session session = ((SharkHibernateUserTransaction) t).getSession(); 219 220 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 221 HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName); 222 223 System.out.println("Group :\n" + group.toString()); 224 System.out.println("subGroup :\n" + subGroup.toString()); 225 226 if ((group == null) || (subGroup == null)) 227 return; 228 229 group.addSubGroup(subGroup); 230 231 session.update(group); 232 233 endTransaction(t, true); 234 } catch (Exception e) { 235 e.printStackTrace(); 236 if (e instanceof RootException) { 237 throw (RootException) e; 238 } 239 throw new RootException(e); 240 } 241 } 242 243 253 public void addUserToGroup(UserTransaction t, String groupName, String username) throws RootException { 254 try { 255 Session session = ((SharkHibernateUserTransaction) t).getSession(); 256 257 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 258 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 259 260 if ((group == null) || (user == null)) 261 return; 262 263 group.addUser(user); 264 user.addGroup(group); 265 266 session.update(group); 267 session.update(user); 269 270 endTransaction(t, true); 271 } catch (Exception e) { 272 e.printStackTrace(); 273 if (e instanceof RootException) { 274 throw (RootException) e; 275 } 276 throw new RootException(e); 277 } 278 } 279 280 292 public void moveGroup(UserTransaction t, String currentParentGroup, String newParentGroup, String subgroupName) throws RootException { 293 try { 294 Session session = ((SharkHibernateUserTransaction) t).getSession(); 295 296 HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName); 297 HibernateGroup currentGroup = (HibernateGroup) session.get(HibernateGroup.class, currentParentGroup); 298 HibernateGroup newGroup = (HibernateGroup) session.get(HibernateGroup.class, newParentGroup); 299 300 if ((subGroup == null) || (currentGroup == null) || (newGroup == null)){ 301 throw new RootException("Either currentParentGroup or newParentGroup or subgroupName doe not exists!"); 302 } 303 304 currentGroup.removeSubGroup(subGroup); 305 newGroup.addSubGroup(subGroup); 306 307 session.update(subGroup); 308 session.update(currentGroup); 309 session.update(newGroup); 310 311 endTransaction(t, true); 312 } catch (Exception e) { 313 if (e instanceof RootException) { 314 throw (RootException)e; 315 } 316 throw new RootException(e); 317 } 318 } 319 320 332 public void moveUser (UserTransaction t,String currentGroup,String newGroup,String username) throws RootException { 333 try { 334 Session session = ((SharkHibernateUserTransaction) t).getSession(); 335 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 336 HibernateGroup currentGrp = (HibernateGroup) session.get(HibernateGroup.class, currentGroup); 337 HibernateGroup newGrp = (HibernateGroup) session.get(HibernateGroup.class, newGroup); 338 339 if ((user == null) || (currentGrp == null) || (newGrp == null)){ 340 throw new RootException("Either currentGroup or newGroup or user username doe not exists!"); 341 } 342 343 currentGrp.removeUser(user); 344 newGrp.addUser(user); 345 346 session.update(user); 347 session.update(currentGrp); 348 session.update(newGrp); 349 350 endTransaction(t, true); 351 } catch (Exception e) { 352 if (e instanceof RootException) { 353 throw (RootException)e; 354 } 355 throw new RootException(e); 356 } 357 } 358 359 369 public void setPassword(UserTransaction t, String username, String password) throws RootException { 370 try { 371 Session session = ((SharkHibernateUserTransaction) t).getSession(); 372 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 373 374 if (user == null) 375 return; 376 377 user.setPasswd(passwordDigest(password)); 378 379 session.update(user); 380 381 endTransaction(t, true); 382 } catch (Exception e) { 383 e.printStackTrace(); 384 if (e instanceof RootException) { 385 throw (RootException) e; 386 } 387 throw new RootException(e); 388 } 389 } 390 391 395 404 public void removeGroup(UserTransaction t, String groupName) throws RootException { 405 try { 406 Session session = ((SharkHibernateUserTransaction) t).getSession(); 407 408 if (groupName != "") { 409 HibernateGroup group = (HibernateGroup) session.load(HibernateGroup.class, groupName); 410 if ((group.getSubGroups().size() != 0) || (group.getUsers().size() != 0)) { 411 return; 412 } 413 Set parentGroups = getParentGroups(t, groupName); 414 if (parentGroups.size() != 0) { 415 for (Iterator it = parentGroups.iterator(); it.hasNext();) { 416 HibernateGroup groupTemp = (HibernateGroup) it.next(); 417 groupTemp.removeSubGroup(group); 418 } 419 } 420 session.delete(group); 421 } 422 423 endTransaction(t, true); 424 } catch (Exception e) { 425 e.printStackTrace(); 426 if (e instanceof RootException) { 427 throw (RootException) e; 428 } 429 throw new RootException(e); 430 } 431 } 432 433 443 public void removeGroupFromGroup(UserTransaction t, String groupName, String subgroupName) throws RootException { 444 try { 445 Session session = ((SharkHibernateUserTransaction) t).getSession(); 446 447 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 448 HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName); 449 450 if ((group == null) || (subGroup == null)) 451 return; 452 453 group.removeSubGroup(subGroup); 454 455 session.update(group); 456 457 endTransaction(t, true); 458 } catch (Exception e) { 459 e.printStackTrace(); 460 if (e instanceof RootException) { 461 throw (RootException) e; 462 } 463 throw new RootException(e); 464 } 465 } 466 475 public void removeGroupTree (UserTransaction t,String groupName) throws RootException { 476 throw new RootException("Not implemented"); 477 } 478 479 488 public void removeUsersFromGroupTree (UserTransaction t,String groupName) throws RootException { 489 throw new RootException("Not implemented"); 490 } 491 492 502 public void removeUserFromGroup(UserTransaction t, String groupName, String username) throws RootException { 503 try { 504 Session session = ((SharkHibernateUserTransaction) t).getSession(); 505 506 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 507 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 508 509 if ((group == null) || (user == null)) 510 return; 511 512 user.removeGroup(group); 513 session.update(user); 514 515 517 endTransaction(t, true); 518 } catch (Exception e) { 519 e.printStackTrace(); 520 if (e instanceof RootException) { 521 throw (RootException) e; 522 } 523 throw new RootException(e); 524 } 525 } 526 527 537 public void removeUser(UserTransaction t, String username) throws RootException { 538 try { 539 Session session = ((SharkHibernateUserTransaction) t).getSession(); 540 541 if (username != "") { 542 HibernateUser user = (HibernateUser) session.load(HibernateUser.class, username); 543 List groups = user.getGroups(); 544 for (Iterator it = groups.iterator(); it.hasNext();) { 545 user.removeGroup((HibernateGroup) it.next()); 546 } 547 session.update(user); 548 } 549 endTransaction(t, true); 550 } catch (Exception e) { 551 if (e instanceof RootException) { 552 throw (RootException) e; 553 } 554 throw new RootException(e); 555 } 556 } 557 558 562 570 public List getAllGroupnames(UserTransaction t) throws RootException { 571 List ret = new ArrayList (); 572 try { 573 Session session = ((SharkHibernateUserTransaction) t).getSession(); 574 Query qGroupsQuery = session.createQuery("from HibernateGroup group"); 575 List groups = qGroupsQuery.list(); 576 577 if (groups.size() != 0) { 578 for (Iterator it = groups.iterator(); it.hasNext();) { 579 ret.add(((HibernateGroup) it.next()).getGid()); 580 } 581 } 582 } catch (Exception e) { 583 throw new RootException(e); 584 } finally { 585 endTransaction(t, false); 586 } 587 return ret; 588 } 589 590 private Set getAllGroupnamesInternal(UserTransaction t) throws RootException { 591 Set ret = new HashSet (); 592 try { 593 Session session = ((SharkHibernateUserTransaction) t).getSession(); 594 Query qGroupsQuery = session.createQuery("from HibernateGroup group"); 595 List groups = qGroupsQuery.list(); 596 597 if (groups.size() != 0) { 598 for (Iterator it = groups.iterator(); it.hasNext();) { 599 ret.add(((HibernateGroup) it.next()).getGid()); 600 } 601 } 602 } catch (Exception e) { 603 throw new RootException(e); 604 } finally { 605 } 606 return ret; 607 } 608 609 617 public List getAllUsers(UserTransaction t) throws RootException { 618 List ret = new ArrayList (); 619 try { 620 Session session = ((SharkHibernateUserTransaction) t).getSession(); 621 Query qUsersQuery = session.createQuery("from HibernateUser user"); 622 List users = qUsersQuery.list(); 623 624 if (users.size() != 0) { 625 for (Iterator it = users.iterator(); it.hasNext();) { 626 ret.add(((HibernateUser) it.next()).getUid()); 627 } 628 } 629 } catch (Exception e) { 630 throw new RootException(e); 631 } finally { 632 endTransaction(t, false); 633 } 634 return ret; 635 } 636 637 648 public List getAllUsers(UserTransaction t, String groupName) throws RootException { 649 List ret = new ArrayList (); 650 Set retSet = new HashSet (); 651 try { 652 Session session = ((SharkHibernateUserTransaction) t).getSession(); 653 HibernateGroup group; 654 if (!groupName.equals("")) { 655 group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 656 if (group == null) 657 return ret; 658 } else { 659 return ret; 660 } 661 662 List users = group.getUsers(); 663 if (users.size() != 0) { 664 for (Iterator it = users.iterator(); it.hasNext();) { 665 retSet.add(((HibernateUser) it.next()).getUid()); 666 } 667 } 668 669 List subGroups = group.getSubGroups(); 670 if (subGroups.size() != 0) { 671 for (Iterator it = subGroups.iterator(); it.hasNext();) { 672 retSet.addAll(getAllUsersInternal(t, ((HibernateGroup) it.next()).getGid())); 673 } 674 } 675 } catch (Exception e) { 676 e.printStackTrace(); 677 throw new RootException(e); 678 } finally { 679 endTransaction(t, false); 680 } 681 ret.addAll(retSet); 682 return ret; 683 } 684 685 private Set getAllUsersInternal(UserTransaction t, String groupName) throws RootException { 686 Set retSet = new HashSet (); 687 try { 688 Session session = ((SharkHibernateUserTransaction) t).getSession(); 689 HibernateGroup group; 690 if (!groupName.equals("")) { 691 group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 692 if (group == null) 693 return retSet; 694 } else { 695 return retSet; 696 } 697 698 List users = group.getUsers(); 699 if (users.size() != 0) { 700 for (Iterator it = users.iterator(); it.hasNext();) { 701 retSet.add(((HibernateUser) it.next()).getUid()); 702 } 703 } 704 705 List subGroups = group.getSubGroups(); 706 if (subGroups.size() != 0) { 707 for (Iterator it = subGroups.iterator(); it.hasNext();) { 708 retSet.addAll(getAllUsersInternal(t, ((HibernateGroup) it.next()).getGid())); 709 } 710 } 711 } catch (Exception e) { 712 e.printStackTrace(); 713 throw new RootException(e); 714 } finally { 715 } 716 return retSet; 717 } 718 719 730 public List getAllUsers( |