Search in sources :

Example 21 with Record

use of org.marc4j.marc.Record 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 22 with Record

use of org.marc4j.marc.Record 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 23 with Record

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

the class SfxJibNlkCsvStreamReader method next.

/**
 * Returns the next record in the iteration.
 *
 * @return Record - the record object
 */
public Record next() {
    record = factory.newRecord();
    SfxNlkRecord sfx = new SfxNlkRecord(iterator.next());
    record.setLeader(factory.newLeader(sfx.isBook() ? TEXT_LEADER_MONOGRAPH : TEXT_LEADER_SERIAL));
    record.addVariableField(factory.newControlField("001", String.valueOf(id.getAndIncrement())));
    Matcher matcher = INSTITUTION.matcher(idPrefix);
    if (matcher.matches()) {
        record.addVariableField(factory.newControlField("003", matcher.group(1)));
    }
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_STRING_005);
    record.addVariableField(factory.newControlField("005", sdf.format(new Date())));
    record.addVariableField(factory.newControlField("008", String.format(TEXT_008, (sfx.getFrom() == null ? "    " : sfx.getFrom()) + (sfx.getTo() == null ? (sfx.isBook() ? "    " : "9999") : sfx.getTo()))));
    if (sfx.getIsbns() != null)
        sfx.getIsbns().forEach(i -> addDataField("020", ' ', ' ', "a", i));
    if (sfx.getIssns() != null)
        sfx.getIssns().forEach(i -> addDataField("022", ' ', ' ', "a", i));
    if (sfx.getAuthor() != null)
        addDataField("100", '1', '#', "a", sfx.getAuthor());
    if (sfx.getTitle() != null) {
        char ind2 = '0';
        if ((matcher = PREFIX.matcher(sfx.getTitle())).matches()) {
            ind2 = Character.forDigit(matcher.group(1).length(), 10);
        }
        addDataField("245", '1', ind2, "a", sfx.getTitle());
    }
    if (sfx.getType() != null)
        addDataField("300", ' ', ' ', "a", sfx.getType());
    if (sfx.getUrl() != null)
        addDataField("856", ' ', ' ', "u", sfx.getUrl());
    addYears(sfx);
    RecordUtils.sortFields(record);
    return record;
}
Also used : Iterator(java.util.Iterator) ISBNUtils(cz.mzk.recordmanager.server.util.identifier.ISBNUtils) RecordUtils(cz.mzk.recordmanager.server.util.RecordUtils) Date(java.util.Date) CSVRecord(org.apache.commons.csv.CSVRecord) SimpleDateFormat(java.text.SimpleDateFormat) Set(java.util.Set) MarcReader(org.marc4j.MarcReader) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Calendar(java.util.Calendar) Matcher(java.util.regex.Matcher) Record(org.marc4j.marc.Record) StringReader(java.io.StringReader) CSVFormat(org.apache.commons.csv.CSVFormat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MarcFactory(org.marc4j.marc.MarcFactory) CSVParser(org.apache.commons.csv.CSVParser) Pattern(java.util.regex.Pattern) InputStream(java.io.InputStream) Matcher(java.util.regex.Matcher) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 24 with Record

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

the class MarcXmlParserImpl method parseRecord.

@Override
public MarcRecord parseRecord(InputStream is) {
    try {
        RecordStack queue = new RecordStack();
        MarcXmlHandler handler = new MarcXmlHandler(queue);
        org.marc4j.MarcXmlParser parser = new org.marc4j.MarcXmlParser(handler);
        parser.parse(new InputSource(is));
        Record record = queue.pop();
        return new MarcRecordImpl(record);
    } catch (org.marc4j.MarcException me) {
        throw new InvalidMarcException(me.getMessage(), me);
    }
}
Also used : RecordStack(org.marc4j.RecordStack) InputSource(org.xml.sax.InputSource) MarcXmlHandler(cz.mzk.recordmanager.server.marc.marc4j.MarcXmlHandler) Record(org.marc4j.marc.Record)

Example 25 with Record

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

the class BmcMarcInterceptor 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)) {
            // remove field 990, 991
            if (!df.getTag().equals("990") && !df.getTag().equals("991"))
                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)

Aggregations

Record (org.marc4j.marc.Record)32 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)21 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)20 DataField (org.marc4j.marc.DataField)13 ControlField (org.marc4j.marc.ControlField)12 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)11 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)10 List (java.util.List)10 TreeSet (java.util.TreeSet)9 InputStream (java.io.InputStream)8 MarcFactory (org.marc4j.marc.MarcFactory)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 MarcFactoryImpl (cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl)6 Date (java.util.Date)6 AbstractTest (cz.mzk.recordmanager.server.AbstractTest)5 HashMap (java.util.HashMap)5 Matcher (java.util.regex.Matcher)5 MarcWriter (org.marc4j.MarcWriter)5 MarcXmlWriter (org.marc4j.MarcXmlWriter)5 JobExecution (org.springframework.batch.core.JobExecution)5