[Task 72215] removed the eperson flag from the REST metadata import script whilst leaving cli unaltered

This commit is contained in:
Raf Ponsaerts
2020-07-28 16:16:05 +02:00
parent 667a362b25
commit bbdc47fbbf
8 changed files with 146 additions and 27 deletions

View File

@@ -182,24 +182,7 @@ public class MetadataImport extends DSpaceRunnable<MetadataImportScriptConfigura
c.turnOffAuthorisationSystem(); c.turnOffAuthorisationSystem();
// Find the EPerson, assign to context // Find the EPerson, assign to context
try { assignCurrentUserInContext(c);
if (commandLine.hasOption('e')) {
EPerson eperson;
String e = commandLine.getOptionValue('e');
if (e.indexOf('@') != -1) {
eperson = EPersonServiceFactory.getInstance().getEPersonService().findByEmail(c, e);
} else {
eperson = EPersonServiceFactory.getInstance().getEPersonService().find(c, UUID.fromString(e));
}
if (eperson == null) {
throw new ParseException("Error, eperson cannot be found: " + e);
}
c.setCurrentUser(eperson);
}
} catch (Exception e) {
throw new ParseException("Unable to find DSpace user: " + e.getMessage());
}
if (authorityControlled == null) { if (authorityControlled == null) {
setAuthorizedMetadataFields(); setAuthorizedMetadataFields();
@@ -277,6 +260,18 @@ public class MetadataImport extends DSpaceRunnable<MetadataImportScriptConfigura
} }
protected void assignCurrentUserInContext(Context context) throws ParseException {
UUID uuid = getEpersonIdentifier();
if (uuid != null) {
try {
EPerson ePerson = EPersonServiceFactory.getInstance().getEPersonService().find(context, uuid);
context.setCurrentUser(ePerson);
} catch (SQLException e) {
log.error("Something went wrong trying to fetch the eperson for uuid: " + uuid, e);
}
}
}
/** /**
* This method determines whether the changes should be applied or not. This is default set to true for the REST * This method determines whether the changes should be applied or not. This is default set to true for the REST
* script as we don't want to interact with the caller. This will be overwritten in the CLI script to ask for * script as we don't want to interact with the caller. This will be overwritten in the CLI script to ask for

View File

@@ -10,7 +10,12 @@ package org.dspace.app.bulkedit;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.UUID;
import org.apache.commons.cli.ParseException;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.scripts.handler.DSpaceRunnableHandler; import org.dspace.scripts.handler.DSpaceRunnableHandler;
/** /**
@@ -30,4 +35,26 @@ public class MetadataImportCLI extends MetadataImport {
return false; return false;
} }
} }
@Override
protected void assignCurrentUserInContext(Context context) throws ParseException {
try {
if (commandLine.hasOption('e')) {
EPerson eperson;
String e = commandLine.getOptionValue('e');
if (e.indexOf('@') != -1) {
eperson = EPersonServiceFactory.getInstance().getEPersonService().findByEmail(context, e);
} else {
eperson = EPersonServiceFactory.getInstance().getEPersonService().find(context, UUID.fromString(e));
}
if (eperson == null) {
throw new ParseException("Error, eperson cannot be found: " + e);
}
context.setCurrentUser(eperson);
}
} catch (Exception e) {
throw new ParseException("Unable to find DSpace user: " + e.getMessage());
}
}
} }

View File

@@ -7,10 +7,21 @@
*/ */
package org.dspace.app.bulkedit; package org.dspace.app.bulkedit;
import org.apache.commons.cli.Options;
import org.dspace.scripts.configuration.ScriptConfiguration; import org.dspace.scripts.configuration.ScriptConfiguration;
/** /**
* The {@link ScriptConfiguration} for the {@link org.dspace.app.bulkedit.MetadataImportCLI} CLI script * The {@link ScriptConfiguration} for the {@link org.dspace.app.bulkedit.MetadataImportCLI} CLI script
*/ */
public class MetadataImportCliScriptConfiguration extends MetadataImportScriptConfiguration<MetadataImportCLI> { public class MetadataImportCliScriptConfiguration extends MetadataImportScriptConfiguration<MetadataImportCLI> {
@Override
public Options getOptions() {
Options options = super.getOptions();
options.addOption("e", "email", true, "email address or user id of user (required if adding new items)");
options.getOption("e").setType(String.class);
options.getOption("e").setRequired(true);
super.options = options;
return options;
}
} }

