Skip to content

Commit

Permalink
MODSOURMAN-1070: Fill in Journal Records for created MARC when INSTAN…
Browse files Browse the repository at this point in the history
…CE_CREATED event received (#834)

MODSOURMAN-1070: Filled in two journals records for Instance and Marc bib during INSTANCE_CREATED event type
  • Loading branch information
Maksat-Galymzhan authored and yaroslav-epam committed Jan 4, 2024
1 parent 56334a0 commit ac69b58
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [MODSOURMAN-1020](https://issues.folio.org/browse/MODSOURMAN-1020) Add table to save incoming records for DI logs
* [MODSOURMAN-1021](https://issues.folio.org/browse/MODSOURMAN-1021) Provide endpoint for getting parsed content for DI log
* [MODSOURMAN-1022](https://issues.folio.org/browse/MODSOURMAN-1022) Remove step of initial saving of incoming records to SRS
* [MODSOURMAN-1070](https://issues.folio.org/browse/MODSOURMAN-1070) Fill in Journal Records for created MARC when INSTANCE_CREATED event received
* [MODSOURMAN-1030](https://issues.folio.org/browse/MODSOURMAN-1030) The number of updated records is not correct displayed in the 'SRS Marc' column in the 'Log summary' table
* [MODSOURMAN-976](https://issues.folio.org/browse/MODSOURMAN-976) Incorrect error counts
* [MODSOURMAN-1093](https://issues.folio.org/browse/MODSOURMAN-1093) EventHandlingUtil hangs forever on error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_ERROR;
import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_INVENTORY_INSTANCE_CREATED;
import static org.folio.rest.jaxrs.model.JournalRecord.EntityType.AUTHORITY;
import static org.folio.rest.jaxrs.model.JournalRecord.EntityType.HOLDINGS;
import static org.folio.rest.jaxrs.model.JournalRecord.EntityType.INSTANCE;
Expand All @@ -52,6 +53,7 @@ public class JournalUtil {
private static final String NOT_MATCHED_NUMBER = "NOT_MATCHED_NUMBER";
public static final String PERMANENT_LOCATION_ID_KEY = "permanentLocationId";
private static final String CENTRAL_TENANT_ID_KEY = "CENTRAL_TENANT_ID";
private static final String CURRENT_EVENT_TYPE = "CURRENT_EVENT_TYPE";

private JournalUtil() {

Expand Down Expand Up @@ -121,17 +123,8 @@ record = new ObjectMapper().readValue(recordAsString, Record.class);
}

String entityAsString = eventPayloadContext.get(entityType.value());
JournalRecord journalRecord = new JournalRecord()
.withJobExecutionId(record.getSnapshotId())
.withSourceId(record.getId())
.withSourceRecordOrder(record.getOrder())
.withEntityType(entityType)
.withActionType(actionType)
.withActionDate(new Date())
.withActionStatus(actionStatus)
// tenantId field is filled in only for the case when record/entity has been changed on central tenant
// by data import initiated on a member tenant
.withTenantId(eventPayload.getContext().get(CENTRAL_TENANT_ID_KEY));
JournalRecord journalRecord = buildCommonJournalRecord(actionStatus, actionType, record, eventPayload)
.withEntityType(entityType);

if (DI_ERROR == DataImportEventTypes.fromValue(eventPayload.getEventType())) {
journalRecord.setError(eventPayloadContext.get(ERROR_KEY));
Expand Down Expand Up @@ -164,6 +157,13 @@ record = new ObjectMapper().readValue(recordAsString, Record.class);
if (entityType == PO_LINE) {
journalRecord.setOrderId(entityJson.getString("purchaseOrderId"));
}
if (eventPayload.getEventType().equals(DI_INVENTORY_INSTANCE_CREATED.value()) ||
(eventPayloadContext.containsKey(CURRENT_EVENT_TYPE)
&& DataImportEventTypes.fromValue(eventPayloadContext.get(CURRENT_EVENT_TYPE)) == DI_INVENTORY_INSTANCE_CREATED
&& entityType.equals(INSTANCE))) {
var journalRecordWithMarcBib = buildJournalRecordWithMarcBibType(actionStatus, actionType, record, eventPayload, eventPayloadContext);
return Lists.newArrayList(journalRecord, journalRecordWithMarcBib);
}
return Lists.newArrayList(journalRecord);
}
if ((entityType == HOLDINGS || entityType == ITEM || eventPayloadContext.get(MULTIPLE_ERRORS_KEY) != null)
Expand All @@ -190,6 +190,32 @@ record = new ObjectMapper().readValue(recordAsString, Record.class);
}
}

private static JournalRecord buildJournalRecordWithMarcBibType(JournalRecord.ActionStatus actionStatus, JournalRecord.ActionType actionType, Record currentRecord,
DataImportEventPayload eventPayload, HashMap<String, String> eventPayloadContext) {
String marcBibEntityAsString = eventPayloadContext.get(MARC_BIBLIOGRAPHIC.value());
String marcBibEntityId = new JsonObject(marcBibEntityAsString).getString(ID_KEY);

return buildCommonJournalRecord(actionStatus, actionType, currentRecord, eventPayload)
.withEntityId(marcBibEntityId)
.withEntityType(MARC_BIBLIOGRAPHIC);
}

private static JournalRecord buildCommonJournalRecord(JournalRecord.ActionStatus actionStatus, JournalRecord.ActionType actionType, Record currentRecord,
DataImportEventPayload eventPayload){
String tenantId = eventPayload.getContext().get(CENTRAL_TENANT_ID_KEY);

return new JournalRecord()
.withJobExecutionId(currentRecord.getSnapshotId())
.withSourceId(currentRecord.getId())
.withSourceRecordOrder(currentRecord.getOrder())
.withActionType(actionType)
.withActionDate(new Date())
.withActionStatus(actionStatus)
// tenantId field is filled in only for the case when record/entity has been changed on central tenant
// by data import initiated on a member tenant
.withTenantId(tenantId);
}

private static List<JournalRecord> processHoldings(JournalRecord.ActionType actionType, JournalRecord.EntityType entityType,
JournalRecord.ActionStatus actionStatus, HashMap<String, String> eventPayloadContext, Record record) {
JsonArray multipleHoldings = getJsonArrayOfHoldings(eventPayloadContext.get(entityType.value()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
public class JournalUtilTest {

private static final String CENTRAL_TENANT_ID_KEY = "CENTRAL_TENANT_ID";
private static final String CURRENT_EVENT_TYPE = "CURRENT_EVENT_TYPE";

@Test
public void shouldBuildJournalRecordsByRecordsWithoutError() {
Expand Down Expand Up @@ -160,6 +161,48 @@ public void shouldBuildJournalRecordForInstance() throws JournalRecordMapperExce
Assert.assertNotNull(journalRecord.get(0).getActionDate());
}

@Test
public void shouldBuildTwoJournalRecordWithInstanceCreatedEvent() throws JournalRecordMapperException {
String instanceId = UUID.randomUUID().toString();
String instanceHrid = UUID.randomUUID().toString();

JsonObject instanceJson = new JsonObject()
.put("id", instanceId)
.put("hrid", instanceHrid);

String recordId = UUID.randomUUID().toString();
String snapshotId = UUID.randomUUID().toString();

JsonObject recordJson = new JsonObject()
.put("id", recordId)
.put("snapshotId", snapshotId)
.put("order", 1);

HashMap<String, String> context = new HashMap<>();
context.put(INSTANCE.value(), instanceJson.encode());
context.put(MARC_BIBLIOGRAPHIC.value(), recordJson.encode());
context.put(CURRENT_EVENT_TYPE, "DI_INVENTORY_INSTANCE_CREATED");

DataImportEventPayload eventPayload = new DataImportEventPayload()
.withEventType("DI_COMPLETED")
.withContext(context);

List<JournalRecord> journalRecord = JournalUtil.buildJournalRecordsByEvent(eventPayload,
CREATE, INSTANCE, COMPLETED);

Assert.assertNotNull(journalRecord);
Assert.assertEquals(2, journalRecord.size());
Assert.assertEquals(snapshotId, journalRecord.get(0).getJobExecutionId());
Assert.assertEquals(recordId, journalRecord.get(0).getSourceId());
Assert.assertEquals(1, journalRecord.get(0).getSourceRecordOrder().intValue());
Assert.assertEquals(INSTANCE, journalRecord.get(0).getEntityType());
Assert.assertEquals(instanceId, journalRecord.get(0).getEntityId());
Assert.assertEquals(instanceHrid, journalRecord.get(0).getEntityHrId());
Assert.assertEquals(CREATE, journalRecord.get(0).getActionType());
Assert.assertEquals(COMPLETED, journalRecord.get(0).getActionStatus());
Assert.assertNotNull(journalRecord.get(0).getActionDate());
}

@Test
public void shouldBuildJournalRecordForAuthority() throws JournalRecordMapperException {
String authorityId = UUID.randomUUID().toString();
Expand Down

0 comments on commit ac69b58

Please sign in to comment.