use of org.marc4j.MarcJsonWriter 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;
}
use of org.marc4j.MarcJsonWriter 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;
}
Aggregations