Search in sources :

Example 6 with MarcWriter

use of org.marc4j.MarcWriter in project RecordManager2 by moravianlibrary.

the class ImportRecordsWriter method writeInner.

protected void writeInner(List<? extends List<Record>> items) throws Exception {
    for (List<Record> records : items) {
        for (Record currentRecord : records) {
            try {
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                MarcWriter marcWriter = new MarcXmlWriter(outStream, true);
                marcWriter.setConverter(ISOCharConvertor.INSTANCE);
                marcWriter.write(currentRecord);
                marcWriter.close();
                // need recordId before interception
                byte[] recordContent = outStream.toByteArray();
                MetadataRecord metadata = parseMetadata(recordContent);
                String recordId = metadata.getUniqueId();
                if (regexpExtractor != null) {
                    recordId = regexpExtractor.extract(recordId);
                }
                if (harvestConfiguration.isInterceptionEnabled()) {
                    MarcRecordInterceptor interceptor = marcInterceptorFactory.getInterceptor(harvestConfiguration, recordId, recordContent);
                    if (interceptor != null) {
                        byte[] recordContentNew = interceptor.intercept();
                        if (!Arrays.equals(recordContent, recordContentNew)) {
                            // if record content was changed, parse metadata again
                            metadata = parseMetadata(recordContentNew);
                            // set intercepted content
                            recordContent = recordContentNew;
                        }
                    }
                }
                HarvestedRecord hr = harvestedRecordDao.findByIdAndHarvestConfiguration(recordId, configurationId);
                if (hr == null) {
                    HarvestedRecordUniqueId id = new HarvestedRecordUniqueId(harvestConfiguration, recordId);
                    hr = new HarvestedRecord(id);
                    // TODO detect format
                    hr.setFormat("marc21-xml");
                    hr.setHarvestedFrom(harvestConfiguration);
                }
                hr.setUpdated(new Date());
                hr.setDeleted(null);
                hr.setRawRecord(recordContent);
                harvestedRecordDao.persist(hr);
                dedupKeysParser.parse(hr, metadata);
                if (harvestConfiguration.isFilteringEnabled() && !hr.getShouldBeProcessed()) {
                    logger.debug("Filtered record: " + hr.getUniqueId());
                    hr.setDeleted(new Date());
                }
                harvestedRecordDao.persist(hr);
                progress.incrementAndLogProgress();
            } catch (Exception e) {
                logger.warn("Error occured in processing record");
                throw e;
            }
        }
    }
}
Also used : MarcRecordInterceptor(cz.mzk.recordmanager.server.marc.intercepting.MarcRecordInterceptor) HarvestedRecordUniqueId(cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MarcXmlWriter(org.marc4j.MarcXmlWriter) Date(java.util.Date) MarcWriter(org.marc4j.MarcWriter) MetadataRecord(cz.mzk.recordmanager.server.metadata.MetadataRecord) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) Record(org.marc4j.marc.Record) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) MetadataRecord(cz.mzk.recordmanager.server.metadata.MetadataRecord) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord)

Example 7 with MarcWriter

use of org.marc4j.MarcWriter in project RecordManager2 by moravianlibrary.

the class MarcRecordImpl method exportToIso2709.

protected String exportToIso2709() {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    MarcWriter writer = new MarcStreamWriter(stream, "UTF-8", true);
    writer.write(record);
    writer.close();
    return new String(stream.toByteArray(), StandardCharsets.UTF_8);
}
Also used : MarcWriter(org.marc4j.MarcWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Aggregations

MarcWriter (org.marc4j.MarcWriter)7 MarcXmlWriter (org.marc4j.MarcXmlWriter)6 Record (org.marc4j.marc.Record)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)4 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)3 MetadataRecord (cz.mzk.recordmanager.server.metadata.MetadataRecord)3 Date (java.util.Date)3 MarcRecordInterceptor (cz.mzk.recordmanager.server.marc.intercepting.MarcRecordInterceptor)2 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)2 HarvestedRecordUniqueId (cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 MarcPermissiveStreamReader (org.marc4j.MarcPermissiveStreamReader)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Cosmotron996 (cz.mzk.recordmanager.server.model.Cosmotron996)1