Search in sources :

Example 1 with MarcRecord

use of cz.mzk.recordmanager.server.marc.MarcRecord in project RecordManager2 by moravianlibrary.

the class AdresarRecordsWriter method writeInner.

protected void writeInner(List<? extends List<Record>> items) throws Exception {
    for (List<Record> records : items) {
        for (Record currentRecord : records) {
            try {
                if (currentRecord == null) {
                    continue;
                }
                MarcRecord marc = new MarcRecordImpl(currentRecord);
                String recordId = marc.getControlField("SYS");
                HarvestedRecord hr = harvestedRecordDAO.findByIdAndHarvestConfiguration(recordId, configurationId);
                if (hr == null) {
                    hr = new HarvestedRecord(new HarvestedRecordUniqueId(configurationId, recordId));
                    hr.setFormat("marc21-xml");
                }
                hr.setUpdated(new Date());
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                MarcWriter marcWriter = new MarcXmlWriter(outStream, true);
                marcWriter.setConverter(ISOCharConvertor.INSTANCE);
                marcWriter.write(currentRecord);
                marcWriter.close();
                hr.setRawRecord(outStream.toByteArray());
                harvestedRecordDAO.persist(hr);
            } catch (Exception e) {
                logger.warn("Error occured in processing record");
                throw e;
            }
        }
    }
}
Also used : HarvestedRecordUniqueId(cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) MarcXmlWriter(org.marc4j.MarcXmlWriter) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord)

Example 2 with MarcRecord

use of cz.mzk.recordmanager.server.marc.MarcRecord in project RecordManager2 by moravianlibrary.

the class CosmotronRecordWriter method processAndSave.

private void processAndSave(HarvestedRecord hr) throws Exception {
    if (hr.getId() == null) {
        String recordId = hr.getUniqueId().getRecordId();
        // save deleted record
        if (hr.getDeleted() != null) {
            Cosmotron996 deleted996;
            if ((deleted996 = cosmotronDao.findByIdAndHarvestConfiguration(recordId, configurationId)) == null) {
                // new deleted record
                super.writeRecord(hr);
            } else {
                // delete existing 996 record
                deleted996.setDeleted(new Date());
                deleted996.setUpdated(new Date());
                deleted996.setRawRecord(new byte[0]);
                cosmotronDao.persist(deleted996);
            }
            return;
        }
        // process not deleted record
        InputStream is = new ByteArrayInputStream(hr.getRawRecord());
        MarcRecord mr = marcXmlParser.parseRecord(is);
        String parentRecordId = CosmotronUtils.getParentId(mr);
        if (parentRecordId == null) {
            // new harvested_record
            super.writeRecord(hr);
        } else {
            // new or updated 996 record
            Cosmotron996 cr = cosmotronDao.findByIdAndHarvestConfiguration(recordId, configurationId);
            if (cr == null) {
                // create new record
                cr = new Cosmotron996(recordId, configurationId);
                cr.setHarvested(new Date());
            }
            cr.setParentRecordId(parentRecordId);
            cr.setUpdated(new Date());
            cr.setDeleted(null);
            cr.setRawRecord(hr.getRawRecord());
            if (!CosmotronUtils.existsFields996(mr)) {
                // record with parent id and without fields 996
                // save and set deleted value
                cr.setDeleted(new Date());
                cr.setParentRecordId(null);
            }
            cosmotronDao.persist(cr);
        }
    } else
        // process existing harvested record
        super.writeRecord(hr);
}
Also used : Cosmotron996(cz.mzk.recordmanager.server.model.Cosmotron996) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) Date(java.util.Date)

Example 3 with MarcRecord

use of cz.mzk.recordmanager.server.marc.MarcRecord in project RecordManager2 by moravianlibrary.

the class PublishDateMarcFunctions method getPublishDateForSortingForArticles.

