Skip to content

Commit

Permalink
Merged dspace-cris-7 into DSC-1233-PubMed-provider-does-not-work-prop…
Browse files Browse the repository at this point in the history
…erly
  • Loading branch information
atarix83 committed Sep 12, 2023
2 parents a068fa4 + 8071287 commit 242db0b
Show file tree
Hide file tree
Showing 29 changed files with 1,687 additions and 3 deletions.
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 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.model;

import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;
import javax.xml.bind.JAXBElement;

/**
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com)
**/
public abstract class AbstractJaxbBuilder<T, C> {

T object;
Class<T> clazz;

protected final ObjectFactory objectFactory = new ObjectFactory();

protected AbstractJaxbBuilder(Class<T> clazz) {
this.clazz = clazz;
}

protected T getObejct() {
if (object == null) {
try {
object = clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
return object;
}

public T build() {
return object;
}

protected void addChildElement(C value, Function<C, JAXBElement<C>> mapper) {
if (value == null) {
return;
}
addChildElement(mapper.apply(value));
}

protected abstract void addChildElement(JAXBElement<C> v);
}
Loading

0 comments on commit 242db0b

Please sign in to comment.