Search in sources :

Example 11 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-storage by folio-org.

the class AdditionalFieldsUtil method getValueFromControlledField.

/**
 * Read value from controlled field in marc record
 *
 * @param record marc record
 * @param tag    tag to read
 * @return value from field
 */
public static String getValueFromControlledField(Record record, String tag) {
    try {
        MarcReader reader = buildMarcReader(record);
        if (reader.hasNext()) {
            org.marc4j.marc.Record marcRecord = reader.next();
            Optional<ControlField> controlField = marcRecord.getControlFields().stream().filter(field -> field.getTag().equals(tag)).findFirst();
            if (controlField.isPresent()) {
                return controlField.get().getData();
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to read controlled field {) from record {}", tag, record.getId(), e);
        return null;
    }
    return null;
}
Also used : MarcWriter(org.marc4j.MarcWriter) StringUtils(org.apache.commons.lang.StringUtils) MarcJsonWriter(org.marc4j.MarcJsonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZonedDateTime(java.time.ZonedDateTime) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) MarcJsonReader(org.marc4j.MarcJsonReader) Subfield(org.marc4j.marc.Subfield) Pair(org.apache.commons.lang3.tuple.Pair) ByteArrayInputStream(java.io.ByteArrayInputStream) MarcFactory(org.marc4j.marc.MarcFactory) DataField(org.marc4j.marc.DataField) JsonObject(io.vertx.core.json.JsonObject) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) PostProcessingException(org.folio.services.exceptions.PostProcessingException) Record(org.folio.rest.jaxrs.model.Record) ParsedRecordDaoUtil(org.folio.dao.util.ParsedRecordDaoUtil) Instant(java.time.Instant) MarcReader(org.marc4j.MarcReader) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) String.format(java.lang.String.format) MarcStreamWriter(org.marc4j.MarcStreamWriter) ControlField(org.marc4j.marc.ControlField) List(java.util.List) Logger(org.apache.logging.log4j.Logger) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) MarcFieldProtectionSetting(org.folio.rest.jaxrs.model.MarcFieldProtectionSetting) LogManager(org.apache.logging.log4j.LogManager) VariableField(org.marc4j.marc.VariableField) ControlField(org.marc4j.marc.ControlField) MarcReader(org.marc4j.MarcReader) PostProcessingException(org.folio.services.exceptions.PostProcessingException)

Example 12 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-storage by folio-org.

the class AdditionalFieldsUtil method addControlledFieldToMarcRecord.

