Search in sources :

Example 1 with Record

use of org.marc4j.marc.Record in project OpenRefine by OpenRefine.

the class MarcImporter method createParserUIInitializationData.

@Override
public JSONObject createParserUIInitializationData(ImportingJob job, java.util.List<JSONObject> fileRecords, String format) {
    if (fileRecords.size() > 0) {
        JSONObject firstFileRecord = fileRecords.get(0);
        File file = ImportingUtilities.getFile(job, firstFileRecord);
        File tempFile = new File(file.getAbsolutePath() + ".xml");
        try {
            InputStream inputStream = new FileInputStream(file);
            OutputStream outputStream = new FileOutputStream(tempFile);
            try {
                MarcWriter writer = new MarcXmlWriter(outputStream, true);
                MarcPermissiveStreamReader reader = new MarcPermissiveStreamReader(inputStream, true, true);
                while (reader.hasNext()) {
                    Record record = reader.next();
                    writer.write(record);
                }
                writer.close();
            } finally {
                try {
                    outputStream.close();
                    inputStream.close();
                    if (// write failed. Most of time because of wrong Marc format
                    tempFile.length() == 0)
                        tempFile.delete();
                    else
                        // only set json if write the temp file successfully:
                        JSONUtilities.safePut(firstFileRecord, "location", JSONUtilities.getString(firstFileRecord, "location", "") + ".xml");
                //                        file.delete(); // get rid of our original file
                } catch (IOException e) {
                // Just ignore - not much we can do anyway
                }
            }
        } catch (IOException e) {
            logger.error("Failed to create temporary XML file from MARC file", e);
        }
    }
    JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
    return options;
}
Also used : JSONObject(org.json.JSONObject) MarcPermissiveStreamReader(org.marc4j.MarcPermissiveStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) MarcXmlWriter(org.marc4j.MarcXmlWriter)

Example 2 with Record

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

the class AdresarRecordsWriter method writeInner.

protected void writeInner(List<? extends List<Record>> items) throws Exception {
    for (List<Record> records : items) {
        for (Record currentRecord : records) {
            try {
                if (currentRecord == null) {
                    continue;
                }
                MarcRecord marc = new MarcRecordImpl(currentRecord);
                String recordId = marc.getControlField("SYS");
                HarvestedRecord hr = harvestedRecordDAO.findByIdAndHarvestConfiguration(recordId, configurationId);
                if (hr == null) {
                    hr = new HarvestedRecord(new HarvestedRecordUniqueId(configurationId, recordId));
                    hr.setFormat("marc21-xml");
                }
                hr.setUpdated(new Date());
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                MarcWriter marcWriter = new MarcXmlWriter(outStream, true);
                marcWriter.setConverter(ISOCharConvertor.INSTANCE);
                marcWriter.write(currentRecord);
                marcWriter.close();
                hr.setRawRecord(outStream.toByteArray());
                harvestedRecordDAO.persist(hr);
            } catch (Exception e) {
                logger.warn("Error occured in processing record");
                throw e;
            }
        }
    }
}
Also used : HarvestedRecordUniqueId(cz.mzk.recordmanager.server.model.HarvestedRecord.HarvestedRecordUniqueId) MarcRecordImpl(cz.mzk.recordmanager.server.marc.MarcRecordImpl) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) MarcXmlWriter(org.marc4j.MarcXmlWriter) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord)

Example 3 with Record

use of org.marc4j.marc.Record 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 4 with Record

use of org.marc4j.marc.Record 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 5 with Record

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

the class MarcAlephStreamReader method nextRecord.

private Record nextRecord() {
    try {
        Record rec = factory.newRecord();
        Matcher matcher;
        parseLine(rec);
        while ((line = br.readLine()) != null) {
            if ((matcher = ID_PATTERN.matcher(line)).matches()) {
                if (lastRecordId == null) {
                    // get first record ID
                    lastRecordId = matcher.group(1);
                } else if (!matcher.group(1).equals(lastRecordId)) {
                    lastRecordId = matcher.group(1);
                    return rec;
                }
            }
            parseLine(rec);
        }
        return rec;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) Record(org.marc4j.marc.Record)

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