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(UserTransaction t, List groupName) throws RootException { 731 List ret = new ArrayList (); 732 for (Iterator it = groupName.iterator(); it.hasNext();) { 733 ret.addAll(getAllUsers(t, (String ) it.next())); 734 } 735 return ret; 736 } 737 738 749 public List getAllImmediateUsers(UserTransaction t, String groupName) throws RootException { 750 throw new RootException("Not implemented"); 751 } 752 753 764 public List getAllSubgroups(UserTransaction t, String groupName) throws RootException { 765 List ret = new ArrayList (); 766 Set retSet = new HashSet (); 767 try { 768 Session session = ((SharkHibernateUserTransaction) t).getSession(); 769 HibernateGroup group; 770 if (!groupName.equals("")) { 771 group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 772 if (group == null) 773 return ret; 774 } else { 775 return ret; 776 } 777 778 List subGroups = group.getSubGroups(); 779 if (subGroups.size() != 0) { 780 for (Iterator it = subGroups.iterator(); it.hasNext();) { 781 String gid = ((HibernateGroup) it.next()).getGid(); 782 retSet.add(gid); 783 retSet.addAll(getAllSubgroupsInternal(t, gid)); 784 } 785 } 786 787 } catch (Exception e) { 788 throw new RootException(e); 789 } finally { 790 endTransaction(t, false); 791 } 792 ret.addAll(retSet); 793 return ret; 794 } 795 796 private Set getAllSubgroupsInternal(UserTransaction t, String groupName) throws RootException { 797 Set retSet = new HashSet (); 798 try { 799 Session session = ((SharkHibernateUserTransaction) t).getSession(); 800 HibernateGroup group; 801 if (!groupName.equals("")) { 802 group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 803 if (group == null) 804 return retSet; 805 } else { 806 return retSet; 807 } 808 809 List subGroups = group.getSubGroups(); 810 System.out.println("subGroups : " + subGroups.toString()); 811 if (subGroups.size() != 0) { 812 for (Iterator it = subGroups.iterator(); it.hasNext();) { 813 String gid = ((HibernateGroup) it.next()).getGid(); 814 retSet.add(gid); 815 retSet.addAll(getAllSubgroupsInternal(t, gid)); 816 } 817 } 818 819 } catch (Exception e) { 820 throw new RootException(e); 821 } finally { 822 } 823 return retSet; 824 } 825 826 838 public List getAllImmediateSubgroups(UserTransaction t, String groupName) throws RootException { 839 throw new RootException("Not implemented"); 840 } 841 842 853 public List getAllSubgroups(UserTransaction t, List groupNames) throws RootException { 854 List ret = new ArrayList (); 855 Set retSet = new HashSet (); 856 for (Iterator it = groupNames.iterator(); it.hasNext();) { 857 retSet.addAll(getAllSubgroupsInternal(t, (String ) it.next())); 858 } 859 ret.addAll(retSet); 860 return ret; 861 } 862 863 private Set getParentGroups(UserTransaction t, String groupName) throws RootException { 864 Set ret = new HashSet (); 865 try { 866 Session session = ((SharkHibernateUserTransaction) t).getSession(); 867 Set allGroups = getAllGroupnamesInternal(t); 870 if (allGroups.size() != 0) { 871 for (Iterator it = allGroups.iterator(); it.hasNext();) { 872 String gid = (String ) it.next(); 873 if (doesGroupBelongToGroupInternal(t, gid, groupName)) { 874 ret.add((HibernateGroup) session.load(HibernateGroup.class, gid)); 875 } 876 } 877 } 878 } catch (Exception e) { 879 e.printStackTrace(); 880 if (e instanceof RootException) { 881 throw (RootException) e; 882 } 883 throw new RootException(e); 884 } 885 return ret; 886 } 887 888 897 public boolean doesGroupExist(UserTransaction t, String groupName) throws RootException { 898 boolean ret = false; 899 try { 900 Session session = ((SharkHibernateUserTransaction) t).getSession(); 901 if (!groupName.equals("") && ((HibernateGroup) session.get(HibernateGroup.class, groupName)) != null) { 902 ret = true; 903 } 904 endTransaction(t, false); 905 } catch (Exception e) { 906 e.printStackTrace(); 907 if (e instanceof RootException) { 908 throw (RootException) e; 909 } 910 throw new RootException(e); 911 } 912 return ret; 913 } 914 915 private boolean doesGroupExistInternal(UserTransaction t, String groupName) throws RootException { 916 boolean ret = false; 917 try { 918 Session session = ((SharkHibernateUserTransaction) t).getSession(); 919 if (!groupName.equals("") && ((HibernateGroup) session.get(HibernateGroup.class, groupName)) != null) { 920 ret = true; 921 } 922 } catch (Exception e) { 923 e.printStackTrace(); 924 if (e instanceof RootException) { 925 throw (RootException) e; 926 } 927 throw new RootException(e); 928 } 929 return ret; 930 } 931 932 943 public boolean doesGroupBelongToGroup(UserTransaction t, String groupName, String subgroupName) throws RootException { 944 boolean ret = false; 945 try { 946 Session session = ((SharkHibernateUserTransaction) t).getSession(); 947 948 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 949 HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName); 950 951 if ((group == null) || (subGroup == null)) 952 return ret; 953 954 ret = group.doesContainSubGroup(subGroup); 955 956 if (!ret) { 957 List group_SubGroups = group.getSubGroups(); 958 if (group_SubGroups.size() != 0) { 959 for (Iterator it = group_SubGroups.iterator(); it.hasNext() && !ret;) { 960 ret = doesGroupBelongToGroupInternal(t, ((HibernateGroup) it.next()).getGid(), subGroup.getGid()); 961 } 962 } 963 } 964 endTransaction(t, false); 965 } catch (Exception e) { 966 e.printStackTrace(); 967 if (e instanceof RootException) { 968 throw (RootException) e; 969 } 970 throw new RootException(e); 971 } 972 return ret; 973 } 974 975 private boolean doesGroupBelongToGroupInternal(UserTransaction t, String groupName, String subgroupName) throws RootException { 976 boolean ret = false; 977 try { 978 Session session = ((SharkHibernateUserTransaction) t).getSession(); 979 980 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 981 HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName); 982 983 if ((group == null) || (subGroup == null)) 984 return ret; 985 986 ret = group.doesContainSubGroup(subGroup); 987 988 if (!ret) { 989 List group_SubGroups = group.getSubGroups(); 990 if (group_SubGroups.size() != 0) { 991 for (Iterator it = group_SubGroups.iterator(); it.hasNext() && !ret;) { 992 ret = doesGroupBelongToGroupInternal(t, ((HibernateGroup) it.next()).getGid(), subGroup.getGid()); 993 } 994 } 995 } 996 } catch (Exception e) { 997 e.printStackTrace(); 998 if (e instanceof RootException) { 999 throw (RootException) e; 1000 } 1001 throw new RootException(e); 1002 } 1003 return ret; 1004 } 1005 1006 1015 public String getGroupDescription(UserTransaction t, String groupName) throws RootException { 1016 String ret = null; 1017 try { 1018 Session session = ((SharkHibernateUserTransaction) t).getSession(); 1019 1020 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 1021 if (group == null) 1022 ret = ""; 1023 1024 ret = group.getDescription(); 1025 1026 endTransaction(t, false); 1027 } catch (Exception e) { 1028 e.printStackTrace(); 1029 if (e instanceof RootException) { 1030 throw (RootException) e; 1031 } 1032 throw new RootException(e); 1033 } 1034 return ret; 1035 } 1036 1037 1047 public boolean doesUserBelongToGroup(UserTransaction t, String groupName, String username) throws RootException { 1048 boolean ret = false; 1049 try { 1050 Session session = ((SharkHibernateUserTransaction) t).getSession(); 1051 1052 HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName); 1053 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 1054 1055 if ((group == null) || (user == null)) 1056 return ret; 1057 1058 ret = group.doesContainUser(user); 1059 1060 if (!ret) { 1061 List group_SubGroups = group.getSubGroups(); 1062 if (group_SubGroups.size() != 0) { 1063 for (Iterator it = group_SubGroups.iterator(); it.hasNext() && !ret;) { 1064 ret = doesUserBelongToGroup(t, ((HibernateGroup) it.next()).getGid(), user.getUid()); 1065 } 1066 } 1067 } 1068 endTransaction(t, false); 1069 } catch (Exception e) { 1070 e.printStackTrace(); 1071 if (e instanceof RootException) { 1072 throw (RootException) e; 1073 } 1074 throw new RootException(e); 1075 } 1076 return ret; 1077 } 1078 1079 1088 public boolean doesUserExist(UserTransaction t, String username) throws RootException { 1089 boolean ret = false; 1090 try { 1091 Session session = ((SharkHibernateUserTransaction) t).getSession(); 1092 if (!username.equals("") && ((HibernateUser) session.get(HibernateUser.class, username)) != null) { 1093 ret = true; 1094 } 1095 1096 endTransaction(t, false); 1097 } catch (Exception e) { 1098 e.printStackTrace(); 1099 if (e instanceof RootException) { 1100 throw (RootException) e; 1101 } 1102 throw new RootException(e); 1103 } 1104 return ret; 1105 } 1106 1107 private boolean doesUserExistInternal(UserTransaction t, String username) throws RootException { 1108 boolean ret = false; 1109 try { 1110 Session session = ((SharkHibernateUserTransaction) t).getSession(); 1111 if (!username.equals("") && ((HibernateUser) session.get(HibernateUser.class, username)) != null) { 1112 ret = true; 1113 } 1114 } catch (Exception e) { 1115 e.printStackTrace(); 1116 if (e instanceof RootException) { 1117 throw (RootException) e; 1118 } 1119 throw new RootException(e); 1120 } 1121 return ret; 1122 } 1123 1124 1135 public String getUserRealName(UserTransaction t, String username) throws RootException { 1136 String ret = null; 1137 try { 1138 Session session = ((SharkHibernateUserTransaction) t).getSession(); 1139 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 1140 1141 if (user == null) 1142 return ret; 1143 1144 String firstName = user.getFirstName(); 1145 String lastName = user.getLastName(); 1146 if (firstName != null) { 1147 if (lastName != null) 1148 ret = firstName + " " + lastName; 1149 } 1150 else { 1151 if (lastName != null) 1152 ret = lastName; 1153 } 1154 1155 endTransaction(t, false); 1156 } catch (Exception e) { 1157 e.printStackTrace(); 1158 if (e instanceof RootException) { 1159 throw (RootException) e; 1160 } 1161 throw new RootException(e); 1162 } 1163 return ret; 1164 } 1165 1166 1177 public String getUserFirstName (UserTransaction t,String username) throws RootException { 1178 String ret = null; 1179 try { 1180 Session session = ((SharkHibernateUserTransaction) t).getSession(); 1181 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 1182 1183 if (user == null) 1184 return ret; 1185 1186 ret = user.getFirstName(); 1187 1188 endTransaction(t, false); 1189 } catch (Exception e) { 1190 if (e instanceof RootException) { 1191 throw (RootException)e; 1192 } 1193 throw new RootException(e); 1194 } 1195 return ret; 1196 } 1197 1198 1208 public String getUserLastName (UserTransaction t,String username) throws RootException { 1209 String ret = null; 1210 try { 1211 Session session = ((SharkHibernateUserTransaction) t).getSession(); 1212 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 1213 1214 if (user == null) 1215 return ret; 1216 1217 ret = user.getLastName(); 1218 1219 endTransaction(t, false); 1220 } catch (Exception e) { 1221 if (e instanceof RootException) { 1222 throw (RootException)e; 1223 } 1224 throw new RootException(e); 1225 } 1226 return ret; 1227 } 1228 1229 1240 public String getUserEMailAddress(UserTransaction t, String username) throws RootException { 1241 String ret = null; 1242 try { 1243 Session session = ((SharkHibernateUserTransaction) t).getSession(); 1244 HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username); 1245 1246 if (user == null) 1247 return ret; 1248 1249 ret = user.getEmail(); 1250 1251 endTransaction(t, false); 1252 } catch (Exception e) { 1253 e.printStackTrace(); 1254 if (e instanceof RootException) { 1255 throw (RootException) e; 1256 } 1257 throw new RootException(e); 1258 } 1259 return ret; 1260 } 1261 1262 1266 private HibernateUser createUser() { 1267 return new HibernateUser(); 1268 } 1269 1270 private HibernateGroup createGroup() { 1271 return new HibernateGroup(); 1272 } 1273 1274 public HibernateUserGroupManager() {} 1275 1276 1280 private void endTransaction(UserTransaction t, boolean commitToo) throws RootException { 1281 try { 1282 if (commitToo) { 1283 t.commit(); 1284 } 1285 } catch (Exception e) { 1286 throw new RootException(e); 1287 } finally { 1288 t.release(); 1289 } 1290 } 1291 1292 private static String passwordDigest(String password) { 1293 return password; 1296 } 1297 1298} 1299 | Popular Tags |