Skip to content

Commit

Permalink
Merged in DSC-1247 (pull request DSpace#1118)
Browse files Browse the repository at this point in the history
[DSC-1247] Allow parameter for executing user to CLI processes (bulk-import)

Approved-by: Stefano Maffei
  • Loading branch information
eskander17 authored and steph-ieffam committed Oct 25, 2023
2 parents e529855 + 0ed1f52 commit 1fcd95e
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,17 @@ public void setup() throws ParseException {
collectionId = commandLine.getOptionValue('c');
filename = commandLine.getOptionValue('f');

if (commandLine.hasOption('e')) {
if (commandLine.hasOption("er")) {
abortOnError = true;
}
}

@Override
public void internalRun() throws Exception {
context = new Context(Context.Mode.BATCH_EDIT);
assignCurrentUserInContext();
assignCurrentUserInContext(context);
assignSpecialGroupsInContext();

context.turnOffAuthorisationSystem();

InputStream inputStream = handler.getFileStream(context, filename)
.orElseThrow(() -> new IllegalArgumentException("Error reading file, the file couldn't be "
+ "found for filename: " + filename));
Expand All @@ -285,6 +283,7 @@ public void internalRun() throws Exception {
}

try {
context.turnOffAuthorisationSystem();
performImport(inputStream);
context.complete();
context.restoreAuthSystemState();
Expand Down Expand Up @@ -1604,7 +1603,7 @@ private void rollback() {
}
}

private void assignCurrentUserInContext() throws SQLException {
protected void assignCurrentUserInContext(Context context) throws SQLException, ParseException {
UUID uuid = getEpersonIdentifier();
if (uuid != null) {
EPerson ePerson = EPersonServiceFactory.getInstance().getEPersonService().find(context, uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
*/
package org.dspace.app.bulkedit;

import java.sql.SQLException;

import org.apache.commons.cli.ParseException;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.factory.EPersonServiceFactory;

/**
* Extension of {@link BulkImport} for CLI.
*
Expand All @@ -15,4 +22,24 @@
*/
public class BulkImportCli extends BulkImport {

@Override
protected void assignCurrentUserInContext(Context context) throws ParseException {
if (commandLine.hasOption('e')) {
String ePersonEmail = commandLine.getOptionValue('e');
try {
EPerson ePerson =
EPersonServiceFactory.getInstance().getEPersonService().findByEmail(context, ePersonEmail);
if (ePerson == null) {
super.handler.logError("EPerson not found: " + ePersonEmail);
throw new IllegalArgumentException("Unable to find a user with email: " + ePersonEmail);
}
context.setCurrentUser(ePerson);
} catch (SQLException e) {
throw new IllegalArgumentException("SQLException trying to find user with email: " + ePersonEmail);
}
} else {
throw new ParseException("Required parameter -e missing!");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package org.dspace.app.bulkedit;

import org.apache.commons.cli.Options;

/**
* Extension of {@link BulkImportScriptConfiguration} for CLI.
*
Expand All @@ -15,5 +17,13 @@
*/
public class BulkImportCliScriptConfiguration<T extends BulkImportCli> extends BulkImportScriptConfiguration<T> {

@Override
public Options getOptions() {
Options options = super.getOptions();
options.addOption("e", "email", true, "email address of user");
options.getOption("e").setRequired(true);
super.options = options;
return options;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public Options getOptions() {
options.getOption("f").setType(InputStream.class);
options.getOption("f").setRequired(true);

options.addOption("e", "concludeOnError", false, "conclude the import at the first error");
options.getOption("e").setType(boolean.class);
options.getOption("e").setRequired(false);
options.addOption("er", "concludeOnError", false, "conclude the import at the first error");
options.getOption("er").setType(boolean.class);
options.getOption("er").setRequired(false);

super.options = options;
}
Expand Down
Loading

0 comments on commit 1fcd95e

Please sign in to comment.