use of org.marc4j.MarcWriter 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;
}
use of org.marc4j.MarcWriter 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;
}
}
}
}
use of org.marc4j.MarcWriter 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 org.marc4j.MarcWriter in project RecordManager2 by moravianlibrary.
the class ImportTezaurusRecordsWriter method write.
@Override
public void write(List<? extends List<Record>> items) throws Exception {
if (config == null) {
config = oaiHarvestConfDao.get(confId);
}
for (List<Record> records : items) {
for (Record currentRecord : records) {
try {
MarcRecord marc = new MarcRecordImpl(currentRecord);
MetadataRecord metadata = metadataFactory.getMetadataRecord(marc, config);
String recordId = metadata.getUniqueId();
TezaurusRecord tr = tezaurusDao.findByIdAndHarvestConfiguration(recordId, config);
if (tr == null) {
tr = new TezaurusRecord();
tr.setRecordId(recordId);
tr.setHarvestedFrom(config);
}
tr.setTezaurusKey(metadata.getTezaurusKey());
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
MarcWriter marcWriter = new MarcXmlWriter(outStream, true);
marcWriter.write(currentRecord);
marcWriter.close();
byte[] recordContent = outStream.toByteArray();
tr.setRawRecord(recordContent);
tezaurusDao.persist(tr);
} catch (Exception e) {
logger.warn("Error occured in processing record");
throw e;
}
}
}
}
use of org.marc4j.MarcWriter in project OpenRefine by OpenRefine.
the class MarcImporter method createParserUIInitializationData.
@Override
public ObjectNode createParserUIInitializationData(ImportingJob job, List<ObjectNode> fileRecords, String format) {
if (fileRecords.size() > 0) {
ObjectNode 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);
}
}
ObjectNode options = super.createParserUIInitializationData(job, fileRecords, format);
return options;
}
Aggregations