Skip to content

Commit

Permalink
Fix #125
Browse files Browse the repository at this point in the history
  • Loading branch information
hmiguim committed Jul 31, 2024
1 parent ffa702a commit da1d34e
Show file tree
Hide file tree
Showing 31 changed files with 429 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void writeToPath(final Map<String, ZipEntryInfo> zipEntryInfos, final Pa
throw new InterruptedException();
}

zipEntryInfo.prepareEntryforZipping();
zipEntryInfo.prepareEntryForZipping();
LOGGER.debug("Writing file {}", zipEntryInfo.getFilePath());
final Path outputPath = Paths.get(path.toString(), zipEntryInfo.getName());
writeFileToPath(zipEntryInfo, outputPath, onlyMets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Path getFilePath() {
}

@Override
public void prepareEntryforZipping() throws IPException {
public void prepareEntryForZipping() throws IPException {
// do nothing
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public METSFileTypeZipEntryInfo(String name, Path filePath, FileType metsFileTyp
}

@Override
public void prepareEntryforZipping() {
public void prepareEntryForZipping() {
// do nothing
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public METSMdRefZipEntryInfo(String name, Path filePath, MdRef metsMdRef) {
}

@Override
public void prepareEntryforZipping() {
public void prepareEntryForZipping() {
// do nothing
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void setSize(long size) {
}

@Override
public void prepareEntryforZipping() throws IPException {
public void prepareEntryForZipping() throws IPException {
try {
METSUtils.marshallMETS(mets, getFilePath(), rootMETS);
} catch (JAXBException | IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static void zip(Map<String, ZipEntryInfo> files, OutputStream out, SIP si
}

file.setChecksum(sip.getChecksum());
file.prepareEntryforZipping();
file.prepareEntryForZipping();

LOGGER.debug("Zipping file {}", file.getFilePath());
ZipEntry entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ZipEntryInfo {

Path getFilePath();

void prepareEntryforZipping() throws IPException;
void prepareEntryForZipping() throws IPException;

String getChecksum();

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/roda_project/commons_ip2/cli/Create.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
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.enums.ChecksumAlgorithm;
import org.roda_project.commons_ip2.cli.model.enums.WriteStrategyEnum;
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;
Expand Down Expand Up @@ -67,12 +68,15 @@ public class Create implements Callable<Integer> {

@CommandLine.Option(names = {"-C",
"--checksum"}, paramLabel = "<algorithm>", description = "Checksum algorithms (possible values: ${COMPLETION-CANDIDATES})")
Checksum checksum = Checksum.SHA256;
ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.SHA256;

@CommandLine.Option(names = {"-d",
"--documentation"}, description = "Path(s) to documentation file(s)", split = ",", paramLabel = "<path>", showDefaultValue = CommandLine.Help.Visibility.NEVER)
List<String> documentation = new ArrayList<>();

@CommandLine.Option(names = {"-s", "--strategy"}, description = "Write strategy to be used (possible values: ${COMPLETION-CANDIDATES})")
WriteStrategyEnum strategy = WriteStrategyEnum.ZIP;

@Override
public Integer call() throws CLIException, InvalidPathException, SIPBuilderException, InterruptedException {
if (!CreateCommandUtils.validateRepresentationDataPaths(representationListArgs)) {
Expand All @@ -98,13 +102,13 @@ public Integer call() throws CLIException, InvalidPathException, SIPBuilderExcep
CommandLine cmd = spec.commandLine();
String commandLineString = String.join(" ", cmd.getParseResult().originalArgs());
LogSystem.logOperatingSystemInfo();
LOGGER.debug("command executed: " + commandLineString);
LOGGER.debug("command executed: {}", commandLineString);

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)
.setChecksum(checksum).setVersion(version).build();
.setChecksum(checksumAlgorithm).setVersion(version).setWriteStrategy(strategy).build();

new CommandLine(this).getOut().printf("E-ARK SIP created at '%s'%n", sipPath.normalize().toAbsolutePath());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/**
* @author Miguel Guimarães <[email protected]>
*/
public enum Checksum {
public enum ChecksumAlgorithm {
SHA3512("SHA3-512"), SHA384("SHA-384"), SHA("SHA"), SHA3384("SHA3-384"), SHA224("SHA-224"), SHA512("SHA-512"),
SHA256("SHA-256"), MD2("MD2"), SHA3256("SHA3-256"), MD5("MD5"), SHA3224("SHA3-224");

private final String type;

Checksum(String checksum) {
ChecksumAlgorithm(String checksum) {
this.type = checksum;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.roda_project.commons_ip2.cli.model.enums;

/**
* @author Miguel Guimarães <[email protected]>
*/
public enum WriteStrategyEnum {
ZIP("Zip"), FOLDER("Folder");

private final String type;

WriteStrategyEnum(String type) {
this.type = type;
}

@Override
public String toString() {
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
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.enums.ChecksumAlgorithm;
import org.roda_project.commons_ip2.cli.model.enums.WriteStrategyEnum;
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.roda_project.commons_ip2.model.impl.eark.out.writers.strategy.WriteStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,11 +37,13 @@ public class SIPBuilder {
private String submitterAgentId;
private String sipId;
private List<String> ancestors;
private Checksum checksum = Checksum.SHA256;
private ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.SHA256;
private List<String> documentation = new ArrayList<>();

private String softwareVersion;
private Boolean overrideSchema;
private boolean overrideSchema;

private WriteStrategyEnum writeStrategyEnum;

public SIPBuilder() {
// Empty Constructor
Expand Down Expand Up @@ -95,8 +99,8 @@ public SIPBuilder setAncestors(List<String> ancestors) {
return this;
}

public SIPBuilder setChecksum(Checksum checksum) {
this.checksum = checksum;
public SIPBuilder setChecksum(ChecksumAlgorithm checksumAlgorithm) {
this.checksumAlgorithm = checksumAlgorithm;
return this;
}

Expand All @@ -110,6 +114,11 @@ public SIPBuilder setSoftwareVersion(String softwareVersion) {
return this;
}

public SIPBuilder setWriteStrategy(WriteStrategyEnum writeStrategyEnum) {
this.writeStrategyEnum = writeStrategyEnum;
return this;
}

public Path build() throws SIPBuilderException, InterruptedException {
final SIP sip = new EARKSIP(SIPBuilderUtils.getOrGenerateID(this.sipId), IPContentType.getMIXED(),
IPContentInformationType.getMIXED(), version.toString());
Expand All @@ -124,8 +133,8 @@ public Path build() throws SIPBuilderException, InterruptedException {
sip.addSubmitterAgent(submitterAgentName, submitterAgentId);
sip.setDescription("SIP created by commons-ip CLI");

if (checksum != null) {
sip.setChecksum(checksum.toString());
if (checksumAlgorithm != null) {
sip.setChecksum(checksumAlgorithm.toString());
}

if (overrideSchema) {
Expand Down Expand Up @@ -167,7 +176,8 @@ public Path build() throws SIPBuilderException, InterruptedException {
}

try {
return sip.build(buildPath);
WriteStrategy writeStrategy = SIPBuilderUtils.getWriteStrategy(writeStrategyEnum, buildPath);
return sip.build(writeStrategy);
} catch (IPException e) {
LOGGER.debug("Unable to create the E-ARK SIP", e);
throw new SIPBuilderException("Unable to create the E-ARK SIP");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
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.args.RepresentationGroup;
import org.roda_project.commons_ip2.cli.model.enums.WriteStrategyEnum;
import org.roda_project.commons_ip2.model.IPContentType;
import org.roda_project.commons_ip2.model.IPDescriptiveMetadata;
import org.roda_project.commons_ip2.model.IPFile;
import org.roda_project.commons_ip2.model.IPRepresentation;
import org.roda_project.commons_ip2.model.MetadataType;
import org.roda_project.commons_ip2.model.SIP;
import org.roda_project.commons_ip2.model.impl.eark.out.writers.factory.FolderWriteStrategyFactory;
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.utils.Utils;

/**
Expand Down Expand Up @@ -193,6 +197,22 @@ private static List<Path> getFilesInDirectory(final Path docPath) throws IOExcep
return filesInDirectory;
}

public static WriteStrategy getWriteStrategy(WriteStrategyEnum writeStrategyEnum, Path buildPath) {
switch (writeStrategyEnum) {
case ZIP -> {
ZipWriteStrategyFactory zipWriteStrategyFactory = new ZipWriteStrategyFactory();
return zipWriteStrategyFactory.create(buildPath);
}
case FOLDER -> {
FolderWriteStrategyFactory folderWriteStrategyFactory = new FolderWriteStrategyFactory();
return folderWriteStrategyFactory.create(buildPath);
}
default -> {
return null;
}
}
}

private SIPBuilderUtils() {
}
}
1 change: 1 addition & 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 @@ -112,6 +112,7 @@ public void setOverride() {
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 @@ -121,6 +121,8 @@ public final class IPConstants {
public static final String SCHEMA_EARK_SIP_RELATIVE_PATH_FROM_RESOURCES = METS_PATH_SEPARATOR + SCHEMAS + "2"
+ METS_PATH_SEPARATOR + SCHEMA_EARK_SIP_FILENAME;

public static final String SIP_FILE_EXTENSION = ".zip";

/** Private empty constructor */
private IPConstants() {
// do nothing
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/org/roda_project/commons_ip2/model/IPInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.roda_project.commons_ip2.model;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
Expand All @@ -20,6 +21,7 @@
import org.roda_project.commons_ip.utils.IPEnums.IPType;
import org.roda_project.commons_ip.utils.IPException;
import org.roda_project.commons_ip.utils.ZipEntryInfo;
import org.roda_project.commons_ip2.model.impl.eark.out.writers.strategy.WriteStrategy;

public interface IPInterface {

Expand Down Expand Up @@ -123,24 +125,19 @@ IPInterface addPreservationMetadataToRepresentation(String representationID, IPM

IPHeader getHeader();

/**
* @param destinationDirectory
* directory where the SIP will be placed into
* @throws InterruptedException
*/
Path build(Path destinationDirectory) throws IPException, InterruptedException;
Path build(WriteStrategy writeStrategy) throws IPException, InterruptedException;

Path build(Path destinationDirectory, boolean onlyManifest) throws IPException, InterruptedException;
Path build(WriteStrategy writeStrategy, boolean onlyManifest) throws IPException, InterruptedException;

Path build(Path destinationDirectory, String fileNameWithoutExtension) throws IPException, InterruptedException;
Path build(WriteStrategy writeStrategy, String fileNameWithoutExtension) throws IPException, InterruptedException;

Path build(Path destinationDirectory, String fileNameWithoutExtension, IPEnums.SipType sipType)
Path build(WriteStrategy writeStrategy, String fileNameWithoutExtension, IPEnums.SipType sipType)
throws IPException, InterruptedException;

Path build(Path destinationDirectory, String fileNameWithoutExtension, boolean onlyManifest)
Path build(WriteStrategy writeStrategy, String fileNameWithoutExtension, boolean onlyManifest)
throws IPException, InterruptedException;

Path build(Path destinationDirectory, String fileNameWithoutExtension, boolean onlyManifest, IPEnums.SipType sipType)
Path build(WriteStrategy writeStrategy, String fileNameWithoutExtension, boolean onlyManifest, IPEnums.SipType sipType)
throws IPException, InterruptedException;

static IPInterface parse(Path source) throws ParseException {
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/org/roda_project/commons_ip2/model/impl/AIPWrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.roda_project.commons_ip2.model.IPMetadata;
import org.roda_project.commons_ip2.model.IPRepresentation;
import org.roda_project.commons_ip2.model.ValidationReport;
import org.roda_project.commons_ip2.model.impl.eark.out.writers.strategy.WriteStrategy;

/**
* AIP decorator (wrapper).
Expand Down Expand Up @@ -326,38 +327,37 @@ public IPHeader getHeader() {
}

@Override
public Path build(final Path destinationDirectory) throws IPException, InterruptedException {
return aip.build(destinationDirectory);
public Path build(WriteStrategy writeStrategy) throws IPException, InterruptedException {
return aip.build(writeStrategy);
}

@Override
public Path build(final Path destinationDirectory, final boolean onlyManifest)
public Path build(WriteStrategy writeStrategy, final boolean onlyManifest)
throws IPException, InterruptedException {
return aip.build(destinationDirectory, onlyManifest);
return aip.build(writeStrategy, onlyManifest);
}

@Override
public Path build(final Path destinationDirectory, final String fileNameWithoutExtension)
public Path build(WriteStrategy writeStrategy, final String fileNameWithoutExtension)
throws IPException, InterruptedException {
return aip.build(destinationDirectory, fileNameWithoutExtension);
return aip.build(writeStrategy, fileNameWithoutExtension);
}

@Override
public Path build(Path destinationDirectory, String fileNameWithoutExtension, IPEnums.SipType sipType)
public Path build(WriteStrategy writeStrategy, String fileNameWithoutExtension, IPEnums.SipType sipType)
throws IPException, InterruptedException {
return aip.build(destinationDirectory, fileNameWithoutExtension);
return aip.build(writeStrategy, fileNameWithoutExtension);
}

@Override
public Path build(final Path destinationDirectory, final String fileNameWithoutExtension, final boolean onlyManifest)
public Path build(WriteStrategy writeStrategy, final String fileNameWithoutExtension, final boolean onlyManifest)
throws IPException, InterruptedException {
return aip.build(destinationDirectory, fileNameWithoutExtension, onlyManifest);
return aip.build(writeStrategy, fileNameWithoutExtension, onlyManifest);
}

@Override
public Path build(Path destinationDirectory, String fileNameWithoutExtension, boolean onlyManifest,
public Path build(WriteStrategy writeStrategy, String fileNameWithoutExtension, boolean onlyManifest,
IPEnums.SipType sipType) throws IPException, InterruptedException {
return aip.build(destinationDirectory, fileNameWithoutExtension, onlyManifest);
return aip.build(writeStrategy, fileNameWithoutExtension, onlyManifest);
}

}
Loading

0 comments on commit da1d34e

Please sign in to comment.