1 40 package org.dspace.content; 41 42 import java.io.ByteArrayInputStream ; 43 import java.io.IOException ; 44 import java.io.InputStream ; 45 import java.sql.SQLException ; 46 import java.util.ArrayList ; 47 import java.util.Date ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.ListIterator ; 52 import java.util.Map ; 53 import java.util.StringTokenizer ; 54 55 import org.apache.log4j.Logger; 56 import org.dspace.authorize.AuthorizeException; 57 import org.dspace.authorize.AuthorizeManager; 58 import org.dspace.authorize.ResourcePolicy; 59 import org.dspace.browse.Browse; 60 import org.dspace.core.ConfigurationManager; 61 import org.dspace.core.Constants; 62 import org.dspace.core.Context; 63 import org.dspace.core.LogManager; 64 import org.dspace.eperson.EPerson; 65 import org.dspace.eperson.Group; 66 import org.dspace.handle.HandleManager; 67 import org.dspace.history.HistoryManager; 68 import org.dspace.search.DSIndexer; 69 import org.dspace.storage.rdbms.DatabaseManager; 70 import org.dspace.storage.rdbms.TableRow; 71 import org.dspace.storage.rdbms.TableRowIterator; 72 73 87 public class Item extends DSpaceObject 88 { 89 92 public final static String ANY = "*"; 93 94 95 private static Logger log = Logger.getLogger(Item.class); 96 97 98 private Context ourContext; 99 100 101 private TableRow itemRow; 102 103 104 private EPerson submitter; 105 106 107 private List bundles; 108 109 110 private List dublinCore; 111 112 113 private String handle; 114 115 119 private boolean dublinCoreChanged; 120 121 130 Item(Context context, TableRow row) throws SQLException 131 { 132 ourContext = context; 133 itemRow = row; 134 dublinCoreChanged = false; 135 dublinCore = new ArrayList (); 136 137 TableRowIterator tri = DatabaseManager.queryTable(ourContext, "MetadataValue", 139 "SELECT * FROM MetadataValue WHERE item_id= ? " + 140 " ORDER BY metadata_field_id, place", 141 itemRow.getIntColumn("item_id")); 142 143 while (tri.hasNext()) 144 { 145 TableRow resultRow = tri.next(); 146 147 int fieldID = resultRow.getIntColumn("metadata_field_id"); 149 MetadataField field = MetadataField.find(context, fieldID); 150 151 if (field == null) 152 { 153 log.error("Loading item - cannot found metadata field " 154 + fieldID); 155 } 156 else 157 { 158 MetadataSchema schema = MetadataSchema.find( 159 context, field.getSchemaID()); 160 161 DCValue dcv = new DCValue(); 163 dcv.element = field.getElement(); 164 dcv.qualifier = field.getQualifier(); 165 dcv.value = resultRow.getStringColumn("text_value"); 166 dcv.language = resultRow.getStringColumn("text_lang"); 167 dcv.schema = schema.getName(); 169 170 dublinCore.add(dcv); 172 } 173 } 174 tri.close(); 176 177 handle = HandleManager.findHandle(context, this); 179 180 context.cache(this, row.getIntColumn("item_id")); 182 } 183 184 195 public static Item find(Context context, int id) throws SQLException 196 { 197 Item fromCache = (Item) context.fromCache(Item.class, id); 199 200 if (fromCache != null) 201 { 202 return fromCache; 203 } 204 205 TableRow row = DatabaseManager.find(context, "item", id); 206 207 if (row == null) 208 { 209 if (log.isDebugEnabled()) 210 { 211 log.debug(LogManager.getHeader(context, "find_item", 212 "not_found,item_id=" + id)); 213 } 214 215 return null; 216 } 217 218 if (log.isDebugEnabled()) 220 { 221 log.debug(LogManager.getHeader(context, "find_item", "item_id=" 222 + id)); 223 } 224 225 return new Item(context, row); 226 } 227 228 239 static Item create(Context context) throws SQLException , AuthorizeException 240 { 241 TableRow row = DatabaseManager.create(context, "item"); 242 Item i = new Item(context, row); 243 244 context.setIgnoreAuthorization(true); 247 i.update(); 248 context.setIgnoreAuthorization(false); 249 250 HistoryManager.saveHistory(context, i, HistoryManager.CREATE, context 251 .getCurrentUser(), context.getExtraLogInfo()); 252 253 log.info(LogManager.getHeader(context, "create_item", "item_id=" 254 + row.getIntColumn("item_id"))); 255 256 return i; 257 } 258 259 268 public static ItemIterator findAll(Context context) throws SQLException 269 { 270 String myQuery = "SELECT * FROM item WHERE in_archive='1'"; 271 272 TableRowIterator rows = DatabaseManager.queryTable(context, "item", myQuery); 273 274 return new ItemIterator(context, rows); 275 } 276 277 288 public static ItemIterator findBySubmitter(Context context, EPerson eperson) 289 throws SQLException 290 { 291 String myQuery = "SELECT * FROM item WHERE in_archive='1' AND submitter_id=" 292 + eperson.getID(); 293 294 TableRowIterator rows = DatabaseManager.queryTable(context, "item", myQuery); 295 296 return new ItemIterator(context, rows); 297 } 298 299 305 public int getID() 306 { 307 return itemRow.getIntColumn("item_id"); 308 } 309 310 313 public String getHandle() 314 { 315 return handle; 316 } 317 318 323 public boolean isArchived() 324 { 325 return itemRow.getBooleanColumn("in_archive"); 326 } 327 328 333 public boolean isWithdrawn() 334 { 335 return itemRow.getBooleanColumn("withdrawn"); 336 } 337 338 345 public Date getLastModified() 346 { 347 Date myDate = itemRow.getDateColumn("last_modified"); 348 349 if (myDate == null) 350 { 351 myDate = new Date (); 352 } 353 354 return myDate; 355 } 356 357 364 public void setArchived(boolean isArchived) 365 { 366 itemRow.setColumn("in_archive", isArchived); 367 } 368 369 375 public void setOwningCollection(Collection c) 376 { 377 itemRow.setColumn("owning_collection", c.getID()); 378 } 379 380 386 public Collection getOwningCollection() throws java.sql.SQLException 387 { 388 Collection myCollection = null; 389 390 int cid = itemRow.getIntColumn("owning_collection"); 392 393 myCollection = Collection.find(ourContext, cid); 394 395 return myCollection; 396 } 397 398 440 public DCValue[] getDC(String element, String qualifier, String lang) 441 { 442 return getMetadata(MetadataSchema.DC_SCHEMA, element, qualifier, lang); 443 } 444 445 491 public DCValue[] getMetadata(String schema, String element, String qualifier, 492 String lang) 493 { 494 List values = new ArrayList (); 496 Iterator i = dublinCore.iterator(); 497 498 while (i.hasNext()) 499 { 500 DCValue dcv = (DCValue) i.next(); 501 502 if (match(schema, element, qualifier, lang, dcv)) 503 { 504 DCValue copy = new DCValue(); 506 copy.element = dcv.element; 507 copy.qualifier = dcv.qualifier; 508 copy.value = dcv.value; 509 copy.language = dcv.language; 510 copy.schema = dcv.schema; 511 512 values.add(copy); 513 } 514 } 515 516 DCValue[] valueArray = new DCValue[values.size()]; 518 valueArray = (DCValue[]) values.toArray(valueArray); 519 520 return valueArray; 521 } 522 523 531 public DCValue[] getMetadata(String mdString) 532 { 533 StringTokenizer dcf = new StringTokenizer (mdString, "."); 534 535 String [] tokens = { "", "", "" }; 536 int i = 0; 537 while(dcf.hasMoreTokens()) 538 { 539 tokens[i] = dcf.nextToken().toLowerCase().trim(); 540 i++; 541 } 542 String schema = tokens[0]; 543 String element = tokens[1]; 544 String qualifier = tokens[2]; 545 546 DCValue[] values; 547 if ("*".equals(qualifier)) 548 { 549 values = getMetadata(schema, element, Item.ANY, Item.ANY); 550 } 551 else if ("".equals(qualifier)) 552 { 553 values = getMetadata(schema, element, null, Item.ANY); 554 } 555 else 556 { 557 values = getMetadata(schema, element, qualifier, Item.ANY); 558 } 559 560 return values; 561 } 562 563 580 public void addDC(String element, String qualifier, String lang, 581 String [] values) 582 { 583 addMetadata(MetadataSchema.DC_SCHEMA, element, qualifier, lang, values); 584 } 585 586 602 public void addDC(String element, String qualifier, String lang, 603 String value) 604 { 605 addMetadata(MetadataSchema.DC_SCHEMA, element, qualifier, lang, value); 606 } 607 608 627 public void addMetadata(String schema, String element, String qualifier, String lang, 628 String [] values) 629 { 630 for (int i = 0; i < values.length; i++) 633 { 634 DCValue dcv = new DCValue(); 635 dcv.schema = schema; 636 dcv.element = element; 637 dcv.qualifier = qualifier; 638 dcv.language = lang; 639 dcv.value = (values[i] == null ? null : values[i].trim()); 640 dublinCore.add(dcv); 641 } 642 643 if (values.length > 0) 644 { 645 dublinCoreChanged = true; 646 } 647 } 648 649 668 public void addMetadata(String schema, String element, String qualifier, 669 String lang, String value) 670 { 671 String [] valArray = new String [1]; 672 valArray[0] = value; 673 674 addMetadata(schema, element, qualifier, lang, valArray); 675 } 676 677 698 public void clearDC(String element, String qualifier, String lang) 699 { 700 clearMetadata(MetadataSchema.DC_SCHEMA, element, qualifier, lang); 701 } 702 703 727 public void clearMetadata(String schema, String element, String qualifier, 728 String lang) 729 { 730 List values = new ArrayList (); 732 Iterator i = dublinCore.iterator(); 733 734 while (i.hasNext()) 735 { 736 DCValue dcv = (DCValue) i.next(); 737 738 if (!match(schema, element, qualifier, lang, dcv)) 739 { 740 values.add(dcv); 741 } 742 } 743 744 dublinCore = values; 746 dublinCoreChanged = true; 747 } 748 749 770 private boolean match(String schema, String element, String qualifier, 771 String language, DCValue dcv) 772 { 773 if (!element.equals(Item.ANY) && !element.equals(dcv.element)) 775 { 776 return false; 778 } 779 780 if (qualifier == null) 781 { 782 if (dcv.qualifier != null) 784 { 785 return false; 787 } 788 } 789 else if (!qualifier.equals(Item.ANY)) 790 { 791 if (!qualifier.equals(dcv.qualifier)) 793 { 794 return false; 795 } 796 } 797 798 if (language == null) 799 { 800 if (dcv.language != null) 802 { 803 return false; 805 } 806 } 807 else if (!language.equals(Item.ANY)) 808 { 809 if (!language.equals(dcv.language)) 811 { 812 return false; 813 } 814 } 815 else if (!schema.equals(Item.ANY)) 816 { 817 if (dcv.schema != null && !dcv.schema.equals(schema)) 818 { 819 return false; 821 } 822 } 823 824 return true; 826 } 827 828 833 public EPerson getSubmitter() throws SQLException 834 { 835 if (submitter == null && !itemRow.isColumnNull("submitter_id")) 836 { 837 submitter = EPerson.find(ourContext, itemRow 838 .getIntColumn("submitter_id")); 839 } 840 return submitter; 841 } 842 843 852 public void setSubmitter(EPerson sub) 853 { 854 submitter = sub; 855 856 if (submitter != null) 857 { 858 itemRow.setColumn("submitter_id", submitter.getID()); 859 } 860 else 861 { 862 itemRow.setColumnNull("submitter_id"); 863 } 864 } 865 866 872 public Collection[] getCollections() throws SQLException 873 { 874 List collections = new ArrayList (); 875 876 TableRowIterator tri = DatabaseManager.queryTable(ourContext,"collection", 878 "SELECT collection.* FROM collection, collection2item WHERE " + 879 "collection2item.collection_id=collection.collection_id AND " + 880 "collection2item.item_id= ? ", 881 itemRow.getIntColumn("item_id")); 882 883 while (tri.hasNext()) 884 { 885 TableRow row = tri.next(); 886 887 Collection fromCache = (Collection) ourContext.fromCache( 889 Collection.class, row.getIntColumn("collection_id")); 890 891 if (fromCache != null) 892 { 893 collections.add(fromCache); 894 } 895 else 896 { 897 collections.add(new Collection(ourContext, row)); 898 } 899 } 900 tri.close(); 902 903 Collection[] collectionArray = new Collection[collections.size()]; 904 collectionArray = (Collection[]) collections.toArray(collectionArray); 905 906 return collectionArray; 907 } 908 909 917 public Community[] getCommunities() throws SQLException 918 { 919 List communities = new ArrayList (); 920 921 TableRowIterator tri = DatabaseManager.queryTable(ourContext,"community", 923 "SELECT community.* FROM community, community2item " + 924 "WHERE community2item.community_id=community.community_id " + 925 "AND community2item.item_id= ? ", 926 itemRow.getIntColumn("item_id")); 927 928 while (tri.hasNext()) 929 { 930 TableRow row = tri.next(); 931 932 Community owner = (Community) ourContext.fromCache(Community.class, 934 row.getIntColumn("community_id")); 935 936 if (owner == null) 937 { 938 owner = new Community(ourContext, row); 939 } 940 941 communities.add(owner); 942 943 Community[] parents = owner.getAllParents(); 945 946 for (int i = 0; i < parents.length; i++) 947 { 948 communities.add(parents[i]); 949 } 950 } 951 tri.close(); 953 954 Community[] communityArray = new Community[communities.size()]; 955 communityArray = (Community[]) communities.toArray(communityArray); 956 957 return communityArray; 958 } 959 960 965 public Bundle[] getBundles() throws SQLException 966 { 967 if (bundles == null) 968 { 969 bundles = new ArrayList (); 970 TableRowIterator tri = DatabaseManager.queryTable(ourContext, "bundle", 972 "SELECT bundle.* FROM bundle, item2bundle WHERE " + 973 "item2bundle.bundle_id=bundle.bundle_id AND " + 974 "item2bundle.item_id= ? ", 975 itemRow.getIntColumn("item_id")); 976 977 while (tri.hasNext()) 978 { 979 TableRow r = tri.next(); 980 981 Bundle fromCache = (Bundle) ourContext.fromCache(Bundle.class, 983 r.getIntColumn("bundle_id")); 984 985 if (fromCache != null) 986 { 987 bundles.add(fromCache); 988 } 989 else 990 { 991 bundles.add(new Bundle(ourContext, r)); 992 } 993 } 994 tri.close(); 996 } 997 998 Bundle[] bundleArray = new Bundle[bundles.size()]; 999 bundleArray = (Bundle[]) bundles.toArray(bundleArray); 1000 1001 return bundleArray; 1002 } 1003 1004 1012 public Bundle[] getBundles(String name) throws SQLException 1013 { 1014 List matchingBundles = new ArrayList (); 1015 1016 Bundle[] bunds = getBundles(); 1018 for (int i = 0; i < bunds.length; i++ ) 1019 { 1020 if (name.equals(bunds[i].getName())) 1021 { 1022 matchingBundles.add(bunds[i]); 1023 } 1024 } 1025 1026 Bundle[] bundleArray = new Bundle[matchingBundles.size()]; 1027 bundleArray = (Bundle[]) matchingBundles.toArray(bundleArray); 1028 1029 return bundleArray; 1030 } 1031 1032 1041 public Bundle createBundle(String name) throws SQLException , 1042 AuthorizeException 1043 { 1044 if ((name == null) || "".equals(name)) 1045 { 1046 throw new SQLException ("Bundle must be created with non-null name"); 1047 } 1048 1049 AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD); 1051 1052 Bundle b = Bundle.create(ourContext); 1053 b.setName(name); 1054 b.update(); 1055 1056 addBundle(b); 1057 1058 return b; 1059 } 1060 1061 1069 public void addBundle(Bundle b) throws SQLException , AuthorizeException 1070 { 1071 AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD); 1073 1074 log.info(LogManager.getHeader(ourContext, "add_bundle", "item_id=" 1075 + getID() + ",bundle_id=" + b.getID())); 1076 1077 Bundle[] bunds = getBundles(); 1079 for (int i = 0; i < bunds.length; i++) 1080 { 1081 if (b.getID() == bunds[i].getID()) 1082 { 1083 return; 1085 } 1086 } 1087 1088 AuthorizeManager.inheritPolicies(ourContext, this, b); 1091 1092 bundles.add(b); 1094 1095 TableRow mappingRow = DatabaseManager.create(ourContext, "item2bundle"); 1097 mappingRow.setColumn("item_id", getID()); 1098 mappingRow.setColumn("bundle_id", b.getID()); 1099 DatabaseManager.update(ourContext, mappingRow); 1100 } 1101 1102 1112 public void removeBundle(Bundle b) throws SQLException , AuthorizeException, 1113 IOException 1114 { 1115 AuthorizeManager.authorizeAction(ourContext, this, Constants.REMOVE); 1117 1118 log.info(LogManager.getHeader(ourContext, "remove_bundle", "item_id=" 1119 + getID() + ",bundle_id=" + b.getID())); 1120 1121 Bundle[] bunds = getBundles(); 1123 1124 for (int i = 0; i < bunds.length; i++) 1125 { 1126 if (b.getID() == bunds[i].getID()) 1127 { 1128 bundles.remove(bunds[i]); 1130 break; 1131 } 1132 } 1133 1134 DatabaseManager.updateQuery(ourContext, 1136 "DELETE FROM item2bundle WHERE item_id= ? " + 1137 "AND bundle_id= ? ", 1138 getID(), b.getID()); 1139 1140 TableRowIterator tri = DatabaseManager.query(ourContext, 1142 "SELECT * FROM item2bundle WHERE bundle_id= ? ", 1143 b.getID()); 1144 1145 if (!tri.hasNext()) 1146 { 1147 AuthorizeManager.addPolicy(ourContext, b, Constants.DELETE, 1155 ourContext.getCurrentUser()); 1156 AuthorizeManager.addPolicy(ourContext, b, Constants.REMOVE, 1157 ourContext.getCurrentUser()); 1158 1159 b.delete(); 1161 } 1162 tri.close(); 1164 } 1165 1166 1179 public Bitstream createSingleBitstream(InputStream is, String name) 1180 throws AuthorizeException, IOException , SQLException 1181 { 1182 Bundle bnd = createBundle(name); 1185 Bitstream bitstream = bnd.createBitstream(is); 1186 addBundle(bnd); 1187 1188 return bitstream; 1190 } 1191 1192 1202 public Bitstream createSingleBitstream(InputStream is) 1203 throws AuthorizeException, IOException , SQLException 1204 { 1205 return createSingleBitstream(is, "ORIGINAL"); 1206 } 1207 1208 1215 public Bitstream[] getNonInternalBitstreams() throws SQLException 1216 { 1217 List bitstreamList = new ArrayList (); 1218 1219 Bundle[] bunds = getBundles(); 1222 1223 for (int i = 0; i < bunds.length; i++) 1224 { 1225 Bitstream[] bitstreams = bunds[i].getBitstreams(); 1226 1227 for (int j = 0; j < bitstreams.length; j++) 1228 { 1229 if (!bitstreams[j].getFormat().isInternal()) 1230 { 1231 bitstreamList.add(bitstreams[j]); 1233 } 1234 } 1235 } 1236 1237 Bitstream[] bsArray = new Bitstream[bitstreamList.size()]; 1238 bsArray = (Bitstream[]) bitstreamList.toArray(bsArray); 1239 1240 return bsArray; 1241 } 1242 1243 1254 public void licenseGranted(String license, EPerson eperson) 1255 throws SQLException , IOException , AuthorizeException 1256 { 1257 String licenseText = "License granted by " + eperson.getFullName() 1259 + " (" + eperson.getEmail() + ") on " 1260 + DCDate.getCurrent().toString() + " (GMT):\n\n" + license; 1261 1262 byte[] licenseBytes = licenseText.getBytes(); 1264 ByteArrayInputStream bais = new ByteArrayInputStream (licenseBytes); 1265 Bitstream b = createSingleBitstream(bais, "LICENSE"); 1266 1267 b.setName("license.txt"); 1269 b.setSource("Written by org.dspace.content.Item"); 1270 1271 BitstreamFormat bf = BitstreamFormat.findByShortDescription(ourContext, 1273 "License"); 1274 b.setFormat(bf); 1275 1276 b.update(); 1277 } 1278 1279 1286 public void removeLicenses() throws SQLException , AuthorizeException, 1287 IOException 1288 { 1289 BitstreamFormat bf = BitstreamFormat.findByShortDescription(ourContext, 1291 "License"); 1292 int licensetype = bf.getID(); 1293 1294 Bundle[] bunds = getBundles(); 1296 1297 for (int i = 0; i < bunds.length; i++) 1298 { 1299 boolean removethisbundle = false; 1300 1301 Bitstream[] bits = bunds[i].getBitstreams(); 1302 1303 for (int j = 0; j < bits.length; j++) 1304 { 1305 BitstreamFormat bft = bits[j].getFormat(); 1306 1307 if (bft.getID() == licensetype) 1308 { 1309 removethisbundle = true; 1310 } 1311 } 1312 1313 if (removethisbundle) 1316 { 1317 removeBundle(bunds[i]); 1318 } 1319 } 1320 } 1321 1322 1329 public void update() throws SQLException , AuthorizeException 1330 { 1331 if (!canEdit()) 1334 { 1335 AuthorizeManager.authorizeAction(ourContext, this, Constants.WRITE); 1336 } 1337 1338 HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY, 1339 ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); 1340 1341 log.info(LogManager.getHeader(ourContext, "update_item", "item_id=" 1342 + getID())); 1343 1344 itemRow.setColumn("last_modified", new Date ()); 1346 1347 int sequence = 0; 1349 Bundle[] bunds = getBundles(); 1350 1351 for (int i = 0; i < bunds.length; i++) 1353 { 1354 Bitstream[] streams = bunds[i].getBitstreams(); 1355 1356 for (int k = 0; k < streams.length; k++) 1357 { 1358 if (streams[k].getSequenceID() > sequence) 1359 { 1360 sequence = streams[k].getSequenceID(); 1361 } 1362 } 1363 } 1364 1365 sequence++; 1367 1368 for (int i = 0; i < bunds.length; i++) 1369 { 1370 Bitstream[] streams = bunds[i].getBitstreams(); 1371 1372 for (int k = 0; k < streams.length; k++) 1373 { 1374 if (streams[k].getSequenceID() < 0) 1375 { 1376 streams[k].setSequenceID(sequence); 1377 sequence++; 1378 streams[k].update(); 1379 } 1380 } 1381 } 1382 1383 if (itemRow.isColumnNull("in_archive")) 1385 { 1386 itemRow.setColumn("in_archive", false); 1387 } 1388 1389 if (itemRow.isColumnNull("withdrawn")) 1390 { 1391 itemRow.setColumn("withdrawn", false); 1392 } 1393 1394 Map elementCount = new HashMap (); 1399 1400 DatabaseManager.update(ourContext, itemRow); 1401 1402 if (dublinCoreChanged) 1404 { 1405 removeMetadataFromDatabase(); 1407 1408 Iterator i = dublinCore.iterator(); 1410 1411 while (i.hasNext()) 1412 { 1413 DCValue dcv = (DCValue) i.next(); 1414 1415 int schemaID; 1417 MetadataSchema schema = MetadataSchema.find(ourContext,dcv.schema); 1418 if (schema == null) { 1419 schemaID = MetadataSchema.DC_SCHEMA_ID; 1420 } else { 1421 schemaID = schema.getSchemaID(); 1422 } 1423 1424 MetadataField field = MetadataField.findByElement(ourContext, 1425 schemaID, dcv.element, dcv.qualifier); 1426 1427 if (field == null) 1428 { 1429 log.warn(LogManager 1431 .getHeader(ourContext, "bad_dc", 1432 "Bad DC field. SchemaID="+String.valueOf(schemaID) 1433 + ", element: \"" 1434 + ((dcv.element == null) ? "null" 1435 : dcv.element) 1436 + "\" qualifier: \"" 1437 + ((dcv.qualifier == null) ? "null" 1438 : dcv.qualifier) 1439 + "\" value: \"" 1440 + ((dcv.value == null) ? "null" 1441 : dcv.value) + "\"")); 1442 1443 throw new SQLException ("bad_dublin_core " 1444 + "SchemaID="+String.valueOf(schemaID)+", " 1445 + dcv.element 1446 + " " + dcv.qualifier); 1447 } 1448 1449 int current = 0; 1451 1452 String key = dcv.element 1454 + ((dcv.qualifier == null) ? "" : ("." + dcv.qualifier)); 1455 1456 Integer currentInteger = (Integer ) elementCount.get(key); 1457 1458 if (currentInteger != null) 1459 { 1460 current = currentInteger.intValue(); 1461 } 1462 1463 current++; 1464 elementCount.put(key, new Integer (current)); 1465 1466 MetadataValue metadata = new MetadataValue(); 1468 metadata.setItemId(getID()); 1469 metadata.setFieldId(field.getFieldID()); 1470 metadata.setValue(dcv.value); 1471 metadata.setLanguage(dcv.language); 1472 metadata.setPlace(current); 1473 metadata.create(ourContext); 1474 } 1475 1476 dublinCoreChanged = false; 1477 } 1478 1479 Browse.itemChanged(ourContext, this); 1481 } 1482 1483 1491 public void withdraw() throws SQLException , AuthorizeException, IOException 1492 { 1493 String timestamp = DCDate.getCurrent().toString(); 1494 1495 String collectionProv = ""; 1497 Collection[] colls = getCollections(); 1498 1499 for (int i = 0; i < colls.length; i++) 1500 { 1501 collectionProv = collectionProv + colls[i].getMetadata("name") 1502 + " (ID: " + colls[i].getID() + ")\n"; 1503 } 1504 1505 if (AuthorizeManager.authorizeActionBoolean(ourContext, 1508 getOwningCollection(), Constants.COLLECTION_ADMIN) 1509 || AuthorizeManager.authorizeActionBoolean(ourContext, 1510 getOwningCollection(), Constants.REMOVE)) 1511 { 1512 } 1514 else 1515 { 1516 throw new AuthorizeException( 1517 "To withdraw item must be COLLECTION_ADMIN or have REMOVE authorization on owning Collection"); 1518 } 1519 1520 itemRow.setColumn("withdrawn", true); 1522 1523 itemRow.setColumn("in_archive", false); 1525 1526 EPerson e = ourContext.getCurrentUser(); 1529 String prov = "Item withdrawn by " + e.getFullName() + " (" 1530 + e.getEmail() + ") on " + timestamp + "\n" 1531 + "Item was in collections:\n" + collectionProv 1532 + InstallItem.getBitstreamProvenanceMessage(this); 1533 1534 addDC("description", "provenance", "en", prov); 1535 1536 update(); 1538 1539 HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY, e, 1541 ourContext.getExtraLogInfo()); 1542 1543 Browse.itemRemoved(ourContext, getID()); 1545 DSIndexer.unIndexContent(ourContext, this); 1546 1547 AuthorizeManager.removeAllPolicies(ourContext, this); 1550 1551 log.info(LogManager.getHeader(ourContext, "withdraw_item", "user=" 1553 + e.getEmail() + ",item_id=" + getID())); 1554 } 1555 1556 1563 public void reinstate() throws SQLException , AuthorizeException, 1564 IOException 1565 { 1566 String timestamp = DCDate.getCurrent().toString(); 1567 1568 String collectionProv = ""; 1571 Collection[] colls = getCollections(); 1572 1573 for (int i = 0; i < colls.length; i++) 1574 { 1575 collectionProv = collectionProv + colls[i].getMetadata("name") 1576 + " (ID: " + colls[i].getID() + ")\n"; 1577 AuthorizeManager.authorizeAction(ourContext, colls[i], 1578 Constants.ADD); 1579 } 1580 1581 itemRow.setColumn("withdrawn", false); 1583 1584 itemRow.setColumn("in_archive", true); 1586 1587 EPerson e = ourContext.getCurrentUser(); 1590 String prov = "Item reinstated by " + e.getFullName() + " (" 1591 + e.getEmail() + ") on " + timestamp + "\n" 1592 + "Item was in collections:\n" + collectionProv 1593 + InstallItem.getBitstreamProvenanceMessage(this); 1594 1595 addDC("description", "provenance", "en", prov); 1596 1597 update(); 1599 1600 HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY, e, 1602 ourContext.getExtraLogInfo()); 1603 1604 DSIndexer.indexContent(ourContext, this); 1608 1609 if (colls.length > 0) 1611 { 1612 inheritCollectionDefaultPolicies(colls[0]); 1617 } 1618 1619 log.info(LogManager.getHeader(ourContext, "reinstate_item", "user=" 1621 + e.getEmail() + ",item_id=" + getID())); 1622 } 1623 1624 1633 void delete() throws SQLException , AuthorizeException, IOException 1634 { 1635 HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE, 1636 ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); 1637 1638 log.info(LogManager.getHeader(ourContext, "delete_item", "item_id=" 1639 + getID())); 1640 1641 ourContext.removeCached(this, getID()); 1643 1644 if (isArchived()) 1646 { 1647 Browse.itemRemoved(ourContext, getID()); 1649 DSIndexer.unIndexContent(ourContext, this); 1650 } 1651 1652 removeMetadataFromDatabase(); 1654 1655 Bundle[] bunds = getBundles(); 1657 1658 for (int i = 0; i < bunds.length; i++) 1659 { 1660 removeBundle(bunds[i]); 1661 } 1662 1663 AuthorizeManager.removeAllPolicies(ourContext, this); 1665 1666 DatabaseManager.updateQuery(ourContext, 1671 "DELETE FROM handle WHERE resource_type_id= ? " + 1672 "AND resource_id= ? ", 1673 Constants.ITEM,getID()); 1674 1675 DatabaseManager.delete(ourContext, itemRow); 1677 } 1678 1679 1684 public void decache() throws SQLException 1685 { 1686 ourContext.removeCached(this, getID()); 1688 if (submitter != null) 1689 { 1690 ourContext.removeCached(submitter, submitter.getID()); 1691 } 1692 if (bundles != null) 1694 { 1695 Bundle[] bunds = getBundles(); 1696 for (int i = 0; i < bunds.length; i++) 1697 { 1698 ourContext.removeCached(bunds[i], bunds[i].getID()); 1699 Bitstream[] bitstreams = bunds[i].getBitstreams(); 1700 for (int j = 0; j < bitstreams.length; j++) 1701 { 1702 ourContext.removeCached(bitstreams[j], bitstreams[j].getID()); 1703 } 1704 } 1705 } 1706 } 1707 1708 1717 public boolean equals(DSpaceObject other) 1718 { 1719 if (this.getType() == other.getType()) 1720 { 1721 if (this.getID() == other.getID()) 1722 { 1723 return true; 1724 } 1725 } 1726 1727 return false; 1728 } 1729 1730 1737 public boolean isOwningCollection(Collection c) 1738 { 1739 int owner_id = itemRow.getIntColumn("owning_collection"); 1740 1741 if (c.getID() == owner_id) 1742 { 1743 return true; 1744 } 1745 1746 return false; 1748 } 1749 1750 1756 private void removeMetadataFromDatabase() throws SQLException 1757 { 1758 DatabaseManager.updateQuery(ourContext, 1759 "DELETE FROM MetadataValue WHERE item_id= ? ", 1760 getID()); 1761 } 1762 1763 1768 public int getType() 1769 { 1770 return Constants.ITEM; 1771 } 1772 1773 1783 public void replaceAllItemPolicies(List newpolicies) throws SQLException , 1784 AuthorizeException 1785 { 1786 AuthorizeManager.removeAllPolicies(ourContext, this); 1788 AuthorizeManager.addPolicies(ourContext, newpolicies, this); 1789 } 1790 1791 1801 public void replaceAllBitstreamPolicies(List newpolicies) 1802 throws SQLException , AuthorizeException 1803 { 1804 Bundle[] bunds = getBundles(); 1807 1808 for (int i = 0; i < bunds.length; i++) 1809 { 1810 Bundle mybundle = bunds[i]; 1811 1812 Bitstream[] bs = mybundle.getBitstreams(); 1813 1814 for (int j = 0; j < bs.length; j++) 1815 { 1816 Bitstream mybitstream = bs[j]; 1817 1818 AuthorizeManager.removeAllPolicies(ourContext, bs[j]); 1820 AuthorizeManager.addPolicies(ourContext, newpolicies, bs[j]); 1821 } 1822 1823 AuthorizeManager.removeAllPolicies(ourContext, mybundle); 1825 AuthorizeManager.addPolicies(ourContext, newpolicies, mybundle); 1826 } 1827 } 1828 1829 1837 public void removeGroupPolicies(Group g) throws SQLException 1838 { 1839 AuthorizeManager.removeGroupPolicies(ourContext, this, g); 1841 1842 Bundle[] bunds = getBundles(); 1844 1845 for (int i = 0; i < bunds.length; i++) 1846 { 1847 Bundle mybundle = bunds[i]; 1848 1849 Bitstream[] bs = mybundle.getBitstreams(); 1850 1851 for (int j = 0; j < bs.length; j++) 1852 { 1853 Bitstream mybitstream = bs[j]; 1854 1855 AuthorizeManager.removeGroupPolicies(ourContext, bs[j], g); 1857 } 1858 1859 AuthorizeManager.removeGroupPolicies(ourContext, mybundle, g); 1861 } 1862 } 1863 1864 1876 public void inheritCollectionDefaultPolicies(Collection c) 1877 throws java.sql.SQLException , AuthorizeException 1878 { 1879 List policies = AuthorizeManager.getPoliciesActionFilter(ourContext, c, 1882 Constants.DEFAULT_ITEM_READ); 1883 1884 Iterator i = policies.iterator(); 1887 1888 if (!i.hasNext()) 1890 { 1891 throw new java.sql.SQLException ("Collection " + c.getID() 1892 + " has no default item READ policies"); 1893 } 1894 1895 while (i.hasNext()) 1896 { 1897 ResourcePolicy rp = (ResourcePolicy) i.next(); 1898 rp.setAction(Constants.READ); 1899 } 1900 1901 replaceAllItemPolicies(policies); 1902 1903 policies = AuthorizeManager.getPoliciesActionFilter(ourContext, c, 1904 Constants.DEFAULT_BITSTREAM_READ); 1905 1906 i = policies.iterator(); 1909 1910 if (!i.hasNext()) 1911 { 1912 throw new java.sql.SQLException ("Collection " + c.getID() 1913 + " has no default bitstream READ policies"); 1914 } 1915 1916 while (i.hasNext()) 1917 { 1918 ResourcePolicy rp = (ResourcePolicy) i.next(); 1919 rp.setAction(Constants.READ); 1920 } 1921 1922 replaceAllBitstreamPolicies(policies); 1923 } 1924 1925 1931 public boolean canEdit() throws java.sql.SQLException 1932 { 1933 if (AuthorizeManager.authorizeActionBoolean(ourContext, this, 1935 Constants.WRITE)) 1936 { 1937 return true; 1938 } 1939 1940 if (getOwningCollection() == null) 1942 { 1943 return true; 1944 } 1945 1946 if (getOwningCollection().canEditBoolean()) 1948 { 1949 return true; 1950 } 1951 1952 if (AuthorizeManager.authorizeActionBoolean(ourContext, 1954 getOwningCollection(), Constants.COLLECTION_ADMIN)) 1955 { 1956 return true; 1957 } 1958 1959 return false; 1960 } 1961} 1962 | Popular Tags |