public static boolean addControlledFieldToMarcRecord(Record record, String field, String value, boolean replace) {
    boolean result = false;
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        if (record != null && record.getParsedRecord() != null && record.getParsedRecord().getContent() != null) {
            if (replace) {
                removeField(record, field);
            }
            MarcReader reader = buildMarcReader(record);
            MarcWriter streamWriter = new MarcStreamWriter(new ByteArrayOutputStream());
            MarcJsonWriter jsonWriter = new MarcJsonWriter(os);
            MarcFactory factory = MarcFactory.newInstance();
            if (reader.hasNext()) {
                org.marc4j.marc.Record marcRecord = reader.next();
                ControlField dataField = factory.newControlField(field, value);
                marcRecord.addVariableField(dataField);
                // use stream writer to recalculate leader
                streamWriter.write(marcRecord);
                jsonWriter.write(marcRecord);
                record.setParsedRecord(record.getParsedRecord().withContent(new JsonObject(os.toString()).encode()));
                result = true;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to add additional controlled field {) to record {}", field, record.getId(), e);
    }
    return result;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) MarcFactory(org.marc4j.marc.MarcFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PostProcessingException(org.folio.services.exceptions.PostProcessingException) ControlField(org.marc4j.marc.ControlField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 13 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-storage by folio-org.

the class AdditionalFieldsUtil method addFieldToMarcRecord.

/**
 * Adds field if it does not exist and a subfield with a value to that field
 *
 * @param record   record that needs to be updated
 * @param field    field that should contain new subfield
 * @param subfield new subfield to add
 * @param value    value of the subfield to add
 * @return true if succeeded, false otherwise
 */
public static boolean addFieldToMarcRecord(Record record, String field, char subfield, String value) {
    boolean result = false;
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        if (record != null && record.getParsedRecord() != null && record.getParsedRecord().getContent() != null) {
            MarcReader reader = buildMarcReader(record);
            MarcWriter streamWriter = new MarcStreamWriter(new ByteArrayOutputStream());
            MarcJsonWriter jsonWriter = new MarcJsonWriter(os);
            MarcFactory factory = MarcFactory.newInstance();
            if (reader.hasNext()) {
                org.marc4j.marc.Record marcRecord = reader.next();
                VariableField variableField = getSingleFieldByIndicators(marcRecord.getVariableFields(field), INDICATOR, INDICATOR);
                DataField dataField;
                if (variableField != null && ((DataField) variableField).getIndicator1() == INDICATOR && ((DataField) variableField).getIndicator2() == INDICATOR) {
                    dataField = (DataField) variableField;
                    marcRecord.removeVariableField(variableField);
                    dataField.removeSubfield(dataField.getSubfield(subfield));
                } else {
                    dataField = factory.newDataField(field, INDICATOR, INDICATOR);
                }
                dataField.addSubfield(factory.newSubfield(subfield, value));
                marcRecord.addVariableField(dataField);
                // use stream writer to recalculate leader
                streamWriter.write(marcRecord);
                jsonWriter.write(marcRecord);
                record.setParsedRecord(record.getParsedRecord().withContent(new JsonObject(os.toString()).encode()));
                result = true;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to add additional subfield {} for field {} to record {}", subfield, field, record.getId(), e);
    }
    return result;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) MarcFactory(org.marc4j.marc.MarcFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VariableField(org.marc4j.marc.VariableField) PostProcessingException(org.folio.services.exceptions.PostProcessingException) DataField(org.marc4j.marc.DataField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 14 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-storage by folio-org.

the class AdditionalFieldsUtil method removeField.

/**
 * remove field from marc record
 *
 * @param record record that needs to be updated
 * @param field  tag of the field
 * @return true if succeeded, false otherwise
 */
public static boolean removeField(Record record, String field) {
    boolean result = false;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        if (record != null && record.getParsedRecord() != null && record.getParsedRecord().getContent() != null) {
            MarcReader reader = buildMarcReader(record);
            MarcWriter marcStreamWriter = new MarcStreamWriter(new ByteArrayOutputStream());
            MarcJsonWriter marcJsonWriter = new MarcJsonWriter(baos);
            if (reader.hasNext()) {
                org.marc4j.marc.Record marcRecord = reader.next();
                VariableField variableField = marcRecord.getVariableField(field);
                if (variableField != null) {
                    marcRecord.removeVariableField(variableField);
                }
                // use stream writer to recalculate leader
                marcStreamWriter.write(marcRecord);
                marcJsonWriter.write(marcRecord);
                record.setParsedRecord(record.getParsedRecord().withContent(new JsonObject(baos.toString()).encode()));
                result = true;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to remove controlled field {) from record {}", field, record.getId(), e);
    }
    return result;
}
Also used : MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) JsonObject(io.vertx.core.json.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VariableField(org.marc4j.marc.VariableField) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter) PostProcessingException(org.folio.services.exceptions.PostProcessingException)

Example 15 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-storage by folio-org.

the class AdditionalFieldsUtil method addDataFieldToMarcRecord.

/**
 * Adds new data field to marc record
 *
 * @param record record that needs to be updated
 * @param tag    tag of data field
 * @param value  value of the field to add
 * @return true if succeeded, false otherwise
 */
public static boolean addDataFieldToMarcRecord(Record record, String tag, char ind1, char ind2, char subfield, String value) {
    boolean result = false;
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        if (record != null && record.getParsedRecord() != null && record.getParsedRecord().getContent() != null) {
            MarcReader reader = buildMarcReader(record);
            MarcWriter streamWriter = new MarcStreamWriter(new ByteArrayOutputStream());
            MarcJsonWriter jsonWriter = new MarcJsonWriter(os);
            MarcFactory factory = MarcFactory.newInstance();
            if (reader.hasNext()) {
                org.marc4j.marc.Record marcRecord = reader.next();
                DataField dataField = factory.newDataField(tag, ind1, ind2);
                dataField.addSubfield(factory.newSubfield(subfield, value));
                addDataFieldInNumericalOrder(dataField, marcRecord);
                // use stream writer to recalculate leader
                streamWriter.write(marcRecord);
                jsonWriter.write(marcRecord);
                record.setParsedRecord(record.getParsedRecord().withContent(new JsonObject(os.toString()).encode()));
                result = true;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to add additional data field {) to record {}", e, tag, record.getId());
    }
    return result;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) MarcFactory(org.marc4j.marc.MarcFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PostProcessingException(org.folio.services.exceptions.PostProcessingException) DataField(org.marc4j.marc.DataField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Aggregations

MarcReader (org.marc4j.MarcReader)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 JsonObject (io.vertx.core.json.JsonObject)10 MarcJsonWriter (org.marc4j.MarcJsonWriter)10 MarcStreamWriter (org.marc4j.MarcStreamWriter)9 MarcWriter (org.marc4j.MarcWriter)9 MarcFactory (org.marc4j.marc.MarcFactory)7 VariableField (org.marc4j.marc.VariableField)7 DataField (org.marc4j.marc.DataField)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 PostProcessingException (org.folio.services.exceptions.PostProcessingException)5 Record (org.marc4j.marc.Record)5 MarcStreamReader (org.marc4j.MarcStreamReader)4 ControlField (org.marc4j.marc.ControlField)4 MarcJsonReader (org.marc4j.MarcJsonReader)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 MarcFieldProtectionSetting (org.folio.rest.jaxrs.model.MarcFieldProtectionSetting)2 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1