Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanLavrov committed Oct 7, 2024
1 parent cc6f6f0 commit b736f3d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ramls/settings/subjectSource.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"description": "label for the subject source",
"type": "string"
},
"code": {
"description": "label for the subject source code",
"type": "string"
},
"source": {
"type": "string",
"description": "label indicating where the subject source entry originates from, i.e. 'folio' or 'local'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,25 @@ public String apply(RuleExecutionContext context) {
}
},

SET_SUBJECT_SOURCE_ID_BY_CODE() {
@Override
public String apply(RuleExecutionContext context) {
List<SubjectSource> subjectSources = context.getMappingParameters().getSubjectSources();
String sourceCode = context.getSubFieldValue();

if (sourceCode == null || subjectSources == null) {
return StringUtils.EMPTY;
}

return subjectSources.stream()
.filter(subjectSource -> subjectSource.getCode() != null
&& subjectSource.getCode().equalsIgnoreCase(sourceCode)) // todo: should we compare ignoring case, should we trim?
.findFirst()
.map(SubjectSource::getId)
.orElse(StringUtils.EMPTY);
}
},

SET_SUBJECT_TYPE_ID() {
private static final String NAME_PARAMETER = "name";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -69,6 +72,7 @@ public class InstanceMappingTest {
private static final String BIB_WITH_720_FIELDS = "src/test/resources/org/folio/processing/mapping/instance/720_fields_samples.mrc";
private static final String BIB_WITH_FIELDS_FOR_ALTERNATIVE_MAPPING = "src/test/resources/org/folio/processing/mapping/instance/fields_for_alternative_mapping_samples.mrc";
private static final String BIB_WITH_FIELDS_FOR_ALTERNATIVE_MAPPING_WITH_PUNCTUATIONS = "src/test/resources/org/folio/processing/mapping/instance/fields_for_alternative_mapping_samples_with_punctuations.mrc";
public static final String BIB_WITH_SUBJECT_SOURCES_CODE_IN_2_SUBFIELD = "src/test/resources/org/folio/processing/mapping/instance/subject_source_codes_in_2_subfield.mrc";
private static final String CLASSIFICATIONS_TEST = "src/test/resources/org/folio/processing/mapping/instance/classificationsTest.mrc";
private static final String INSTANCES_CLASSIFICATIONS_PATH = "src/test/resources/org/folio/processing/mapping/instance/classificationsTestInstance.json";
private static final String DEFAULT_MAPPING_RULES_PATH = "src/test/resources/org/folio/processing/mapping/instance/rules.json";
Expand Down Expand Up @@ -594,6 +598,58 @@ public void testMarcToInstanceWithRepeatableSubjectsButWithoutIndicators() throw
});
}

@Test
public void testMarcToSubjectSourceMappingByCodeFromSubfield() throws IOException {
MarcReader reader = new MarcStreamReader(new ByteArrayInputStream(
TestUtil.readFileFromPath(BIB_WITH_SUBJECT_SOURCES_CODE_IN_2_SUBFIELD).getBytes(StandardCharsets.UTF_8)));

JsonObject mappingRules = new JsonObject(TestUtil.readFileFromPath(DEFAULT_MAPPING_RULES_PATH));
List<SubjectSource> subjectSources = new ObjectMapper()
.readValue(new File(DEFAULT_SUBJECT_SOURCES_PATH), new TypeReference<>() {});

final String FIRST_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d40";
final String SECOND_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d41";
final String THIRD_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d42";
final String FOURTH_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d45";
final String FIFTH_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d46";

Map<String, String> subjectValueToSourceId = Map.of(
"Subject heading 600", FIRST_LIBRARY_SOURCE_ID,
"Subject heading 610", SECOND_LIBRARY_SOURCE_ID,
"Subject heading 611", THIRD_LIBRARY_SOURCE_ID,
"Subject heading 630", FOURTH_LIBRARY_SOURCE_ID,
"Subject heading 647", FIFTH_LIBRARY_SOURCE_ID,
"Subject heading 648", FIRST_LIBRARY_SOURCE_ID,
"Subject heading 650", SECOND_LIBRARY_SOURCE_ID,
"Subject heading 651", THIRD_LIBRARY_SOURCE_ID,
"Subject heading 655", FOURTH_LIBRARY_SOURCE_ID
);

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
while (reader.hasNext()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
MarcJsonWriter writer = new MarcJsonWriter(os);
Record record = reader.next();
writer.write(record);
JsonObject marc = new JsonObject(os.toString());
Instance instance = mapper.mapRecord(marc, new MappingParameters().withSubjectSources(subjectSources), mappingRules);

assertNotNull(instance.getSubjects());
assertEquals(9, instance.getSubjects().size());
instance.getSubjects().forEach(subject -> {
assertNotNull(subject.getValue());
assertEquals(subjectValueToSourceId.get(subject.getValue()), subject.getSourceId());
});



// Validator validator = factory.getValidator();
// Set<ConstraintViolation<Instance>> violations = validator.validate(instance);
// assertTrue(violations.isEmpty());

}
}

