use of org.marc4j.marc.VariableField in project RecordManager2 by moravianlibrary.
the class RecordImpl method getVariableFields.
/**
* Gets a {@link List} of {@link VariableField}s with the supplied tag.
*/
public List<VariableField> getVariableFields(String aTag) {
List<VariableField> fields = new ArrayList<VariableField>();
Iterator<? extends VariableField> iterator = getIterator(aTag);
while (iterator.hasNext()) {
VariableField field = iterator.next();
if (field.getTag().equals(aTag)) {
fields.add(field);
}
}
return fields;
}
use of org.marc4j.marc.VariableField 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;
}
use of org.marc4j.marc.VariableField 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;
}
use of org.marc4j.marc.VariableField 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;
}
use of org.marc4j.marc.VariableField 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;
}
Aggregations