-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(specification-subfields-validation) implement "Missing Subfield"…
… Rule Validation
- Loading branch information
1 parent
15e9c86
commit 694c3b1
Showing
10 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...ain/java/org/folio/rspec/validation/validator/marc/impl/MissingSubfieldRuleValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.folio.rspec.validation.validator.marc.impl; | ||
|
||
import java.util.List; | ||
import org.folio.rspec.domain.dto.DefinitionType; | ||
import org.folio.rspec.domain.dto.SeverityType; | ||
import org.folio.rspec.domain.dto.SpecificationFieldDto; | ||
import org.folio.rspec.domain.dto.SubfieldDto; | ||
import org.folio.rspec.domain.dto.ValidationError; | ||
import org.folio.rspec.i18n.TranslationProvider; | ||
import org.folio.rspec.utils.SpecificationUtils; | ||
import org.folio.rspec.validation.validator.SpecificationRuleCode; | ||
import org.folio.rspec.validation.validator.SpecificationRuleValidator; | ||
import org.folio.rspec.validation.validator.marc.model.MarcRuleCode; | ||
import org.folio.rspec.validation.validator.marc.model.MarcSubfield; | ||
import org.folio.rspec.validation.validator.marc.model.Reference; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
public class MissingSubfieldRuleValidator | ||
implements SpecificationRuleValidator<List<MarcSubfield>, SpecificationFieldDto> { | ||
|
||
private static final String CODE_KEY = "code"; | ||
|
||
private final TranslationProvider translationProvider; | ||
|
||
MissingSubfieldRuleValidator(TranslationProvider translationProvider) { | ||
this.translationProvider = translationProvider; | ||
} | ||
|
||
@Override | ||
public List<ValidationError> validate(List<MarcSubfield> subfields, SpecificationFieldDto specification) { | ||
var requiredSubFields = SpecificationUtils.requiredSubfields(specification.getSubfields()); | ||
|
||
return requiredSubFields.keySet().stream() | ||
.filter(subFieldCode -> isMissing(subfields, subFieldCode)) | ||
.map(subFieldCode -> buildError(specification.getTag(), requiredSubFields.get(subFieldCode))) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public SpecificationRuleCode supportedRule() { | ||
return MarcRuleCode.MISSING_SUBFIELD; | ||
} | ||
|
||
@Override | ||
public DefinitionType definitionType() { | ||
return DefinitionType.SUBFIELD; | ||
} | ||
|
||
@Override | ||
public SeverityType severity() { | ||
return SeverityType.ERROR; | ||
} | ||
|
||
private ValidationError buildError(String tag, SubfieldDto definition) { | ||
var message = translationProvider.format(ruleCode(), CODE_KEY); | ||
return ValidationError.builder() | ||
.path(Reference.forSubfield(Reference.forTag(tag), definition.getCode().charAt(0)).toString()) | ||
.definitionType(definitionType()) | ||
.definitionId(definition.getId()) | ||
.severity(severity()) | ||
.ruleCode(ruleCode()) | ||
.message(message) | ||
.build(); | ||
} | ||
|
||
private boolean isMissing(List<MarcSubfield> marcSubfields, Character subFieldCode) { | ||
return CollectionUtils.isEmpty(marcSubfields) || marcSubfields.stream() | ||
.noneMatch(subfield -> isSubfieldEquals(subfield, subFieldCode)); | ||
} | ||
|
||
private boolean isSubfieldEquals(MarcSubfield subfield, Character subFieldCode) { | ||
return subfield.reference() != null | ||
&& subfield.reference().getSubfield() != null | ||
&& subfield.reference().getSubfield().equals(subFieldCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
mod-record-specifications-validator/src/test/resources/testdata/marc-subfield-record.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{ | ||
"fields": [ | ||
{ | ||
"010": { | ||
"ind1": "#", | ||
"ind2": " ", | ||
"subfields": [ | ||
{ | ||
"a": "LC control number" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"035": { | ||
"ind1": "#", | ||
"ind2": " ", | ||
"subfields": [ | ||
{ | ||
"a": "System control number" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"047": { | ||
"ind1": "#", | ||
"ind2": " ", | ||
"subfields": [ | ||
{ | ||
"a": "Form of musical composition code" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"100": { | ||
"ind1": "#", | ||
"ind2": " ", | ||
"subfields": [ | ||
{ | ||
"a": "Personal name" | ||
}, | ||
{ | ||
"d": "1756-1791." | ||
}, | ||
{ | ||
"0": "12345" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"245": { | ||
"ind1": "#", | ||
"ind2": " ", | ||
"subfields": [ | ||
{ | ||
"a": "Title" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"650": { | ||
"ind1": "#", | ||
"ind2": " ", | ||
"subfields": [ | ||
{ | ||
"a": "Topical term" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"leader": "01750ccm a2200421 4500" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters