1 package org.enhydra.shark.usergroup; 2 3 import java.sql.SQLException ; 4 import java.util.*; 5 6 import org.enhydra.dods.DODS; 7 import org.enhydra.shark.api.RootException; 8 import org.enhydra.shark.api.UserTransaction; 9 import org.enhydra.shark.api.internal.usergroup.UserGroupManager; 10 import org.enhydra.shark.api.internal.working.CallbackUtilities; 11 import org.enhydra.shark.usergroup.data.*; 12 import org.enhydra.shark.usertransaction.SharkDODSUserTransaction; 13 import org.enhydra.shark.utilities.MiscUtilities; 14 import org.enhydra.shark.utilities.dods.DODSUtilities; 15 16 import com.lutris.appserver.server.sql.*; 17 18 24 public class DODSUserGroupManager implements UserGroupManager { 25 26 private static final String LDB_PARAM_NAME = "DODSUserGroupManager.DatabaseName"; 27 private CallbackUtilities callback; 28 private LogicalDatabase db; 29 30 33 public DODSUserGroupManager () { 34 db = null; 35 } 36 37 46 public void configure (CallbackUtilities cus) throws RootException { 47 if (null == cus) 48 throw new RootException("Cannot configure without call back impl."); 49 callback = cus; 50 DODSUtilities.init(callback.getProperties()); 51 String dbName = callback 52 .getProperty(LDB_PARAM_NAME, DODS.getDatabaseManager().getDefaultDB()); 53 try { 54 db = DODS.getDatabaseManager().findLogicalDatabase(dbName); 55 } catch (DatabaseManagerException e) { 56 throw new RootException("Couldn't find logical database.", e); 57 } 58 callback.debug("DODSUserGroupManager configured"); 59 } 60 61 68 public List getAllGroupnames (UserTransaction t) throws RootException { 69 DBTransaction dbt = beginTransaction(t); 70 List ret = new ArrayList(); 71 GroupQuery qry = new GroupQuery(dbt); 72 try { 73 while (true) { 74 GroupDO group = qry.getNextDO(); 75 if (null == group) 76 break; 77 ret.add(group.getGroupid()); 78 } 79 } catch (Exception e) { 80 throw new RootException(e); 81 } finally { 82 endTransaction(t, dbt, false); 83 } 84 return ret; 85 } 86 87 95 public List getAllUsers (UserTransaction t) throws RootException { 96 DBTransaction dbt = beginTransaction(t); 97 List ret = new ArrayList(); 98 UserQuery qry = new UserQuery(dbt); 99 try { 100 while (true) { 101 UserDO user = qry.getNextDO(); 102 if (null == user) { 103 break; 104 } 105 ret.add(user.getUserid()); 106 } 107 } catch (Exception e) { 108 throw new RootException(e); 109 } finally { 110 endTransaction(t, dbt, false); 111 } 112 return ret; 113 } 114 115 124 public List getAllUsers (UserTransaction t,String groupName) throws RootException { 125 DBTransaction dbt = beginTransaction(t); 126 List ret = new ArrayList(); 127 GroupQuery gqry = new GroupQuery(dbt); 128 try { 129 gqry.setQueryGroupid(groupName); 130 gqry.requireUniqueInstance(); 131 GroupDO gd = gqry.getNextDO(); 132 133 if (gd == null) 134 throw new RootException("Group " + groupName + "does not exist."); 135 136 UserLinkQuery lqry = new UserLinkQuery(dbt); 137 lqry.setQueryGroupid(gd); 138 while (true) { 139 UserLinkDO user = lqry.getNextDO(); 140 if (null == user) { 141 break; 142 } 143 ret.add(user.getUserid().getUserid()); 144 } 145 146 GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt); 147 groupLinkQ.setQueryGroupid(gd); 148 while (true) { 149 GroupLinkDO subGroup = groupLinkQ.getNextDO(); 150 if (null == subGroup) { 151 break; 152 } 153 List l = getAllUsers(t,subGroup.getSub_gid().getGroupid()); 154 if (!l.isEmpty()){ 155 for (int i=0;i<l.size();i++){ 156 if (!ret.contains((String )l.get(i))){ 157 ret.add((String )l.get(i)); 158 } 159 } 160 } 161 } 162 } catch (Exception e) { 163 throw new RootException(e); 164 } finally { 165 endTransaction(t, dbt, false); 166 } 167 return ret; 168 } 169 170 179 public List getAllUsers (UserTransaction t,List groupNames) throws RootException { 180 List ret = new ArrayList(); 181 for (Iterator it = groupNames.iterator(); it.hasNext();) { 182 ret.addAll(getAllUsers(t, (String )it.next())); 183 } 184 return ret; 185 } 186 187 197 public List getAllImmediateUsers (UserTransaction t,String groupName) throws RootException { 198 199 DBTransaction dbt = beginTransaction(t); 200 List ret = new ArrayList(); 201 GroupQuery gqry = new GroupQuery(dbt); 202 try { 203 gqry.setQueryGroupid(groupName); 204 gqry.requireUniqueInstance(); 205 GroupDO gd = gqry.getNextDO(); 206 207 if (gd == null) 208 throw new RootException("Group " + groupName + "does not exist."); 209 210 UserLinkQuery lqry = new UserLinkQuery(dbt); 211 lqry.setQueryGroupid(gd); 212 while (true) { 213 UserLinkDO user = lqry.getNextDO(); 214 if (null == user) { 215 break; 216 } 217 ret.add(user.getUserid().getUserid()); 218 } 219 } catch (Exception e) { 220 throw new RootException(e); 221 } finally { 222 endTransaction(t, dbt, false); 223 } 224 return ret; 225 } 226 227 236 public List getAllSubgroups (UserTransaction t, String groupName) throws RootException { 237 DBTransaction dbt = beginTransaction(t); 238 List ret = new ArrayList(); 239 GroupQuery gqry = new GroupQuery(dbt); 240 try { 241 gqry.setQueryGroupid(groupName); 242 gqry.requireUniqueInstance(); 243 GroupDO gd = gqry.getNextDO(); 244 245 if (gd == null) 246 throw new RootException("Group " + groupName + "does not exist."); 247 248 GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt); 249 groupLinkQ.setQueryGroupid(gd); 250 while (true) { 251 GroupLinkDO subGroup = groupLinkQ.getNextDO(); 252 if (null == subGroup) { 253 break; 254 } 255 String subId = subGroup.getSub_gid().getGroupid(); 256 if (!ret.contains(subId)) { 257 ret.add(subId); 258 List l = getAllSubgroups(t, subGroup.getSub_gid().getGroupid()); 259 if (!l.isEmpty()){ 260 for (int i=0;i<l.size();i++){ 261 if (!ret.contains((String )l.get(i))){ 262 ret.add((String )l.get(i)); 263 } 264 } 265 } 266 } 267 } 268 } catch (Exception e) { 269 throw new RootException(e); 270 } finally { 271 endTransaction(t, dbt, false); 272 } 273 return ret; 274 } 275 276 285 public List getAllSubgroups (UserTransaction t, List groupNames) throws RootException { 286 List ret = new ArrayList(); 287 for (Iterator it = groupNames.iterator(); it.hasNext();) { 288 ret.addAll(getAllSubgroups(t, (String )it.next())); 289 } 290 return ret; 291 } 292 302 public List getAllImmediateSubgroups (UserTransaction t, String groupName) throws RootException { 303 DBTransaction dbt = beginTransaction(t); 304 List ret = new ArrayList(); 305 GroupQuery gqry = new GroupQuery(dbt); 306 try { 307 gqry.setQueryGroupid(groupName); 308 gqry.requireUniqueInstance(); 309 GroupDO gd = gqry.getNextDO(); 310 311 if (gd == null) 312 throw new RootException("Group " + groupName + "does not exist."); 313 314 GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt); 315 groupLinkQ.setQueryGroupid(gd); 316 while (true) { 317 GroupLinkDO subGroup = groupLinkQ.getNextDO(); 318 if (null == subGroup) { 319 break; 320 } 321 ret.add(subGroup.getSub_gid().getGroupid()); 322 } 323 } catch (Exception e) { 324 throw new RootException(e); 325 } finally { 326 endTransaction(t, dbt, false); 327 } 328 return ret; 329 } 330 331 340 public void createGroup (UserTransaction t, 341 String groupName, 342 String description) throws RootException { 343 344 if (doesGroupExist(t,groupName)) { 345 throw new RootException("Group already exists"); 346 } 347 DBTransaction dbt = beginTransaction(t); 348 try { 349 GroupDO group = GroupDO.createVirgin(dbt); 350 group.setGroupid(groupName); 351 group.setDescription(description); 352 group.save(); 353 endTransaction(t, dbt, true); 354 } catch (Exception e) { 355 if (e instanceof RootException) { 356 throw (RootException)e; 357 } 358 throw new RootException(e); 359 } 360 } 361 362 370 public void removeGroup (UserTransaction t,String groupName) throws RootException { 371 DBTransaction dbt = beginTransaction(t); 372 try { 373 GroupQuery qry = new GroupQuery(dbt); 374 qry.setQueryGroupid(groupName); 375 qry.requireUniqueInstance(); 376 qry.getNextDO().delete(); 377 endTransaction(t, dbt, true); 378 } catch (Exception e) { 379 if (e instanceof RootException) { 380 throw (RootException)e; 381 } 382 throw new RootException(e); 383 } 384 } 385 386 395 public boolean doesGroupExist (UserTransaction t,String groupName) throws RootException { 396 boolean ret = false; 397 DBTransaction dbt = beginTransaction(t); 398 try { 399 GroupQuery qry = new GroupQuery(dbt); 400 qry.setQueryGroupid(groupName); 401 qry.requireUniqueInstance(); 402 ret = null != qry.getNextDO(); 403 endTransaction(t, dbt, false); 404 } catch (Exception e) { 405 if (e instanceof RootException) { 406 throw (RootException)e; 407 } 408 throw new RootException(e); 409 } 410 return ret; 411 } 412 413 424 public boolean doesGroupBelongToGroup (UserTransaction t,String groupName, String subgroupName) throws RootException { 425 boolean ret = false; 426 DBTransaction dbt = beginTransaction(t); 427 try { 428 GroupQuery gQry = new GroupQuery(dbt); 429 gQry.setQueryGroupid(groupName); 430 gQry.requireUniqueInstance(); 431 GroupDO group = gQry.getNextDO(); 432 433 if (group == null) 434 throw new RootException("Group " + groupName + "does not exist."); 435 436 GroupQuery gQry1 = new GroupQuery(dbt); 437 gQry1.setQueryGroupid(subgroupName); 438 gQry1.requireUniqueInstance(); 439 GroupDO subgroup = gQry1.getNextDO(); 440 441 if (subgroup == null) 442 throw new RootException("Group " + subgroupName + "does not exist."); 443 444 GroupLinkQuery lQry = new GroupLinkQuery(dbt); 445 lQry.setQueryGroupid(group); 446 lQry.setQuerySub_gid(subgroup); 447 lQry.requireUniqueInstance(); 448 GroupLinkDO result = lQry.getNextDO(); 449 450 if (result != null) 451 ret = true; 452 else { 453 GroupLinkQuery lQry1 = new GroupLinkQuery(dbt); 454 lQry1.setQueryGroupid(group); 455 result = lQry1.getNextDO(); 456 while ((!ret) && (result != null)) { 457 ret = doesGroupBelongToGroup(t, result.getSub_gid(), subgroup); 458 result = lQry1.getNextDO(); 459 } 460 } 461 endTransaction(t, dbt, false); 462 } catch (Exception e) { 463 if (e instanceof RootException) { 464 throw (RootException)e; 465 } 466 throw new RootException(e); 467 } 468 return ret; 469 } 470 471 private boolean doesGroupBelongToGroup (UserTransaction t,GroupDO group, GroupDO subgroup) throws RootException { 472 boolean ret = false; 473 DBTransaction dbt = beginTransaction(t); 474 try { 475 GroupLinkQuery lQry = new GroupLinkQuery(dbt); 476 lQry.setQueryGroupid(group); 477 lQry.setQuerySub_gid(subgroup); 478 lQry.requireUniqueInstance(); 479 GroupLinkDO result = lQry.getNextDO(); 480 481 if (result != null) 482 ret = true; 483 else { 484 GroupLinkQuery lQry1 = new GroupLinkQuery(dbt); 485 lQry1.setQueryGroupid(group); 486 result = lQry1.getNextDO(); 487 while ((!ret) && (result != null)) { 488 ret = doesGroupBelongToGroup(t, result.getSub_gid(), subgroup); 489 result = lQry1.getNextDO(); 490 } 491 } 492 endTransaction(t, dbt, false); 493 } catch (Exception e) { 494 if (e instanceof RootException) { 495 throw (RootException)e; 496 } 497 throw new RootException(e); 498 } 499 return ret; 500 } 501 502 511 public void updateGroup (UserTransaction t,String groupName,String description) throws RootException { 512 DBTransaction dbt = beginTransaction(t); 513 try { 514 GroupQuery qry = new GroupQuery(dbt); 515 qry.setQueryGroupid(groupName); 516 qry.requireUniqueInstance(); 517 GroupDO group = qry.getNextDO(); 518 519 if (group == null) 520 throw new RootException("Group " + groupName + "does not exist."); 521 522 group.setDescription(description); 523 group.save(); 524 endTransaction(t, dbt, true); 525 } catch (Exception e) { 526 if (e instanceof RootException) { 527 throw (RootException)e; 528 } 529 throw new RootException(e); 530 } 531 } 532 533 542 public void addGroupToGroup (UserTransaction t,String groupName,String subgroupName) throws RootException { 543 DBTransaction dbt = beginTransaction(t); 544 try { 545 GroupQuery gQry = new GroupQuery(dbt); 546 gQry.setQueryGroupid(groupName); 547 gQry.requireUniqueInstance(); 548 GroupDO group = gQry.getNextDO(); 549 550 if (group == null) 551 throw new RootException("Group " + groupName + "does not exist."); 552 553 GroupQuery gQry1 = new GroupQuery(dbt); 554 gQry1.setQueryGroupid(subgroupName); 555 gQry1.requireUniqueInstance(); 556 GroupDO subgroup = gQry1.getNextDO(); 557 558 if (subgroup == null) 559 throw new RootException("Group " + subgroupName + "does not exist."); 560 561 GroupLinkDO GroupLink = GroupLinkDO.createVirgin(dbt); 562 GroupLink.setSub_gid(subgroup); 563 GroupLink.setGroupid(group); 564 GroupLink.save(); 565 566 endTransaction(t, dbt, true); 567 } catch (Exception e) { 568 if (e instanceof RootException) { 569 throw (RootException)e; 570 } 571 throw new RootException(e); 572 } 573 } 574 575 584 public void removeGroupFromGroup (UserTransaction t,String groupName,String subgroupName) throws RootException { 585 DBTransaction dbt = beginTransaction(t); 586 try { 587 GroupQuery gQry = new GroupQuery(dbt); 588 gQry.setQueryGroupid(groupName); 589 gQry.requireUniqueInstance(); 590 GroupDO group = gQry.getNextDO(); 591 592 if (group == null) 593 throw new RootException("Group " + groupName + "does not exist."); 594 595 GroupQuery gQry1 = new GroupQuery(dbt); 596 gQry1.setQueryGroupid(subgroupName); 597 gQry1.requireUniqueInstance(); 598 GroupDO subgroup = gQry1.getNextDO(); 599 600 if (subgroup == null) 601 throw new RootException("Group " + subgroupName + "does not exist."); 602 603 GroupLinkQuery lQry = new GroupLinkQuery(dbt); 604 lQry.setQuerySub_gid(subgroup); 605 lQry.setQueryGroupid(group); 606 lQry.requireUniqueInstance(); 607 lQry.getNextDO().delete(); 608 609 endTransaction(t, dbt, true); 610 } catch (Exception e) { 611 if (e instanceof RootException) { 612 throw (RootException)e; 613 } 614 throw new RootException(e); 615 } 616 } 617 618 627 public void removeGroupTree (UserTransaction t,String groupName) throws RootException { 628 DBTransaction dbt = beginTransaction(t); 629 try { 630 GroupQuery gQry = new GroupQuery(dbt); 631 gQry.setQueryGroupid(groupName); 632 gQry.requireUniqueInstance(); 633 GroupDO group = gQry.getNextDO(); 634 635 if (group == null) 636 throw new RootException("Group " + groupName + "does not exist."); 637 638 GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt); 639 groupLinkQ.setQueryGroupid(group); 640 641 while (true) { 642 GroupLinkDO groupLinkDo = groupLinkQ.getNextDO(); 643 if (null == groupLinkDo) { 644 break; 645 } 646 removeChildTree(t, groupLinkDo); 647 } 648 removeUsersFromGroup(t, group); 649 group.delete(); 650 651 endTransaction(t, dbt, true); 652 } catch (Exception e) { 653 if (e instanceof RootException) { 654 throw (RootException)e; 655 } 656 throw new RootException(e); 657 } 658 } 659 660 private void removeChildTree (UserTransaction t, GroupLinkDO groupLink) throws RootException { 661 DBTransaction dbt = beginTransaction(t); 662 try { 663 GroupDO group = groupLink.getSub_gid(); 664 665 GroupLinkQuery gQry = new GroupLinkQuery(dbt); 666 gQry.setQueryGroupid(group); 667 GroupLinkDO subgroupLink = gQry.getNextDO(); 668 669 while (subgroupLink != null) { 670 removeChildTree(t, subgroupLink); 671 subgroupLink = gQry.getNextDO(); 672 } 673 674 GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt); 675 groupLinkQ.setQuerySub_gid(group); 676 677 int ii = groupLinkQ.getCount(); 678 679 if (groupLinkQ.getCount() > 1) { 680 groupLink.delete(); 681 } 682 else { 683 removeUsersFromGroup(t, group); 684 group.delete(); 685 } 686 687 endTransaction(t, dbt, true); 688 } catch (Exception e) { 689 if (e instanceof RootException) { 690 throw (RootException)e; 691 } 692 throw new RootException(e); 693 } 694 } 695 696 775 776 private void removeUsersFromGroup (UserTransaction t, GroupDO group) throws RootException { 777 778 DBTransaction dbt = beginTransaction(t); 779 try { 780 UserLinkQuery userLinkQ = new UserLinkQuery(dbt); 781 userLinkQ.setQueryGroupid(group); 782 783 UserLinkDO[] userLink_dos = userLinkQ.getDOArray(); 784 for (int i = 0; i < userLink_dos.length; i++ ) { 785 UserLinkDO userLink_do = userLink_dos[i]; 786 UserDO user_do = userLink_do.getUserid(); 787 788 UserLinkQuery lQry = new UserLinkQuery(dbt); 789 lQry.setQueryUserid(user_do); 790 791 if (lQry.getCount() == 1) { 792 user_do.delete(); 793 } 794 else { 795 userLink_do.delete(); 796 } 797 } 798 endTransaction(t, dbt, true); 799 } catch (Exception e) { 800 if (e instanceof RootException) { 801 throw (RootException)e; 802 } 803 throw new RootException(e); 804 } 805 } 806 807 808 809 818 public void removeUsersFromGroupTree (UserTransaction t,String groupName) throws RootException { 819 DBTransaction dbt = beginTransaction(t); 820 try { 821 GroupQuery gQry = new GroupQuery(dbt); 822 gQry.setQueryGroupid(groupName); 823 gQry.requireUniqueInstance(); 824 GroupDO group = gQry.getNextDO(); 825 826 if (group == null) 827 throw new RootException("Group " + groupName + "does not exist."); 828 829 GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt); 830 groupLinkQ.setQueryGroupid(group); 831 832 while (true) { 833 GroupLinkDO groupLink_do = groupLinkQ.getNextDO(); 834 if (null == groupLink_do) { 835 break; 836 } 837 removeUsersFromGroupTree(t, groupLink_do.getSub_gid()); 838 } 839 840 removeUsersFromGroup(t, group); 841 842 843 endTransaction(t, dbt, true); 844 } catch (Exception e) { 845 if (e instanceof RootException) { 846 throw (RootException)e; 847 } 848 throw new RootException(e); 849 } 850 } 851 852 private void removeUsersFromGroupTree (UserTransaction t, GroupDO group) throws RootException { 853 DBTransaction dbt = beginTransaction(t); 854 try { 855 856 GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt); 857 groupLinkQ.setQueryGroupid(group); 858 859 while (true) { 860 GroupLinkDO groupLink_do = groupLinkQ.getNextDO(); 861 if (null == groupLink_do) { 862 break; 863 } 864 removeUsersFromGroupTree(t, groupLink_do.getSub_gid()); 865 } 866 removeUsersFromGroup(t, group); 867 868 endTransaction(t, dbt, true); 869 } catch (Exception e) { 870 if (e instanceof RootException) { 871 throw (RootException)e; 872 } 873 throw new RootException(e); 874 } 875 } 876 877 888 public void moveGroup (UserTransaction t,String currentParentGroup,String newParentGroup,String subgroupName) throws RootException { 889 DBTransaction dbt = beginTransaction(t); 890 try { 891 GroupQuery gQry = new GroupQuery(dbt); 892 gQry.setQueryGroupid(subgroupName); 893 gQry.requireUniqueInstance(); 894 GroupDO subgroup = gQry.getNextDO(); 895 896 if (subgroup == null) 897 throw new RootException("Group " + subgroupName + "does not exist."); 898 899 GroupQuery gQry1 = new GroupQuery(dbt); 900 gQry1.setQueryGroupid(currentParentGroup); 901 gQry1.requireUniqueInstance(); 902 GroupDO currentGroup = gQry1.getNextDO(); 903 904 if (currentGroup == null) 905 throw new RootException("Group " + currentParentGroup + "does not exist."); 906 907 GroupQuery gQry2 = new GroupQuery(dbt); 908 gQry2.setQueryGroupid(newParentGroup); 909 gQry2.requireUniqueInstance(); 910 GroupDO newGroup = gQry2.getNextDO(); 911 912 if (newGroup == null) 913 throw new RootException("Group " + newParentGroup + "does not exist."); 914 915 GroupLinkQuery lQry = new GroupLinkQuery(dbt); 916 lQry.setQuerySub_gid(subgroup); 917 lQry.setQueryGroupid(currentGroup); 918 lQry.requireUniqueInstance(); 919 lQry.getNextDO().delete(); 920 921 GroupLinkDO GroupLink = GroupLinkDO.createVirgin(dbt); 922 GroupLink.setSub_gid(subgroup); 923 GroupLink.setGroupid(newGroup); 924 GroupLink.save(); 925 926 endTransaction(t, dbt, true); 927 } catch (Exception e) { 928 if (e instanceof RootException) { 929 throw (RootException)e; 930 } 931 throw new RootException(e); 932 } 933 } 934 935 944 public String getGroupDescription (UserTransaction t,String groupName) throws RootException { 945 String ret = null; 946 DBTransaction dbt = beginTransaction(t); 947 try { 948 GroupQuery qry = new GroupQuery(dbt); 949 qry.setQueryGroupid(groupName); 950 qry.requireUniqueInstance(); 951 ret = qry.getNextDO().getDescription(); 952 953 endTransaction(t, dbt, false); 954 } catch (Exception e) { 955 if (e instanceof RootException) { 956 throw (RootException)e; 957 } 958 throw new RootException(e); 959 } 960 return ret; 961 } 962 963 972 public void addUserToGroup (UserTransaction t,String groupName,String username) throws RootException { 973 DBTransaction dbt = beginTransaction(t); 974 try { 975 GroupQuery gQry = new GroupQuery(dbt); 976 gQry.setQueryGroupid(groupName); 977 gQry.requireUniqueInstance(); 978 GroupDO group = gQry.getNextDO(); 979 980 if (group == null) 981 throw new RootException("Group " + groupName + "does not exist."); 982 983 UserQuery uQry = new UserQuery(dbt); 984 uQry.setQueryUserid(username); 985 uQry.requireUniqueInstance(); 986 UserDO user = uQry.getNextDO(); 987 988 if (user == null) 989 throw new RootException("User " + username + "does not exist."); 990 991 UserLinkDO UserLink = UserLinkDO.createVirgin(dbt); 992 UserLink.setUserid(user); 993 UserLink.setGroupid(group); 994 UserLink.save(); 995 996 endTransaction(t, dbt, true); 997 } catch (Exception e) { 998 if (e instanceof RootException) { 999 throw (RootException)e; 1000 } 1001 throw new RootException(e); 1002 } 1003 } 1004 1005 1014 public void removeUserFromGroup (UserTransaction t,String groupName,String username) throws RootException { 1015 DBTransaction dbt = beginTransaction(t); 1016 try { 1017 GroupQuery gQry = new GroupQuery(dbt); 1018 gQry.setQueryGroupid(groupName); 1019 gQry.requireUniqueInstance(); 1020 GroupDO group = gQry.getNextDO(); 1021 1022 if (group == null) 1023 throw new RootException("Group " + groupName + "does not exist."); 1024 1025 UserQuery uQry = new UserQuery(dbt); 1026 uQry.setQueryUserid(username); 1027 uQry.requireUniqueInstance(); 1028 UserDO user = uQry.getNextDO(); 1029 1030 if (user == null) 1031 throw new RootException("User " + username + "does not exist."); 1032 1033 UserLinkQuery lQry = new UserLinkQuery(dbt); 1034 lQry.setQueryUserid(user); 1035 lQry.setQueryGroupid(group); 1036 lQry.requireUniqueInstance(); 1037 lQry.getNextDO().delete(); 1038 1039 endTransaction(t, dbt, true); 1040 } catch (Exception e) { 1041 if (e instanceof RootException) { 1042 throw (RootException)e; 1043 } 1044 throw new RootException(e); 1045 } 1046 } 1047 1048 1059 public void moveUser (UserTransaction t,String currentGroup,String newGroup,String username) throws RootException { 1060 DBTransaction dbt = beginTransaction(t); 1061 try { 1062 GroupQuery gQry1 = new GroupQuery(dbt); 1063 gQry1.setQueryGroupid(currentGroup); 1064 gQry1.requireUniqueInstance(); 1065 GroupDO currentG = gQry1.getNextDO(); 1066 1067 if (currentG == null) 1068 throw new RootException("Group " + currentGroup + "does not exist."); 1069 1070 GroupQuery gQry2 = new GroupQuery(dbt); 1071 gQry2.setQueryGroupid(newGroup); 1072 gQry2.requireUniqueInstance(); 1073 GroupDO newG = gQry2.getNextDO(); 1074 1075 if (newG == null) 1076 throw new RootException("Group " + newGroup + "does not exist."); 1077 1078 UserQuery uQry = new UserQuery(dbt); 1079 uQry.setQueryUserid(username); 1080 uQry.requireUniqueInstance(); 1081 UserDO user = uQry.getNextDO(); 1082 1083 if (user == null) 1084 throw new RootException("User " + username + "does not exist."); 1085 1086 UserLinkQuery lQry = new UserLinkQuery(dbt); 1087 lQry.setQueryUserid(user); 1088 lQry.setQueryGroupid(currentG); 1089 lQry.requireUniqueInstance(); 1090 lQry.getNextDO().delete(); 1091 1092 UserLinkDO UserLink = UserLinkDO.createVirgin(dbt); 1093 UserLink.setUserid(user); 1094 UserLink.setGroupid(newG); 1095 UserLink.save(); 1096 1097 endTransaction(t, dbt, true); 1098 } catch (Exception e) { 1099 if (e instanceof RootException) { 1100 throw (RootException)e; 1101 } 1102 throw new RootException(e); 1103 } 1104 1105 1106 } 1107 1108 1119 public boolean doesUserBelongToGroup (UserTransaction t,String groupName,String username) throws RootException { 1120 boolean ret = false; 1121 DBTransaction dbt = beginTransaction(t); 1122 try { 1123 GroupQuery gQry = new GroupQuery(dbt); 1124 gQry.setQueryGroupid(groupName); 1125 gQry.requireUniqueInstance(); 1126 GroupDO group = gQry.getNextDO(); 1127 1128 if (group == null) 1129 throw new RootException("Group " + groupName + "does not exist."); 1130 1131 UserQuery uQry = new UserQuery(dbt); 1132 uQry.setQueryUserid(username); 1133 uQry.requireUniqueInstance(); 1134 UserDO user = uQry.getNextDO(); 1135 1136 if (user == null) 1137 throw new RootException("User " + username + "does not exist."); 1138 1139 UserLinkQuery lQry = new UserLinkQuery(dbt); 1140 lQry.setQueryUserid(user); 1141 lQry.setQueryGroupid(group); 1142 lQry.requireUniqueInstance(); 1143 UserLinkDO result = lQry.getNextDO(); 1145 1146 if (result != null) 1147 ret = true; 1148 else { 1149 GroupLinkQuery lQry1 = new GroupLinkQuery(dbt); 1150 lQry1.setQueryGroupid(group); 1151 GroupLinkDO result1 = lQry1.getNextDO(); 1152 while ((!ret) && (result1 != null)) { 1153 ret = doesUserBelongToGroup(t, result1.getSub_gid(), user); 1154 result1 = lQry1.getNextDO(); 1155 } 1156 } 1157 1158 endTransaction(t, dbt, false); 1159 } catch (Exception e) { 1160 if (e instanceof RootException) { 1161 throw (RootException)e; 1162 } 1163 throw new RootException(e); 1164 } 1165 return ret; 1166 } 1167 1168 private boolean doesUserBelongToGroup (UserTransaction t,GroupDO group, UserDO user) throws RootException { 1169 boolean ret = false; 1170 DBTransaction dbt = beginTransaction(t); 1171 try { 1172 UserLinkQuery lQry = new UserLinkQuery(dbt); 1173 lQry.setQueryGroupid(group); 1174 lQry.setQueryUserid(user); 1175 lQry.requireUniqueInstance(); 1176 UserLinkDO result = lQry.getNextDO(); 1177 1178 if (result != null) 1179 ret = true; 1180 else { 1181 GroupLinkQuery lQry1 = new GroupLinkQuery(dbt); 1182 lQry1.setQueryGroupid(group); 1183 GroupLinkDO result1 = lQry1.getNextDO(); 1184 while ((!ret) && (result1 != null)) { 1185 ret = doesUserBelongToGroup(t, result1.getSub_gid(), user); 1186 result1 = lQry1.getNextDO(); 1187 } 1188 } 1189 endTransaction(t, dbt, false); 1190 } catch (Exception e) { 1191 if (e instanceof RootException) { 1192 throw (RootException)e; 1193 } 1194 throw new RootException(e); 1195 } 1196 return ret; 1197 } 1198 1199 1218 public void createUser (UserTransaction t, 1219 String groupName, 1220 String username, 1221 String password, 1222 String firstName, 1223 String lastName, 1224 String emailAddress) throws RootException { 1225 if (doesUserExist(t,username)) { 1226 throw new RootException("User already exists"); 1227 } 1228 1229 DBTransaction dbt = beginTransaction(t); 1230 try { 1231 GroupQuery gQry = new GroupQuery(dbt); 1232 gQry.setQueryGroupid(groupName); 1233 gQry.requireUniqueInstance(); 1234 GroupDO group = gQry.getNextDO(); 1235 1236 UserDO user = UserDO.createVirgin(dbt); 1237 user.setUserid(username); 1238 user.setPasswd(MiscUtilities.passwordDigest(password)); 1239 user.setFirstname(firstName); 1240 user.setLastname(lastName); 1241 user.setEmail(emailAddress); 1242 user.save(); 1243 1244 UserLinkDO UserLink = UserLinkDO.createVirgin(dbt); 1245 UserLink.setUserid(user); 1246 UserLink.setGroupid(group); 1247 UserLink.save(); 1248 1249 endTransaction(t, dbt, true); 1250 } catch (Exception e) { 1251 if (e instanceof RootException) { 1252 throw (RootException)e; 1253 } 1254 throw new RootException(e); 1255 } 1256 } 1257 1258 1271 public void updateUser (UserTransaction t, 1272 String username, 1273 String firstName, 1274 String lastName, 1275 String emailAddress) throws RootException { 1276 DBTransaction dbt = beginTransaction(t); 1277 try { 1278 UserQuery uQry = new UserQuery(dbt); 1279 uQry.setQueryUserid(username); 1280 uQry.requireUniqueInstance(); 1281 UserDO user = uQry.getNextDO(); 1282 1283 if (user == null) 1284 throw new RootException("User " + username + "does not exist."); 1285 1286 user.setFirstname(firstName); 1287 user.setLastname(lastName); 1288 user.setEmail(emailAddress); 1289 user.save(); 1290 1291 endTransaction(t, dbt, true); 1292 } catch (Exception e) { 1293 if (e instanceof RootException) { 1294 throw (RootException)e; 1295 } 1296 throw new RootException(e); 1297 } 1298 } 1299 1300 1309 public void removeUser (UserTransaction t,String username) throws RootException { 1310 DBTransaction dbt = beginTransaction(t); 1311 try { 1312 UserQuery uQry = new UserQuery(dbt); 1313 uQry.setQueryUserid(username); 1314 uQry.requireUniqueInstance(); 1315 uQry.getNextDO().delete(); 1316 1317 endTransaction(t, dbt, true); 1318 } catch (Exception e) { 1319 if (e instanceof RootException) { 1320 throw (RootException)e; 1321 } 1322 throw new RootException(e); 1323 } 1324 } 1325 1326 1335 public boolean doesUserExist (UserTransaction t,String username) throws RootException { 1336 boolean ret = false; 1337 DBTransaction dbt = beginTransaction(t); 1338 try { 1339 UserQuery uQry = new UserQuery(dbt); 1340 uQry.setQueryUserid(username); 1341 uQry.requireUniqueInstance(); 1342 ret = null != uQry.getNextDO(); 1343 1344 endTransaction(t, dbt, false); 1345 } catch (Exception e) { 1346 if (e instanceof RootException) { 1347 throw (RootException)e; 1348 } 1349 throw new RootException(e); 1350 } 1351 return ret; 1352 } 1353 1354 1363 public void setPassword (UserTransaction t,String username,String password) throws RootException { 1364 DBTransaction dbt = beginTransaction(t); 1365 try { 1366 UserQuery uQry = new UserQuery(dbt); 1367 uQry.setQueryUserid(username); 1368 uQry.requireUniqueInstance(); 1369 UserDO user = uQry.getNextDO(); 1370 user.setPasswd(MiscUtilities.passwordDigest(password)); 1371 user.save(); 1372 1373 endTransaction(t, dbt, true); 1374 } catch (Exception e) { 1375 if (e instanceof RootException) { 1376 throw (RootException)e; 1377 } 1378 throw new RootException(e); 1379 } 1380 } 1381 1382 1392 public String getUserRealName (UserTransaction t,String username) throws RootException { 1393 String ret = null; 1394 DBTransaction dbt = beginTransaction(t); 1395 try { 1396 UserQuery uQry = new UserQuery(dbt); 1397 uQry.setQueryUserid(username); 1398 uQry.requireUniqueInstance(); 1399 UserDO resultDO = uQry.getNextDO(); 1400 ret = resultDO.getFirstname(); 1401 String lastName = resultDO.getLastname(); 1402 if (ret != null) { 1403 if (lastName != null) 1404 ret = ret + " " + lastName; 1405 } 1406 else { 1407 if (lastName != null) 1408 ret = lastName; 1409 } 1410 1411 endTransaction(t, dbt, false); 1412 } catch (Exception e) { 1413 if (e instanceof RootException) { 1414 throw (RootException)e; 1415 } 1416 throw new RootException(e); 1417 } 1418 return ret; 1419 } 1420 1421 1430 public String getUserFirstName (UserTransaction t,String username) throws RootException { 1431 String ret = null; 1432 DBTransaction dbt = beginTransaction(t); 1433 try { 1434 UserQuery uQry = new UserQuery(dbt); 1435 uQry.setQueryUserid(username); 1436 uQry.requireUniqueInstance(); 1437 ret = uQry.getNextDO().getFirstname(); 1438 1439 endTransaction(t, dbt, false); 1440 } catch (Exception e) { 1441 if (e instanceof RootException) { 1442 throw (RootException)e; 1443 } 1444 throw new RootException(e); 1445 } 1446 return ret; 1447 } 1448 1449 1458 public String getUserLastName (UserTransaction t,String username) throws RootException { 1459 String ret = null; 1460 DBTransaction dbt = beginTransaction(t); 1461 try { 1462 UserQuery uQry = new UserQuery(dbt); 1463 uQry.setQueryUserid(username); 1464 uQry.requireUniqueInstance(); 1465 ret = uQry.getNextDO().getLastname(); 1466 1467 endTransaction(t, dbt, false); 1468 } catch (Exception e) { 1469 if (e instanceof RootException) { 1470 throw (RootException)e; 1471 } 1472 throw new RootException(e); 1473 } 1474 return ret; 1475 } 1476 1477 1487 public String getUserEMailAddress (UserTransaction t,String username) throws RootException { 1488 String ret = null; 1489 DBTransaction dbt = beginTransaction(t); 1490 try { 1491 UserQuery uQry = new UserQuery(dbt); 1492 uQry.setQueryUserid(username); 1493 uQry.requireUniqueInstance(); 1494 ret = uQry.getNextDO().getEmail(); 1495 1496 endTransaction(t, dbt, false); 1497 } catch (Exception e) { 1498 if (e instanceof RootException) { 1499 throw (RootException)e; 1500 } 1501 throw new RootException(e); 1502 } 1503 return ret; 1504 } 1505 1506 private void endTransaction(UserTransaction t, 1507 DBTransaction dbt, 1508 boolean commitToo) throws RootException { 1509 if (!dbt.getDatabaseName().equals(db.getName())) { 1510 try { 1511 if (commitToo) { 1512 dbt.commit(); 1513 } 1514 } catch (SQLException e) { 1515 throw new RootException(e); 1516 } finally { 1517 dbt.release(); 1518 } 1519 } 1520 } 1521 private DBTransaction beginTransaction(UserTransaction t) throws RootException { 1522 DBTransaction dbt = ((SharkDODSUserTransaction)t).getDODSTransaction(); 1523 if (!dbt.getDatabaseName().equals(db.getName())) { 1524 try { 1525 dbt = db.createTransaction(); 1526 } catch (SQLException e) { 1527 throw new RootException(e); 1528 } 1529 } 1530 return dbt; 1531 } 1532} 1533 | Popular Tags |