View File

@@ -57,9 +57,6 @@ public class MetadataImportScriptConfiguration<T extends MetadataImport> extends
options.addOption("f", "file", true, "source file"); options.addOption("f", "file", true, "source file");
options.getOption("f").setType(InputStream.class); options.getOption("f").setType(InputStream.class);
options.getOption("f").setRequired(true); options.getOption("f").setRequired(true);
options.addOption("e", "email", true, "email address or user id of user (required if adding new items)");
options.getOption("e").setType(String.class);
options.getOption("e").setRequired(true);
options.addOption("s", "silent", false, options.addOption("s", "silent", false,
"silent operation - doesn't request confirmation of changes USE WITH CAUTION"); "silent operation - doesn't request confirmation of changes USE WITH CAUTION");
options.getOption("s").setType(boolean.class); options.getOption("s").setType(boolean.class);

View File

@@ -141,7 +141,7 @@ public class EPerson extends DSpaceObject implements DSpaceObjectLegacySupport {
return false; return false;
} }
final EPerson other = (EPerson) obj; final EPerson other = (EPerson) obj;
if (this.getID() != other.getID()) { if (!this.getID().equals(other.getID())) {
return false; return false;
} }
if (!StringUtils.equals(this.getEmail(), other.getEmail())) { if (!StringUtils.equals(this.getEmail(), other.getEmail())) {

View File

@@ -7,10 +7,12 @@
*/ */
package org.dspace.app.bulkedit; package org.dspace.app.bulkedit;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
import java.io.File; import java.io.File;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dspace.AbstractIntegrationTest; import org.dspace.AbstractIntegrationTest;
import org.dspace.app.launcher.ScriptLauncher; import org.dspace.app.launcher.ScriptLauncher;
@@ -22,9 +24,15 @@ import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService; import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService; import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService; import org.dspace.content.service.ItemService;
import org.dspace.scripts.DSpaceRunnable;
import org.dspace.scripts.configuration.ScriptConfiguration;
import org.dspace.scripts.factory.ScriptServiceFactory;
import org.dspace.scripts.service.ScriptService;
import org.dspace.services.ConfigurationService; import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory; import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
public class MetadataImportTest extends AbstractIntegrationTest { public class MetadataImportTest extends AbstractIntegrationTest {
@@ -33,6 +41,9 @@ public class MetadataImportTest extends AbstractIntegrationTest {
private CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService(); private CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
private ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); private ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test @Test
public void metadataImportTest() throws Exception { public void metadataImportTest() throws Exception {
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
@@ -50,6 +61,7 @@ public class MetadataImportTest extends AbstractIntegrationTest {
StringUtils.equals( StringUtils.equals(
itemService.getMetadata(importedItem, "dc", "contributor", "author", Item.ANY).get(0).getValue(), itemService.getMetadata(importedItem, "dc", "contributor", "author", Item.ANY).get(0).getValue(),
"Donald, SmithImported")); "Donald, SmithImported"));
assertEquals(importedItem.getSubmitter(), eperson);
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
itemService.delete(context, itemService.find(context, importedItem.getID())); itemService.delete(context, itemService.find(context, importedItem.getID()));
@@ -57,4 +69,24 @@ public class MetadataImportTest extends AbstractIntegrationTest {
communityService.delete(context, communityService.find(context, community.getID())); communityService.delete(context, communityService.find(context, community.getID()));
context.restoreAuthSystemState(); context.restoreAuthSystemState();
} }
@Test(expected = ParseException.class)
public void metadataImportWithoutEPersonParameterTest()
throws IllegalAccessException, InstantiationException, ParseException {
String fileLocation = new File(testProps.get("test.importcsv").toString()).getAbsolutePath();
String[] args = new String[] {"metadata-import", "-f", fileLocation, "-s"};
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
ScriptService scriptService = ScriptServiceFactory.getInstance().getScriptService();
ScriptConfiguration scriptConfiguration = scriptService.getScriptConfiguration(args[0]);
DSpaceRunnable script = null;
if (scriptConfiguration != null) {
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
}
if (script != null) {
script.initialize(args, testDSpaceRunnableHandler, null);
script.run();
}
}
} }

View File

@@ -479,11 +479,9 @@ public class CSVMetadataImportReferenceIT extends AbstractEntityIntegrationTest
out.close(); out.close();
try { try {
if (validateOnly) { if (validateOnly) {
return runDSpaceScript("metadata-import", "-f", csvFile.getAbsolutePath(), "-e", "admin@email.com", return runDSpaceScript("metadata-import", "-f", csvFile.getAbsolutePath(), "-s", "-v");
"-s", "-v");
} else { } else {
return runDSpaceScript("metadata-import", "-f", csvFile.getAbsolutePath(), "-e", "admin@email.com", return runDSpaceScript("metadata-import", "-f", csvFile.getAbsolutePath(), "-s");
"-s");
} }
} finally { } finally {
csvFile.delete(); csvFile.delete();

View File

@@ -10,6 +10,7 @@ package org.dspace.app.rest.csv;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -24,6 +25,7 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.dspace.app.rest.builder.CollectionBuilder; import org.dspace.app.rest.builder.CollectionBuilder;
import org.dspace.app.rest.builder.CommunityBuilder; import org.dspace.app.rest.builder.CommunityBuilder;
import org.dspace.app.rest.builder.ItemBuilder; import org.dspace.app.rest.builder.ItemBuilder;
@@ -233,11 +235,68 @@ public class CsvImportIT extends AbstractEntityIntegrationTest {
out.close(); out.close();
out = null; out = null;
runDSpaceScript("metadata-import", "-f", filename, "-e", "admin@email.com", "-s"); runDSpaceScript("metadata-import", "-f", filename, "-s");
File file = new File(filename); File file = new File(filename);
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
} }
} }
@Test
public void createRelationshipsWithCsvImportWithSpecifiedEPersonParameterTest() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
Item article = ItemBuilder.createItem(context, col1)
.withTitle("Article")
.withIssueDate("2017-10-17")
.withRelationshipType("Publication")
.build();
String csvLineString = "+," + col1.getHandle() + ",TestItemB,Person," + article
.getID().toString();
String[] csv = {"id,collection,dc.title,relationship.type,relation.isPublicationOfAuthor", csvLineString};
String filename = "test.csv";
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(filename), "UTF-8"));
for (String csvLine : csv) {
out.write(csvLine + "\n");
}
out.flush();
out.close();
out = null;
runDSpaceScript("metadata-import", "-f", filename, "-e", "admin@email.com", "-s");
File file = new File(filename);
if (file.exists()) {
file.delete();
}
Iterator<Item> itemIteratorItem = itemService.findByMetadataField(context, "dc", "title", null, "TestItemB");
Item item = itemIteratorItem.next();
List<Relationship> relationships = relationshipService.findByItem(context, item);
assertThat("Relationship list size is 1", relationships.size(), equalTo(1));
getClient().perform(get("/api/core/items/" + item.getID())).andExpect(status().isOk());
getClient().perform(get("/api/core/relationships/" + relationships.get(0).getID()).param("projection", "full"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.leftPlace", is(0)))
.andExpect(jsonPath("$.rightPlace", is(0)))
.andExpect(jsonPath("$", Matchers.is(RelationshipMatcher.matchRelationship(relationships.get(0)))));
assertFalse(StringUtils.equalsIgnoreCase(item.getSubmitter().getEmail(), "admin@email.com"));
}
} }