Search in sources :

Example 1 with MarcJsonReader

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;
}
Also used : MarcFieldProtectionSetting(org.folio.rest.jaxrs.model.MarcFieldProtectionSetting) ControlField(org.marc4j.marc.ControlField) ByteArrayInputStream(java.io.ByteArrayInputStream) MarcReader(org.marc4j.MarcReader) MarcJsonReader(org.marc4j.MarcJsonReader) VariableField(org.marc4j.marc.VariableField)

Example 2 with MarcJsonReader

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());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MarcReader(org.marc4j.MarcReader) MarcJsonReader(org.marc4j.MarcJsonReader) Record(org.marc4j.marc.Record) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(org.apache.commons.lang3.time.StopWatch)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 MarcJsonReader (org.marc4j.MarcJsonReader)2 MarcReader (org.marc4j.MarcReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 MarcFieldProtectionSetting (org.folio.rest.jaxrs.model.MarcFieldProtectionSetting)1 ControlField (org.marc4j.marc.ControlField)1 Record (org.marc4j.marc.Record)1 VariableField (org.marc4j.marc.VariableField)1