Search in sources :

Example 1 with MarcRecordInterceptor

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

the class OAIItemProcessor method createHarvestedRecord.

protected HarvestedRecord createHarvestedRecord(OAIRecord record) throws TransformerException {
    String recordId = idExtractor.extract(record.getHeader().getIdentifier());
    HarvestedRecord rec = recordDao.findByIdAndHarvestConfiguration(recordId, configuration);
    boolean deleted = record.getHeader().isDeleted() || record.getMetadata().getElement().getTagName().equals(METADATA_ERROR);
    byte[] recordContent = (deleted) ? null : asByteArray(record.getMetadata().getElement());
    if (recordContent != null && configuration.isInterceptionEnabled()) {
        MarcRecordInterceptor interceptor = marcInterceptorFactory.getInterceptor(configuration, recordId, recordContent);
        if (interceptor != null) {
            // in case of invalid MARC is error processed later
            recordContent = interceptor.intercept();
        }
    }
    if (rec == null) {
        // create new record
        HarvestedRecordUniqueId id = new HarvestedRecordUniqueId(configuration, recordId);
        rec = new HarvestedRecord(id);
        rec.setHarvestedFrom(configuration);
        rec.setFormat(format);
    } else if ((deleted && rec.getDeleted() != null && (rec.getRawRecord() == null || rec.getRawRecord().length == 0)) || Arrays.equals(recordContent, rec.getRawRecord())) {
        rec.setUpdated(new Date());
        rec.setShouldBeProcessed(false);
        // no change in record
        return rec;
    }
    rec.setShouldBeProcessed(true);
    rec.setUpdated(new Date());
    if (record.getHeader().getDatestamp() != null) {
        rec.setHarvested(record.getHeader().getDatestamp());
    }
    if (record.getHeader().getDatestamp() != null) {
        rec.setTemporalOldOaiTimestamp(rec.getOaiTimestamp());
        rec.setOaiTimestamp(record.getHeader().getDatestamp());
    }
    if (deleted) {
        rec.setDeleted(new Date());
        rec.setRawRecord(new byte[0]);
        return rec;
    } else {
        rec.setDeleted(null);
        rec.setRawRecord(recordContent);
    }
    return rec;
}
Also used : MarcRecordInterceptor(cz.mzk.recordmanager.server.marc.intercepting.MarcRecordInterceptor) HarvestedRecordUniqueId(cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId) Date(java.util.Date) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord)

Example 2 with MarcRecordInterceptor

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

the class ImportCosmotron996RecordsWriter method write.

@Override
public void write(List<? extends List<Record>> items) throws Exception {
    if (harvestConfiguration == null) {
        harvestConfiguration = oaiHarvestConfigurationDao.get(configurationId);
    }
    for (List<Record> records : items) {
        for (Record currentRecord : records) {
            try {
                MarcRecord marc = new MarcRecordImpl(currentRecord);
                String parentId = CosmotronUtils.getParentId(marc);
                MetadataRecord metadata = metadataFactory.getMetadataRecord(marc, harvestConfiguration);
                String recordId = metadata.getUniqueId();
                Cosmotron996 c996 = cosmotron996Dao.findByIdAndHarvestConfiguration(recordId, configurationId);
                if (c996 == null) {
                    c996 = new Cosmotron996();
                    c996.setRecordId(recordId);
                    c996.setHarvestedFrom(configurationId);
                }
                c996.setParentRecordId(parentId);
                c996.setUpdated(new Date());
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                MarcWriter marcWriter = new MarcXmlWriter(outStream, true);
                marcWriter.write(currentRecord);
                marcWriter.close();
                byte[] recordContent = outStream.toByteArray();
                if (harvestConfiguration.isInterceptionEnabled()) {
                    MarcRecordInterceptor interceptor = marcInterceptorFactory.getInterceptor(harvestConfiguration, recordId, recordContent);
                    if (interceptor != null) {
                        recordContent = interceptor.intercept();
                    }
                }
                c996.setDeleted(null);
                c996.setRawRecord(recordContent);
                cosmotron996Dao.persist(c996);
            } catch (Exception e) {
                logger.warn("Error occured in processing record");
                throw e;
            }
        }
    }
}
Also used : MarcRecordInterceptor(cz.mzk.recordmanager.server.marc.intercepting.MarcRecordInterceptor) Cosmotron996(cz.mzk.recordmanager.server.model.Cosmotron996) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) MarcXmlWriter(org.marc4j.MarcXmlWriter) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) MarcWriter(org.marc4j.MarcWriter) MetadataRecord(cz.mzk.recordmanager.server.metadata.MetadataRecord) Record(org.marc4j.marc.Record) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) MetadataRecord(cz.mzk.recordmanager.server.metadata.MetadataRecord)

Example 3 with MarcRecordInterceptor

use of cz.mzk.recordmanager.server.marc.intercepting.MarcRecordInterceptor 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)

Aggregations

MarcRecordInterceptor (cz.mzk.recordmanager.server.marc.intercepting.MarcRecordInterceptor)3 Date (java.util.Date)3 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)2 MetadataRecord (cz.mzk.recordmanager.server.metadata.MetadataRecord)2 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)2 HarvestedRecordUniqueId (cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 MarcWriter (org.marc4j.MarcWriter)2 MarcXmlWriter (org.marc4j.MarcXmlWriter)2 Record (org.marc4j.marc.Record)2 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)1 Cosmotron996 (cz.mzk.recordmanager.server.model.Cosmotron996)1