From 097820a00039118ed3b8138e89c21ff94382ea19 Mon Sep 17 00:00:00 2001 From: Philip Vissenaekens Date: Tue, 17 Dec 2019 17:15:07 +0100 Subject: [PATCH] 64558: DSPACE - 270 - Changes to CLI exports --- .../org/dspace/app/itemexport/ItemExport.java | 73 +++++++++++++++++-- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/dspace/modules/additions/src/main/java/org/dspace/app/itemexport/ItemExport.java b/dspace/modules/additions/src/main/java/org/dspace/app/itemexport/ItemExport.java index 6b2a0428..6a4cda0f 100644 --- a/dspace/modules/additions/src/main/java/org/dspace/app/itemexport/ItemExport.java +++ b/dspace/modules/additions/src/main/java/org/dspace/app/itemexport/ItemExport.java @@ -20,6 +20,7 @@ import org.dspace.handle.HandleManager; import org.dspace.storage.rdbms.DatabaseManager; import org.dspace.storage.rdbms.TableRowIterator; +import org.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.scanner.Constant; import javax.mail.MessagingException; import java.io.*; @@ -127,7 +128,7 @@ private void mainImpl(String[] argv) throws Exception{ logAndPrintMessage("Error, item cannot be found: " + myIDString, Level.ERROR); } } - else + else if(myType == Constants.COLLECTION) { if (myIDString.indexOf('/') != -1) { @@ -210,7 +211,7 @@ private void validateArguments() { // now validate the args if (myType == -1) { - System.out.println("type must be either COLLECTION or ITEM (-h for help)"); + System.out.println("type must be either COLLECTION, ITEM or SITE (-h for help)"); System.exit(1); } @@ -226,7 +227,7 @@ private void validateArguments() { System.exit(1); } - if (myIDString == null) + if (myIDString == null && myType != Constants.SITE) { System.out.println("ID must be set to either a database ID or a handle (-h for help)"); System.exit(1); @@ -256,6 +257,9 @@ else if ("COLLECTION".equals(typeString)) { myType = Constants.COLLECTION; } + else if("SITE".equals(typeString)){ + myType = Constants.SITE; + } } if (line.hasOption('i')) // id @@ -293,7 +297,7 @@ else if ("COLLECTION".equals(typeString)) private Options createOptions() { Options options = new Options(); - options.addOption("t", "type", true, "type: COLLECTION or ITEM"); + options.addOption("t", "type", true, "type: COLLECTION, ITEM or SITE"); options.addOption("i", "id", true, "ID or handle of thing to export"); options.addOption("d", "dest", true, "destination where you want items to go"); options.addOption("m", "migrate", false, "export for migration (remove handle and metadata that will be re-created in new system)"); @@ -316,11 +320,15 @@ private void exportItems(Context c) throws Exception { myItems.add(myItem.getID()); items = new ItemIterator(c, myItems); } - else + else if(mycollection != null) { logAndPrintMessage("Exporting from collection: " + myIDString,Level.INFO); items = getItemsIterator(mycollection, dateFile, c); } + else { + logAndPrintMessage("Exporting all items",Level.INFO); + items = getAllItemsIterator(dateFile, c); + } exportAsZip(c, items, destDirName, zipFileName, seqStart, migrate, handleBasedDirectoryStructure); } else @@ -330,7 +338,7 @@ private void exportItems(Context c) throws Exception { // it's only a single item exportItem(c, myItem, destDirName, seqStart, migrate, handleBasedDirectoryStructure); } - else + else if (mycollection != null) { logAndPrintMessage("Exporting from collection: " + myIDString, Level.INFO); @@ -348,6 +356,21 @@ private void exportItems(Context c) throws Exception { } } } + else { + logAndPrintMessage("Exporting all items", Level.INFO); + + // it's a collection, so do a bunch of items + ItemIterator i = getAllItemsIterator(dateFile, c); + + try + { + exportItem(c, i, destDirName, seqStart, migrate, handleBasedDirectoryStructure); + } + finally + { + i.close(); + } + } } } @@ -380,6 +403,44 @@ private static ItemIterator getItemsIterator(Collection mycollection, Date lastD return new ItemIterator(context, rows); } + private static ItemIterator getAllItemsIterator(DateFile dateFile, Context context) + throws SQLException { + ItemIterator items; + Date lastDate = dateFile.getLastDate(); + if (lastDate != DateFile.NO_DATE) { + items = getAllItemsIterator(lastDate, context); + } else { + items = getAllItemsIterator(context); + } + return items; + } + + private static ItemIterator getAllItemsIterator(Date lastDate, Context context) { + String myQuery = "SELECT item.* FROM item WHERE item.in_archive='1' AND item.last_modified > ? "; + + TableRowIterator rows; + try { + rows = DatabaseManager.queryTable(context, "item", myQuery, new Timestamp(lastDate.getTime())); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return new ItemIterator(context, rows); + } + + private static ItemIterator getAllItemsIterator(Context context) { + String myQuery = "SELECT item.* FROM item WHERE item.in_archive='1' "; + + TableRowIterator rows; + try { + rows = DatabaseManager.queryTable(context, "item", myQuery); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return new ItemIterator(context, rows); + } + private static void exportItem(Context c, ItemIterator i, String destDirName, int seqStart, boolean migrate, boolean handleBasedDirectoryStructure) throws Exception { int mySequenceNumber = seqStart;