Search in sources :

Example 56 with DataField

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

the class NlkMarcInterceptor 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("996")) {
                /*
					 * MAPPING
					 * if exists 996 $d, $y, $v or $i => 996 $d = "$d / $y / $v / $i / $p"
					 */
                DataField newDf = marcFactory.newDataField();
                newDf.setTag(tag);
                newDf.setIndicator1(df.getIndicator1());
                newDf.setIndicator2(df.getIndicator2());
                StringJoiner sj = new StringJoiner(" / ");
                for (Subfield sf : df.getSubfields()) {
                    if (Chars.contains(new char[] { 'd', 'y', 'v', 'i' }, sf.getCode())) {
                        if (!sf.getData().isEmpty()) {
                            sj.add(sf.getData());
                        }
                    }
                }
                if (sj.length() > 0) {
                    Subfield sfp = df.getSubfield('p');
                    if (!sfp.getData().isEmpty()) {
                        sj.add(sfp.getData());
                    }
                    for (Subfield sf : df.getSubfields()) {
                        if (sf.getCode() == 'd') {
                            Subfield newSf = marcFactory.newSubfield('d');
                            newSf.setData(sj.toString());
                            newDf.addSubfield(newSf);
                        } else
                            newDf.addSubfield(sf);
                    }
                } else
                    newDf = df;
                processField996(newDf);
                newRecord.addVariableField(newDf);
            } else if (df.getTag().equals("990") || df.getTag().equals("991")) {
                continue;
            } else {
                newRecord.addVariableField(df);
            }
        }
    }
    return new MarcRecordImpl(newRecord).export(IOFormat.XML_MARC).getBytes(StandardCharsets.UTF_8);
}
Also used : 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) StringJoiner(java.util.StringJoiner) Subfield(org.marc4j.marc.Subfield)

Example 57 with DataField

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

