Skip to content

Commit

Permalink
The JSON representation of a subfield is always a string #348
Browse files Browse the repository at this point in the history
  • Loading branch information
pkiraly committed Nov 7, 2023
1 parent ea9e2a5 commit c807b9e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,13 @@ public String asJson() {
for (DataField field : datafields) {
if (field != null) {
Map<String, Object> fieldMap = new LinkedHashMap<>();
fieldMap.put("ind1", field.getInd1());
fieldMap.put("ind2", field.getInd2());

Map<String, String> subfields = new LinkedHashMap<>();
for (MarcSubfield subfield : field.getSubfields()) {
subfields.put(subfield.getCode(), subfield.getValue());
if (!schemaType.equals(SchemaType.PICA)) {
fieldMap.put("ind1", field.getInd1());
fieldMap.put("ind2", field.getInd2());
}
fieldMap.put("subfields", subfields);

fieldMap.put("subfields", exportSubfieldsToJson(field));

String tag = field.getOccurrence() != null
? field.getTag() + "/" + field.getOccurrence()
Expand All @@ -458,6 +457,24 @@ public String asJson() {
return json;
}

private static Map<String, Object> exportSubfieldsToJson(DataField field) {
Map<String, Object> subfields = new LinkedHashMap<>();
for (MarcSubfield subfield : field.getSubfields()) {
if (!subfields.containsKey(subfield.getCode()))
subfields.put(subfield.getCode(), subfield.getValue());
else {
if (subfields.get(subfield.getCode()) instanceof String) {
String storedValue = (String) subfields.get(subfield.getCode());
List<String> list = new ArrayList<>();
list.add(storedValue);
subfields.put(subfield.getCode(), list);
}
((List)subfields.get(subfield.getCode())).add(subfield.getValue());
}
}
return subfields;
}

public boolean isIgnorableField(String tag, IgnorableFields ignorableFields) {
if (ignorableFields == null)
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package de.gwdg.metadataqa.marc.utils.pica.reader;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.gwdg.metadataqa.api.util.FileUtils;
import de.gwdg.metadataqa.marc.MarcFactory;
import de.gwdg.metadataqa.marc.cli.CliTestUtils;
import de.gwdg.metadataqa.marc.dao.record.BibliographicRecord;
import de.gwdg.metadataqa.marc.utils.pica.PicaSchemaManager;
import de.gwdg.metadataqa.marc.utils.pica.PicaSchemaReader;
import org.junit.Test;
import org.marc4j.marc.DataField;
import org.marc4j.marc.Record;
import org.marc4j.marc.VariableField;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class PicaNormalizedReaderTest {

Expand Down Expand Up @@ -55,4 +70,61 @@ public void lineWith0xC285() throws IOException, URISyntaxException {
String key = new String(new byte[]{-62, -123}, StandardCharsets.UTF_8);
System.err.println(key);
}

@Test
public void test045T_mar4j() throws IOException, URISyntaxException {
PicaNormalizedReader reader = new PicaNormalizedReader(FileUtils.getPath("pica/045T.pica").toString());
Record record = null;
if (reader.hasNext())
record = reader.next();
assertEquals("010705201", record.getControlNumber());
for (DataField dataField : record.getDataFields()) {
if (dataField.getTag().equals("045T")) {
assertEquals(2, dataField.getSubfields('k').size());
assertEquals("Psychologie", dataField.getSubfields('k').get(0).getData());
assertEquals("Sozialpsychologie", dataField.getSubfields('k').get(1).getData());
}
}
}

@Test
public void test045T_picaRecord() throws IOException, URISyntaxException {
PicaSchemaManager schema = PicaSchemaReader.createSchema(CliTestUtils.getTestResource("pica/schema/k10plus.json"));
PicaNormalizedReader reader = new PicaNormalizedReader(FileUtils.getPath("pica/045T.pica").toString());
Record record = null;
if (reader.hasNext())
record = reader.next();
BibliographicRecord bibRecord = MarcFactory.createPicaFromMarc4j(record, schema);

assertEquals("010705201", bibRecord.getId());
List<de.gwdg.metadataqa.marc.dao.DataField> tags = bibRecord.getDatafield("045T");
assertEquals(1, tags.size());
de.gwdg.metadataqa.marc.dao.DataField tag = tags.get(0);
assertEquals(2, tag.getSubfield("k").size());
assertEquals("Psychologie", tag.getSubfield("k").get(0).getValue());
assertEquals("Sozialpsychologie", tag.getSubfield("k").get(1).getValue());
}

@Test
public void test045T_asJson() throws IOException, URISyntaxException {
PicaSchemaManager schema = PicaSchemaReader.createSchema(CliTestUtils.getTestResource("pica/schema/k10plus.json"));
PicaNormalizedReader reader = new PicaNormalizedReader(FileUtils.getPath("pica/045T.pica").toString());
Record record = null;
if (reader.hasNext())
record = reader.next();
BibliographicRecord bibRecord = MarcFactory.createPicaFromMarc4j(record, schema);

String json = bibRecord.asJson();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> configuration = mapper.readValue(json, new TypeReference<>(){});
assertNotNull(configuration.get("045T"));
assertTrue(configuration.get("045T") instanceof ArrayList);
assertEquals(1, ((List) configuration.get("045T")).size());
assertTrue(((List)configuration.get("045T")).get(0) instanceof Map);
Map<String, Object> tag = (Map) ((List) configuration.get("045T")).get(0);
Map<String, Object> subfields = (Map) tag.get("subfields");
assertTrue(subfields.get("k") instanceof List);
assertEquals("Psychologie", ((List<?>) subfields.get("k")).get(0));
assertEquals("Sozialpsychologie", ((List<?>) subfields.get("k")).get(1));
}
}
1 change: 1 addition & 0 deletions src/test/resources/pica/045T.pica
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
001@ 020,22,70,90,2003,2005,2007001A 02000:08-03-85001B 0BSZ:27-04-22t09:51:23.000001U 0utf8002@ 0Aau002C aTextbtxt002D aohne Hilfsmittel zu benutzenbn002E aBandbnc003@ 0010705201003O aOCoLC010723592v2012-12-31003S 0028178874003T aOCoLC010723592004A 00253350301006L iBVB0803653336x006Y 00084I137162007G iGBV0010705201009@ bBlocktest010@ aeng011@ a1984n1984017L aGBV_ILN_20017L aISIL_DE-84017L aSYSFLAG_1017L aGBV_KXP019@ aXD-US021A aRoutine complicationsdtroubles with talk between doctors and patientshCandace West028A dCandaceaWestBVerfasserIn4aut033A pBloomington, Ind.nIndiana Univ. Press034D aXIV, 199 S034M a199 S044K 9104544988VTsv17gnd/4003157-33208850414aArzt044K 9104544767VTsv17gnd/4044903-83209062827aPatient044K 9106263722VTsv17gnd/4031887-4320899467XaKommunikationsstörung045F a610.69/6AOCLC045R 91270878786VTkv7rvk/152507:129053200878786aXC 2800jAllgemeinesNX-YJMedizinNXCJAllgemeine Medizin, Krankenhauswesen, KrankenpflegeNXC 2500-XC 2999JMedizin und GeisteswissenschaftenNXC 2800-XC 2899JArzt und Patient, Medizinische Ethik, Vivisektion045T 91271443775VTkv3201443775aCV 3500jKommunikation, Massenmedien, soziale Beeinflussung, soziale MachtkPsychologiekSozialpsychologie046X aebc20200919fDE-640z2047A a720 ff. bvb; 580 ff. bvb017L aGBV_ILN_22017L aISIL_DE-18017L aGBV_ILN_22_i02107017L aGBV_ILN_70017L aISIL_DE-89017L aGBV_ILN_90017L aISIL_DE-Hil2017L aGBV_ILN_2003017L aISIL_DE-25017L aGBV_ILN_2005017L aISIL_DE-291017L aGBV_ILN_2007017L aISIL_DE-352

0 comments on commit c807b9e

Please sign in to comment.