Search in sources :

Example 6 with ControlField

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

the class FilterCaslinRecordsWriter method write.

@Override
public void write(List<? extends HarvestedRecordUniqueId> items) throws Exception {
    for (HarvestedRecordUniqueId uniqueId : items) {
        try {
            HarvestedRecord hr = hrDao.get(uniqueId);
            if (hr == null || hr.getRawRecord().length == 0)
                continue;
            MarcRecord marc = marcXmlParser.parseRecord(new ByteArrayInputStream(hr.getRawRecord()));
            Record record = marcXmlParser.parseUnderlyingRecord(new ByteArrayInputStream(hr.getRawRecord()));
            Boolean updated = false;
            Record newRecord = new RecordImpl();
            MarcFactory marcFactory = new MarcFactoryImpl();
            newRecord.setLeader(record.getLeader());
            for (ControlField cf : record.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)) {
                    // add $q0 when sigla is in db
                    if (df.getTag().equals("996")) {
                        if (caslinFilter.filter(df.getSubfield('e').getData()) && (df.getSubfield('q') == null || !df.getSubfield('q').getData().equals("0"))) {
                            df.addSubfield(marcFactory.newSubfield('q', "0"));
                            updated = true;
                        }
                    }
                    newRecord.addVariableField(df);
                }
            }
            hr.setRawRecord(new MarcRecordImpl(newRecord).export(IOFormat.XML_MARC).getBytes(StandardCharsets.UTF_8));
            if (hr.getDeleted() == null && !mrFactory.getMetadataRecord(hr).matchFilter()) {
                hr.setDeleted(new Date());
                updated = true;
            }
            if (updated) {
                hr.setUpdated(new Date());
                hrDao.persist(hr);
            }
        } catch (Exception ex) {
            logger.error(String.format("Exception thrown when filtering harvested_record with id=%s", uniqueId), ex);
        }
    }
}
Also used : HarvestedRecordUniqueId(cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId) 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) Date(java.util.Date) ControlField(org.marc4j.marc.ControlField) DataField(org.marc4j.marc.DataField) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) TreeSet(java.util.TreeSet) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) Record(org.marc4j.marc.Record) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) List(java.util.List) MarcFactoryImpl(cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord)

Example 7 with ControlField

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

the class KkvyMarcInterceptor method intercept.

@Override
public byte[] intercept() {
    if (super.getRecord() == null) {
        return new byte[0];
    }
    MarcRecord marc = new MarcRecordImpl(super.getRecord());
    Record newRecord = new RecordImpl();
    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 996l = VF
            if (df.getTag().equals("996")) {
                if (df.getSubfield('l') != null && df.getSubfield('l').getData().trim().equals("VF"))
                    continue;
            }
            processField996(df);
            newRecord.addVariableField(df);
        }
    }
    return new MarcRecordImpl(newRecord).export(IOFormat.XML_MARC).getBytes(StandardCharsets.UTF_8);
}
Also used : MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) ControlField(org.marc4j.marc.ControlField) DataField(org.marc4j.marc.DataField) TreeSet(java.util.TreeSet) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) Record(org.marc4j.marc.Record) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) List(java.util.List) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) RecordImpl(cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)

Example 8 with ControlField

use of org.marc4j.marc.ControlField 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 9 with ControlField

use of org.marc4j.marc.ControlField 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 10 with ControlField

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

the class MarcISO2709StreamReader method parseRecord.

private void parseRecord(Record record, byte[] byteArray, byte[] recordBuf, int recordLength) {
    Leader ldr;
    ldr = factory.newLeader();
    ldr.setRecordLength(recordLength);
    int directoryLength = 0;
    try {
        parseLeader(ldr, byteArray);
        directoryLength = ldr.getBaseAddressOfData() - (24 + 1);
    } catch (IOException e) {
        throw new MarcException("error parsing leader with data: " + new String(byteArray), e);
    } catch (MarcException e) {
        throw new MarcException("error parsing leader with data: " + new String(byteArray), e);
    }
    // if MARC 21 then check encoding
    switch(ldr.getCharCodingScheme()) {
        case ' ':
            if (!override) {
                encoding = "ISO-8859-1";
            }
            break;
        case 'a':
            if (!override) {
                encoding = "UTF8";
            }
    }
    record.setLeader(ldr);
    if ((directoryLength % 12) != 0) {
        throw new MarcException("invalid directory");
    }
    DataInputStream inputrec = new DataInputStream(new ByteArrayInputStream(recordBuf));
    int size = directoryLength / 12;
    String[] tags = new String[size];
    int[] lengths = new int[size];
    byte[] tag = new byte[3];
    byte[] length = new byte[4];
    byte[] start = new byte[5];
    String tmp;
    try {
        for (int i = 0; i < size; i++) {
            inputrec.readFully(tag);
            tmp = new String(tag);
            tags[i] = tmp;
            inputrec.readFully(length);
            tmp = new String(length);
            lengths[i] = Integer.parseInt(tmp);
            inputrec.readFully(start);
        }
        if (inputrec.read() != Constants.FT) {
            throw new MarcException("expected field terminator at end of directory");
        }
        for (int i = 0; i < size; i++) {
            getFieldLength(inputrec);
            // If tag is for a ControlField; else, try as DataField
            if (Constants.CF_TAG_PATTERN.matcher(tags[i]).find()) {
                byteArray = new byte[lengths[i] - 1];
                inputrec.readFully(byteArray);
                if (inputrec.read() != Constants.FT) {
                    throw new MarcException("expected field terminator at end of field");
                }
                ControlField field = factory.newControlField();
                field.setTag(tags[i]);
                field.setData(getDataAsString(byteArray));
                record.addVariableField(field);
            } else {
                byteArray = new byte[lengths[i]];
                inputrec.readFully(byteArray);
                try {
                    record.addVariableField(parseDataField(tags[i], byteArray));
                } catch (IOException e) {
                    throw new MarcException("error parsing data field for tag: " + tags[i] + " with data: " + new String(byteArray), e);
                }
            }
        }
        if (inputrec.read() != Constants.RT) {
            throw new MarcException("expected record terminator");
        }
    } catch (IOException e) {
        throw new MarcException("an error occured reading input", e);
    } catch (NumberFormatException e) {
        throw new MarcException();
    }
}
Also used : MarcException(org.marc4j.MarcException) Leader(org.marc4j.marc.Leader) ControlField(org.marc4j.marc.ControlField) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

ControlField (org.marc4j.marc.ControlField)14 DataField (org.marc4j.marc.DataField)13 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)12 Record (org.marc4j.marc.Record)12 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)11 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)11 List (java.util.List)9 TreeSet (java.util.TreeSet)9 MarcFactoryImpl (cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl)6 MarcFactory (org.marc4j.marc.MarcFactory)6 Subfield (org.marc4j.marc.Subfield)4 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Matcher (java.util.regex.Matcher)2 Pair (org.apache.commons.lang3.tuple.Pair)2 Cosmotron996 (cz.mzk.recordmanager.server.model.Cosmotron996)1 HarvestedRecordUniqueId (cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId)1 DataInputStream (java.io.DataInputStream)1