Skip to content

Commit

Permalink
[Task 70911] applied feedback to the MetadataExport and MetadataImpor…
Browse files Browse the repository at this point in the history
…t scripts as well as general Process functionality feedback
  • Loading branch information
Raf-atmire committed May 28, 2020
1 parent ce9ba12 commit f19bf96
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
public class MetadataExport extends DSpaceRunnable<MetadataExportScriptConfiguration> {

private Context context = null;
private boolean help = false;
private String filename = null;
private String handle = null;
Expand All @@ -40,13 +39,20 @@ public class MetadataExport extends DSpaceRunnable<MetadataExportScriptConfigura

@Override
public void internalRun() throws Exception {

if (help) {
handler.logInfo("\nfull export: metadata-export -f filename");
handler.logInfo("partial export: metadata-export -i handle -f filename");
printHelp();
return;
}

Context context = new Context();
context.turnOffAuthorisationSystem();
try {
context.setCurrentUser(ePersonService.find(context, this.getEpersonIdentifier()));
} catch (SQLException e) {
handler.handleException(e);
}
DSpaceCSV dSpaceCSV = metadataDSpaceCsvExportService
.handleExport(context, exportAllItems, exportAllMetadata, handle,
handler);
Expand All @@ -63,8 +69,6 @@ public MetadataExportScriptConfiguration getScriptConfiguration() {

@Override
public void setup() throws ParseException {
context = new Context();
context.turnOffAuthorisationSystem();

if (commandLine.hasOption('h')) {
help = true;
Expand All @@ -83,11 +87,5 @@ public void setup() throws ParseException {
exportAllItems = true;
}
handle = commandLine.getOptionValue('i');

try {
context.setCurrentUser(ePersonService.find(context, this.getEpersonIdentifier()));
} catch (SQLException e) {
handler.handleException(e);
}
}
}
105 changes: 51 additions & 54 deletions dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@
* @author Stuart Lewis
*/
public class MetadataImport extends DSpaceRunnable<MetadataImportScriptConfiguration> {
/**
* The Context
*/
Context c;

/**
* The DSpaceCSV object we're processing
Expand Down Expand Up @@ -181,6 +177,35 @@ public void internalRun() throws Exception {
printHelp();
return;
}
// Create a context
Context c = null;
try {
c = new Context();
c.turnOffAuthorisationSystem();
} catch (Exception e) {
throw new ParseException("Unable to create a new DSpace Context: " + e.getMessage());
}

// Find the EPerson, assign to context
try {
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) {
setAuthorizedMetadataFields();
}
Expand All @@ -207,7 +232,7 @@ public void internalRun() throws Exception {
if (!commandLine.hasOption('s') || validateOnly) {
// See what has changed
try {
changes = runImport(false, useWorkflow, workflowNotify, useTemplate);
changes = runImport(c, false, useWorkflow, workflowNotify, useTemplate);
} catch (MetadataImportException mie) {
throw mie;
}
Expand Down Expand Up @@ -237,7 +262,7 @@ public void internalRun() throws Exception {
if (change && !validateOnly) {
try {
// Make the changes
changes = runImport(true, useWorkflow, workflowNotify, useTemplate);
changes = runImport(c, true, useWorkflow, workflowNotify, useTemplate);
} catch (MetadataImportException mie) {
throw mie;
}
Expand Down Expand Up @@ -310,35 +335,6 @@ public void setup() throws ParseException {
}
validateOnly = commandLine.hasOption('v');


// Create a context
try {
c = new Context();
c.turnOffAuthorisationSystem();
} catch (Exception e) {
throw new ParseException("Unable to create a new DSpace Context: " + e.getMessage());
}

// Find the EPerson, assign to context
try {
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());
}

// Is this a silent run?
change = false;
}
Expand All @@ -354,7 +350,7 @@ public void setup() throws ParseException {
* @return An array of BulkEditChange elements representing the items that have changed
* @throws MetadataImportException if something goes wrong
*/
public List<BulkEditChange> runImport(boolean change,
public List<BulkEditChange> runImport(Context c, boolean change,
boolean useWorkflow,
boolean workflowNotify,
boolean useTemplate)
Expand All @@ -371,7 +367,7 @@ public List<BulkEditChange> runImport(boolean change,
for (DSpaceCSVLine line : toImport) {
// Resolve target references to other items
populateRefAndRowMap(line, line.getID());
line = resolveEntityRefs(line);
line = resolveEntityRefs(c, line);
// Get the DSpace item to compare with
UUID id = line.getID();

Expand Down Expand Up @@ -403,7 +399,7 @@ public List<BulkEditChange> runImport(boolean change,
throw new MetadataImportException("Missing collection from item " + item.getHandle());
}
List<Collection> actualCollections = item.getCollections();
compare(item, collections, actualCollections, whatHasChanged, change);
compare(c, item, collections, actualCollections, whatHasChanged, change);
}

// Iterate through each metadata element in the csv line
Expand All @@ -422,7 +418,7 @@ public List<BulkEditChange> runImport(boolean change,
}
}
// Compare
compareAndUpdate(item, fromCSV, change, md, whatHasChanged, line);
compareAndUpdate(c, item, fromCSV, change, md, whatHasChanged, line);
}
}

Expand Down Expand Up @@ -498,7 +494,7 @@ public List<BulkEditChange> runImport(boolean change,
}

// Add all the values from the CSV line
add(fromCSV, md, whatHasChanged);
add(c, fromCSV, md, whatHasChanged);
}
}

Expand Down Expand Up @@ -624,7 +620,7 @@ public List<BulkEditChange> runImport(boolean change,

// Return the changes
if (!change) {
validateExpressedRelations();
validateExpressedRelations(c);
}
return changes;
}
Expand All @@ -642,7 +638,7 @@ public List<BulkEditChange> runImport(boolean change,
* @throws AuthorizeException if there is an authorization problem with permissions
* @throws MetadataImportException custom exception for error handling within metadataimport
*/
protected void compareAndUpdate(Item item, String[] fromCSV, boolean change,
protected void compareAndUpdate(Context c, Item item, String[] fromCSV, boolean change,
String md, BulkEditChange changes, DSpaceCSVLine line)
throws SQLException, AuthorizeException, MetadataImportException {
// Log what metadata element we're looking at
Expand Down Expand Up @@ -720,7 +716,7 @@ protected void compareAndUpdate(Item item, String[] fromCSV, boolean change,
// Compare from current->csv
for (int v = 0; v < fromCSV.length; v++) {
String value = fromCSV[v];
BulkEditMetadataValue dcv = getBulkEditValueFromCSV(language, schema, element, qualifier, value,
BulkEditMetadataValue dcv = getBulkEditValueFromCSV(c, language, schema, element, qualifier, value,
fromAuthority);
if (fromAuthority != null) {
value = dcv.getValue() + csv.getAuthoritySeparator() + dcv.getAuthority() + csv
Expand Down Expand Up @@ -957,7 +953,7 @@ private void addRelationship(Context c, Item item, String typeName, String value
* @throws IOException Can be thrown when moving items in communities
* @throws MetadataImportException If something goes wrong to be reported back to the user
*/
protected void compare(Item item,
protected void compare(Context c, Item item,
List<String> collections,
List<Collection> actualCollections,
BulkEditChange bechange,
Expand Down Expand Up @@ -1054,8 +1050,8 @@ protected void compare(Item item,
// Remove from old owned collection (if still a member)
if (bechange.getOldOwningCollection() != null) {
boolean found = false;
for (Collection c : item.getCollections()) {
if (c.getID().equals(bechange.getOldOwningCollection().getID())) {
for (Collection collection : item.getCollections()) {
if (collection.getID().equals(bechange.getOldOwningCollection().getID())) {
found = true;
}
}
Expand All @@ -1082,7 +1078,7 @@ protected void compare(Item item,
* @throws SQLException when an SQL error has occurred (querying DSpace)
* @throws AuthorizeException If the user can't make the changes
*/
protected void add(String[] fromCSV, String md, BulkEditChange changes)
protected void add(Context c, String[] fromCSV, String md, BulkEditChange changes)
throws SQLException, AuthorizeException {
// Don't add owning collection or action
if (("collection".equals(md)) || ("action".equals(md))) {
Expand Down Expand Up @@ -1120,7 +1116,7 @@ protected void add(String[] fromCSV, String md, BulkEditChange changes)

// Add all the values
for (String value : fromCSV) {
BulkEditMetadataValue dcv = getBulkEditValueFromCSV(language, schema, element, qualifier, value,
BulkEditMetadataValue dcv = getBulkEditValueFromCSV(c, language, schema, element, qualifier, value,
fromAuthority);
if (fromAuthority != null) {
value = dcv.getValue() + csv.getAuthoritySeparator() + dcv.getAuthority() + csv
Expand All @@ -1134,7 +1130,7 @@ protected void add(String[] fromCSV, String md, BulkEditChange changes)
}
}

protected BulkEditMetadataValue getBulkEditValueFromCSV(String language, String schema, String element,
protected BulkEditMetadataValue getBulkEditValueFromCSV(Context c, String language, String schema, String element,
String qualifier, String value,
AuthorityValue fromAuthority) {
// Look to see if it should be removed
Expand Down Expand Up @@ -1401,7 +1397,7 @@ private void setAuthorizedMetadataFields() {
* @return a copy, with all references resolved.
* @throws MetadataImportException if there is an error resolving any entity target reference.
*/
public DSpaceCSVLine resolveEntityRefs(DSpaceCSVLine line) throws MetadataImportException {
public DSpaceCSVLine resolveEntityRefs(Context c, DSpaceCSVLine line) throws MetadataImportException {
DSpaceCSVLine newLine = new DSpaceCSVLine(line.getID());
UUID originId = evaluateOriginId(line.getID());
for (String key : line.keys()) {
Expand Down Expand Up @@ -1642,7 +1638,7 @@ private UUID evaluateOriginId(@Nullable UUID originId) {
* Validate every relation modification expressed in the CSV.
*
*/
private void validateExpressedRelations() throws MetadataImportException {
private void validateExpressedRelations(Context c) throws MetadataImportException {
for (String targetUUID : entityRelationMap.keySet()) {
String targetType = null;
try {
Expand Down Expand Up @@ -1696,7 +1692,7 @@ private void validateExpressedRelations() throws MetadataImportException {
// Attempt lookup in processed map first before looking in archive.
if (entityTypeMap.get(UUID.fromString(originRefererUUID)) != null) {
originType = entityTypeMap.get(UUID.fromString(originRefererUUID));
validateTypesByTypeByTypeName(targetType, originType, typeName, originRow);
validateTypesByTypeByTypeName(c, targetType, originType, typeName, originRow);
} else {
// Origin item may be archived; check there.
// Add to errors if Realtionship.type cannot be derived.
Expand All @@ -1710,7 +1706,7 @@ private void validateExpressedRelations() throws MetadataImportException {
if (relTypes.size() > 0) {
relTypeValue = relTypes.get(0).getValue();
originType = entityTypeService.findByEntityType(c, relTypeValue).getLabel();
validateTypesByTypeByTypeName(targetType, originType, typeName, originRow);
validateTypesByTypeByTypeName(c, targetType, originType, typeName, originRow);
} else {
relationValidationErrors.add("Error on CSV row " + originRow + ":" + "\n" +
"Cannot resolve Entity type for reference: "
Expand Down Expand Up @@ -1749,7 +1745,8 @@ private void validateExpressedRelations() throws MetadataImportException {
* @param typeName left or right typeName of the respective Relationship.
* @return the UUID of the item.
*/
private void validateTypesByTypeByTypeName(String targetType, String originType, String typeName, String originRow)
private void validateTypesByTypeByTypeName(Context c,
String targetType, String originType, String typeName, String originRow)
throws MetadataImportException {
try {
RelationshipType foundRelationshipType = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public List<String> getFileNamesFromInputStreamOptions() {

/**
* Generic getter for the epersonIdentifier
* This EPerson identifier variable is the uuid of the eperson that's running the script
* @return the epersonIdentifier value of this DSpaceRunnable
*/
public UUID getEpersonIdentifier() {
Expand All @@ -144,6 +145,7 @@ public UUID getEpersonIdentifier() {

/**
* Generic setter for the epersonIdentifier
* This EPerson identifier variable is the uuid of the eperson that's running the script
* @param epersonIdentifier The epersonIdentifier to be set on this DSpaceRunnable
*/
public void setEpersonIdentifier(UUID epersonIdentifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ProcessFilesRestController {
ProcessResourceHalLinkFactory processResourceHalLinkFactory;

@RequestMapping(method = RequestMethod.GET, value = "/{fileType}")
@PreAuthorize("hasAuthority('ADMIN')")
@PreAuthorize("hasPermission(#processId, 'PROCESS', 'READ')")
public PagedModel<BitstreamResource> listFilesWithTypeFromProcess(
@PathVariable(name = "processId") Integer processId,
@PathVariable(name = "fileType") String fileType,
Expand All @@ -82,7 +82,7 @@ public PagedModel<BitstreamResource> listFilesWithTypeFromProcess(


@RequestMapping(method = RequestMethod.GET, value = "/name/{fileName:.+}")
@PreAuthorize("hasAuthority('ADMIN')")
@PreAuthorize("hasPermission(#processId, 'PROCESS', 'READ')")
public BitstreamResource getBitstreamByName(@PathVariable(name = "processId") Integer processId,
@PathVariable(name = "fileName") String fileName)
throws SQLException, AuthorizeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ protected void delete(Context context, Integer integer)
try {
processService.delete(context, processService.find(context, integer));
} catch (SQLException | IOException e) {
log.error("Something went wrong trying to find Process with id: " + integer);
log.error("Something went wrong trying to find Process with id: " + integer, e);
throw new RuntimeException(e.getMessage(), e);
}
}

Expand Down

0 comments on commit f19bf96

Please sign in to comment.