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;
}
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;
}
}
}
}
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;
}
}
}
}
Aggregations