Search in sources :

Example 1 with MarcReader

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

the class MarcRecordParser method parseRecord.

@Override
public ParsedResult parseRecord(String rawRecord) {
    ParsedResult result = new ParsedResult();
    try {
        MarcReader reader = new MarcStreamReader(new ByteArrayInputStream(rawRecord.getBytes(DEFAULT_CHARSET)), DEFAULT_CHARSET.name());
        if (reader.hasNext()) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            MarcJsonWriter writer = new MarcJsonWriter(os);
            Record record = reader.next();
            List<MarcError> errorList = record.getErrors();
            if (errorList == null || errorList.isEmpty()) {
                writer.write(record);
                result.setParsedRecord(new JsonObject(new String(os.toByteArray())));
            } else {
                List<JsonObject> preparedErrors = new ArrayList<>();
                errorList.forEach(e -> preparedErrors.add(buildErrorObject(e)));
                prepareResultWithError(result, preparedErrors);
            }
            return result;
        } else {
            result.setParsedRecord(new JsonObject());
        }
    } catch (Exception e) {
        LOGGER.error("Error during parse MARC record from raw record", e);
        prepareResultWithError(result, Collections.singletonList(new JsonObject().put("name", e.getClass().getName()).put("message", e.getMessage())));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) MarcStreamReader(org.marc4j.MarcStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MarcReader(org.marc4j.MarcReader) Record(org.marc4j.marc.Record) MarcError(org.marc4j.MarcError) MarcJsonWriter(org.marc4j.MarcJsonWriter)

Example 2 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-manager 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(new String(baos.toByteArray())).encode()));
                result = true;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to remove controlled field {} from record {}", field, record.getId(), e);
    }
    return result;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VariableField(org.marc4j.marc.VariableField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 3 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-manager 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(new String(os.toByteArray())).encode()));
                result = true;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to add additional data field {} to record {}", tag, record.getId(), e);
    }
    return result;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) MarcFactory(org.marc4j.marc.MarcFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataField(org.marc4j.marc.DataField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 4 with MarcReader

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

the class AdditionalFieldsUtil method hasIndicator.

public static boolean hasIndicator(Record record, char subfield) {
    MarcReader reader = buildMarcReader(record);
    if (reader.hasNext()) {
        org.marc4j.marc.Record marcRecord = reader.next();
        VariableField variableField = getSingleFieldByIndicators(marcRecord.getVariableFields(TAG_999), INDICATOR, INDICATOR);
        return Objects.nonNull(variableField) && Objects.nonNull(((DataField) variableField).getSubfield(subfield));
    }
    return false;
}
Also used : DataField(org.marc4j.marc.DataField) MarcReader(org.marc4j.MarcReader) VariableField(org.marc4j.marc.VariableField)

Example 5 with MarcReader

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

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