private String getPublishDateForSortingForArticles(MarcFunctionContext ctx) {
    for (String year : ctx.record().getFields("773", "", '9')) {
        if (year.length() > 4)
            year = year.substring(0, 4);
        if (SINGLE_YEAR_PATTERN.matcher(year).matches()) {
            int yearInt = Integer.parseInt(year);
            if (MIN_YEAR < yearInt && yearInt <= MAX_YEAR) {
                return year;
            }
        }
    }
    MarcRecord mr = ctx.record();
    String field008 = mr.getControlField("008");
    if (field008 != null && field008.length() >= 12) {
        String yearS = field008.substring(7, 11);
        if (SINGLE_YEAR_PATTERN.matcher(yearS).matches()) {
            int yearI = Integer.parseInt(yearS);
            if ((1500 < yearI) && (yearI < (MAX_YEAR))) {
                return yearS;
            }
        }
    }
    return null;
}
Also used : MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord)

Example 4 with MarcRecord

use of cz.mzk.recordmanager.server.marc.MarcRecord in project RecordManager2 by moravianlibrary.

the class PublishDateMarcFunctions method getPublishDate.

public Set<Integer> getPublishDate(MarcFunctionContext ctx) {
    MarcRecord record = ctx.record();
    Set<Integer> years = new TreeSet<Integer>();
    years.addAll(parseRanges(getPublishDateFromFields(ctx)));
    String field008 = record.getControlField("008");
    years.addAll(parsePublishDateFrom008(field008));
    return years;
}
Also used : TreeSet(java.util.TreeSet) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord)

Example 5 with MarcRecord

use of cz.mzk.recordmanager.server.marc.MarcRecord in project RecordManager2 by moravianlibrary.

the class CosmotronUpdate996Writer method updateMarc.

private void updateMarc(HarvestedRecord parentRec, List<Cosmotron996> childRecs) {
    Record record = marcXmlParser.parseUnderlyingRecord(parentRec.getRawRecord());
    Record newRecord = new RecordImpl();
    newRecord.setLeader(record.getLeader());
    for (ControlField cf : record.getControlFields()) {
        newRecord.addVariableField(cf);
    }
    for (DataField df : record.getDataFields()) {
        // remove old fields 996
        if (!df.getTag().equals("996")) {
            newRecord.addVariableField(df);
        }
    }
    for (Cosmotron996 new996 : childRecs) {
        if (new996.getDeleted() != null)
            continue;
        MarcRecord marcRecord996 = parseMarcRecord(new996.getRawRecord());
        for (DataField df : get996(marcRecord996)) {
            newRecord.addVariableField(df);
        }
    }
    parentRec.setRawRecord(new MarcRecordImpl(newRecord).export(IOFormat.XML_MARC).getBytes(StandardCharsets.UTF_8));
}
Also used : ControlField(org.marc4j.marc.ControlField) DataField(org.marc4j.marc.DataField) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) Cosmotron996(cz.mzk.recordmanager.server.model.Cosmotron996) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) Record(org.marc4j.marc.Record) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) RecordImpl(cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)

Aggregations

MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)46 ByteArrayInputStream (java.io.ByteArrayInputStream)22 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)21 InputStream (java.io.InputStream)19 Record (org.marc4j.marc.Record)19 AbstractTest (cz.mzk.recordmanager.server.AbstractTest)15 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)15 DataField (org.marc4j.marc.DataField)15 Test (org.testng.annotations.Test)15 List (java.util.List)13 ArrayList (java.util.ArrayList)11 TreeSet (java.util.TreeSet)11 ControlField (org.marc4j.marc.ControlField)11 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)10 MarcFunctionContext (cz.mzk.recordmanager.server.scripting.marc.MarcFunctionContext)9 JobParameters (org.springframework.batch.core.JobParameters)8 HashMap (java.util.HashMap)7 JobParameter (org.springframework.batch.core.JobParameter)7 MarcFactoryImpl (cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl)5 MarcFactory (org.marc4j.marc.MarcFactory)5