@Test
public void testMarc720ToInstanceContributors() throws IOException {
MarcReader reader = new MarcStreamReader(new ByteArrayInputStream(TestUtil.readFileFromPath(BIB_WITH_720_FIELDS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
[ {
[
{
"id" : "e894d0dc-621d-4b1d-98f6-6f7120eb0d40",
"name" : "Library of Congress Subject Headings",
"code" : "lcsh",
"source" : "folio"
}, {
"id" : "e894d0dc-621d-4b1d-98f6-6f7120eb0d41",
"name" : "Library of Congress Children's and Young Adults' Subject Headings",
"code" : "cyac",
"source" : "folio"
}, {
"id" : "e894d0dc-621d-4b1d-98f6-6f7120eb0d42",
"name" : "Medical Subject Headings",
"code" : "mesh",
"source" : "folio"
}, {
"id" : "e894d0dc-621d-4b1d-98f6-6f7120eb0d43",
Expand All @@ -21,13 +25,12 @@
}, {
"id" : "e894d0dc-621d-4b1d-98f6-6f7120eb0d45",
"name" : "Canadian Subject Headings",
"code" : "cash",
"source" : "folio"
}, {
"id" : "e894d0dc-621d-4b1d-98f6-6f7120eb0d46",
"name" : "Répertoire de vedettes-matière",
"code" : "rvm",
"source" : "folio"
}, {
"id" : "e894d0dc-621d-4b1d-98f6-6f7120eb0d47",
"name" : "Source specified in subfield $2",
"source" : "folio"
} ]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
01001nam a2200301 4500001000800000003000400008005001700012008004100029010001600070020001500086035001900101035001800120040001300138041000800151050002600159082001400185100001800199245010000217250002300317260005600340300003000396350001100426500003500437504003200472600003200504600008000536655008300616in23605ICU19971010231800.0741204r1975 nyu b 00000 eng u a 74011027 a0151238154 a(ICU)BID297516 a(OCoLC)948019 aDLCcDLC0 aeng0 aPT2613.R338bZ78 1975 a833/.9/140 aReddick, John14aThe 'Danzig trilogy' of Günter Grass;ba study of The tin drum, Cat and mouse, and Dog years. a[1st American ed.]0 aNew York,bHarcourt Brace Jovanovichc[1975, c1974] aviii, 289, [1] p.c22 cm. a$13.50 a"A Helen and Kurt Wolff book." aBibliography: p. 287-[290].17aGrass, Günter,d1927-201517aGrass, Günter,d1927-2015.2fast0http://id.worldcat.org/fast/fst00057296 7aCriticism, interpretation, etc.2fast0http://id.worldcat.org/fast/fst01411635

0 comments on commit b736f3d

Please sign in to comment.