Search in sources :

Example 11 with Record

use of org.marc4j.marc.Record in project RecordManager2 by moravianlibrary.

the class MarcFactoryImpl method newRecord.

/**
 * Returns a new {@link Record} with the supplied {@link Leader}.
 */
@Override
public Record newRecord(Leader leader) {
    Record record = new RecordImpl();
    record.setLeader(leader);
    return record;
}
Also used : Record(org.marc4j.marc.Record)

Example 12 with Record

use of org.marc4j.marc.Record in project RecordManager2 by moravianlibrary.

the class FilterCaslinRecordsWriter method write.

@Override
public void write(List<? extends HarvestedRecordUniqueId> items) throws Exception {
    for (HarvestedRecordUniqueId uniqueId : items) {
        try {
            HarvestedRecord hr = hrDao.get(uniqueId);
            if (hr == null || hr.getRawRecord().length == 0)
                continue;
            MarcRecord marc = marcXmlParser.parseRecord(new ByteArrayInputStream(hr.getRawRecord()));
            Record record = marcXmlParser.parseUnderlyingRecord(new ByteArrayInputStream(hr.getRawRecord()));
            Boolean updated = false;
            Record newRecord = new RecordImpl();
            MarcFactory marcFactory = new MarcFactoryImpl();
            newRecord.setLeader(record.getLeader());
            for (ControlField cf : record.getControlFields()) {
                newRecord.addVariableField(cf);
            }
            Map<String, List<DataField>> dfMap = marc.getAllFields();
            for (String tag : new TreeSet<String>(dfMap.keySet())) {
                for (DataField df : dfMap.get(tag)) {
                    // add $q0 when sigla is in db
                    if (df.getTag().equals("996")) {
                        if (caslinFilter.filter(df.getSubfield('e').getData()) && (df.getSubfield('q') == null || !df.getSubfield('q').getData().equals("0"))) {
                            df.addSubfield(marcFactory.newSubfield('q', "0"));
                            updated = true;
                        }
                    }
                    newRecord.addVariableField(df);
                }
            }
            hr.setRawRecord(new MarcRecordImpl(newRecord).export(IOFormat.XML_MARC).getBytes(StandardCharsets.UTF_8));
            if (hr.getDeleted() == null && !mrFactory.getMetadataRecord(hr).matchFilter()) {
                hr.setDeleted(new Date());
                updated = true;
            }
            if (updated) {
                hr.setUpdated(new Date());
                hrDao.persist(hr);
            }
        } catch (Exception ex) {
            logger.error(String.format("Exception thrown when filtering harvested_record with id=%s", uniqueId), ex);
        }
    }
}
Also used : HarvestedRecordUniqueId(cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) MarcFactory(org.marc4j.marc.MarcFactory) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) RecordImpl(cz.mzk.recordmanager.server.marc.marc4j.RecordImpl) Date(java.util.Date) ControlField(org.marc4j.marc.ControlField) DataField(org.marc4j.marc.DataField) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) TreeSet(java.util.TreeSet) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) Record(org.marc4j.marc.Record) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) List(java.util.List) MarcFactoryImpl(cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord)

Example 13 with Record

use of org.marc4j.marc.Record in project RecordManager2 by moravianlibrary.

the class AgrovocConvertorWriter method after.

