Skip to content

Commit 7f17fd0

Browse files
authored
Merge branch 'development' into othermdtype-fix
2 parents df9d7f9 + a0967a0 commit 7f17fd0

File tree

8 files changed

+253
-60
lines changed

8 files changed

+253
-60
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>org.roda-community</groupId>
77
<artifactId>commons-ip2</artifactId>
88
<packaging>jar</packaging>
9-
<version>2.9.0-SNAPSHOT</version>
9+
<version>2.10.0-SNAPSHOT</version>
1010
<name>Commons IP</name>
1111
<description>Library to validate and manipulate E-ARK Information Packages, so to create or process E-ARK SIP and AIP and also validate them against official specifications.</description>
1212
<url>https://keeps.github.io/commons-ip/</url>

src/main/java/org/roda_project/commons_ip2/model/impl/eark/EARKAIP.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private AIP parseEARKAIPFromPath(final Path aipPath, String version) throws Pars
104104

105105
metsUtils.processOtherMetadata(metsWrapper, aip, LOGGER, null, aip.getBasePath());
106106

107-
metsUtils.processPreservationMetadata(metsWrapper, aip, LOGGER, null, aip.getBasePath());
107+
metsUtils.processPreservationMetadata(metsWrapper, aip, null, aip.getBasePath());
108108

109109
metsUtils.processRepresentations(metsWrapper, aip, LOGGER);
110110

src/main/java/org/roda_project/commons_ip2/model/impl/eark/EARKSIP.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ private SIP parseEARKSIP(final Path source, final Path destinationDirectory) thr
123123
earkUtils.preProcessStructMap(metsWrapper, structMap);
124124
earkUtils.processDescriptiveMetadata(metsWrapper, sip, LOGGER, null, sip.getBasePath());
125125
earkUtils.processOtherMetadata(metsWrapper, sip, LOGGER, null, sip.getBasePath());
126-
earkUtils.processPreservationMetadata(metsWrapper, sip, LOGGER, null, sip.getBasePath());
127-
earkUtils.processTechnicalMetadata(metsWrapper, sip, LOGGER, null, sip.getBasePath());
128-
earkUtils.processSourceMetadata(metsWrapper, sip, LOGGER, null, sip.getBasePath());
129-
earkUtils.processRightsMetadata(metsWrapper, sip, LOGGER, null, sip.getBasePath());
126+
earkUtils.processPreservationMetadata(metsWrapper, sip, null, sip.getBasePath());
127+
earkUtils.processTechnicalMetadata(metsWrapper, sip, null, sip.getBasePath());
128+
earkUtils.processSourceMetadata(metsWrapper, sip, null, sip.getBasePath());
129+
earkUtils.processRightsMetadata(metsWrapper, sip, null, sip.getBasePath());
130130
earkUtils.processRepresentations(metsWrapper, sip, LOGGER);
131131
earkUtils.processSchemasMetadata(metsWrapper, sip, sip.getBasePath());
132132
earkUtils.processDocumentationMetadata(metsWrapper, sip, sip.getBasePath());

src/main/java/org/roda_project/commons_ip2/model/impl/eark/EARKUtils.java

Lines changed: 181 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.List;
1818
import java.util.Map;
1919
import java.util.Optional;
20+
import java.util.stream.Collectors;
21+
import java.util.stream.Stream;
2022

