17
17
import java .util .List ;
18
18
import java .util .Map ;
19
19
import java .util .Optional ;
20
+ import java .util .stream .Collectors ;
21
+ import java .util .stream .Stream ;
20
22
21
23
import org .apache .commons .lang3 .StringUtils ;
22
24
import org .roda_project .commons_ip .model .ParseException ;
25
27
import org .roda_project .commons_ip .utils .IPException ;
26
28
import org .roda_project .commons_ip .utils .ValidationConstants ;
27
29
import org .roda_project .commons_ip .utils .ZipEntryInfo ;
30
+ import org .roda_project .commons_ip2 .mets_v1_12 .beans .AmdSecType ;
28
31
import org .roda_project .commons_ip2 .mets_v1_12 .beans .DivType ;
29
32
import org .roda_project .commons_ip2 .mets_v1_12 .beans .DivType .Fptr ;
30
33
import org .roda_project .commons_ip2 .mets_v1_12 .beans .DivType .Mptr ;
@@ -137,7 +140,7 @@ protected void addOtherMetadataToZipAndMETS(Map<String, ZipEntryInfo> zipEntries
137
140
}
138
141
}
139
142
}
140
-
143
+
141
144
protected void addTechnicalMetadataToZipAndMETS (Map <String , ZipEntryInfo > zipEntries , MetsWrapper metsWrapper ,
142
145
List <IPMetadata > technicalMetadata , String representationId ) throws IPException , InterruptedException {
143
146
if (technicalMetadata != null && !technicalMetadata .isEmpty ()) {
@@ -152,8 +155,8 @@ protected void addTechnicalMetadataToZipAndMETS(Map<String, ZipEntryInfo> zipEnt
152
155
MdRef mdRef = metsGenerator .addTechnicalMetadataToMETS (metsWrapper , tm , technicalMetadataPath );
153
156
154
157
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 ;
157
160
}
158
161
ZIPUtils .addMdRefFileToZip (zipEntries , file .getPath (), technicalMetadataPath , mdRef );
159
162
}
@@ -181,7 +184,7 @@ protected void addSourceMetadataToZipAndMETS(Map<String, ZipEntryInfo> zipEntrie
181
184
}
182
185
}
183
186
}
184
-
187
+
185
188
protected void addRightsMetadataToZipAndMETS (Map <String , ZipEntryInfo > zipEntries , MetsWrapper metsWrapper ,
186
189
List <IPMetadata > rightsMetadata , String representationId ) throws IPException , InterruptedException {
187
190
if (rightsMetadata != null && !rightsMetadata .isEmpty ()) {
@@ -280,11 +283,11 @@ else if (!IPEnums.SipType.EARK2S.equals(sipType)) {
280
283
// representation preservation metadata
281
284
addPreservationMetadataToZipAndMETS (zipEntries , representationMETSWrapper ,
282
285
representation .getPreservationMetadata (), representationId );
283
-
286
+
284
287
// representation technical metadata
285
- addTechnicalMetadataToZipAndMETS (zipEntries , representationMETSWrapper ,
286
- representation . getTechnicalMetadata (), representationId );
287
-
288
+ addTechnicalMetadataToZipAndMETS (zipEntries , representationMETSWrapper , representation . getTechnicalMetadata (),
289
+ representationId );
290
+
288
291
// representation source metadata
289
292
addSourceMetadataToZipAndMETS (zipEntries , representationMETSWrapper ,
290
293
representation .getSourceMetadata (), representationId );
@@ -686,20 +689,16 @@ protected IPInterface processRepresentations(MetsWrapper metsWrapper, IPInterfac
686
689
processDescriptiveMetadata (representationMetsWrapper , ip , logger , representation , representationBasePath );
687
690
688
691
// process preservation metadata
689
- processPreservationMetadata (representationMetsWrapper , ip , logger , representation ,
690
- representationBasePath );
691
-
692
+ processPreservationMetadata (representationMetsWrapper , ip , representation , representationBasePath );
693
+
692
694
// process technical metadata
693
- processTechnicalMetadata (representationMetsWrapper , ip , logger , representation ,
694
- representationBasePath );
695
-
695
+ processTechnicalMetadata (representationMetsWrapper , ip , representation , representationBasePath );
696
+
696
697
// process source metadata
697
- processSourceMetadata (representationMetsWrapper , ip , logger , representation ,
698
- representationBasePath );
699
-
698
+ processSourceMetadata (representationMetsWrapper , ip , representation , representationBasePath );
699
+
700
700
// process rights metadata
701
- processRightsMetadata (representationMetsWrapper , ip , logger , representation ,
702
- representationBasePath );
701
+ processRightsMetadata (representationMetsWrapper , ip , representation , representationBasePath );
703
702
704
703
// process other metadata
705
704
processOtherMetadata (representationMetsWrapper , ip , logger , representation , representationBasePath );
@@ -774,8 +773,55 @@ protected void preProcessStructMap(MetsWrapper metsWrapper, StructMapType struct
774
773
775
774
protected void processDescriptiveMetadata (MetsWrapper metsWrapper , IPInterface ip , Logger logger ,
776
775
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
+ }
777
808
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
+ }
779
825
}
780
826
781
827
protected void processOtherMetadata (MetsWrapper metsWrapper , IPInterface ip , Logger logger ,
@@ -784,28 +830,126 @@ protected void processOtherMetadata(MetsWrapper metsWrapper, IPInterface ip, Log
784
830
processMetadata (ip , logger , representation , metsWrapper .getOtherMetadataDiv (), IPConstants .OTHER , basePath );
785
831
}
786
832
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 ();
789
839
790
- processMetadata (ip , logger , representation , metsWrapper .getMetadataDiv (), IPConstants .PRESERVATION , basePath );
840
+ processMdRef (mdRef , metadataType , ip , representation , basePath );
841
+ }
842
+ }
791
843
}
792
-
793
- protected void processTechnicalMetadata (MetsWrapper metsWrapper , IPInterface ip , Logger logger ,
794
- IPRepresentation representation , Path basePath ) throws IPException {
795
844
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
+ }
797
855
}
798
-
799
- protected void processSourceMetadata (MetsWrapper metsWrapper , IPInterface ip , Logger logger ,
800
- IPRepresentation representation , Path basePath ) throws IPException {
801
856
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
+ }
803
868
}
804
-
805
- protected void processRightsMetadata (MetsWrapper metsWrapper , IPInterface ip , Logger logger ,
806
- IPRepresentation representation , Path basePath ) throws IPException {
807
869
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
+ }
809
953
}
810
954
811
955
protected void processMetadata (IPInterface ip , Logger logger , IPRepresentation representation , DivType div ,
@@ -814,8 +958,8 @@ protected void processMetadata(IPInterface ip, Logger logger, IPRepresentation r
814
958
List <Object > objects = null ;
815
959
if (IPConstants .DESCRIPTIVE .equals (metadataType ) || IPConstants .OTHER .equals (metadataType )) {
816
960
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 )) {
819
963
objects = div .getADMID ();
820
964
}
821
965
0 commit comments