Search in sources :

Example 6 with MarcJsonWriter

use of org.marc4j.MarcJsonWriter in project mod-source-record-manager by folio-org.

the class MarcImportEventsHandlerTest method marcRecordToJsonContent.

private String marcRecordToJsonContent(org.marc4j.marc.Record marcRecord) {
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        MarcJsonWriter jsonWriter = new MarcJsonWriter(os);
        jsonWriter.write(marcRecord);
        return os.toString(StandardCharsets.UTF_8);
    } catch (IOException e) {
        return null;
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MarcJsonWriter(org.marc4j.MarcJsonWriter)

Example 7 with MarcJsonWriter

use of org.marc4j.MarcJsonWriter in project mod-source-record-manager 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(new String(os.toByteArray())).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) DataField(org.marc4j.marc.DataField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 8 with MarcJsonWriter

use of org.marc4j.MarcJsonWriter in project mod-source-record-manager by folio-org.

the class AdditionalFieldsUtil method addControlledFieldToMarcRecord.

/**
 * Adds new controlled field to marc record
 *
 * @param record record that needs to be updated
 * @param field  tag of controlled field
 * @param value  value of the field to add
 * @return true if succeeded, false otherwise
 */
public static boolean addControlledFieldToMarcRecord(Record record, String field, 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();
                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(new String(os.toByteArray())).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) ControlField(org.marc4j.marc.ControlField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 9 with MarcJsonWriter

use of org.marc4j.MarcJsonWriter 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 10 with MarcJsonWriter

use of org.marc4j.MarcJsonWriter 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)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 MarcJsonWriter (org.marc4j.MarcJsonWriter)12 JsonObject (io.vertx.core.json.JsonObject)10 MarcReader (org.marc4j.MarcReader)9 MarcStreamWriter (org.marc4j.MarcStreamWriter)8 MarcWriter (org.marc4j.MarcWriter)8 MarcFactory (org.marc4j.marc.MarcFactory)6 PostProcessingException (org.folio.services.exceptions.PostProcessingException)4 DataField (org.marc4j.marc.DataField)4 VariableField (org.marc4j.marc.VariableField)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ControlField (org.marc4j.marc.ControlField)2 Record (org.marc4j.marc.Record)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 MarcError (org.marc4j.MarcError)1 MarcStreamReader (org.marc4j.MarcStreamReader)1 MarcXmlReader (org.marc4j.MarcXmlReader)1