2123
import org.apache.commons.lang3.StringUtils;
2224
import org.roda_project.commons_ip.model.ParseException;
@@ -25,6 +27,7 @@
2527
import org.roda_project.commons_ip.utils.IPException;
2628
import org.roda_project.commons_ip.utils.ValidationConstants;
2729
import org.roda_project.commons_ip.utils.ZipEntryInfo;
30+
import org.roda_project.commons_ip2.mets_v1_12.beans.AmdSecType;
2831
import org.roda_project.commons_ip2.mets_v1_12.beans.DivType;
2932
import org.roda_project.commons_ip2.mets_v1_12.beans.DivType.Fptr;
3033
import org.roda_project.commons_ip2.mets_v1_12.beans.DivType.Mptr;
@@ -137,7 +140,7 @@ protected void addOtherMetadataToZipAndMETS(Map<String, ZipEntryInfo> zipEntries
137140
}
138141
}
139142
}
140-
143+
141144
protected void addTechnicalMetadataToZipAndMETS(Map<String, ZipEntryInfo> zipEntries, MetsWrapper metsWrapper,
142145
List<IPMetadata> technicalMetadata, String representationId) throws IPException, InterruptedException {
143146
if (technicalMetadata != null && !technicalMetadata.isEmpty()) {
@@ -152,8 +155,8 @@ protected void addTechnicalMetadataToZipAndMETS(Map<String, ZipEntryInfo> zipEnt
152155
MdRef mdRef = metsGenerator.addTechnicalMetadataToMETS(metsWrapper, tm, technicalMetadataPath);
153156

154157
if (representationId != null) {
155-
technicalMetadataPath = IPConstants.REPRESENTATIONS_FOLDER + representationId
156-
+ IPConstants.ZIP_PATH_SEPARATOR + technicalMetadataPath;
158+
technicalMetadataPath = IPConstants.REPRESENTATIONS_FOLDER + representationId + IPConstants.ZIP_PATH_SEPARATOR
159+
+ technicalMetadataPath;
157160
}
158161
ZIPUtils.addMdRefFileToZip(zipEntries, file.getPath(), technicalMetadataPath, mdRef);
159162
}
@@ -181,7 +184,7 @@ protected void addSourceMetadataToZipAndMETS(Map<String, ZipEntryInfo> zipEntrie
181184
}
182185
}
183186
}
184-
187+
185188
protected void addRightsMetadataToZipAndMETS(Map<String, ZipEntryInfo> zipEntries, MetsWrapper metsWrapper,
186189
List<IPMetadata> rightsMetadata, String representationId) throws IPException, InterruptedException {
187190
if (rightsMetadata != null && !rightsMetadata.isEmpty()) {
@@ -280,11 +283,11 @@ else if (!IPEnums.SipType.EARK2S.equals(sipType)) {
280283
// representation preservation metadata
281284
addPreservationMetadataToZipAndMETS(zipEntries, representationMETSWrapper,
282285
representation.getPreservationMetadata(), representationId);
283-
286+
284287
// representation technical metadata
285-
addTechnicalMetadataToZipAndMETS(zipEntries, representationMETSWrapper,
286-
representation.getTechnicalMetadata(), representationId);
287-
288+
addTechnicalMetadataToZipAndMETS(zipEntries, representationMETSWrapper, representation.getTechnicalMetadata(),
289+
representationId);
290+
288291
// representation source metadata
289292
addSourceMetadataToZipAndMETS(zipEntries, representationMETSWrapper,
290293
representation.getSourceMetadata(), representationId);
@@ -686,20 +689,16 @@ protected IPInterface processRepresentations(MetsWrapper metsWrapper, IPInterfac
686689
processDescriptiveMetadata(representationMetsWrapper, ip, logger, representation, representationBasePath);
687690

688691
// process preservation metadata
689-
processPreservationMetadata(representationMetsWrapper, ip, logger, representation,
690-
representationBasePath);
691-
692+
processPreservationMetadata(representationMetsWrapper, ip, representation, representationBasePath);
693+
692694
// process technical metadata
693-
processTechnicalMetadata(representationMetsWrapper, ip, logger, representation,
694-
representationBasePath);
695-
695+
processTechnicalMetadata(representationMetsWrapper, ip, representation, representationBasePath);
696+
696697
// process source metadata
697-
processSourceMetadata(representationMetsWrapper, ip, logger, representation,
698-
representationBasePath);
699-
698+
processSourceMetadata(representationMetsWrapper, ip, representation, representationBasePath);
699+
700700
// process rights metadata
701-
processRightsMetadata(representationMetsWrapper, ip, logger, representation,
702-
representationBasePath);
701+
processRightsMetadata(representationMetsWrapper, ip, representation, representationBasePath);
703702

704703
// process other metadata
705704
processOtherMetadata(representationMetsWrapper, ip, logger, representation, representationBasePath);
@@ -774,8 +773,55 @@ protected void preProcessStructMap(MetsWrapper metsWrapper, StructMapType struct
774773

775774
protected void processDescriptiveMetadata(MetsWrapper metsWrapper, IPInterface ip, Logger logger,
776775
IPRepresentation representation, Path basePath) throws IPException {
776+
String metadataType = IPConstants.DESCRIPTIVE;
777+
List<MdSecType> dmdSec = metsWrapper.getMets().getDmdSec();
778+
for (MdSecType mdSecType : dmdSec) {
779+
MdRef mdRef = mdSecType.getMdRef();
780+
if (mdRef != null) {
781+
String href = Utils.extractedRelativePathFromHref(mdRef);
782+
Path filePath = basePath.resolve(href);
783+
if (Files.exists(filePath)) {
784+
List<String> fileRelativeFolders = Utils
785+
.getFileRelativeFolders(basePath.resolve(IPConstants.METADATA).resolve(metadataType), filePath);
786+
787+
Optional<IPFileInterface> metadataFile = validateMetadataFile(ip, filePath, mdRef, fileRelativeFolders);
788+
if (metadataFile.isPresent()) {
789+
ValidationUtils.addInfo(ip.getValidationReport(),
790+
ValidationConstants.getMetadataFileFoundWithMatchingChecksumString(metadataType), ip.getBasePath(),
791+
filePath);
792+
793+
MetadataType dmdType = new MetadataType(mdRef.getMDTYPE().toUpperCase());
794+
String dmdVersion = null;
795+
try {
796+
dmdVersion = mdRef.getMDTYPEVERSION();
797+
if (StringUtils.isNotBlank(mdRef.getOTHERMDTYPE())) {
798+
dmdType.setOtherType(mdRef.getOTHERMDTYPE());
799+
}
800+
logger.debug("Metadata type valid: {}", dmdType);
801+
} catch (NullPointerException | IllegalArgumentException e) {
802+
// do nothing and use already defined values for metadataType &
803+
// metadataVersion
804+
logger.debug("Setting metadata type to {}", dmdType);
805+
ValidationUtils.addEntry(ip.getValidationReport(), ValidationConstants.UNKNOWN_DESCRIPTIVE_METADATA_TYPE,
806+
ValidationEntry.LEVEL.WARN, "Setting metadata type to " + dmdType, ip.getBasePath(), filePath);
807+
}
777808

778-
processMetadata(ip, logger, representation, metsWrapper.getMetadataDiv(), IPConstants.DESCRIPTIVE, basePath);
809+
IPDescriptiveMetadata descriptiveMetadata = new IPDescriptiveMetadata(mdRef.getID(), metadataFile.get(),
810+
dmdType, dmdVersion);
811+
descriptiveMetadata.setCreateDate(mdRef.getCREATED());
812+
if (representation == null) {
813+
ip.addDescriptiveMetadata(descriptiveMetadata);
814+
} else {
815+
representation.addDescriptiveMetadata(descriptiveMetadata);
816+
}
817+
}
818+
} else {
819+
ValidationUtils.addIssue(ip.getValidationReport(),
820+
ValidationConstants.getMetadataFileNotFoundString(metadataType), ValidationEntry.LEVEL.ERROR,
821+
ip.getBasePath(), filePath);
822+
}
823+
}
824+
}
779825
}
780826

781827
protected void processOtherMetadata(MetsWrapper metsWrapper, IPInterface ip, Logger logger,
@@ -784,28 +830,126 @@ protected void processOtherMetadata(MetsWrapper metsWrapper, IPInterface ip, Log
784830
processMetadata(ip, logger, representation, metsWrapper.getOtherMetadataDiv(), IPConstants.OTHER, basePath);
785831
}
786832

787-
protected void processPreservationMetadata(MetsWrapper metsWrapper, IPInterface ip, Logger logger,
788-
IPRepresentation representation, Path basePath) throws IPException {
833+
protected void processPreservationMetadata(MetsWrapper metsWrapper, IPInterface ip, IPRepresentation representation,
834+
Path basePath) throws IPException {
835+
String metadataType = IPConstants.PRESERVATION;
836+
for (AmdSecType amdSecType : metsWrapper.getMets().getAmdSec()) {
837+
for (MdSecType mdSecType : amdSecType.getDigiprovMD()) {
838+
MdRef mdRef = mdSecType.getMdRef();
789839

790-
processMetadata(ip, logger, representation, metsWrapper.getMetadataDiv(), IPConstants.PRESERVATION, basePath);
840+
processMdRef(mdRef, metadataType, ip, representation, basePath);
841+
}
842+
}
791843
}
792-
793-
protected void processTechnicalMetadata(MetsWrapper metsWrapper, IPInterface ip, Logger logger,
794-
IPRepresentation representation, Path basePath) throws IPException {
795844

796-
processMetadata(ip, logger, representation, metsWrapper.getMetadataDiv(), IPConstants.TECHNICAL, basePath);
845+
protected void processTechnicalMetadata(MetsWrapper metsWrapper, IPInterface ip, IPRepresentation representation,
846+
Path basePath) throws IPException {
847+
String metadataType = IPConstants.TECHNICAL;
848+
for (AmdSecType amdSecType : metsWrapper.getMets().getAmdSec()) {
849+
for (MdSecType mdSecType : amdSecType.getTechMD()) {
850+
MdRef mdRef = mdSecType.getMdRef();
851+
852+
processMdRef(mdRef, metadataType, ip, representation, basePath);
853+
}
854+
}
797855
}
798-
799-
protected void processSourceMetadata(MetsWrapper metsWrapper, IPInterface ip, Logger logger,
800-
IPRepresentation representation, Path basePath) throws IPException {
801856

802-
processMetadata(ip, logger, representation, metsWrapper.getMetadataDiv(), IPConstants.SOURCE, basePath);
857+
protected void processSourceMetadata(MetsWrapper metsWrapper, IPInterface ip, IPRepresentation representation,
858+
Path basePath) throws IPException {
859+
860+
String metadataType = IPConstants.SOURCE;
861+
for (AmdSecType amdSecType : metsWrapper.getMets().getAmdSec()) {
862+
for (MdSecType mdSecType : amdSecType.getSourceMD()) {
863+
MdRef mdRef = mdSecType.getMdRef();
864+
865+
processMdRef(mdRef, metadataType, ip, representation, basePath);
866+
}
867+
}
803868
}
804-
805-
protected void processRightsMetadata(MetsWrapper metsWrapper, IPInterface ip, Logger logger,
806-
IPRepresentation representation, Path basePath) throws IPException {
807869

808-
processMetadata(ip, logger, representation, metsWrapper.getMetadataDiv(), IPConstants.RIGHTS, basePath);
870+
protected void processRightsMetadata(MetsWrapper metsWrapper, IPInterface ip, IPRepresentation representation,
871+
Path basePath) throws IPException {
872+
String metadataType = IPConstants.RIGHTS;
873+
for (AmdSecType amdSecType : metsWrapper.getMets().getAmdSec()) {
874+
for (MdSecType mdSecType : amdSecType.getRightsMD()) {
875+
MdRef mdRef = mdSecType.getMdRef();
876+
877+
processMdRef(mdRef, metadataType, ip, representation, basePath);
878+
}
879+
}
880+
}
881+
882+
private void processMdRef(MdRef mdRef, String metadataType, IPInterface ip, IPRepresentation representation,
883+
Path basePath) throws IPException {
884+
if (mdRef != null) {
885+
String href = Utils.extractedRelativePathFromHref(mdRef);
886+
Path filePath = basePath.resolve(href);
887+
if (Files.exists(filePath)) {
888+
List<String> fileRelativeFolders = Utils
889+
.getFileRelativeFolders(basePath.resolve(IPConstants.METADATA).resolve(metadataType), filePath);
890+
891+
Optional<IPFileInterface> metadataFile = validateMetadataFile(ip, filePath, mdRef, fileRelativeFolders);
892+
if (metadataFile.isPresent()) {
893+
ValidationUtils.addInfo(ip.getValidationReport(),
894+
ValidationConstants.getMetadataFileFoundWithMatchingChecksumString(metadataType), ip.getBasePath(),
895+
filePath);
896+
IPMetadata ipMetadata = new IPMetadata(metadataFile.get());
897+
ipMetadata.setCreateDate(mdRef.getCREATED());
898+
ipMetadata.setMetadataType(MetadataType.MetadataTypeEnum.valueOf(mdRef.getMDTYPE()));
899+
ipMetadata.setId(mdRef.getID());
900+
addMetadata(ip, representation, ipMetadata, metadataType);
901+
}
902+
} else {
903+
ValidationUtils.addIssue(ip.getValidationReport(),
904+
ValidationConstants.getMetadataFileNotFoundString(metadataType), ValidationEntry.LEVEL.ERROR,
905+
ip.getBasePath(), filePath);
906+
}
907+
}
908+
}
909+
910+
private void addMetadata(IPInterface ip, IPRepresentation representation, IPMetadata metadata, String metadataType)
911+
throws IPException {
912+
if (representation == null) {
913+
switch (metadataType) {
914+
case IPConstants.PRESERVATION:
915+
ip.addPreservationMetadata(metadata);
916+
break;
917+
case IPConstants.RIGHTS:
918+
ip.addRightsMetadata(metadata);
919+
break;
920+
case IPConstants.TECHNICAL:
921+
ip.addTechnicalMetadata(metadata);
922+
break;
923+
case IPConstants.SOURCE:
924+
ip.addSourceMetadata(metadata);
925+
break;
926+
case IPConstants.OTHER:
927+
ip.addOtherMetadata(metadata);
928+
break;
929+
default:
930+
throw new IPException("Unknown metadata type: " + metadataType);
931+
}
932+
} else {
933+
switch (metadataType) {
934+
case IPConstants.PRESERVATION:
935+
representation.addPreservationMetadata(metadata);
936+
break;
937+
case IPConstants.RIGHTS:
938+
representation.addRightsMetadata(metadata);
939+
break;
940+
case IPConstants.TECHNICAL:
941+
representation.addTechnicalMetadata(metadata);
942+
break;
943+
case IPConstants.SOURCE:
944+
representation.addSourceMetadata(metadata);
945+
break;
946+
case IPConstants.OTHER:
947+
representation.addOtherMetadata(metadata);
948+
break;
949+
default:
950+
throw new IPException("Unknown metadata type: " + metadataType);
951+
}
952+
}
809953
}
810954

811955
protected void processMetadata(IPInterface ip, Logger logger, IPRepresentation representation, DivType div,
@@ -814,8 +958,8 @@ protected void processMetadata(IPInterface ip, Logger logger, IPRepresentation r
814958
List<Object> objects = null;
815959
if (IPConstants.DESCRIPTIVE.equals(metadataType) || IPConstants.OTHER.equals(metadataType)) {
816960
objects = div.getDMDID();
817-
} else if (IPConstants.PRESERVATION.equals(metadataType) || IPConstants.RIGHTS.equals(metadataType)
818-
|| IPConstants.TECHNICAL.equals(metadataType) || IPConstants.SOURCE.equals(metadataType)) {
961+
} else if (IPConstants.PRESERVATION.equals(metadataType) || IPConstants.RIGHTS.equals(metadataType)
962+
|| IPConstants.TECHNICAL.equals(metadataType) || IPConstants.SOURCE.equals(metadataType)) {
819963
objects = div.getADMID();
820964
}
821965

0 commit comments

Comments
 (0)