Skip to content

Commit

Permalink
Fixed #210
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosjepard authored and hmiguim committed Oct 20, 2023
1 parent d6b5965 commit e2c404f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
19 changes: 12 additions & 7 deletions src/main/java/org/roda_project/commons_ip2/cli/Create.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package org.roda_project.commons_ip2.cli;

import static org.roda_project.commons_ip2.cli.model.ExitCodes.EXIT_CODE_OK;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import org.roda_project.commons_ip2.cli.model.exception.CLIException;
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.model.args.MetadataGroup;
import org.roda_project.commons_ip2.cli.model.args.RepresentationGroup;
import org.roda_project.commons_ip2.cli.model.enums.CSIPVersion;
import org.roda_project.commons_ip2.cli.model.enums.Checksum;
import org.roda_project.commons_ip2.cli.utils.CLI.CreateCommandUtils;
import org.roda_project.commons_ip2.cli.model.exception.CLIException;
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.SIPBuilder;
import picocli.CommandLine;
import org.roda_project.commons_ip2.cli.utils.CLI.CreateCommandUtils;

import static org.roda_project.commons_ip2.cli.model.ExitCodes.EXIT_CODE_OK;
import picocli.CommandLine;

/**
* @author Miguel Guimarães <mguimaraes@keep.pt>
Expand All @@ -33,9 +34,13 @@ public class Create implements Callable<Integer> {
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "display this help and exit")
boolean help;


@CommandLine.Option(names = {"-T", "--target-only"}, description = "Adds only the files for the representations")
boolean targetOnly;

@CommandLine.Option(names = {"--override-schema"}, description = "Overrides default schema")
boolean overrideSchema;

@CommandLine.Option(names = {"-v",
"--version"}, description = "E-ARK SIP specification version (possible values: ${COMPLETION-CANDIDATES})")
CSIPVersion version = CSIPVersion.V210;
Expand Down Expand Up @@ -86,7 +91,7 @@ public Integer call() throws CLIException, InvalidPathException, SIPBuilderExcep
throw new CLIException("At least one section must be present, metadata or representation");
}

final Path sipPath = new SIPBuilder().setMetadataArgs(metadataListArgs)
final Path sipPath = new SIPBuilder().setMetadataArgs(metadataListArgs).setOverride(overrideSchema)
.setRepresentationArgs(representationListArgs).setTargetOnly(targetOnly).setSipId(sipId).setAncestors(ancestors)
.setDocumentation(documentation).setSoftwareVersion(getClass().getPackage().getImplementationVersion())
.setPath(path).setSubmitterAgentId(submitterAgentId).setSubmitterAgentName(submitterAgentName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package org.roda_project.commons_ip2.cli.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.roda_project.commons_ip.utils.IPException;
import org.roda_project.commons_ip2.cli.model.exception.SIPBuilderException;
import org.roda_project.commons_ip2.cli.model.args.MetadataGroup;
import org.roda_project.commons_ip2.cli.model.args.RepresentationGroup;
import org.roda_project.commons_ip2.cli.model.enums.CSIPVersion;
import org.roda_project.commons_ip2.cli.model.enums.Checksum;
import org.roda_project.commons_ip2.cli.model.exception.SIPBuilderException;
import org.roda_project.commons_ip2.model.IPContentInformationType;
import org.roda_project.commons_ip2.model.IPContentType;
import org.roda_project.commons_ip2.model.SIP;
import org.roda_project.commons_ip2.model.impl.eark.EARKSIP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
* @author Miguel Guimarães <mguimaraes@keep.pt>
*/
Expand All @@ -39,6 +39,7 @@ public class SIPBuilder {
private List<String> documentation = new ArrayList<>();

private String softwareVersion;
private Boolean overrideSchema;

public SIPBuilder() {
// Empty Constructor
Expand All @@ -49,6 +50,11 @@ public SIPBuilder setMetadataArgs(List<MetadataGroup> metadataArgs) {
return this;
}

public SIPBuilder setOverride(Boolean override) {
this.overrideSchema = override;
return this;
}

public SIPBuilder setRepresentationArgs(List<RepresentationGroup> representationArgs) {
this.representationArgs = representationArgs;
return this;
Expand Down Expand Up @@ -122,6 +128,10 @@ public Path build() throws SIPBuilderException, InterruptedException {
sip.setChecksum(checksum.toString());
}

if (overrideSchema) {
sip.setOverride();
}

try {
SIPBuilderUtils.addMetadataGroupsToSIP(sip, metadataArgs);
} catch (IPException e) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/roda_project/commons_ip2/model/IP.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public abstract class IP implements IPInterface {
private ValidationReport validationReport;

private String checksumAlgorithm;
private boolean override;

public IP() {
this.setId(Utils.generateRandomAndPrefixedUUID());
Expand Down Expand Up @@ -104,6 +105,13 @@ public void setChecksum(final String checksum) {
this.checksumAlgorithm = checksum;
}

public void setOverride() {
this.override = true;
}

public Boolean getOverride() {
return override;
}
public String getChecksum() {
return this.checksumAlgorithm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public Path build(final Path destinationDirectory, final String fileNameWithoutE
try {
Map<String, ZipEntryInfo> zipEntries = getZipEntries();
//default metadata need to be added before creating the mets in order to add them in the mets file
earkUtils.addDefaultSchemas(LOGGER, getSchemas(), buildDir);
earkUtils.addDefaultSchemas(LOGGER, getSchemas(), buildDir, getOverride());

boolean isMetadataOther = (this.getOtherMetadata() != null && !this.getOtherMetadata().isEmpty());
boolean isMetadata = ((this.getDescriptiveMetadata() != null && !this.getDescriptiveMetadata().isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,24 +292,40 @@ protected void addDocumentationToZipAndMETS(Map<String, ZipEntryInfo> zipEntries
}
}

protected void addDefaultSchemas(Logger logger, List<IPFileInterface> schemas, Path buildDir)
protected void addDefaultSchemas(Logger logger, List<IPFileInterface> schemas, Path buildDir, Boolean override)
throws InterruptedException {
try {
if (Thread.interrupted()) {
throw new InterruptedException();
}
String tempSchema = schemas.get(0).getFileName();
if (!override) {
if (tempSchema.equals(IPConstants.SCHEMA_EARK_CSIP_FILENAME)
|| tempSchema.equals(IPConstants.SCHEMA_EARK_SIP_FILENAME)
|| tempSchema.equals(IPConstants.SCHEMA_METS_FILENAME_WITH_VERSION)
|| tempSchema.equals(IPConstants.SCHEMA_XLINK_FILENAME)) {
schemas.remove(0);
tempSchema = "";
}
}

Path earkCsipSchema = Utils.copyResourceFromClasspathToDir(EARKSIP.class, buildDir,
IPConstants.SCHEMA_EARK_CSIP_FILENAME, IPConstants.SCHEMA_EARK_CSIP_RELATIVE_PATH_FROM_RESOURCES);
schemas.add(new IPFile(earkCsipSchema, IPConstants.SCHEMA_EARK_CSIP_FILENAME));
if (!tempSchema.equals(IPConstants.SCHEMA_EARK_CSIP_FILENAME))
schemas.add(new IPFile(earkCsipSchema, IPConstants.SCHEMA_EARK_CSIP_FILENAME));
Path earkSipSchema = Utils.copyResourceFromClasspathToDir(EARKSIP.class, buildDir,
IPConstants.SCHEMA_EARK_SIP_FILENAME, IPConstants.SCHEMA_EARK_SIP_RELATIVE_PATH_FROM_RESOURCES);
schemas.add(new IPFile(earkSipSchema, IPConstants.SCHEMA_EARK_SIP_FILENAME));
if (!tempSchema.equals(IPConstants.SCHEMA_EARK_SIP_FILENAME))
schemas.add(new IPFile(earkSipSchema, IPConstants.SCHEMA_EARK_SIP_FILENAME));
Path metsSchema = Utils.copyResourceFromClasspathToDir(EARKSIP.class, buildDir,
IPConstants.SCHEMA_METS_FILENAME_WITH_VERSION, IPConstants.SCHEMA_METS_RELATIVE_PATH_FROM_RESOURCES);
schemas.add(new IPFile(metsSchema, IPConstants.SCHEMA_METS_FILENAME_WITH_VERSION));
if (!tempSchema.equals(IPConstants.SCHEMA_METS_FILENAME_WITH_VERSION))
schemas.add(new IPFile(metsSchema, IPConstants.SCHEMA_METS_FILENAME_WITH_VERSION));
Path xlinkSchema = Utils.copyResourceFromClasspathToDir(EARKSIP.class, buildDir,
IPConstants.SCHEMA_XLINK_FILENAME, IPConstants.SCHEMA_XLINK_RELATIVE_PATH_FROM_RESOURCES);
schemas.add(new IPFile(xlinkSchema, IPConstants.SCHEMA_XLINK_FILENAME));
if (!tempSchema.equals(IPConstants.SCHEMA_XLINK_FILENAME))
schemas.add(new IPFile(xlinkSchema, IPConstants.SCHEMA_XLINK_FILENAME));

} catch (IOException e) {
logger.error("Error while trying to add default schemas", e);
}
Expand Down Expand Up @@ -876,4 +892,5 @@ protected IPInterface processSubmissionMetadata(final MetsWrapper metsWrapper, f
return processFile(ip, metsWrapper.getSubmissionsDiv(), IPConstants.SUBMISSION, basePath);
}


}

0 comments on commit e2c404f

Please sign in to comment.