1 40 package org.dspace.content; 41 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.sql.*; 45 import java.util.ArrayList ; 46 import java.util.List ; 47 import java.util.MissingResourceException ; 48 49 import org.apache.log4j.Logger; 50 import org.dspace.authorize.AuthorizeException; 51 import org.dspace.authorize.AuthorizeManager; 52 import org.dspace.authorize.ResourcePolicy; 53 import org.dspace.core.ConfigurationManager; 54 import org.dspace.core.Constants; 55 import org.dspace.core.Context; 56 import org.dspace.core.I18N; 57 import org.dspace.core.LogManager; 58 import org.dspace.eperson.Group; 59 import org.dspace.handle.HandleManager; 60 import org.dspace.history.HistoryManager; 61 import org.dspace.search.DSIndexer; 62 import org.dspace.storage.rdbms.DatabaseManager; 63 import org.dspace.storage.rdbms.TableRow; 64 import org.dspace.storage.rdbms.TableRowIterator; 65 import org.dspace.workflow.WorkflowItem; 66 67 81 public class Collection extends DSpaceObject 82 { 83 84 private static Logger log = Logger.getLogger(Collection.class); 85 86 87 private Context ourContext; 88 89 90 private TableRow collectionRow; 91 92 93 private Bitstream logo; 94 95 96 private Item template; 97 98 99 private String handle; 100 101 105 private Group[] workflowGroup; 106 107 108 private Group submitters; 109 110 111 private Group admins; 112 113 122 Collection(Context context, TableRow row) throws SQLException 123 { 124 ourContext = context; 125 collectionRow = row; 126 127 if (collectionRow.isColumnNull("logo_bitstream_id")) 129 { 130 logo = null; 131 } 132 else 133 { 134 logo = Bitstream.find(ourContext, collectionRow 135 .getIntColumn("logo_bitstream_id")); 136 } 137 138 if (collectionRow.isColumnNull("template_item_id")) 140 { 141 template = null; 142 } 143 else 144 { 145 template = Item.find(ourContext, collectionRow 146 .getIntColumn("template_item_id")); 147 } 148 149 workflowGroup = new Group[3]; 151 152 workflowGroup[0] = groupFromColumn("workflow_step_1"); 153 workflowGroup[1] = groupFromColumn("workflow_step_2"); 154 workflowGroup[2] = groupFromColumn("workflow_step_3"); 155 156 submitters = groupFromColumn("submitter"); 157 admins = groupFromColumn("admin"); 158 159 handle = HandleManager.findHandle(context, this); 161 162 context.cache(this, row.getIntColumn("collection_id")); 164 } 165 166 177 public static Collection find(Context context, int id) throws SQLException 178 { 179 Collection fromCache = (Collection) context.fromCache(Collection.class, 181 id); 182 183 if (fromCache != null) 184 { 185 return fromCache; 186 } 187 188 TableRow row = DatabaseManager.find(context, "collection", id); 189 190 if (row == null) 191 { 192 if (log.isDebugEnabled()) 193 { 194 log.debug(LogManager.getHeader(context, "find_collection", 195 "not_found,collection_id=" + id)); 196 } 197 198 return null; 199 } 200 201 if (log.isDebugEnabled()) 203 { 204 log.debug(LogManager.getHeader(context, "find_collection", 205 "collection_id=" + id)); 206 } 207 208 return new Collection(context, row); 209 } 210 211 222 static Collection create(Context context) throws SQLException, 223 AuthorizeException 224 { 225 TableRow row = DatabaseManager.create(context, "collection"); 226 Collection c = new Collection(context, row); 227 c.handle = HandleManager.createHandle(context, c); 228 229 Group anonymousGroup = Group.find(context, 0); 232 233 ResourcePolicy myPolicy = ResourcePolicy.create(context); 234 myPolicy.setResource(c); 235 myPolicy.setAction(Constants.READ); 236 myPolicy.setGroup(anonymousGroup); 237 myPolicy.update(); 238 239 myPolicy = ResourcePolicy.create(context); 241 myPolicy.setResource(c); 242 myPolicy.setAction(Constants.DEFAULT_ITEM_READ); 243 myPolicy.setGroup(anonymousGroup); 244 myPolicy.update(); 245 246 myPolicy = ResourcePolicy.create(context); 247 myPolicy.setResource(c); 248 myPolicy.setAction(Constants.DEFAULT_BITSTREAM_READ); 249 myPolicy.setGroup(anonymousGroup); 250 myPolicy.update(); 251 252 HistoryManager.saveHistory(context, c, HistoryManager.CREATE, context 253 .getCurrentUser(), context.getExtraLogInfo()); 254 255 log.info(LogManager.getHeader(context, "create_collection", 256 "collection_id=" + row.getIntColumn("collection_id")) 257 + ",handle=" + c.handle); 258 259 return c; 260 } 261 262 272 public static Collection[] findAll(Context context) throws SQLException 273 { 274 TableRowIterator tri = DatabaseManager.queryTable(context, "collection", 275 "SELECT * FROM collection ORDER BY name"); 276 277 List collections = new ArrayList (); 278 279 while (tri.hasNext()) 280 { 281 TableRow row = tri.next(); 282 283 Collection fromCache = (Collection) context.fromCache( 285 Collection.class, row.getIntColumn("collection_id")); 286 287 if (fromCache != null) 288 { 289 collections.add(fromCache); 290 } 291 else 292 { 293 collections.add(new Collection(context, row)); 294 } 295 } 296 tri.close(); 298 299 Collection[] collectionArray = new Collection[collections.size()]; 300 collectionArray = (Collection[]) collections.toArray(collectionArray); 301 302 return collectionArray; 303 } 304 305 311 public ItemIterator getItems() throws SQLException 312 { 313 String myQuery = "SELECT item.* FROM item, collection2item WHERE " 314 + "item.item_id=collection2item.item_id AND " 315 + "collection2item.collection_id= ? " 316 + "AND item.in_archive='1'"; 317 318 TableRowIterator rows = DatabaseManager.queryTable(ourContext, "item", 319 myQuery,getID()); 320 321 return new ItemIterator(ourContext, rows); 322 } 323 324 330 public ItemIterator getAllItems() throws SQLException 331 { 332 String myQuery = "SELECT item.* FROM item, collection2item WHERE " 333 + "item.item_id=collection2item.item_id AND " 334 + "collection2item.collection_id= ? "; 335 336 TableRowIterator rows = DatabaseManager.queryTable(ourContext, "item", 337 myQuery,getID()); 338 339 return new ItemIterator(ourContext, rows); 340 } 341 342 347 public int getID() 348 { 349 return collectionRow.getIntColumn("collection_id"); 350 } 351 352 public String getHandle() 353 { 354 return handle; 355 } 356 357 368 public String getMetadata(String field) 369 { 370 return collectionRow.getStringColumn(field); 371 } 372 373 385 public void setMetadata(String field, String value) throws MissingResourceException 386 { 387 if ((field.trim()).equals("name") && (value.trim()).equals("")) 388 { 389 try 390 { 391 value = I18N.message("untitled", Collection.class); 392 } 393 catch (MissingResourceException e) 394 { 395 value = "Untitled"; 396 } 397 } 398 collectionRow.setColumn(field, value); 399 } 400 401 407 public Bitstream getLogo() 408 { 409 return logo; 410 } 411 412 428 public Bitstream setLogo(InputStream is) throws AuthorizeException, 429 IOException , SQLException 430 { 431 if (!((is == null) && AuthorizeManager.authorizeActionBoolean( 435 ourContext, this, Constants.DELETE))) 436 { 437 canEdit(); 438 } 439 440 if (!collectionRow.isColumnNull("logo_bitstream_id")) 442 { 443 logo.delete(); 444 } 445 446 if (is == null) 447 { 448 collectionRow.setColumnNull("logo_bitstream_id"); 449 logo = null; 450 451 log.info(LogManager.getHeader(ourContext, "remove_logo", 452 "collection_id=" + getID())); 453 } 454 else 455 { 456 Bitstream newLogo = Bitstream.create(ourContext, is); 457 collectionRow.setColumn("logo_bitstream_id", newLogo.getID()); 458 logo = newLogo; 459 460 List policies = AuthorizeManager.getPoliciesActionFilter( 463 ourContext, this, Constants.READ); 464 AuthorizeManager.addPolicies(ourContext, policies, newLogo); 465 466 log.info(LogManager.getHeader(ourContext, "set_logo", 467 "collection_id=" + getID() + "logo_bitstream_id=" 468 + newLogo.getID())); 469 } 470 471 return logo; 472 } 473 474 488 public Group createWorkflowGroup(int step) throws SQLException, 489 AuthorizeException 490 { 491 AuthorizeManager.authorizeAction(ourContext, this, Constants.WRITE); 493 494 if (workflowGroup[step - 1] == null) 495 { 496 Group g = Group.create(ourContext); 497 g.setName("COLLECTION_" + getID() + "_WORKFLOW_STEP_" + step); 498 g.update(); 499 setWorkflowGroup(step, g); 500 501 AuthorizeManager.addPolicy(ourContext, this, Constants.ADD, g); 502 } 503 504 return workflowGroup[step - 1]; 505 } 506 507 517 public void setWorkflowGroup(int step, Group g) 518 { 519 workflowGroup[step - 1] = g; 520 521 if (g == null) 522 { 523 collectionRow.setColumnNull("workflow_step_" + step); 524 } 525 else 526 { 527 collectionRow.setColumn("workflow_step_" + step, g.getID()); 528 } 529 } 530 531 541 public Group getWorkflowGroup(int step) 542 { 543 return workflowGroup[step - 1]; 544 } 545 546 556 public Group createSubmitters() throws SQLException, AuthorizeException 557 { 558 AuthorizeManager.authorizeAction(ourContext, this, Constants.WRITE); 560 561 if (submitters == null) 562 { 563 submitters = Group.create(ourContext); 564 submitters.setName("COLLECTION_" + getID() + "_SUBMIT"); 565 submitters.update(); 566 } 567 568 collectionRow.setColumn("submitter", submitters.getID()); 570 571 AuthorizeManager.addPolicy(ourContext, this, Constants.ADD, submitters); 572 573 return submitters; 574 } 575 576 587 public Group getSubmitters() 588 { 589 return submitters; 590 } 591 592 601 public Group createAdministrators() throws SQLException, AuthorizeException 602 { 603 AuthorizeManager.authorizeAction(ourContext, this, Constants.WRITE); 605 606 if (admins == null) 607 { 608 admins = Group.create(ourContext); 609 admins.setName("COLLECTION_" + getID() + "_ADMIN"); 610 admins.update(); 611 } 612 613 AuthorizeManager.addPolicy(ourContext, this, 614 Constants.COLLECTION_ADMIN, admins); 615 616 collectionRow.setColumn("admin", admins.getID()); 618 619 if (submitters != null) 621 { 622 AuthorizeManager.addPolicy(ourContext, submitters, Constants.ADD, 623 admins); 624 } 625 626 return admins; 627 } 628 629 640 public Group getAdministrators() 641 { 642 return admins; 643 } 644 645 652 public String getLicense() 653 { 654 String license = collectionRow.getStringColumn("license"); 655 656 if ((license == null) || license.equals("")) 657 { 658 license = ConfigurationManager.getDefaultSubmissionLicense(); 660 } 661 662 return license; 663 } 664 665 670 public boolean hasCustomLicense() 671 { 672 String license = collectionRow.getStringColumn("license"); 673 674 return ((license != null) && !license.equals("")); 675 } 676 677 684 public void setLicense(String license) 685 { 686 if (license == null) 687 { 688 collectionRow.setColumnNull("license"); 689 } 690 else 691 { 692 collectionRow.setColumn("license", license); 693 } 694 } 695 696 704 public Item getTemplateItem() throws SQLException 705 { 706 return template; 707 } 708 709 718 public void createTemplateItem() throws SQLException, AuthorizeException 719 { 720 canEdit(); 722 723 if (template == null) 724 { 725 template = Item.create(ourContext); 726 collectionRow.setColumn("template_item_id", template.getID()); 727 728 log.info(LogManager.getHeader(ourContext, "create_template_item", 729 "collection_id=" + getID() + ",template_item_id=" 730 + template.getID())); 731 } 732 } 733 734 745 public void removeTemplateItem() throws SQLException, AuthorizeException, 746 IOException 747 { 748 canEdit(); 750 751 collectionRow.setColumnNull("template_item_id"); 752 DatabaseManager.update(ourContext, collectionRow); 753 754 if (template != null) 755 { 756 log.info(LogManager.getHeader(ourContext, "remove_template_item", 757 "collection_id=" + getID() + ",template_item_id=" 758 + template.getID())); 759 760 template.delete(); 761 template = null; 762 } 763 } 764 765 776 public void addItem(Item item) throws SQLException, AuthorizeException 777 { 778 AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD); 780 781 log.info(LogManager.getHeader(ourContext, "add_item", "collection_id=" 782 + getID() + ",item_id=" + item.getID())); 783 784 TableRow row = DatabaseManager.create(ourContext, "collection2item"); 786 787 row.setColumn("collection_id", getID()); 788 row.setColumn("item_id", item.getID()); 789 790 DatabaseManager.update(ourContext, row); 791 } 792 793 802 public void removeItem(Item item) throws SQLException, AuthorizeException, 803 IOException 804 { 805 AuthorizeManager.authorizeAction(ourContext, this, Constants.REMOVE); 807 808 log.info(LogManager.getHeader(ourContext, "remove_item", 809 "collection_id=" + getID() + ",item_id=" + item.getID())); 810 811 DatabaseManager.updateQuery(ourContext, 812 "DELETE FROM collection2item WHERE collection_id= ? "+ 813 "AND item_id= ? ", 814 getID(), item.getID()); 815 816 TableRowIterator tri = DatabaseManager.query(ourContext, 818 "SELECT * FROM collection2item WHERE item_id= ? ", 819 item.getID()); 820 821 if (!tri.hasNext()) 822 { 823 AuthorizeManager.addPolicy(ourContext, item, Constants.DELETE, 830 ourContext.getCurrentUser()); 831 AuthorizeManager.addPolicy(ourContext, item, Constants.REMOVE, 832 ourContext.getCurrentUser()); 833 834 item.delete(); 836 } 837 tri.close(); 839 } 840 841 849 public void update() throws SQLException, IOException , AuthorizeException 850 { 851 canEdit(); 853 854 HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY, 855 ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); 856 857 log.info(LogManager.getHeader(ourContext, "update_collection", 858 "collection_id=" + getID())); 859 860 DatabaseManager.update(ourContext, collectionRow); 861 862 DSIndexer.reIndexContent(ourContext, this); 865 } 866 867 public boolean canEditBoolean() throws java.sql.SQLException 868 { 869 try 870 { 871 canEdit(); 872 873 return true; 874 } 875 catch (AuthorizeException e) 876 { 877 return false; 878 } 879 } 880 881 public void canEdit() throws AuthorizeException, SQLException 882 { 883 Community[] parents = getCommunities(); 884 885 for (int i = 0; i < parents.length; i++) 886 { 887 if (AuthorizeManager.authorizeActionBoolean(ourContext, parents[i], 888 Constants.WRITE)) 889 { 890 return; 891 } 892 893 if (AuthorizeManager.authorizeActionBoolean(ourContext, parents[i], 894 Constants.ADD)) 895 { 896 return; 897 } 898 } 899 900 AuthorizeManager.authorizeAnyOf(ourContext, this, new int[] { 901 Constants.WRITE, Constants.COLLECTION_ADMIN }); 902 } 903 904 913 void delete() throws SQLException, AuthorizeException, IOException 914 { 915 log.info(LogManager.getHeader(ourContext, "delete_collection", 916 "collection_id=" + getID())); 917 918 DSIndexer.unIndexContent(ourContext, this); 920 921 ourContext.removeCached(this, getID()); 923 924 HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE, 925 ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); 926 927 DatabaseManager.updateQuery(ourContext, 929 "DELETE FROM subscription WHERE collection_id= ? ", 930 getID()); 931 932 removeTemplateItem(); 934 935 ItemIterator items = getAllItems(); 937 938 while (items.hasNext()) 939 { 940 Item item = items.next(); 941 942 if (item.isOwningCollection(this)) 943 { 944 Collection[] collections = item.getCollections(); 947 for (int i=0; i< collections.length; i++) 948 { 949 collections[i].removeItem(item); 950 } 951 952 } 953 else 955 { 956 removeItem(item); 957 } 958 } 959 960 setLogo(null); 962 963 AuthorizeManager.removeAllPolicies(ourContext, this); 965 966 WorkflowItem[] wfarray = WorkflowItem 968 .findByCollection(ourContext, this); 969 970 for (int x = 0; x < wfarray.length; x++) 971 { 972 Item myItem = wfarray[x].getItem(); 974 wfarray[x].deleteWrapper(); 975 myItem.delete(); 976 } 977 978 WorkspaceItem[] wsarray = WorkspaceItem.findByCollection(ourContext, 980 this); 981 982 for (int x = 0; x < wsarray.length; x++) 983 { 984 wsarray[x].deleteAll(); 985 } 986 987 DatabaseManager.delete(ourContext, collectionRow); 989 990 Group g = null; 992 993 g = getWorkflowGroup(1); 994 995 if (g != null) 996 { 997 g.delete(); 998 } 999 1000 g = getWorkflowGroup(2); 1001 1002 if (g != null) 1003 { 1004 g.delete(); 1005 } 1006 1007 g = getWorkflowGroup(3); 1008 1009 if (g != null) 1010 { 1011 g.delete(); 1012 } 1013 1014 g = getAdministrators(); 1016 1017 if (g != null) 1018 { 1019 g.delete(); 1020 } 1021 1022 g = getSubmitters(); 1024 1025 if (g != null) 1026 { 1027 g.delete(); 1028 } 1029 } 1030 1031 1037 public Community[] getCommunities() throws SQLException 1038 { 1039 TableRowIterator tri = DatabaseManager.queryTable(ourContext,"community", 1041 "SELECT community.* FROM community, community2collection WHERE " + 1042 "community.community_id=community2collection.community_id " + 1043 "AND community2collection.collection_id= ? ", 1044 getID()); 1045 1046 List communities = new ArrayList (); 1048 1049 while (tri.hasNext()) 1050 { 1051 TableRow row = tri.next(); 1052 1053 Community owner = (Community) ourContext.fromCache(Community.class, 1055 row.getIntColumn("community_id")); 1056 1057 if (owner == null) 1058 { 1059 owner = new Community(ourContext, row); 1060 } 1061 1062 communities.add(owner); 1063 1064 Community[] parents = owner.getAllParents(); 1066 1067 for (int i = 0; i < parents.length; i++) 1068 { 1069 communities.add(parents[i]); 1070 } 1071 } 1072 tri.close(); 1074 1075 Community[] communityArray = new Community[communities.size()]; 1076 communityArray = (Community[]) communities.toArray(communityArray); 1077 1078 return communityArray; 1079 } 1080 1081 1091 public boolean equals(Object other) 1092 { 1093 if (!(other instanceof Collection)) 1094 { 1095 return false; 1096 } 1097 1098 return (getID() == ((Collection) other).getID()); 1099 } 1100 1101 1110 private Group groupFromColumn(String col) throws SQLException 1111 { 1112 if (collectionRow.isColumnNull(col)) 1113 { 1114 return null; 1115 } 1116 1117 return Group.find(ourContext, collectionRow.getIntColumn(col)); 1118 } 1119 1120 1125 public int getType() 1126 { 1127 return Constants.COLLECTION; 1128 } 1129 1130 1144 public static Collection[] findAuthorized(Context context, Community comm, 1145 int actionID) throws java.sql.SQLException 1146 { 1147 List myResults = new ArrayList (); 1148 1149 Collection[] myCollections = null; 1150 1151 if (comm != null) 1152 { 1153 myCollections = comm.getCollections(); 1154 } 1155 else 1156 { 1157 myCollections = Collection.findAll(context); 1158 } 1159 1160 for (int i = 0; i < myCollections.length; i++) 1162 { 1163 if (AuthorizeManager.authorizeActionBoolean(context, 1164 myCollections[i], actionID)) 1165 { 1166 myResults.add(myCollections[i]); 1167 } 1168 } 1169 1170 myCollections = new Collection[myResults.size()]; 1171 myCollections = (Collection[]) myResults.toArray(myCollections); 1172 1173 return myCollections; 1174 } 1175 1176 1181 public int countItems() 1182 throws SQLException 1183 { 1184 String query = "SELECT count(*) FROM collection2item, item WHERE " 1185 + "collection2item.collection_id = ? " 1186 + "AND collection2item.item_id = item.item_id " 1187 + "AND in_archive ='1' AND item.withdrawn='0' "; 1188 1189 PreparedStatement statement = ourContext.getDBConnection().prepareStatement(query); 1190 statement.setInt(1,getID()); 1191 1192 ResultSet rs = statement.executeQuery(); 1193 1194 rs.next(); 1195 int itemcount = rs.getInt(1); 1196 1197 statement.close(); 1198 1199 return itemcount; 1200 } 1201} 1202 | Popular Tags |