Search in sources :

Example 1 with Cosmotron996

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);
}
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 2 with Cosmotron996

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

Example 3 with Cosmotron996

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;
            }
        }
    }
}
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 4 with Cosmotron996

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;
}
Also used : Cosmotron996(cz.mzk.recordmanager.server.model.Cosmotron996)

Example 5 with Cosmotron996

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());
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) OAIHarvestConfiguration(cz.mzk.recordmanager.server.model.OAIHarvestConfiguration) HashMap(java.util.HashMap) Cosmotron996(cz.mzk.recordmanager.server.model.Cosmotron996) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JobParameters(org.springframework.batch.core.JobParameters) JobParameter(org.springframework.batch.core.JobParameter) Test(org.testng.annotations.Test) AbstractTest(cz.mzk.recordmanager.server.AbstractTest)

Aggregations

Cosmotron996 (cz.mzk.recordmanager.server.model.Cosmotron996)9 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 AbstractTest (cz.mzk.recordmanager.server.AbstractTest)3 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)3 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)3 OAIHarvestConfiguration (cz.mzk.recordmanager.server.model.OAIHarvestConfiguration)3 Date (java.util.Date)3 Record (org.marc4j.marc.Record)3 JobExecution (org.springframework.batch.core.JobExecution)3 JobParameter (org.springframework.batch.core.JobParameter)3 JobParameters (org.springframework.batch.core.JobParameters)3 Test (org.testng.annotations.Test)3 Cosmotron996RowMapper (cz.mzk.recordmanager.server.jdbc.Cosmotron996RowMapper)1 MarcRecordInterceptor (cz.mzk.recordmanager.server.marc.intercepting.MarcRecordInterceptor)1 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)1 MetadataRecord (cz.mzk.recordmanager.server.metadata.MetadataRecord)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1