Skip to content

Commit

Permalink
MODSOURCE-785: Update 005 field when set MARC for deletion. (#632)
Browse files Browse the repository at this point in the history
* MODSOURCE-785: Implementation added.

* MODSOURCE-785: Implementation added[2].

* MODSOURCE-785: Reordering 005 field added.

* MODSOURCE-785: Test case improved.

* MODSOURCE-785: NEWS updated.

* MODSOURCE-785: Code improved.

* MODSOURCE-785: Code improved[2].

* MODSOURCE-785: NEWS updated.
  • Loading branch information
VRohach authored Jul 24, 2024
1 parent 1d7da94 commit 2aa801b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [MODINV-1044](https://folio-org.atlassian.net/browse/MODINV-1044) Additional Requirements - Update Data Import logic to normalize OCLC 035 values
* [MODSOURMAN-1200](https://folio-org.atlassian.net/browse/MODSOURMAN-1200) Find record by match id on update generation
* [MODINV-1049](https://folio-org.atlassian.net/browse/MODINV-1049) Existing "035" field is not retained the original position in imported record
* [MODSOURCE-785](https://folio-org.atlassian.net/browse/MODSOURCE-785) Update 005 field when set MARC for deletion

## 2024-03-20 5.8.0
* [MODSOURCE-733](https://issues.folio.org/browse/MODSOURCE-733) Reduce Memory Allocation of Strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,4 @@ public static JsonObject normalize(Object content) {
? new JsonObject((String) content)
: JsonObject.mapFrom(content);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import static org.folio.dao.util.MarcUtil.reorderMarcRecordFields;
import static org.folio.dao.util.RecordDaoUtil.RECORD_NOT_FOUND_TEMPLATE;
import static org.folio.dao.util.RecordDaoUtil.ensureRecordForeignKeys;
import static org.folio.dao.util.RecordDaoUtil.ensureRecordHasId;
Expand Down Expand Up @@ -55,6 +56,7 @@
import org.folio.rest.jaxrs.model.RecordMatchingDto;
import org.folio.rest.jaxrs.model.RecordsIdentifiersCollection;
import org.folio.services.exceptions.DuplicateRecordException;
import org.folio.services.util.AdditionalFieldsUtil;
import org.folio.services.util.TypeConnection;
import org.jooq.Condition;
import org.jooq.OrderField;
Expand Down Expand Up @@ -342,6 +344,7 @@ public Future<Void> deleteRecordById(String id, IdType idType, String tenantId)
return recordDao.getRecordByExternalId(id, idType, tenantId)
.map(recordOptional -> recordOptional.orElseThrow(() -> new NotFoundException(format(NOT_FOUND_MESSAGE, Record.class.getSimpleName(), id))))
.map(record -> {
update005field(record);
record.withState(Record.State.DELETED);
record.setAdditionalInfo(record.getAdditionalInfo().withSuppressDiscovery(true));
ParsedRecordDaoUtil.updateLeaderStatus(record.getParsedRecord(), DELETED_LEADER_RECORD_STATUS);
Expand Down Expand Up @@ -496,4 +499,14 @@ private Future<RecordsIdentifiersCollection> processDefaultMatchField(MatchField
.withIdentifiers(identifiers).withTotalRecords(recordCollection.getTotalRecords()))));
}

private static void update005field(Record targetRecord) {
if (targetRecord.getParsedRecord() != null && targetRecord.getParsedRecord().getContent() != null) {
AdditionalFieldsUtil.updateLatestTransactionDate(targetRecord);
var sourceContent = targetRecord.getParsedRecord().getContent().toString();
var targetContent = targetRecord.getParsedRecord().getContent().toString();
var content = reorderMarcRecordFields(sourceContent, targetContent);
targetRecord.getParsedRecord().setContent(content);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -623,17 +623,25 @@ private static VariableField getSingleFieldByIndicators(List<VariableField> list
/**
* Updates field 005 for case when this field is not protected.
*
* @param record record to update
* @param targetRecord record to update
* @param mappingParameters mapping parameters
*/
public static void updateLatestTransactionDate(Record record, MappingParameters mappingParameters) {
if (isField005NeedToUpdate(record, mappingParameters)) {
public static void updateLatestTransactionDate(Record targetRecord, MappingParameters mappingParameters) {
if (isField005NeedToUpdate(targetRecord, mappingParameters)) {
updateLatestTransactionDate(targetRecord);
}
}

/**
* Updates field 005.
* @param targetRecord record to update
*/
public static void updateLatestTransactionDate(Record targetRecord) {
String date = AdditionalFieldsUtil.dateTime005Formatter.format(ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
boolean isLatestTransactionDateUpdated = AdditionalFieldsUtil.addControlledFieldToMarcRecord(record, AdditionalFieldsUtil.TAG_005, date, true);
boolean isLatestTransactionDateUpdated = AdditionalFieldsUtil.addControlledFieldToMarcRecord(targetRecord, AdditionalFieldsUtil.TAG_005, date, true);
if (!isLatestTransactionDateUpdated) {
throw new PostProcessingException(format("Failed to update field '005' to record with id '%s'", record.getId()));
throw new PostProcessingException(format("Failed to update field '005' to record with id '%s'", targetRecord.getId()));
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import static org.hamcrest.Matchers.nullValue;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -832,15 +834,18 @@ public void shouldDeleteExistingMarcRecordOnDeleteByRecordId(TestContext testCon
}

@Test
public void shouldDeleteExistingMarcRecordOnDeleteByInstanceId(TestContext testContext) {
public void shouldDeleteExistingMarcRecordOnDeleteByInstanceIdAndUpdate005FieldWithCurrentDate(TestContext testContext) {
postSnapshots(testContext, snapshot_1);

String srsId = UUID.randomUUID().toString();
String instanceId = UUID.randomUUID().toString();

String currentDate = "20240718132044.6";
ParsedRecord parsedRecord = new ParsedRecord().withId(srsId)
.withContent(new JsonObject().put("leader", "01542ccm a2200361 4500")
.put("fields", new JsonArray().add(new JsonObject().put("999", new JsonObject()
.put("fields", new JsonArray()
.add(new JsonObject().put("005", currentDate))
.add(new JsonObject().put("999", new JsonObject()
.put("subfields", new JsonArray().add(new JsonObject().put("s", srsId)).add(new JsonObject().put("i", instanceId)))))));

Record newRecord = new Record()
Expand Down Expand Up @@ -887,6 +892,13 @@ public void shouldDeleteExistingMarcRecordOnDeleteByInstanceId(TestContext testC
Assert.assertEquals(true, deletedRecord.getAdditionalInfo().getSuppressDiscovery());
Assert.assertEquals("d", ParsedRecordDaoUtil.getLeaderStatus(deletedRecord.getParsedRecord()));

//Complex verifying "005" field is NOT empty inside parsed record.
LinkedHashMap<String, ArrayList<LinkedHashMap<String, String>>> content = (LinkedHashMap<String, ArrayList<LinkedHashMap<String, String>>>) deletedRecord.getParsedRecord().getContent();
LinkedHashMap<String, String> map = content.get("fields").get(0);
String resulted005FieldValue = map.get("005");
Assert.assertNotNull(resulted005FieldValue);
Assert.assertNotEquals(currentDate, resulted005FieldValue);

async.complete();
}

Expand Down

0 comments on commit 2aa801b

Please sign in to comment.