1 40 package org.dspace.eperson; 41 42 import java.sql.SQLException ; 43 import java.util.ArrayList ; 44 import java.util.HashMap ; 45 import java.util.HashSet ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.Map ; 49 import java.util.Set ; 50 51 import org.apache.log4j.Logger; 52 import org.dspace.authorize.AuthorizeException; 53 import org.dspace.authorize.AuthorizeManager; 54 import org.dspace.content.DSpaceObject; 55 import org.dspace.core.ConfigurationManager; 56 import org.dspace.core.Constants; 57 import org.dspace.core.Context; 58 import org.dspace.core.LogManager; 59 import org.dspace.storage.rdbms.DatabaseManager; 60 import org.dspace.storage.rdbms.TableRow; 61 import org.dspace.storage.rdbms.TableRowIterator; 62 63 69 public class Group extends DSpaceObject 70 { 71 public static final int ID = 0; 74 public static final int NAME = 1; 76 77 private static Logger log = Logger.getLogger(Group.class); 78 79 80 private Context myContext; 81 82 83 private TableRow myRow; 84 85 86 private List epeople = new ArrayList (); 87 88 private List groups = new ArrayList (); 89 90 91 private boolean epeopleChanged = false; 92 93 private boolean groupsChanged = false; 94 95 96 private boolean isDataLoaded = false; 97 98 104 Group(Context context, TableRow row) throws SQLException 105 { 106 myContext = context; 107 myRow = row; 108 109 context.cache(this, row.getIntColumn("eperson_group_id")); 111 } 112 113 119 public void loadData() 120 { 121 if (!isDataLoaded) 123 { 124 try 128 { 129 TableRowIterator tri = DatabaseManager.queryTable(myContext,"eperson", 131 "SELECT eperson.* FROM eperson, epersongroup2eperson WHERE " + 132 "epersongroup2eperson.eperson_id=eperson.eperson_id AND " + 133 "epersongroup2eperson.eperson_group_id= ?", 134 myRow.getIntColumn("eperson_group_id")); 135 136 while (tri.hasNext()) 137 { 138 TableRow r = (TableRow) tri.next(); 139 140 EPerson fromCache = (EPerson) myContext.fromCache( 142 EPerson.class, r.getIntColumn("eperson_id")); 143 144 if (fromCache != null) 145 { 146 epeople.add(fromCache); 147 } 148 else 149 { 150 epeople.add(new EPerson(myContext, r)); 151 } 152 } 153 154 tri.close(); 155 156 tri = DatabaseManager.queryTable(myContext,"epersongroup", 158 "SELECT epersongroup.* FROM epersongroup, group2group WHERE " + 159 "group2group.child_id=epersongroup.eperson_group_id AND "+ 160 "group2group.parent_id= ? ", 161 myRow.getIntColumn("eperson_group_id")); 162 163 while (tri.hasNext()) 164 { 165 TableRow r = (TableRow) tri.next(); 166 167 Group fromCache = (Group) myContext.fromCache(Group.class, 169 r.getIntColumn("eperson_group_id")); 170 171 if (fromCache != null) 172 { 173 groups.add(fromCache); 174 } 175 else 176 { 177 groups.add(new Group(myContext, r)); 178 } 179 } 180 181 tri.close(); 182 183 } 184 catch (Exception e) 185 { 186 throw new RuntimeException (e); 187 } 188 isDataLoaded = true; 189 } 190 } 191 192 198 public static Group create(Context context) throws SQLException , 199 AuthorizeException 200 { 201 if (!AuthorizeManager.isAdmin(context)) 203 { 204 throw new AuthorizeException( 205 "You must be an admin to create an EPerson Group"); 206 } 207 208 TableRow row = DatabaseManager.create(context, "epersongroup"); 210 211 Group g = new Group(context, row); 212 213 log.info(LogManager.getHeader(context, "create_group", "group_id=" 214 + g.getID())); 215 216 return g; 217 } 218 219 224 public int getID() 225 { 226 return myRow.getIntColumn("eperson_group_id"); 227 } 228 229 234 public String getName() 235 { 236 return myRow.getStringColumn("name"); 237 } 238 239 245 public void setName(String name) 246 { 247 myRow.setColumn("name", name); 248 } 249 250 256 public void addMember(EPerson e) 257 { 258 loadData(); 260 if (isMember(e)) 261 { 262 return; 263 } 264 265 epeople.add(e); 266 epeopleChanged = true; 267 } 268 269 274 public void addMember(Group g) 275 { 276 loadData(); 278 if (isMember(g)) 280 { 281 return; 282 } 283 284 groups.add(g); 285 groupsChanged = true; 286 } 287 288 294 public void removeMember(EPerson e) 295 { 296 loadData(); 298 if (epeople.remove(e)) 299 { 300 epeopleChanged = true; 301 } 302 } 303 304 309 public void removeMember(Group g) 310 { 311 loadData(); 313 if (groups.remove(g)) 314 { 315 groupsChanged = true; 316 } 317 } 318 319 325 public boolean isMember(EPerson e) 326 { 327 if (getID() == 0) 329 { 330 return true; 331 } 332 333 loadData(); 335 return epeople.contains(e); 336 } 337 338 345 public boolean isMember(Group g) 346 { 347 loadData(); 349 return groups.contains(g); 350 } 351 352 362 public static boolean isMember(Context c, int groupid) throws SQLException 363 { 364 if (groupid == 0) 366 { 367 return true; 368 } 369 370 if (c.inSpecialGroup(groupid)) 373 { 374 return true; 375 } 376 377 EPerson currentuser = c.getCurrentUser(); 378 379 if (currentuser != null) 381 { 382 return epersonInGroup(c, groupid, currentuser); 383 } 384 385 return false; 387 } 388 389 397 public static Group[] allMemberGroups(Context c, EPerson e) 398 throws SQLException 399 { 400 List groupList = new ArrayList (); 401 402 Set myGroups = allMemberGroupIDs(c, e); 403 Iterator i = myGroups.iterator(); 405 406 while (i.hasNext()) 407 { 408 groupList.add(Group.find(c, ((Integer ) i.next()).intValue())); 409 } 410 411 return (Group[]) groupList.toArray(new Group[0]); 412 } 413 414 422 public static Set allMemberGroupIDs(Context c, EPerson e) 423 throws SQLException 424 { 425 428 TableRowIterator tri = DatabaseManager.queryTable(c, "epersongroup2eperson", 429 "SELECT * FROM epersongroup2eperson WHERE eperson_id= ?", 430 e.getID()); 431 432 Set groupIDs = new HashSet (); 433 434 while (tri.hasNext()) 435 { 436 TableRow row = tri.next(); 437 438 int childID = row.getIntColumn("eperson_group_id"); 439 440 groupIDs.add(new Integer (childID)); 441 } 442 443 tri.close(); 444 445 Group[] specialGroups = c.getSpecialGroups(); 448 for(int j=0; j<specialGroups.length;j++) 449 groupIDs.add(new Integer (specialGroups[j].getID())); 450 451 455 String groupQuery = ""; 456 457 Iterator i = groupIDs.iterator(); 458 459 Object [] parameters = new Object [groupIDs.size()]; 461 int idx = 0; 462 while (i.hasNext()) 463 { 464 int groupID = ((Integer ) i.next()).intValue(); 465 466 parameters[idx++] = new Integer (groupID); 467 468 groupQuery += "child_id= ? "; 469 if (i.hasNext()) 470 groupQuery += " OR "; 471 } 472 473 if ("".equals(groupQuery)) 474 { 475 return groupIDs; 477 } 478 479 tri = DatabaseManager.queryTable(c, "group2groupcache", 483 "SELECT * FROM group2groupcache WHERE " + groupQuery, 484 parameters); 485 486 while (tri.hasNext()) 487 { 488 TableRow row = tri.next(); 489 490 int parentID = row.getIntColumn("parent_id"); 491 492 groupIDs.add(new Integer (parentID)); 493 } 494 495 tri.close(); 496 497 return groupIDs; 498 } 499 500 501 513 public static EPerson[] allMembers(Context c, Group g) 514 throws SQLException 515 { 516 List epersonList = new ArrayList (); 517 518 Set myEpeople = allMemberIDs(c, g); 519 Iterator i = myEpeople.iterator(); 521 522 while (i.hasNext()) 523 { 524 epersonList.add(EPerson.find(c, ((Integer ) i.next()).intValue())); 525 } 526 527 return (EPerson[]) epersonList.toArray(new EPerson[0]); 528 } 529 530 541 public static Set allMemberIDs(Context c, Group g) 542 throws SQLException 543 { 544 Set epeopleIDs = new HashSet (); 547 548 TableRowIterator tri = DatabaseManager.queryTable(c, "group2groupcache", 550 "SELECT * FROM group2groupcache WHERE parent_id= ? ", 551 g.getID()); 552 553 Set groupIDs = new HashSet (); 554 555 while (tri.hasNext()) 556 { 557 TableRow row = tri.next(); 558 559 int childID = row.getIntColumn("child_id"); 560 561 groupIDs.add(new Integer (childID)); 562 } 563 564 tri.close(); 565 566 570 Object [] parameters = new Object [groupIDs.size()+1]; 571 int idx = 0; 572 Iterator i = groupIDs.iterator(); 573 574 parameters[idx++] = new Integer (g.getID()); 576 String epersonQuery = "eperson_group_id= ? "; 577 if (i.hasNext()) 578 epersonQuery += " OR "; 579 580 while (i.hasNext()) 581 { 582 int groupID = ((Integer ) i.next()).intValue(); 583 parameters[idx++] = new Integer (groupID); 584 585 epersonQuery += "eperson_group_id= ? "; 586 if (i.hasNext()) 587 epersonQuery += " OR "; 588 } 589 590 tri = DatabaseManager.queryTable(c, "epersongroup2eperson", 594 "SELECT * FROM epersongroup2eperson WHERE " + epersonQuery, 595 parameters); 596 597 while (tri.hasNext()) 598 { 599 TableRow row = tri.next(); 600 601 int epersonID = row.getIntColumn("eperson_id"); 602 603 epeopleIDs.add(new Integer (epersonID)); 604 } 605 606 tri.close(); 607 608 return epeopleIDs; 609 } 610 611 private static boolean epersonInGroup(Context c, int groupID, EPerson e) 612 throws SQLException 613 { 614 Set groupIDs = Group.allMemberGroupIDs(c, e); 615 616 return groupIDs.contains(new Integer (groupID)); 617 } 618 619 625 public static Group find(Context context, int id) throws SQLException 626 { 627 Group fromCache = (Group) context.fromCache(Group.class, id); 629 630 if (fromCache != null) 631 { 632 return fromCache; 633 } 634 635 TableRow row = DatabaseManager.find(context, "epersongroup", id); 636 637 if (row == null) 638 { 639 return null; 640 } 641 else 642 { 643 return new Group(context, row); 644 } 645 } 646 647 655 public static Group findByName(Context context, String name) 656 throws SQLException 657 { 658 TableRow row = DatabaseManager.findByUnique(context, "epersongroup", 659 "name", name); 660 661 if (row == null) 662 { 663 return null; 664 } 665 else 666 { 667 Group fromCache = (Group) context.fromCache(Group.class, row 669 .getIntColumn("eperson_group_id")); 670 671 if (fromCache != null) 672 { 673 return fromCache; 674 } 675 else 676 { 677 return new Group(context, row); 678 } 679 } 680 } 681 682 692 public static Group[] findAll(Context context, int sortField) 693 throws SQLException 694 { 695 String s; 696 697 switch (sortField) 698 { 699 case ID: 700 s = "eperson_group_id"; 701 702 break; 703 704 case NAME: 705 s = "name"; 706 707 break; 708 709 default: 710 s = "name"; 711 } 712 713 TableRowIterator rows = DatabaseManager.queryTable( 716 context, "epersongroup", 717 "SELECT * FROM epersongroup ORDER BY "+s); 718 719 List gRows = rows.toList(); 720 721 Group[] groups = new Group[gRows.size()]; 722 723 for (int i = 0; i < gRows.size(); i++) 724 { 725 TableRow row = (TableRow) gRows.get(i); 726 727 Group fromCache = (Group) context.fromCache(Group.class, row 729 .getIntColumn("eperson_group_id")); 730 731 if (fromCache != null) 732 { 733 groups[i] = fromCache; 734 } 735 else 736 { 737 groups[i] = new Group(context, row); 738 } 739 } 740 741 return groups; 742 } 743 744 745 755 public static Group[] search(Context context, String query) 756 throws SQLException 757 { 758 return search(context, query, -1, -1); 759 } 760 761 775 public static Group[] search(Context context, String query, int offset, int limit) 776 throws SQLException 777 { 778 String params = "%"+query.toLowerCase()+"%"; 779 String dbquery = "SELECT * FROM epersongroup WHERE name ILIKE ? OR eperson_group_id = ? ORDER BY name ASC "; 780 781 if (offset >= 0 && limit > 0) { 782 dbquery += "LIMIT " + limit + " OFFSET " + offset; 783 } 784 785 Integer int_param; 787 try { 788 int_param = Integer.valueOf(query); 789 } 790 catch (NumberFormatException e) { 791 int_param = new Integer (-1); 792 } 793 794 TableRowIterator rows = 795 DatabaseManager.query(context, dbquery, new Object []{params, int_param}); 796 797 List groupRows = rows.toList(); 798 Group[] groups = new Group[groupRows.size()]; 799 800 for (int i = 0; i < groupRows.size(); i++) 801 { 802 TableRow row = (TableRow) groupRows.get(i); 803 804 Group fromCache = (Group) context.fromCache(Group.class, row 806 .getIntColumn("eperson_group_id")); 807 808 if (fromCache != null) 809 { 810 groups[i] = fromCache; 811 } 812 else 813 { 814 groups[i] = new Group(context, row); 815 } 816 } 817 return groups; 818 } 819 820 831 public static int searchResultCount(Context context, String query) 832 throws SQLException 833 { 834 String params = "%"+query.toLowerCase()+"%"; 835 String dbquery = "SELECT count(*) as count FROM epersongroup WHERE name ILIKE ? OR eperson_group_id = ? "; 836 837 Integer int_param; 839 try { 840 int_param = Integer.valueOf(query); 841 } 842 catch (NumberFormatException e) { 843 int_param = new Integer (-1); 844 } 845 846 TableRow row = DatabaseManager.querySingle(context, dbquery, new Object [] {params, int_param}); 848 849 Long count; 851 if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) 852 { 853 count = new Long (row.getIntColumn("count")); 854 } 855 else { 857 count = new Long (row.getLongColumn("count")); 858 } 859 860 return count.intValue(); 861 } 862 863 864 868 public void delete() throws SQLException 869 { 870 myContext.removeCached(this, getID()); 873 874 AuthorizeManager.removeGroupPolicies(myContext, getID()); 876 877 DatabaseManager.updateQuery(myContext, 879 "DELETE FROM EPersonGroup2EPerson WHERE eperson_group_id= ? ", 880 getID()); 881 882 DatabaseManager.updateQuery(myContext, 884 "DELETE FROM group2groupcache WHERE parent_id= ? OR child_id= ? ", 885 getID(),getID()); 886 887 DatabaseManager.updateQuery(myContext, 889 "DELETE FROM group2group WHERE parent_id= ? OR child_id= ? ", 890 getID(),getID()); 891 892 deleteEpersonGroup2WorkspaceItem(); 894 895 DatabaseManager.delete(myContext, myRow); 897 898 epeople.clear(); 899 900 log.info(LogManager.getHeader(myContext, "delete_group", "group_id=" 901 + getID())); 902 } 903 904 907 private void deleteEpersonGroup2WorkspaceItem() throws SQLException 908 { 909 DatabaseManager.updateQuery(myContext, 910 "DELETE FROM EPersonGroup2WorkspaceItem WHERE eperson_group_id= ? ", 911 getID()); 912 } 913 914 917 public EPerson[] getMembers() 918 { 919 loadData(); 921 EPerson[] myArray = new EPerson[epeople.size()]; 922 myArray = (EPerson[]) epeople.toArray(myArray); 923 924 return myArray; 925 } 926 927 932 public Group[] getMemberGroups() 933 { 934 loadData(); 936 Group[] myArray = new Group[groups.size()]; 937 myArray = (Group[]) groups.toArray(myArray); 938 939 return myArray; 940 } 941 942 945 public boolean isEmpty() 946 { 947 loadData(); 949 if ((epeople.size() == 0) && (groups.size() == 0)) 950 { 951 return true; 952 } 953 else 954 { 955 return false; 956 } 957 } 958 959 962 public void update() throws SQLException , AuthorizeException 963 { 964 DatabaseManager.update(myContext, myRow); 966 967 if (epeopleChanged) 969 { 970 DatabaseManager.updateQuery(myContext, 972 "delete from epersongroup2eperson where eperson_group_id= ? ", 973 getID()); 974 975 Iterator i = epeople.iterator(); 977 978 while (i.hasNext()) 979 { 980 EPerson e = (EPerson) i.next(); 981 982 TableRow mappingRow = DatabaseManager.create(myContext, 983 "epersongroup2eperson"); 984 mappingRow.setColumn("eperson_id", e.getID()); 985 mappingRow.setColumn("eperson_group_id", getID()); 986 DatabaseManager.update(myContext, mappingRow); 987 } 988 989 epeopleChanged = false; 990 } 991 992 if (groupsChanged) 994 { 995 DatabaseManager.updateQuery(myContext, 997 "delete from group2group where parent_id= ? ", 998 getID()); 999 1000 Iterator i = groups.iterator(); 1002 1003 while (i.hasNext()) 1004 { 1005 Group g = (Group) i.next(); 1006 1007 TableRow mappingRow = DatabaseManager.create(myContext, 1008 "group2group"); 1009 mappingRow.setColumn("parent_id", getID()); 1010 mappingRow.setColumn("child_id", g.getID()); 1011 DatabaseManager.update(myContext, mappingRow); 1012 } 1013 1014 rethinkGroupCache(); 1016 1017 groupsChanged = false; 1018 } 1019 1020 log.info(LogManager.getHeader(myContext, "update_group", "group_id=" 1021 + getID())); 1022 } 1023 1024 1034 public boolean equals(Object other) 1035 { 1036 if (!(other instanceof Group)) 1037 { 1038 return false; 1039 } 1040 1041 return (getID() == ((Group) other).getID()); 1042 } 1043 1044 public int getType() 1045 { 1046 return Constants.GROUP; 1047 } 1048 1049 public String getHandle() 1050 { 1051 return null; 1052 } 1053 1054 1059 private void rethinkGroupCache() throws SQLException 1060 { 1061 TableRowIterator tri = DatabaseManager.queryTable(myContext, "group2group", 1063 "SELECT * FROM group2group"); 1064 1065 Map parents = new HashMap (); 1066 1067 while (tri.hasNext()) 1068 { 1069 TableRow row = (TableRow) tri.next(); 1070 1071 Integer parentID = new Integer (row.getIntColumn("parent_id")); 1072 Integer childID = new Integer (row.getIntColumn("child_id")); 1073 1074 if (!parents.containsKey(parentID)) 1076 { 1077 Set children = new HashSet (); 1078 1079 children.add(childID); 1081 parents.put(parentID, children); 1082 } 1083 else 1084 { 1085 Set children = (Set ) parents.get(parentID); 1088 children.add(childID); 1089 } 1090 } 1091 1092 tri.close(); 1093 1094 1100 Iterator i = parents.keySet().iterator(); 1101 1102 while (i.hasNext()) 1103 { 1104 Integer parentID = (Integer ) i.next(); 1105 1106 Set myChildren = getChildren(parents, parentID); 1107 1108 Iterator j = myChildren.iterator(); 1109 1110 while (j.hasNext()) 1111 { 1112 Integer childID = (Integer ) j.next(); 1114 1115 ((Set ) parents.get(parentID)).add(childID); 1116 } 1117 } 1118 1119 DatabaseManager.updateQuery(myContext, 1121 "DELETE FROM group2groupcache WHERE id >= 0"); 1122 1123 Iterator pi = parents.keySet().iterator(); 1126 while (pi.hasNext()) 1127 { 1128 Integer parent = (Integer ) pi.next(); 1129 1130 Set children = (Set ) parents.get(parent); 1131 Iterator ci = children.iterator(); 1133 while (ci.hasNext()) 1134 { 1135 Integer child = (Integer ) ci.next(); 1136 1137 TableRow row = DatabaseManager.create(myContext, 1138 "group2groupcache"); 1139 1140 int parentID = parent.intValue(); 1141 int childID = child.intValue(); 1142 1143 row.setColumn("parent_id", parentID); 1144 row.setColumn("child_id", childID); 1145 1146 DatabaseManager.update(myContext, row); 1147 } 1148 } 1149 } 1150 1151 1161 private Set getChildren(Map parents, Integer parent) 1162 { 1163 Set myChildren = new HashSet (); 1164 1165 if (!parents.containsKey(parent)) 1167 return myChildren; 1168 1169 Set children = (Set ) parents.get(parent); 1171 1172 Iterator i = children.iterator(); 1174 1175 while (i.hasNext()) 1176 { 1177 Integer childID = (Integer ) i.next(); 1178 1179 myChildren.add(childID); 1181 1182 myChildren.addAll(getChildren(parents, childID)); 1184 } 1185 1186 return myChildren; 1187 } 1188} 1189 | Popular Tags |