Search in sources :

Example 11 with Subfield

use of org.marc4j.marc.Subfield in project RecordManager2 by moravianlibrary.

the class DefaultMarcInterceptor method processField996.

/**
 * add item id to field 996
 *
 * @param df
 */
protected void processField996(DataField df) {
    if (df.getTag().equals("996")) {
        String itemIdType = conf.getItemId();
        for (Subfield sf : df.getSubfields(ITEM_ID_SUBFIELD_CHAR)) {
            df.removeSubfield(sf);
        }
        boolean missing = false;
        String sigla;
        List<String> getSiglas = null;
        getSiglas = SIGLA_MAPPING.get(conf.getId().toString());
        if (getSiglas != null && !getSiglas.isEmpty()) {
            sigla = getSiglas.get(0);
        } else {
            List<Sigla> siglas = conf.getSiglas();
            sigla = !siglas.isEmpty() ? siglas.get(0).getUniqueId().getSigla() : "";
        }
        if (itemIdType.equals("aleph")) {
            String j = df.getSubfield('j') != null ? df.getSubfield('j').getData().toUpperCase() : "";
            String w = df.getSubfield('w') != null ? df.getSubfield('w').getData() : "";
            String u = df.getSubfield('u') != null ? df.getSubfield('u').getData() : "";
            if (j.equals("") || w.equals("") || u.equals(""))
                missing = true;
            else if (recordId != null)
                df.addSubfield(MARC_FACTORY.newSubfield(ITEM_ID_SUBFIELD_CHAR, sigla + "." + recordId.replace("-", "") + "." + j + w + u));
        } else if (itemIdType.equals("tre")) {
            String w = df.getSubfield('w') != null ? df.getSubfield('w').getData() : "";
            if (w.equals(""))
                missing = true;
            else
                df.addSubfield(MARC_FACTORY.newSubfield(ITEM_ID_SUBFIELD_CHAR, sigla + "." + w));
        } else if (itemIdType.equals("nlk")) {
            String a = df.getSubfield('a') != null ? df.getSubfield('a').getData() : "";
            if (a.equals(""))
                missing = true;
            else
                df.addSubfield(MARC_FACTORY.newSubfield(ITEM_ID_SUBFIELD_CHAR, sigla + "." + a));
        } else if (itemIdType.equals("svkul")) {
            String b = df.getSubfield('b') != null ? df.getSubfield('b').getData() : "";
            if (b.equals(""))
                missing = true;
            else {
                if (b.startsWith("31480") && b.length() >= 8) {
                    b = b.substring(5);
                }
                df.addSubfield(MARC_FACTORY.newSubfield(ITEM_ID_SUBFIELD_CHAR, sigla + "." + b));
            }
        } else if (itemIdType.equals("other")) {
            String b = df.getSubfield('b') != null ? df.getSubfield('b').getData() : "";
            if (b.equals(""))
                missing = true;
            else
                df.addSubfield(MARC_FACTORY.newSubfield(ITEM_ID_SUBFIELD_CHAR, sigla + "." + b));
        }
        if (missing)
            logger.info(String.format("Missing data for itemId: import_confid=%d, 001=%s", conf.getId(), record.getControlNumber()));
    }
}
Also used : Sigla(cz.mzk.recordmanager.server.model.Sigla) Subfield(org.marc4j.marc.Subfield)

Example 12 with Subfield

use of org.marc4j.marc.Subfield in project RecordManager2 by moravianlibrary.

the class MzkNormsMarcInterceptor method intercept.

@Override
public byte[] intercept() {
    if (super.getRecord() == null) {
        return new byte[0];
    }
    MarcRecord marc = new MarcRecordImpl(super.getRecord());
    Record newRecord = new RecordImpl();
    MarcFactory marcFactory = new MarcFactoryImpl();
    newRecord.setLeader(getRecord().getLeader());
    for (ControlField cf : super.getRecord().getControlFields()) {
        newRecord.addVariableField(cf);
    }
    Map<String, List<DataField>> dfMap = marc.getAllFields();
    for (String tag : new TreeSet<String>(dfMap.keySet())) {
        // sorted tags
        for (DataField df : dfMap.get(tag)) {
            // kill fields 996, 910 and 540
            if (df.getTag().equals("996"))
                continue;
            if (df.getTag().equals("910"))
                continue;
            if (df.getTag().equals("540")) {
                if (df.getSubfield('a').getData().contains("Normy lze objednat u pultu ve Studovně novin " + "a časopisů (2.p.) a studovat se mohou pouze ve studovně."))
                    continue;
            }
            if (df.getTag().equals("520")) {
                /*
					 * MAPPING
					 * 520 a Norma je platná = 500a
					 */
                List<Pair<Character, Character>> directMapping = new ArrayList<>();
                directMapping.add(Pair.of('a', 'a'));
                DataField newDf = marcFactory.newDataField("500", ' ', ' ');
                for (Pair<Character, Character> mapping : directMapping) {
                    Subfield sf = df.getSubfield(mapping.getLeft());
                    if (!sf.getData().contains("Norma je platná")) {
                        continue;
                    }
                    newDf.addSubfield(sf);
                }
                newRecord.addVariableField(newDf);
            } else {
                newRecord.addVariableField(df);
            }
        }
    }
    return new MarcRecordImpl(newRecord).export(IOFormat.XML_MARC).getBytes(StandardCharsets.UTF_8);
}
Also used : ArrayList(java.util.ArrayList) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) MarcFactory(org.marc4j.marc.MarcFactory) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) RecordImpl(cz.mzk.recordmanager.server.marc.marc4j.RecordImpl) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) ControlField(org.marc4j.marc.ControlField) DataField(org.marc4j.marc.DataField) TreeSet(java.util.TreeSet) Record(org.marc4j.marc.Record) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) ArrayList(java.util.ArrayList) List(java.util.List) MarcFactoryImpl(cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl) Subfield(org.marc4j.marc.Subfield) Pair(org.apache.commons.lang3.tuple.Pair)