@AfterStep
protected void after() {
    try {
        writer = new PrintWriter(filename, "UTF-8");
        for (String key : altLabel.keySet()) {
            if (!prefLabel.containsKey(key)) {
                continue;
            }
            Record record = factory.newRecord();
            record.setLeader(factory.newLeader("-----nz--a22-----n--4500"));
            record.addVariableField(factory.newControlField("001", key));
            record.addVariableField(factory.newDataField("150", ' ', ' ', "a", prefLabel.get(key).get(0)));
            addLabel(record, key, altLabel);
            writer.println((new MarcRecordImpl(record)).export(IOFormat.LINE_MARC));
        }
        writer.close();
    } catch (FileNotFoundException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}
Also used : MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Record(org.marc4j.marc.Record) PrintWriter(java.io.PrintWriter) AfterStep(org.springframework.batch.core.annotation.AfterStep)

Example 14 with Record

use of org.marc4j.marc.Record in project RecordManager2 by moravianlibrary.

the class GenerateItemIdWriter method write.

@Override
public void write(List<? extends HarvestedRecordUniqueId> items) throws Exception {
    for (HarvestedRecordUniqueId uniqueId : items) {
        try {
            progress.incrementAndLogProgress();
            HarvestedRecord hr = hrDao.get(uniqueId);
            if (hr == null || hr.getRawRecord() == null || hr.getRawRecord().length == 0 || !hr.getFormat().equals("marc21-xml") || hr.getHarvestedFrom().getItemId() == null) {
                continue;
            }
            Record record = marcXmlParser.parseUnderlyingRecord(new ByteArrayInputStream(hr.getRawRecord()));
            hr.setRawRecord(new DefaultMarcInterceptor(record, hr.getHarvestedFrom(), uniqueId.getRecordId()).intercept());
        } catch (Exception ex) {
            logger.error(String.format("Exception thrown in harvested_record with id=%s", uniqueId), ex);
        }
    }
}
Also used : HarvestedRecordUniqueId(cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId) ByteArrayInputStream(java.io.ByteArrayInputStream) Record(org.marc4j.marc.Record) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) DefaultMarcInterceptor(cz.mzk.recordmanager.server.marc.intercepting.DefaultMarcInterceptor) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord)

Example 15 with Record

use of org.marc4j.marc.Record in project RecordManager2 by moravianlibrary.

the class CosmotronHarvestJobTest method test996BeforeRecord.

@Test
public void test996BeforeRecord() throws Exception {
    reset(httpClient);
    InputStream response0 = this.getClass().getResourceAsStream("/sample/Identify.xml");
    InputStream response1 = this.getClass().getResourceAsStream("/sample/cosmotron/996BeforeRecord.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);
    HarvestedRecord hr = recordDao.findByIdAndHarvestConfiguration("CbvkUsCat" + Constants.COSMOTRON_RECORD_ID_CHAR + "m0000002", config);
    Assert.assertNotNull(hr);
    Assert.assertNotNull(cosmotronDao.findByIdAndHarvestConfiguration("CbvkUsCat" + Constants.COSMOTRON_RECORD_ID_CHAR + "0000003", config));
    Assert.assertNotNull(cosmotronDao.findByIdAndHarvestConfiguration("CbvkUsCat" + Constants.COSMOTRON_RECORD_ID_CHAR + "0000004", config));
    Assert.assertNotNull(cosmotronDao.findByIdAndHarvestConfiguration("CbvkUsCat" + Constants.COSMOTRON_RECORD_ID_CHAR + "0000005", config));
    InputStream is = new ByteArrayInputStream(hr.getRawRecord());
    Record record = marcXmlParser.parseUnderlyingRecord(is);
    MarcRecord marcRecord = new MarcRecordImpl(record);
    Assert.assertEquals(marcRecord.getDataFields("996").size(), 3);
}
Also used : OAIHarvestConfiguration(cz.mzk.recordmanager.server.model.OAIHarvestConfiguration) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) JobExecution(org.springframework.batch.core.JobExecution) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) Record(org.marc4j.marc.Record) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) JobParameters(org.springframework.batch.core.JobParameters) JobParameter(org.springframework.batch.core.JobParameter) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) Test(org.testng.annotations.Test) AbstractTest(cz.mzk.recordmanager.server.AbstractTest)

Aggregations

Record (org.marc4j.marc.Record)32 MarcRecordImpl (cz.mzk.recordmanager.server.marc.MarcRecordImpl)21 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)20 DataField (org.marc4j.marc.DataField)13 ControlField (org.marc4j.marc.ControlField)12 RecordImpl (cz.mzk.recordmanager.server.marc.marc4j.RecordImpl)11 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)10 List (java.util.List)10 TreeSet (java.util.TreeSet)9 InputStream (java.io.InputStream)8 MarcFactory (org.marc4j.marc.MarcFactory)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 MarcFactoryImpl (cz.mzk.recordmanager.server.marc.marc4j.MarcFactoryImpl)6 Date (java.util.Date)6 AbstractTest (cz.mzk.recordmanager.server.AbstractTest)5 HashMap (java.util.HashMap)5 Matcher (java.util.regex.Matcher)5 MarcWriter (org.marc4j.MarcWriter)5 MarcXmlWriter (org.marc4j.MarcXmlWriter)5 JobExecution (org.springframework.batch.core.JobExecution)5