the class SkatMarcInterceptor 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())) {
        for (DataField df : dfMap.get(tag)) {
            if (df.getTag().equals("910")) {
                /*
					 * MAPPING
					 * 910 a = 996e
					 * 910 b = 996c
					 * 910 r + s  = 996 d
					 * 910x = 996w
					 * 910p = 996p 
					 */
                List<Pair<Character, Character>> directMapping = new ArrayList<>();
                directMapping.add(Pair.of('a', 'e'));
                directMapping.add(Pair.of('b', 'c'));
                directMapping.add(Pair.of('x', 'w'));
                directMapping.add(Pair.of('p', 'p'));
                DataField newDf = marcFactory.newDataField("996", ' ', ' ');
                for (Pair<Character, Character> mapping : directMapping) {
                    if (df.getSubfields(mapping.getLeft()).isEmpty()) {
                        continue;
                    }
                    newDf.addSubfield(marcFactory.newSubfield(mapping.getRight(), df.getSubfields(mapping.getLeft()).stream().map(Subfield::getData).collect(Collectors.joining(","))));
                }
                String joinedContent = "";
                if (df.getSubfield('r') != null) {
                    joinedContent += df.getSubfield('r').getData();
                }
                if (df.getSubfield('s') != null) {
                    if (!joinedContent.isEmpty())
                        joinedContent += ", ";
                    joinedContent += df.getSubfield('s').getData();
                }
                if (!joinedContent.isEmpty()) {
                    newDf.addSubfield(marcFactory.newSubfield('d', joinedContent));
                }
                if (df.getSubfield('a') != null && cf.filter(df.getSubfield('a').getData())) {
                    newDf.addSubfield(marcFactory.newSubfield('q', "0"));
                }
                newDf.addSubfield(marcFactory.newSubfield('s', "NZ"));
                newRecord.addVariableField(newDf);
            } else if (df.getTag().equals("996")) {
                if ((df.getSubfield('e') != null) && (cf.filter(df.getSubfield('e').getData())) && (df.getSubfield('q') == null || !df.getSubfield('q').getData().equals("0"))) {
                    df.addSubfield(marcFactory.newSubfield('q', "0"));
                }
                newRecord.addVariableField(df);
            } 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) Pair(org.apache.commons.lang3.tuple.Pair)

Example 58 with DataField

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

the class AdresarStreamReader method next.

@Override
public Record next() {
    record = null;
    DataField df = null;
    try {
        while (xmlReader.hasNext()) {
            switch(xmlReader.getEventType()) {
                case XMLStreamReader.START_ELEMENT:
                    switch(xmlReader.getLocalName()) {
                        case ELEMENT_RECORD:
                            record = factory.newRecord();
                            break;
                        case ELEMENT_FIXFIELD:
                            if (getAttr(ATTR_NAME_ID).equalsIgnoreCase("LDR")) {
                                record.setLeader(factory.newLeader(xmlReader.getElementText()));
                            } else if (getAttr(ATTR_NAME_ID).equalsIgnoreCase("SYS")) {
                                record.addVariableField(factory.newControlField("SYS", xmlReader.getElementText()));
                            }
                            break;
                        case ELEMENT_VARFIELD:
                            df = factory.newDataField(getAttr(ATTR_NAME_ID), getAttr(ATTR_I1_NAME).charAt(0), getAttr(ATTR_I2_NAME).charAt(0));
                            break;
                        case ELEMENT_SUBFIELD:
                            if (df != null) {
                                df.addSubfield(factory.newSubfield(getAttr(ATTR_LABEL_NAME).charAt(0), xmlReader.getElementText()));
                            }
                            break;
                    }
                    break;
                case XMLStreamReader.END_ELEMENT:
                    switch(xmlReader.getLocalName()) {
                        case ELEMENT_RECORD:
                            while (xmlReader.hasNext() && xmlReader.getEventType() != XMLStreamReader.START_ELEMENT) {
                                xmlReader.next();
                            }
                            return record;
                        case ELEMENT_VARFIELD:
                            record.addVariableField(df);
                            break;
                    }
                    break;
            }
            xmlReader.next();
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DataField(org.marc4j.marc.DataField) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 59 with DataField

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

the class MetadataMarcRecord method getISMNs.

@Override
public List<Ismn> getISMNs() {
    List<Ismn> results = new ArrayList<>();
    Long ismnCounter = 0L;
    Ismn ismn;
    for (DataField df : underlayingMarc.getDataFields("024")) {
        try {
            ismn = ISMNUtils.createIsmn(df);
        } catch (NoDataException nde) {
            continue;
        } catch (NumberFormatException nfe) {
            logger.info(String.format("Invalid ISMN: %s", nfe.getMessage()));
            continue;
        }
        ismn.setOrderInRecord(++ismnCounter);
        results.add(ismn);
    }
    return results;
}
Also used : DataField(org.marc4j.marc.DataField) ArrayList(java.util.ArrayList) Ismn(cz.mzk.recordmanager.server.model.Ismn) NoDataException(cz.mzk.recordmanager.server.util.identifier.NoDataException)

Example 60 with DataField

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

the class MetadataMarcRecord method getMetaproxyBool.

@Override
public Boolean getMetaproxyBool() {
    Set<String> fieldTags = underlayingMarc.getAllFields().keySet();
    if (underlayingMarc.getControlField("008") == null)
        return false;
    if (!fieldTags.contains("300") || !fieldTags.contains("245") || (!fieldTags.contains("260") && !fieldTags.contains("264")))
        return false;
    boolean f245ind1 = false;
    for (DataField df : underlayingMarc.getDataFields("245")) {
        if (df.getIndicator1() == '0')
            f245ind1 = true;
    }
    if (!f245ind1) {
        boolean f1xxOr7xx = false;
        for (String tag : fieldTags) {
            if (METAPROXY_TAG_PATTERN.matcher(tag).matches()) {
                f1xxOr7xx = true;
                break;
            }
        }
        if (!f1xxOr7xx)
            return false;
    }
    return true;
}
Also used : DataField(org.marc4j.marc.DataField)

Aggregations

DataField (org.marc4j.marc.DataField)82 ArrayList (java.util.ArrayList)18 Subfield (org.marc4j.marc.Subfield)17 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)15 ControlField (org.marc4j.marc.ControlField)13 Record (org.marc4j.marc.Record)13 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)12 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)11 List (java.util.List)11 TreeSet (java.util.TreeSet)11 Matcher (java.util.regex.Matcher)11 MarcFactory (org.marc4j.marc.MarcFactory)8 MarcFactoryImpl (cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 NoDataException (cz.mzk.recordmanager.server.util.identifier.NoDataException)4 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)3 IOException (java.io.IOException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 ImportConfiguration (cz.mzk.recordmanager.server.model.ImportConfiguration)2 ShortTitle (cz.mzk.recordmanager.server.model.ShortTitle)2