Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dspace 270 Changes to CLI exports #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)");
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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();
}
}
}
}

Expand Down Expand Up @@ -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;
Expand Down