Example 13 with Subfield

use of org.marc4j.marc.Subfield in project RecordManager2 by moravianlibrary.

the class OpenlibMarcInterceptor method intercept.

@Override
public byte[] intercept() {
    if (super.getRecord() == null) {
        return new byte[0];
    }
    MarcRecord marc = new MarcRecordImpl(super.getRecord());
    Record newRecord = new RecordImpl();
    MarcFactory marcFactory = new MarcFactoryImpl();
    newRecord.setLeader(getRecord().getLeader());
    for (ControlField cf : super.getRecord().getControlFields()) {
        newRecord.addVariableField(cf);
    }
    Map<String, List<DataField>> dfMap = marc.getAllFields();
    for (String tag : new TreeSet<String>(dfMap.keySet())) {
        // sorted tags
        for (DataField df : dfMap.get(tag)) {
            if (df.getTag().equals("856")) {
                // kill field 85641
                if (df.getIndicator1() == '4' && df.getIndicator2() == '1')
                    continue;
                if (df.getIndicator1() == '4' && df.getIndicator2() == '2') {
                    Subfield sf = df.getSubfield('u');
                    if (sf != null) {
                        Matcher matcher = OPENLIBRARY_URL.matcher(sf.getData());
                        if (matcher.matches()) {
                            DataField newDf = marcFactory.newDataField("856", '4', '2');
                            newDf.addSubfield(df.getSubfield('u'));
                            newDf.addSubfield(marcFactory.newSubfield('y', TEXT_856y));
                            newRecord.addVariableField(newDf);
                        } else
                            newRecord.addVariableField(df);
                    }
                }
            } else
                newRecord.addVariableField(df);
        }
    }
    return new MarcRecordImpl(newRecord).export(IOFormat.XML_MARC).getBytes(StandardCharsets.UTF_8);
}
Also used : Matcher(java.util.regex.Matcher) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) MarcFactory(org.marc4j.marc.MarcFactory) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) RecordImpl(cz.mzk.recordmanager.server.marc.marc4j.RecordImpl) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) ControlField(org.marc4j.marc.ControlField) DataField(org.marc4j.marc.DataField) TreeSet(java.util.TreeSet) Record(org.marc4j.marc.Record) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) List(java.util.List) MarcFactoryImpl(cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl) Subfield(org.marc4j.marc.Subfield)

Example 14 with Subfield

use of org.marc4j.marc.Subfield in project RecordManager2 by moravianlibrary.

the class DataFieldImpl method toString.

/**
 * Returns a string representation of this data field.
 * <p>
 * Example:
 *
 * <pre>
 *    245 10$aSummerland /$cMichael Chabon.
 * </pre>
 *
 * @return A string representation of this data field
 */
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(super.toString());
    sb.append(' ');
    sb.append(getIndicator1());
    sb.append(getIndicator2());
    for (Subfield subfield : mySubfields) {
        sb.append(subfield.toString());
    }
    return sb.toString();
}
Also used : Subfield(org.marc4j.marc.Subfield)

Example 15 with Subfield

use of org.marc4j.marc.Subfield in project RecordManager2 by moravianlibrary.

the class MarcFactoryImpl method newDataField.

/**
 * Creates a new data field with the given tag and indicators and subfields
 * and returns the instance.
 *
 * @return DataField
 */
@Override
public DataField newDataField(String tag, char ind1, char ind2, String... subfieldCodesAndData) {
    DataField df = new DataFieldImpl(tag, ind1, ind2);
    if (subfieldCodesAndData.length % 2 == 1) {
        throw new MarcException("Error: must provide even number of parameters for subfields: code, data, code, data, ...");
    }
    for (int i = 0; i < subfieldCodesAndData.length; i += 2) {
        if (subfieldCodesAndData[i].length() != 1) {
            throw new MarcException("Error: subfieldCode must be a single character");
        }
        Subfield sf = newSubfield(subfieldCodesAndData[i].charAt(0), subfieldCodesAndData[i + 1]);
        df.addSubfield(sf);
    }
    return (df);
}
Also used : MarcException(org.marc4j.MarcException) DataField(org.marc4j.marc.DataField) Subfield(org.marc4j.marc.Subfield)

Aggregations

Subfield (org.marc4j.marc.Subfield)24 DataField (org.marc4j.marc.DataField)17 ArrayList (java.util.ArrayList)8 Matcher (java.util.regex.Matcher)6 ControlField (org.marc4j.marc.ControlField)4 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)3 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)3 MarcFactoryImpl (cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl)3 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)3 List (java.util.List)3 TreeSet (java.util.TreeSet)3 MarcFactory (org.marc4j.marc.MarcFactory)3 Record (org.marc4j.marc.Record)3 IOException (java.io.IOException)2 Oclc (cz.mzk.recordmanager.server.model.Oclc)1 Sigla (cz.mzk.recordmanager.server.model.Sigla)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashSet (java.util.HashSet)1 StringJoiner (java.util.StringJoiner)1 Pair (org.apache.commons.lang3.tuple.Pair)1