Skip to content

Commit

Permalink
ALPHA erms
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosjepard committed Nov 28, 2024
1 parent f85e668 commit b6ff492
Show file tree
Hide file tree
Showing 42 changed files with 3,028 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
<dependency>
<groupId>org.roda-project</groupId>
<artifactId>commons-ip2</artifactId>
<version>2.6.2</version>
<version>2.9.0-SNAPSHOT</version>
</dependency>
<!-- CLI dependecies -->
<!-- https://mvnrepository.com/artifact/info.picocli/picocli -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,93 @@
package org.keeps.digitalpreservation.commonsip.citserms.builder;


import org.roda_project.commons_ip.utils.IPEnums;
import org.roda_project.commons_ip2.cli.model.args.RepresentationGroup;
import org.roda_project.commons_ip.utils.IPException;
import org.roda_project.commons_ip2.cli.model.args.MetadataGroup;
import org.roda_project.commons_ip2.cli.model.exception.SIPBuilderException;
import org.roda_project.commons_ip2.cli.utils.SIPBuilder;
import org.roda_project.commons_ip2.cli.utils.SIPBuilderUtils;
import org.roda_project.commons_ip2.model.IPContentType;
import org.roda_project.commons_ip2.model.impl.eark.EARKSIP;
import org.roda_project.commons_ip2.model.IPContentInformationType;
import org.roda_project.commons_ip2.model.IPRepresentation;
import org.roda_project.commons_ip2.model.impl.eark.EARKSIP;
import org.roda_project.commons_ip2.model.impl.eark.out.writers.factory.ZipWriteStrategyFactory;
import org.roda_project.commons_ip2.model.impl.eark.out.writers.strategy.WriteStrategy;
import org.roda_project.commons_ip2.model.impl.eark.out.writers.strategy.ZipWriteStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class CITSBuilder {

public void build() throws IPException, InterruptedException {
private static final Logger LOGGER = LoggerFactory.getLogger(CITSBuilder.class);;
private List<MetadataGroup> metadataArgs = new ArrayList<>();
private List<RepresentationGroup> representationArgs = new ArrayList<>();
private boolean targetOnly;
private String path;
private String submitterAgentName;
private String submitterAgentId;
private String sipId;
private List<String> ancestors;
private List<String> documentation = new ArrayList<>();

private String softwareVersion;
private Boolean overrideSchema;


public CITSBuilder setMetadataArgs(List<MetadataGroup> metadataArgs) {
this.metadataArgs = metadataArgs;
return this;
}

public CITSBuilder setRepresentationArgs(List<RepresentationGroup> representationArgs) {
this.representationArgs = representationArgs;
return this;
}

public CITSBuilder setSubmitterAgentName(String submitterAgentName) {
this.submitterAgentName = submitterAgentName;
return this;
}

public void build() throws InterruptedException, IPException, IPException, SIPBuilderException {
EARKSIP earksip = new EARKSIP();
earksip.setProfile("https://citssiard.dilcis.eu/profile/E-ARK-SIARD-ROOT.xml");
//Isto ainda n se sabe
//earksip.setProfile("https://citssiard.dilcis.eu/profile/E-ARK-SIARD-ROOT.xml");


IPContentInformationType erms = new IPContentInformationType("citserms_v2_1");
erms.setOtherType("citserms_v2_1");

earksip.addSubmitterAgent(submitterAgentName, submitterAgentId);

IPContentInformationType siard2 = new IPContentInformationType("SIARD2");
siard2.setOtherType("SIARD_2.1");
earksip.setContentType(IPContentType.getDataset());

earksip.setContentInformationType(siard2);
earksip.setContentInformationType(erms);

earksip.setId("test");
try {
SIPBuilderUtils.addMetadataGroupsToSIP(earksip, metadataArgs);
} catch (IPException e) {
LOGGER.debug("Cannot add metadata to the SIP", e);
throw new SIPBuilderException("Cannot add metadata to the SIP.");
}

IPRepresentation ipRepresentation = new IPRepresentation();
try {
SIPBuilderUtils.addRepresentationGroupsToErmsSIP(earksip, representationArgs, true);
} catch (IPException e) {
LOGGER.debug("Cannot add representation to the SIP", e);
throw new SIPBuilderException("Cannot add representation to the SIP");
}

ipRepresentation.setContentInformationType(new IPContentInformationType(IPContentInformationType.IPContentInformationTypeEnum.SIARD2));
WriteStrategy writeStrategy = new ZipWriteStrategyFactory().create(Paths.get(System.getProperty("user.dir")));

earksip.addRepresentation(ipRepresentation);
Path build = earksip.build(writeStrategy, null, IPEnums.SipType.ERMS);

Path build = earksip.build(Paths.get("/home/mguimaraes/Desktop"));
System.out.printf(build.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.keeps.digitalpreservation.commonsip.citserms.cli;

import org.keeps.digitalpreservation.commonsip.citserms.builder.CITSBuilder;
import org.keeps.digitalpreservation.commonsip.citserms.cli.exception.CLIException;
import org.keeps.digitalpreservation.commonsip.citserms.cli.model.ExitCodes;
import org.keeps.digitalpreservation.commonsip.citserms.cli.model.args.MetadataGroup;
import org.keeps.digitalpreservation.commonsip.citserms.cli.model.args.RepresentationGroup;
import org.roda_project.commons_ip2.cli.model.args.RepresentationGroup;
import org.roda_project.commons_ip.utils.IPException;
import org.roda_project.commons_ip2.cli.model.args.MetadataGroup;
import org.roda_project.commons_ip2.cli.model.exception.InvalidPathException;
import org.roda_project.commons_ip2.cli.model.exception.SIPBuilderException;
import org.roda_project.commons_ip2.cli.utils.CLI.CreateCommandUtils;
import picocli.CommandLine;

import java.util.ArrayList;
Expand All @@ -15,7 +17,7 @@
/**
* @author Miguel Guimarães <[email protected]>
*/
@CommandLine.Command(name = "create", description = "Creates E-ARK CITS SIARD packages%n", showDefaultValues = true)
@CommandLine.Command(name = "create", description = "Creates E-ARK CITS ERMS packages%n", showDefaultValues = true)
public class Create implements Callable<Integer> {

@CommandLine.ArgGroup(exclusive = false, multiplicity = "0..*", heading = "%nThis is the descriptive metadata section:%n")
Expand All @@ -24,6 +26,9 @@ public class Create implements Callable<Integer> {
@CommandLine.ArgGroup(exclusive = false, multiplicity = "0..*", heading = "%nThis is the representation section:%n")
List<RepresentationGroup> representationListArgs = new ArrayList<>();

@CommandLine.Option(names = {"--submitter-name"}, description = "Submitter agent name", paramLabel = "<name>")
String submitterAgentName;

@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Show this help message and exit.")
boolean help;

Expand All @@ -35,15 +40,20 @@ public class Create implements Callable<Integer> {
List<String> documentation = new ArrayList<>();

@Override
public Integer call() throws CLIException {
public Integer call() throws InvalidPathException {

if (!CreateCommandUtils.validateMetadataSchemaPaths(metadataListArgs)) {
throw new InvalidPathException("Make sure if all the descriptive metadata schema paths exists");
}

//if (representationListArgs.isEmpty()) {
// throw new CLIException("There MUST be a minimum of one representation");
// }

CITSBuilder builder = new CITSBuilder();
try {
builder.build();
} catch (IPException | InterruptedException e) {
builder.setMetadataArgs(metadataListArgs).setRepresentationArgs(representationListArgs).setSubmitterAgentName(submitterAgentName).build();
} catch (InterruptedException | IPException | SIPBuilderException e) {
System.out.printf(e.getMessage());
System.out.println("TESTE!!!!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* @author Miguel Guimarães <[email protected]>
*/
@CommandLine.Command(name = "commons-ip-cits-siard", subcommands = {Create.class,
@CommandLine.Command(name = "commons-ip-cits-ERMS", subcommands = {Create.class,
Validate.class}, mixinStandardHelpOptions = true, versionProvider = VersionProvider.class)
public class Main implements Runnable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,119 @@
package org.keeps.digitalpreservation.commonsip.citserms.cli;

import org.keeps.digitalpreservation.commonsip.citserms.validator.EARKERMSValidator;
import org.roda_project.commons_ip2.cli.model.ExitCodes;
import org.roda_project.commons_ip2.cli.model.enums.ReportTypeEnums;
import org.roda_project.commons_ip2.cli.model.exception.CLIException;
import org.roda_project.commons_ip2.cli.model.exception.ValidationException;
import org.roda_project.commons_ip2.cli.utils.CLI.ValidateCommandUtils;
import org.roda_project.commons_ip2.utils.LogSystem;
import org.roda_project.commons_ip2.validator.EARKPyIPValidator;
import org.roda_project.commons_ip2.validator.EARKSIPValidator;
import org.roda_project.commons_ip2.validator.observer.ProgressValidationLoggerObserver;
import org.roda_project.commons_ip2.validator.reporter.ValidationReportOutputJSONPyIP;
import org.roda_project.commons_ip2.validator.reporter.ValidationReportOutputJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import picocli.CommandLine;


import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.Callable;

import static org.roda_project.commons_ip2.cli.model.enums.ReportTypeEnums.ReportType.COMMONS_IP;

/**
* @author Miguel Guimarães <[email protected]>
*/
@CommandLine.Command(name = "validate", showDefaultValues = true, description = "Validates E-ARK CITS SIARD packages against the specification")
public class Validate {
public class Validate implements Callable<Integer> {

private static final Logger LOGGER = LoggerFactory.getLogger(org.roda_project.commons_ip2.cli.Validate.class);
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "display this help and exit")
boolean help;

@CommandLine.Option(names = {"-i",
"--inputs"}, split = ",", required = true, description = "Paths to the SIPs archive file or files", paramLabel = "<path>")
List<String> sipPaths;

@CommandLine.Option(names = {"-r",
"--reporter-type"}, paramLabel = "<type>", description = "Report type (possible values: ${COMPLETION-CANDIDATES})")
ReportTypeEnums.ReportType reportType = COMMONS_IP;

@CommandLine.Option(names = {"-o",
"--output-report-dir"}, paramLabel = "<path>", description = "Path to save the validation report. If not set a report will be generated in the same folder as the IP package.")
String reportPathDir = System.getProperty("user.dir");

@CommandLine.Option(names = {"-v",
"--verbose"}, description = "Verbose command line output with all validation steps")
boolean verbose;

@CommandLine.Option(names = {"--specification-version"}, description = "E-ARK CSIP version")
String version = "2.1.0";

public Integer call() throws ValidationException, CLIException {
for (String sip : sipPaths) {
try {
handleSipValidation(sip, reportPathDir, verbose);
} catch (IOException e) {
throw new ValidationException("Unable to create necessary files to start validation");
} catch (ParserConfigurationException | SAXException | NoSuchAlgorithmException e) {
throw new ValidationException("Failed to validate");
}
}

return ExitCodes.EXIT_CODE_OK;
}

private void handleSipValidation(final String sip, final String reportPathDir,
final boolean verbose)
throws IOException, ParserConfigurationException, SAXException, CLIException, NoSuchAlgorithmException {
final Path sipPath = Paths.get(sip);

Path reportPath = ValidateCommandUtils.obtainReportPath(sipPath, reportPathDir);
CommandLine cmd = spec.commandLine();
String commandLineString = String.join(" ", cmd.getParseResult().originalArgs());


LogSystem.logOperatingSystemInfo();
LOGGER.debug("command executed: {}", commandLineString);
switch (reportType) {
case COMMONS_IP -> {
final OutputStream outputStream = ValidateCommandUtils.createReportOutputStream(reportPath);
if (outputStream != null) {
final ValidationReportOutputJson jsonReporter = new ValidationReportOutputJson(sipPath, outputStream);
final EARKSIPValidator earksipValidator = new EARKSIPValidator(jsonReporter, version);
final EARKERMSValidator earkermsValidator = new EARKERMSValidator(jsonReporter, version);
if (verbose) {
earksipValidator.addObserver(new ProgressValidationLoggerObserver());
}
earksipValidator.validate(version);
earkermsValidator.validate(version);
}
}
case PYIP -> {
final ValidationReportOutputJSONPyIP jsonReporter = new ValidationReportOutputJSONPyIP(reportPath, sipPath);
final EARKPyIPValidator earkPyIPValidator = new EARKPyIPValidator(jsonReporter, version);
if (verbose) {
earkPyIPValidator.addObserver(new ProgressValidationLoggerObserver());
}
earkPyIPValidator.validate();
}
default -> throw new CLIException("Unexpected value: " + reportType);
}


new CommandLine(this).getOut().printf("E-ARK SIP validation report at '%s'%n",
reportPath.normalize().toAbsolutePath());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.keeps.digitalpreservation.commonsip.citserms.model;

import org.keeps.digitalpreservation.commonsip.citserms.model.beans.ErmsType;

import java.nio.file.Path;

/**
* @author Carlos Afonso <[email protected]>
*/
public class ErmsWrapper {
private ErmsType erms;
private Path ermsPath;

public ErmsWrapper(ErmsType erms, Path ermsPath) {
super();
this.erms = erms;
this.ermsPath = ermsPath;
}

public ErmsType getErms() {
return erms;
}

public void setErms(ErmsType mets) {
this.erms = erms;
}

public Path getErmsPath() {
return ermsPath;
}

public void setErmsPath(Path ermsPath) {
this.ermsPath = ermsPath;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.keeps.digitalpreservation.commonsip.citserms.model;

/**
* @author Carlos Afonso <[email protected]>
*/
public class IPErmsConstants {

public static final String SCHEMA_ERMS_RELATIVE_PATH_FROM_RESOURCES = "/schema/v2/ERMS.xsd";
public static final String SCHEMA_ERMS_FILENAME_WITH_VERSION = "ERMS.xsd";

public static final String ERMS_FILE = "ERMS.xml";

public static final String ERMS_FILE_NAME = "ERMS";

public static final String ERMS_FILE_EXTENSION = ".xml";

public static boolean ERMS_ENCODE_AND_DECODE_HREF = true;

}
Loading

0 comments on commit b6ff492

Please sign in to comment.