use of cz.mzk.recordmanager.server.model.Cosmotron996 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);
}
use of cz.mzk.recordmanager.server.model.Cosmotron996 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));
}
use of cz.mzk.recordmanager.server.model.Cosmotron996 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.model.Cosmotron996 in project RecordManager2 by moravianlibrary.
the class Cosmotron996RowMapper method mapRow.
@Override
public Cosmotron996 mapRow(ResultSet rs, int rowNum) throws SQLException {
Cosmotron996 record = new Cosmotron996();
record.setRecordId(rs.getString("record_id"));
record.setHarvestedFrom(rs.getLong("import_conf_id"));
record.setUpdated(rs.getDate("updated"));
record.setDeleted(rs.getDate("deleted"));
record.setHarvested(rs.getDate("harvested"));
record.setRawRecord(rs.getBytes("raw_record"));
return record;
}
use of cz.mzk.recordmanager.server.model.Cosmotron996 in project RecordManager2 by moravianlibrary.
the class CosmotronHarvestJobTest method testRecordWithParentIdAndWithout996.
@Test
public void testRecordWithParentIdAndWithout996() throws Exception {
reset(httpClient);
InputStream response0 = this.getClass().getResourceAsStream("/sample/Identify.xml");
InputStream response1 = this.getClass().getResourceAsStream("/sample/cosmotron/ParentIdNot996.xml");
expect(httpClient.executeGet("http://katalog.cbvk.cz/i2/i2.ws.oai.cls?verb=Identify")).andReturn(response0);
expect(httpClient.executeGet("http://katalog.cbvk.cz/i2/i2.ws.oai.cls?verb=ListRecords&metadataPrefix=oai_marcxml_cpk")).andReturn(response1);
replay(httpClient);
final Long confID = 328L;
Map<String, JobParameter> params = new HashMap<>();
params.put(Constants.JOB_PARAM_CONF_ID, new JobParameter(confID));
JobExecution exec = jobExecutor.execute(Constants.JOB_ID_HARVEST_COSMOTRON, new JobParameters(params));
Assert.assertEquals(exec.getExitStatus(), ExitStatus.COMPLETED);
OAIHarvestConfiguration config = configDao.get(confID);
Cosmotron996 cosmoRec = cosmotronDao.findByIdAndHarvestConfiguration("CbvkUsCat_0000003", config);
Assert.assertNotNull(cosmoRec);
Assert.assertNotNull(cosmoRec.getDeleted());
}
Aggregations