Skip to content

Commit

Permalink
add more options to specify an item
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarko committed Nov 26, 2024
1 parent 1b2bb8e commit fd51faf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
53 changes: 44 additions & 9 deletions dspace-api/src/main/java/org/dspace/administer/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@
import org.dspace.content.Bitstream;
import org.dspace.content.BitstreamFormat;
import org.dspace.content.Bundle;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamFormatService;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.WorkspaceItemService;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.EPersonService;
import org.dspace.identifier.IdentifierNotFoundException;
import org.dspace.identifier.IdentifierNotResolvableException;
import org.dspace.identifier.factory.IdentifierServiceFactory;
import org.dspace.identifier.service.IdentifierService;
import org.dspace.scripts.DSpaceRunnable;
import org.dspace.scripts.configuration.ScriptConfiguration;
import org.dspace.utils.DSpace;
Expand All @@ -45,11 +51,15 @@ public class FileDownloader extends DSpaceRunnable<FileDownloaderConfiguration>
private static final Logger log = LoggerFactory.getLogger(FileDownloader.class);
private boolean help = false;
private UUID itemUUID;
private int workspaceID;
private String pid;
private URI uri;
private String epersonMail;
private String bitstreamName;
private EPersonService epersonService;
private ItemService itemService;
private WorkspaceItemService workspaceItemService;
private IdentifierService identifierService;
private BitstreamService bitstreamService;
private BitstreamFormatService bitstreamFormatService;
private final HttpClient httpClient = HttpClient.newBuilder()
Expand Down Expand Up @@ -85,8 +95,8 @@ public void setup() throws ParseException {
throw new ParseException("No URL option has been provided");
}

if (!commandLine.hasOption("i")) {
throw new ParseException("No item option has been provided");
if (!commandLine.hasOption("i") && !commandLine.hasOption("w") && !commandLine.hasOption("p")) {
throw new ParseException("No item id option has been provided");
}

if (getEpersonIdentifier() == null && !commandLine.hasOption("e")) {
Expand All @@ -96,15 +106,24 @@ public void setup() throws ParseException {

this.epersonService = EPersonServiceFactory.getInstance().getEPersonService();
this.itemService = ContentServiceFactory.getInstance().getItemService();
this.workspaceItemService = ContentServiceFactory.getInstance().getWorkspaceItemService();
this.bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
this.bitstreamFormatService = ContentServiceFactory.getInstance().getBitstreamFormatService();
this.identifierService = IdentifierServiceFactory.getInstance().getIdentifierService();

try {
uri = new URI(commandLine.getOptionValue("u"));
} catch (URISyntaxException e) {
throw new ParseException("The provided URL is not a valid URL");
}
itemUUID = UUID.fromString(commandLine.getOptionValue("i"));

if (commandLine.hasOption("i")) {
itemUUID = UUID.fromString(commandLine.getOptionValue("i"));
} else if (commandLine.hasOption("w")) {
workspaceID = Integer.parseInt(commandLine.getOptionValue("w"));
} else if (commandLine.hasOption("p")) {
pid = commandLine.getOptionValue("p");
}

epersonMail = commandLine.getOptionValue("e");

Expand All @@ -130,13 +149,10 @@ public void internalRun() throws Exception {
Context context = new Context();
context.setCurrentUser(getEperson(context));

// Download the file from the given url
// and save it to the item with the given UUID

//find the item by the given uuid
Item item = itemService.find(context, itemUUID);
//find the item by the given id
Item item = findItem(context);
if (item == null) {
throw new IllegalArgumentException("No item found for the given UUID");
throw new IllegalArgumentException("No item found for the given ID");
}

HttpRequest request = HttpRequest.newBuilder()
Expand Down Expand Up @@ -166,6 +182,25 @@ public void internalRun() throws Exception {
context.commit();
}

private Item findItem(Context context) throws SQLException {
if (itemUUID != null) {
return itemService.find(context, itemUUID);
} else if (workspaceID != 0) {
return workspaceItemService.find(context, workspaceID).getItem();
} else {
try {
DSpaceObject dso = identifierService.resolve(context, pid);
if (dso instanceof Item) {
return (Item) dso;
} else {
throw new IllegalArgumentException("The provided identifier does not resolve to an item");
}
} catch (IdentifierNotFoundException | IdentifierNotResolvableException e) {
throw new IllegalArgumentException(e);
}
}
}

private void saveFileToItem(Context context, Item item, InputStream is, String name)
throws SQLException, AuthorizeException, IOException {
log.debug("Saving file to item {}", item.getID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.dspace.administer;

import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.dspace.scripts.configuration.ScriptConfiguration;

Expand Down Expand Up @@ -44,14 +45,20 @@ public Options getOptions() {
if (options == null) {

Options options = new Options();
OptionGroup ids = new OptionGroup();

options.addOption("h", "help", false, "help");

options.addOption("u", "url", true, "source url");
options.getOption("u").setRequired(true);

options.addOption("i", "item", true, "item uuid");
options.getOption("i").setRequired(true);
options.addOption("i", "uuid", true, "item uuid");
options.addOption("w", "wsid", true, "workspace id");
options.addOption("p", "pid", true, "item pid (e.g. handle or doi)");
ids.addOption(options.getOption("i"));
ids.addOption(options.getOption("w"));
ids.addOption(options.getOption("p"));
ids.setRequired(true);

options.addOption("e", "eperson", true, "eperson email");
options.getOption("e").setRequired(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testDownloadFileError() throws Exception {
assertEquals(oldBitCount, newBitCount);
return;
}
assertEquals(0, 1);
assertEquals("Not expecting to get here", 0, 1);
}


Expand Down

0 comments on commit fd51faf

Please sign in to comment.