use of org.marc4j.MarcJsonReader in project mod-source-record-storage by folio-org.
the class AdditionalFieldsUtil method isField005NeedToUpdate.
/**
* Checks whether field 005 needs to be updated or this field is protected.
*
* @param record record to check
* @param mappingParameters
* @return true for case when field 005 have to updated
*/
private static boolean isField005NeedToUpdate(Record record, MappingParameters mappingParameters) {
boolean needToUpdate = true;
List<MarcFieldProtectionSetting> fieldProtectionSettings = mappingParameters.getMarcFieldProtectionSettings();
if ((fieldProtectionSettings != null) && !fieldProtectionSettings.isEmpty()) {
MarcReader reader = new MarcJsonReader(new ByteArrayInputStream(record.getParsedRecord().getContent().toString().getBytes()));
if (reader.hasNext()) {
org.marc4j.marc.Record marcRecord = reader.next();
for (VariableField field : marcRecord.getVariableFields(AdditionalFieldsUtil.TAG_005)) {
needToUpdate = isNotProtected(fieldProtectionSettings, (ControlField) field);
break;
}
}
}
return needToUpdate;
}
use of org.marc4j.MarcJsonReader in project mod-oai-pmh by folio-org.
the class MarcXmlMapper method convert.
/**
* Convert MarcJson to MarcXML.
*
* @param source String representation of MarcJson source.
* @return byte[] representation of MarcXML
*/
public byte[] convert(String source) {
var operationId = UUID.randomUUID().toString();
metricsCollectingService.startMetric(operationId, PARSE_XML);
StopWatch timer = logger.isDebugEnabled() ? StopWatch.createStarted() : null;
/*
* Fix indicators which comes like "ind1": "\\" in the source string and values are converted to '\'
* which contradicts to the MARC21slim.xsd schema. So replacing unexpected char by space
*/
source = DOUBLE_BACKSLASH_PATTERN.matcher(source).replaceAll(" ");
try (InputStream inputStream = new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8))) {
MarcReader marcJsonReader = new MarcJsonReader(inputStream);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Record record = marcJsonReader.next();
MarcXmlWriter.writeSingleRecord(record, out, false, false);
return out.toByteArray();
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
metricsCollectingService.endMetric(operationId, PARSE_XML);
if (timer != null) {
timer.stop();
logger.debug("Marc-json converted to MarcXml after {} ms.", timer.getTime());
}
}
}
Aggregations