Skip to content

Commit

Permalink
Merged dspace-cris-7 into DSC-1240
Browse files Browse the repository at this point in the history
  • Loading branch information
corrad82-4s committed Sep 29, 2023
2 parents 5f63749 + d7ca5e1 commit 5c266ae
Show file tree
Hide file tree
Showing 53 changed files with 2,413 additions and 155 deletions.
19 changes: 11 additions & 8 deletions dspace-api/src/main/java/org/dspace/app/bulkedit/BulkImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static org.apache.commons.lang3.StringUtils.isAllBlank;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.split;
import static org.apache.commons.lang3.StringUtils.splitByWholeSeparator;
import static org.apache.commons.lang3.StringUtils.startsWith;
import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCauseMessage;
import static org.apache.commons.lang3.math.NumberUtils.isCreatable;
Expand Down Expand Up @@ -609,7 +609,8 @@ private boolean areMetadataValuesValid(Row row, boolean manyMetadataValuesAllowe
for (int index = firstMetadataIndex; index < row.getLastCellNum(); index++) {

String cellValue = WorkbookUtils.getCellValue(row, index);
String[] values = isNotBlank(cellValue) ? split(cellValue, METADATA_SEPARATOR) : new String[] { "" };
String[] values = isNotBlank(cellValue) ? splitByWholeSeparator(cellValue, METADATA_SEPARATOR)
: new String[] { "" };
if (values.length > 1 && !manyMetadataValuesAllowed) {
handleValidationErrorOnRow(row, "Multiple metadata value on the same cell not allowed "
+ "in the metadata group sheets: " + cellValue);
Expand Down Expand Up @@ -743,7 +744,7 @@ private List<String> validateAccessConditions(Row row) {
Map<String, AccessConditionOption> accessConditionOptions = getUploadAccessConditions();

return Arrays.stream(getAccessConditionValues(row))
.map(accessCondition -> split(accessCondition, ACCESS_CONDITION_ATTRIBUTES_SEPARATOR)[0])
.map(accessCondition -> splitByWholeSeparator(accessCondition, ACCESS_CONDITION_ATTRIBUTES_SEPARATOR)[0])
.filter(accessConditionName -> !accessConditionOptions.containsKey(accessConditionName))
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -788,14 +789,14 @@ private List<AccessCondition> buildAccessConditions(Row row, String[] accessCond
}

return Arrays.stream(accessConditions)
.map(accessCondition -> split(accessCondition, ACCESS_CONDITION_ATTRIBUTES_SEPARATOR))
.map(accessCondition -> splitByWholeSeparator(accessCondition, ACCESS_CONDITION_ATTRIBUTES_SEPARATOR))
.map(accessConditionAttributes -> buildAccessCondition(accessConditionAttributes))
.collect(Collectors.toList());
}

private String[] getAccessConditionValues(Row row) {
String accessConditionCellValue = getCellValue(row, ACCESS_CONDITION_HEADER);
return split(accessConditionCellValue, METADATA_SEPARATOR);
return splitByWholeSeparator(accessConditionCellValue, METADATA_SEPARATOR);
}

private AccessCondition buildAccessCondition(String[] accessCondition) {
Expand Down Expand Up @@ -1306,12 +1307,13 @@ private void removeSingleMetadata(DSpaceObject dso, MetadataField field, String
}

private String getMetadataField(String field) {
return field.contains(LANGUAGE_SEPARATOR_PREFIX) ? split(field, LANGUAGE_SEPARATOR_PREFIX)[0] : field;
return field.contains(LANGUAGE_SEPARATOR_PREFIX) ? splitByWholeSeparator(field, LANGUAGE_SEPARATOR_PREFIX)[0]
: field;
}

private String getMetadataLanguage(String field) {
if (field.contains(LANGUAGE_SEPARATOR_PREFIX)) {
return split(field, LANGUAGE_SEPARATOR_PREFIX)[1].replace(LANGUAGE_SEPARATOR_SUFFIX, "");
return splitByWholeSeparator(field, LANGUAGE_SEPARATOR_PREFIX)[1].replace(LANGUAGE_SEPARATOR_SUFFIX, "");
}
return null;
}
Expand Down Expand Up @@ -1364,7 +1366,8 @@ private MultiValuedMap<String, MetadataValueVO> getMetadataFromRow(Row row, Map<
if (index >= firstMetadataIndex) {

String cellValue = WorkbookUtils.getCellValue(row, index);
String[] values = isNotBlank(cellValue) ? split(cellValue, METADATA_SEPARATOR) : new String[] { "" };
String[] values = isNotBlank(cellValue) ? splitByWholeSeparator(cellValue, METADATA_SEPARATOR)
: new String[] { "" };

List<MetadataValueVO> metadataValues = Arrays.stream(values)
.map(value -> buildMetadataValueVO(row, value, isMetadataGroupsSheet))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.metadata.export;

/**
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
**/
public class DspaceExportMetadataSchemaException extends Exception {

public DspaceExportMetadataSchemaException(Exception e) {
super(e);
}

public DspaceExportMetadataSchemaException(String message, Exception e) {
super(message, e);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.metadata.export;

import java.io.File;

import org.apache.commons.cli.ParseException;
import org.dspace.core.Context;

/**
* This script can be use to export a given {@code MetadataSchema} into its
* registry file, that respects the standard DTD / XSD DSpace xml registry.
* <p/>
* This script is supposed to work with the CLI (command-line-interface),
* it accepts only two parameters {@code -i <schema-id> -f <file-path>}
* respectively representing:
* <ul>
* <li><b>{@code schema-id}</b>: id of the schema to export</li>
* <li><b>{@code file-path}</b>:full file path of the file that will contain the export</li>
* <ul>
*
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
*
*/
public class MetadataSchemaExportCliScript extends MetadataSchemaExportScript {

protected String filename;

@Override
public void setup() throws ParseException {
super.setup();
filename = commandLine.getOptionValue('f');
}

@Override
protected File getExportedFile(Context context) throws DspaceExportMetadataSchemaException {
try {
File file = new File(filename);
return metadataSchemaExportService.exportMetadataSchemaToFile(context, metadataSchema, file);
} catch (DspaceExportMetadataSchemaException e) {
handler.logError("Problem occured while exporting the schema to file: " + filename, e);
throw e;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.metadata.export;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

/**
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
*
*/
public class MetadataSchemaExportCliScriptConfiguration
extends MetadataSchemaExportScriptConfiguration<MetadataSchemaExportCliScript> {

@Override
public Options getOptions() {
Options options = super.getOptions();

options.addOption(
Option.builder("f").longOpt("file")
.desc("The temporary file-name to use")
.hasArg()
.build()
);

return options;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.metadata.export;

import java.io.File;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.text.MessageFormat;

import org.apache.commons.cli.ParseException;
import org.dspace.app.metadata.export.service.MetadataExportServiceFactory;
import org.dspace.app.metadata.export.service.MetadataSchemaExportService;
import org.dspace.content.MetadataSchema;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.MetadataSchemaService;
import org.dspace.core.Context;
import org.dspace.scripts.DSpaceRunnable;
import org.dspace.services.factory.DSpaceServicesFactory;

/**
* This script can be use to export a given {@code MetadataSchema} into its
* registry file, that respects the standard DTD / XSD DSpace xml registry.
* <p/>
* This script is supposed to work with the webapp, it accepts only one
* parameter {@code -i <schema-id>} representing the id of the schema that
* will be exported.
*
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
**/
public class MetadataSchemaExportScript
extends DSpaceRunnable<MetadataSchemaExportScriptConfiguration<MetadataSchemaExportScript>> {

protected static String REGISTRY_FILENAME_TEMPLATE = "{0}-types.xml";

protected MetadataSchemaService metadataSchemaService =
ContentServiceFactory.getInstance().getMetadataSchemaService();

protected MetadataSchemaExportService metadataSchemaExportService =
MetadataExportServiceFactory.getInstance().getMetadataSchemaExportService();

protected boolean help;
protected int id;

protected MetadataSchema metadataSchema;

@Override
public MetadataSchemaExportScriptConfiguration getScriptConfiguration() {
return DSpaceServicesFactory
.getInstance().getServiceManager()
.getServiceByName("export-schema", MetadataSchemaExportScriptConfiguration.class);
}

@Override
public void setup() throws ParseException {
help = commandLine.hasOption('h');
try {
id = Integer.parseInt(commandLine.getOptionValue('i'));
} catch (Exception e) {
handler.logError("Cannot parse the id argument ( " + id + " )! You should provide an integer!");
throw new ParseException("Cannot parse the id argument ( " + id + " )! You should provide an integer!");
}
}

@Override
public void internalRun() throws Exception {
if (help) {
printHelp();
return;
}

Context context = new Context();
try {
validate(context);
exportMetadataSchema(context);
} catch (Exception e) {
context.abort();
throw e;
}
}

private void validate(Context context) throws SQLException, ParseException {
metadataSchema = this.metadataSchemaService.find(context, id);
if (metadataSchema == null) {
handler.logError("Cannot find the metadata-schema with id: " + id);
throw new ParseException("Cannot find the metadata-schema with id: " + id);
}
}

private void exportMetadataSchema(Context context) throws Exception {
handler.logInfo(
"Exporting the metadata-schema file for the schema " + metadataSchema.getName()
);
try {
File tempFile = getExportedFile(context);

handler.logInfo(
"Exported to file: " + tempFile.getAbsolutePath()
);

try (FileInputStream fis = new FileInputStream(tempFile)) {
handler.logInfo("Summarizing export ...");
context.turnOffAuthorisationSystem();
handler.writeFilestream(
context, getFilename(metadataSchema), fis, "application/xml", false
);
context.restoreAuthSystemState();
}
} catch (Exception e) {
handler.logError("Problem occured while exporting the schema!", e);
throw e;
}
}

protected String getFilename(MetadataSchema ms) {
return MessageFormat.format(REGISTRY_FILENAME_TEMPLATE, ms.getName());
}

protected File getExportedFile(Context context) throws DspaceExportMetadataSchemaException {
return this.metadataSchemaExportService.exportMetadataSchemaToFile(context, metadataSchema);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.metadata.export;

import java.sql.SQLException;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.core.Context;
import org.dspace.scripts.configuration.ScriptConfiguration;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Configuration of the Script {@code MetadataSchemaExportScript}
*
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
**/
public class MetadataSchemaExportScriptConfiguration<T extends MetadataSchemaExportScript>
extends ScriptConfiguration<T> {

@Autowired
private AuthorizeService authorizeService;

private Class<T> dspaceRunnableClass;

@Override
public Class<T> getDspaceRunnableClass() {
return this.dspaceRunnableClass;
}

@Override
public void setDspaceRunnableClass(Class<T> dspaceRunnableClass) {
this.dspaceRunnableClass = dspaceRunnableClass;
}

@Override
public boolean isAllowedToExecute(Context context) {
try {
return authorizeService.isAdmin(context);
} catch (SQLException e) {
throw new RuntimeException("SQLException occurred when checking if the current user is an admin", e);
}
}

@Override
public Options getOptions() {
Options options = new Options();

options.addOption(
Option.builder("i").longOpt("id")
.desc("Metadata schema id")
.hasArg()
.required()
.build()
);

options.addOption(
Option.builder("h").longOpt("help")
.desc("help")
.hasArg(false)
.required(false)
.build()
);

return options;
}
}
Loading

0 comments on commit 5c266ae

Please sign in to comment.