|
30 | 30 | import gov.loc.repository.bagit.exceptions.conformance.BagitVersionIsNotAcceptableException; |
31 | 31 | import gov.loc.repository.bagit.exceptions.conformance.FetchFileNotAllowedException; |
32 | 32 | import gov.loc.repository.bagit.exceptions.conformance.MetatdataValueIsNotAcceptableException; |
| 33 | +import gov.loc.repository.bagit.exceptions.conformance.MetatdataValueIsNotRepeatableException; |
33 | 34 | import gov.loc.repository.bagit.exceptions.conformance.RequiredManifestNotPresentException; |
34 | 35 | import gov.loc.repository.bagit.exceptions.conformance.RequiredMetadataFieldNotPresentException; |
35 | 36 | import gov.loc.repository.bagit.exceptions.conformance.RequiredTagFileNotPresentException; |
@@ -59,14 +60,15 @@ private BagProfileChecker(){ |
59 | 60 | * |
60 | 61 | * @throws FetchFileNotAllowedException if there is a fetch file when the profile prohibits it |
61 | 62 | * @throws MetatdataValueIsNotAcceptableException if a metadata value is not in the list of acceptable values |
| 63 | + * @throws MetatdataValueIsNotRepeatableException if a metadata value shows up more than once when not repeatable |
62 | 64 | * @throws RequiredMetadataFieldNotPresentException if a metadata field is not present but it should be |
63 | 65 | * @throws RequiredManifestNotPresentException if a payload or tag manifest type is not present but should be |
64 | 66 | * @throws BagitVersionIsNotAcceptableException if the version of the bag is not in the list of acceptable versions |
65 | 67 | * @throws RequiredTagFileNotPresentException if a tag file is not present but should be |
66 | 68 | */ |
67 | 69 | public static void bagConformsToProfile(final InputStream jsonProfile, final Bag bag) throws JsonParseException, JsonMappingException, |
68 | 70 | IOException, FetchFileNotAllowedException, RequiredMetadataFieldNotPresentException, MetatdataValueIsNotAcceptableException, |
69 | | - RequiredManifestNotPresentException, BagitVersionIsNotAcceptableException, RequiredTagFileNotPresentException{ |
| 71 | + RequiredManifestNotPresentException, BagitVersionIsNotAcceptableException, RequiredTagFileNotPresentException, MetatdataValueIsNotRepeatableException{ |
70 | 72 |
|
71 | 73 | final BagitProfile profile = parseBagitProfile(jsonProfile); |
72 | 74 | checkFetch(bag.getRootDir(), profile.isFetchFileAllowed(), bag.getItemsToFetch()); |
@@ -101,30 +103,48 @@ private static void checkFetch(final Path rootDir, final boolean allowFetchFile, |
101 | 103 | } |
102 | 104 |
|
103 | 105 | private static void checkMetadata(final Metadata bagMetadata, final Map<String, BagInfoRequirement> bagInfoEntryRequirements) |
104 | | - throws RequiredMetadataFieldNotPresentException, MetatdataValueIsNotAcceptableException{ |
| 106 | + throws RequiredMetadataFieldNotPresentException, MetatdataValueIsNotAcceptableException, MetatdataValueIsNotRepeatableException{ |
105 | 107 |
|
106 | 108 | for(final Entry<String, BagInfoRequirement> bagInfoEntryRequirement : bagInfoEntryRequirements.entrySet()){ |
107 | 109 | final boolean metadataContainsKey = bagMetadata.contains(bagInfoEntryRequirement.getKey()); |
108 | 110 |
|
109 | | - logger.debug(messages.getString("checking_metadata_entry_required"), bagInfoEntryRequirement.getKey()); |
110 | | - //is it required and not there? |
111 | | - if(bagInfoEntryRequirement.getValue().isRequired() && !metadataContainsKey){ |
112 | | - throw new RequiredMetadataFieldNotPresentException(messages.getString("required_metadata_field_not_present_error"), bagInfoEntryRequirement.getKey()); |
113 | | - } |
| 111 | + checkIfMetadataEntryIsRequired(bagInfoEntryRequirement, metadataContainsKey); |
| 112 | + |
| 113 | + checkForAcceptableValues(bagMetadata, bagInfoEntryRequirement); |
114 | 114 |
|
115 | | - //a size of zero implies that all values are acceptable |
116 | | - if(!bagInfoEntryRequirement.getValue().getAcceptableValues().isEmpty()){ |
117 | | - logger.debug(messages.getString("check_values_acceptable"), bagInfoEntryRequirement.getKey()); |
118 | | - for(final String metadataValue : bagMetadata.get(bagInfoEntryRequirement.getKey())){ |
119 | | - if(!bagInfoEntryRequirement.getValue().getAcceptableValues().contains(metadataValue)){ |
120 | | - throw new MetatdataValueIsNotAcceptableException(messages.getString("metadata_value_not_acceptable_error"), |
121 | | - bagInfoEntryRequirement.getKey(), bagInfoEntryRequirement.getValue().getAcceptableValues(), metadataValue); |
122 | | - } |
| 115 | + checkForNoneRepeatableMetadata(bagMetadata, bagInfoEntryRequirement, metadataContainsKey); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static void checkIfMetadataEntryIsRequired(final Entry<String, BagInfoRequirement> bagInfoEntryRequirement, final boolean metadataContainsKey) throws RequiredMetadataFieldNotPresentException{ |
| 120 | + logger.debug(messages.getString("checking_metadata_entry_required"), bagInfoEntryRequirement.getKey()); |
| 121 | + //is it required and not there? |
| 122 | + if(bagInfoEntryRequirement.getValue().isRequired() && !metadataContainsKey){ |
| 123 | + throw new RequiredMetadataFieldNotPresentException(messages.getString("required_metadata_field_not_present_error"), bagInfoEntryRequirement.getKey()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private static void checkForAcceptableValues(final Metadata bagMetadata, final Entry<String, BagInfoRequirement> bagInfoEntryRequirement) throws MetatdataValueIsNotAcceptableException{ |
| 128 | + //a size of zero implies that all values are acceptable |
| 129 | + if(!bagInfoEntryRequirement.getValue().getAcceptableValues().isEmpty()){ |
| 130 | + logger.debug(messages.getString("check_values_acceptable"), bagInfoEntryRequirement.getKey()); |
| 131 | + for(final String metadataValue : bagMetadata.get(bagInfoEntryRequirement.getKey())){ |
| 132 | + if(!bagInfoEntryRequirement.getValue().getAcceptableValues().contains(metadataValue)){ |
| 133 | + throw new MetatdataValueIsNotAcceptableException(messages.getString("metadata_value_not_acceptable_error"), |
| 134 | + bagInfoEntryRequirement.getKey(), bagInfoEntryRequirement.getValue().getAcceptableValues(), metadataValue); |
123 | 135 | } |
124 | 136 | } |
125 | 137 | } |
126 | 138 | } |
127 | 139 |
|
| 140 | + private static void checkForNoneRepeatableMetadata(final Metadata bagMetadata, final Entry<String, BagInfoRequirement> bagInfoEntryRequirement, final boolean metadataContainsKey) throws MetatdataValueIsNotRepeatableException{ |
| 141 | + //if it is none repeatable, but shows up multiple times |
| 142 | + if(!bagInfoEntryRequirement.getValue().isRepeatable() && metadataContainsKey |
| 143 | + && bagMetadata.get(bagInfoEntryRequirement.getKey()).size() > 1){ |
| 144 | + throw new MetatdataValueIsNotRepeatableException(messages.getString("metadata_value_not_repeatable_error"), bagInfoEntryRequirement.getKey()); |
| 145 | + } |
| 146 | + } |
| 147 | + |
128 | 148 | @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") |
129 | 149 | private static void requiredManifestsExist(final Set<Manifest> manifests, final List<String> requiredManifestTypes, final boolean isPayloadManifest) throws RequiredManifestNotPresentException{ |
130 | 150 | final Set<String> manifestTypesPresent = new HashSet<>(); |
|
0 commit comments