Search in sources :

Example 1 with MarcFactory

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

the class MarcRecordImpl method addDataField.

@Override
public void addDataField(String tag, char ind1, char ind2, String... subfields) {
    MarcFactory factory = MarcFactoryImpl.newInstance();
    DataField df = factory.newDataField(tag, ind1, ind2, subfields);
    record.addVariableField(df);
}
Also used : DataField(org.marc4j.marc.DataField) MarcFactory(org.marc4j.marc.MarcFactory)

Example 2 with MarcFactory

use of org.marc4j.marc.MarcFactory 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 3 with MarcFactory

use of org.marc4j.marc.MarcFactory 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 4 with MarcFactory

use of org.marc4j.marc.MarcFactory 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 5 with MarcFactory

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

the class MarcRecordFactory method recordFactory.

/* 
	 * Leader:
	 * 	 3 character tag 000
	 * 	 space
	 * 	 24 character tag's value
	 *   example: "000 00759cam a2200229 a 4500"
	 *
	 * ControlField:
	 *   3 character tag 00[1-9]
	 *   space
	 *   tag's value
	 *   example: "001 001445152"
	 * 
	 * DataField:
	 *   3 character tag - all digits
	 *   space
	 *   indicator1
	 *   indicator2
	 *   tag's value, perhaps with internal Subfields: 1 character $
	 *                                                 1 character tag
	 *                                                 value
	 *                                                 example: "$aCambridge" 
	 *   example: "991 00$b201503$cNOV$gg$en"
	 *   
	 *   or
	 *   
	 *   3 character tag - all digits
	 *   space
	 *   tag's value, perhaps with internal Subfields
	 *   example - indicators set on ' ': "991 $b201503$cNOV$gg$en"
	 */
public static MarcRecordImpl recordFactory(List<String> data) throws Exception {
    MarcFactory marcFactory = MarcFactoryImpl.newInstance();
    Record record = marcFactory.newRecord();
    record.addVariableField(marcFactory.newControlField("001", "0"));
    for (String field : data) {
        String key;
        String value;
        if (field.length() >= 3) {
            key = field.substring(0, 3);
            value = (field.length() > 3) ? field.substring(4, field.length()) : "";
        } else
            continue;
        // Leader
        if (Pattern.matches("000", key)) {
            if (value.length() > 24)
                value = value.substring(0, 24);
            else
                value = String.format("%-24s", value);
            record.setLeader(marcFactory.newLeader(value));
        } else // ControlField
        if (Pattern.matches("00[1-9]", key))
            record.addVariableField(marcFactory.newControlField(key, value));
        else // DataField
        if (Pattern.matches("\\w{3}", key)) {
            if (value.isEmpty())
                record.addVariableField(marcFactory.newDataField(key, ' ', ' '));
            else if (value.length() >= 2) {
                // Indicators
                DataField dataField;
                if (value.charAt(0) == '$')
                    dataField = marcFactory.newDataField(key, ' ', ' ');
                else if (value.charAt(1) == '$')
                    dataField = marcFactory.newDataField(key, value.charAt(0), ' ');
                else
                    dataField = marcFactory.newDataField(key, value.charAt(0), value.charAt(1));
                // Subfield
                if (value.length() > 2) {
                    Pattern pattern = Pattern.compile("\\$([a-zA-Z0-9])([^$]*)(.*)");
                    Matcher matcher = pattern.matcher(value);
                    while (matcher.find()) {
                        dataField.addSubfield(marcFactory.newSubfield(matcher.group(1).charAt(0), matcher.group(2)));
                        matcher = pattern.matcher(matcher.group(3));
                    }
                }
                record.addVariableField(dataField);
            }
        }
    }
    return new MarcRecordImpl(record);
}
Also used : Pattern(java.util.regex.Pattern) DataField(org.marc4j.marc.DataField) Matcher(java.util.regex.Matcher) MarcFactory(org.marc4j.marc.MarcFactory) Record(org.marc4j.marc.Record)

Aggregations

DataField (org.marc4j.marc.DataField)8 MarcFactory (org.marc4j.marc.MarcFactory)8 Record (org.marc4j.marc.Record)7 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)6 MarcFactoryImpl (cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl)6 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)6 ControlField (org.marc4j.marc.ControlField)6 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)5 List (java.util.List)5 TreeSet (java.util.TreeSet)5 Matcher (java.util.regex.Matcher)3 Subfield (org.marc4j.marc.Subfield)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Pair (org.apache.commons.lang3.tuple.Pair)2 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)1 HarvestedRecordUniqueId (cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 StringJoiner (java.util.StringJoiner)1