use of org.folio.rest.jaxrs.model.ExternalIdsHolder in project mod-source-record-storage by folio-org.
the class SourceStorageBatchApiTest method shouldReturnIdWhenRecordTypeIsMarcHoldings.
@Test
public void shouldReturnIdWhenRecordTypeIsMarcHoldings(TestContext testContext) {
postSnapshots(testContext, snapshot_1, snapshot_2);
Record recordMarcHoldings = new Record().withId(FOURTH_UUID).withSnapshotId(snapshot_2.getJobExecutionId()).withRecordType(RecordType.MARC_HOLDING).withRawRecord(rawRecord).withParsedRecord(marcRecord).withMatchedId(FOURTH_UUID).withOrder(1).withExternalIdsHolder(new ExternalIdsHolder().withInstanceId(UUID.randomUUID().toString()).withInstanceHrid(VALID_HRID)).withState(Record.State.OLD);
postRecords(testContext, record_1, record_2, record_3, recordMarcHoldings);
Async async = testContext.async();
RestAssured.given().spec(spec).body(Collections.singletonList(VALID_HRID)).when().post(SOURCE_STORAGE_BATCH_VERIFIED_RECORDS).then().statusCode(HttpStatus.SC_OK).body("invalidMarcBibIds", contains(VALID_HRID));
async.complete();
}
use of org.folio.rest.jaxrs.model.ExternalIdsHolder in project mod-source-record-storage by folio-org.
the class AdditionalFieldsUtilTest method shouldNotAdd035AndAdd001FieldsIf001And003FieldsNotExists.
@Test
public void shouldNotAdd035AndAdd001FieldsIf001And003FieldsNotExists() {
// given
String parsedContent = "{\"leader\":\"00115nam 22000731a 4500\",\"fields\":[{\"003\":\"in001\"},{\"507\":{\"subfields\":[{\"a\":\"data\"}],\"ind1\":\" \",\"ind2\":\" \"}},{\"500\":{\"subfields\":[{\"a\":\"data\"}],\"ind1\":\" \",\"ind2\":\" \"}}]}";
String expectedParsedContent = "{\"leader\":\"00086nam 22000611a 4500\",\"fields\":[{\"001\":\"in001\"},{\"507\":{\"subfields\":[{\"a\":\"data\"}],\"ind1\":\" \",\"ind2\":\" \"}},{\"500\":{\"subfields\":[{\"a\":\"data\"}],\"ind1\":\" \",\"ind2\":\" \"}}]}";
ParsedRecord parsedRecord = new ParsedRecord();
parsedRecord.setContent(parsedContent);
Record record = new Record().withId(UUID.randomUUID().toString()).withParsedRecord(parsedRecord).withGeneration(0).withState(Record.State.ACTUAL).withExternalIdsHolder(new ExternalIdsHolder().withInstanceId("001").withInstanceHrid("in001"));
JsonObject jsonObject = new JsonObject("{\"hrid\":\"in001\"}");
Pair<Record, JsonObject> pair = Pair.of(record, jsonObject);
// when
AdditionalFieldsUtil.fillHrIdFieldInMarcRecord(pair);
// then
Assert.assertEquals(expectedParsedContent, parsedRecord.getContent());
}
use of org.folio.rest.jaxrs.model.ExternalIdsHolder in project mod-source-record-storage by folio-org.
the class RecordDaoImpl method updateParsedRecords.
@Override
public Future<ParsedRecordsBatchResponse> updateParsedRecords(RecordCollection recordCollection, String tenantId) {
Promise<ParsedRecordsBatchResponse> promise = Promise.promise();
Set<String> recordTypes = new HashSet<>();
List<Record> records = new ArrayList<>();
List<String> errorMessages = new ArrayList<>();
List<UpdateConditionStep<RecordsLbRecord>> recordUpdates = new ArrayList<>();
List<UpdateConditionStep<org.jooq.Record>> parsedRecordUpdates = new ArrayList<>();
Field<UUID> prtId = field(name(ID), UUID.class);
Field<JSONB> prtContent = field(name(CONTENT), JSONB.class);
List<ParsedRecord> parsedRecords = recordCollection.getRecords().stream().map(this::validateParsedRecordId).peek(record -> {
// make sure only one record type
recordTypes.add(record.getRecordType().name());
if (recordTypes.size() > 1) {
throw new BadRequestException("Batch record collection only supports single record type");
}
UpdateSetFirstStep<RecordsLbRecord> updateFirstStep = DSL.update(RECORDS_LB);
UpdateSetMoreStep<RecordsLbRecord> updateStep = null;
// check for external record properties to update
ExternalIdsHolder externalIdsHolder = record.getExternalIdsHolder();
AdditionalInfo additionalInfo = record.getAdditionalInfo();
Metadata metadata = record.getMetadata();
if (Objects.nonNull(externalIdsHolder)) {
var recordType = record.getRecordType();
String externalId = getExternalId(externalIdsHolder, recordType);
String externalHrid = getExternalHrid(externalIdsHolder, recordType);
if (StringUtils.isNotEmpty(externalId)) {
updateStep = updateFirstStep.set(RECORDS_LB.EXTERNAL_ID, UUID.fromString(externalId));
}
if (StringUtils.isNotEmpty(externalHrid)) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.EXTERNAL_HRID, externalHrid);
}
}
if (Objects.nonNull(additionalInfo)) {
if (Objects.nonNull(additionalInfo.getSuppressDiscovery())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.SUPPRESS_DISCOVERY, additionalInfo.getSuppressDiscovery());
}
}
if (Objects.nonNull(metadata)) {
if (StringUtils.isNotEmpty(metadata.getCreatedByUserId())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.CREATED_BY_USER_ID, UUID.fromString(metadata.getCreatedByUserId()));
}
if (Objects.nonNull(metadata.getCreatedDate())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.CREATED_DATE, metadata.getCreatedDate().toInstant().atOffset(ZoneOffset.UTC));
}
if (StringUtils.isNotEmpty(metadata.getUpdatedByUserId())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.UPDATED_BY_USER_ID, UUID.fromString(metadata.getUpdatedByUserId()));
}
if (Objects.nonNull(metadata.getUpdatedDate())) {
updateStep = (Objects.isNull(updateStep) ? updateFirstStep : updateStep).set(RECORDS_LB.UPDATED_DATE, metadata.getUpdatedDate().toInstant().atOffset(ZoneOffset.UTC));
}
}
// only attempt update if has id and external values to update
if (Objects.nonNull(updateStep) && Objects.nonNull(record.getId())) {
records.add(record);
recordUpdates.add(updateStep.where(RECORDS_LB.ID.eq(UUID.fromString(record.getId()))));
}
try {
RecordType recordType = toRecordType(record.getRecordType().name());
recordType.formatRecord(record);
parsedRecordUpdates.add(DSL.update(table(name(recordType.getTableName()))).set(prtContent, JSONB.valueOf(ParsedRecordDaoUtil.normalizeContent(record.getParsedRecord()))).where(prtId.eq(UUID.fromString(record.getParsedRecord().getId()))));
} catch (Exception e) {
errorMessages.add(format(INVALID_PARSED_RECORD_MESSAGE_TEMPLATE, record.getId(), e.getMessage()));
// if invalid parsed record, set id to null to filter out
record.getParsedRecord().setId(null);
}
}).map(Record::getParsedRecord).filter(parsedRecord -> Objects.nonNull(parsedRecord.getId())).collect(Collectors.toList());
try (Connection connection = getConnection(tenantId)) {
DSL.using(connection).transaction(ctx -> {
DSLContext dsl = DSL.using(ctx);
// update records
int[] recordUpdateResults = dsl.batch(recordUpdates).execute();
// check record update results
for (int i = 0; i < recordUpdateResults.length; i++) {
int result = recordUpdateResults[i];
if (result == 0) {
errorMessages.add(format("Record with id %s was not updated", records.get(i).getId()));
}
}
// update parsed records
int[] parsedRecordUpdateResults = dsl.batch(parsedRecordUpdates).execute();
// check parsed record update results
List<ParsedRecord> parsedRecordsUpdated = new ArrayList<>();
for (int i = 0; i < parsedRecordUpdateResults.length; i++) {
int result = parsedRecordUpdateResults[i];
ParsedRecord parsedRecord = parsedRecords.get(i);
if (result == 0) {
errorMessages.add(format("Parsed Record with id '%s' was not updated", parsedRecord.getId()));
} else {
parsedRecordsUpdated.add(parsedRecord);
}
}
promise.complete(new ParsedRecordsBatchResponse().withErrorMessages(errorMessages).withParsedRecords(parsedRecordsUpdated).withTotalRecords(parsedRecordsUpdated.size()));
});
} catch (SQLException e) {
LOG.error("Failed to update records", e);
promise.fail(e);
}
return promise.future();
}
use of org.folio.rest.jaxrs.model.ExternalIdsHolder in project mod-source-record-storage by folio-org.
the class RecordDaoUtil method toExternalIdsHolder.
private static ExternalIdsHolder toExternalIdsHolder(RecordsLb pojo) {
ExternalIdsHolder externalIdsHolder = new ExternalIdsHolder();
var externalIdOptional = Optional.ofNullable(pojo.getExternalId()).map(UUID::toString);
var externalHridOptional = Optional.ofNullable(pojo.getExternalHrid());
if (RecordType.MARC_BIB == pojo.getRecordType()) {
externalIdOptional.ifPresent(externalIdsHolder::setInstanceId);
externalHridOptional.ifPresent(externalIdsHolder::setInstanceHrid);
} else if (RecordType.MARC_HOLDING == pojo.getRecordType()) {
externalIdOptional.ifPresent(externalIdsHolder::setHoldingsId);
externalHridOptional.ifPresent(externalIdsHolder::setHoldingsHrid);
} else if (RecordType.MARC_AUTHORITY == pojo.getRecordType()) {
externalIdOptional.ifPresent(externalIdsHolder::setAuthorityId);
externalHridOptional.ifPresent(externalIdsHolder::setAuthorityHrid);
}
return externalIdsHolder;
}
use of org.folio.rest.jaxrs.model.ExternalIdsHolder in project mod-source-record-manager by folio-org.
the class ChangeManagerParsedRecordsAPITest method shouldUpdateParsedRecordOnPut.
@Test
public void shouldUpdateParsedRecordOnPut(TestContext testContext) throws InterruptedException {
Async async = testContext.async();
ParsedRecordDto parsedRecordDto = new ParsedRecordDto().withId(UUID.randomUUID().toString()).withParsedRecord(new ParsedRecord().withId(UUID.randomUUID().toString()).withContent("{\"leader\":\"01240cas a2200397 4500\",\"fields\":[]}")).withRecordType(ParsedRecordDto.RecordType.MARC_HOLDING).withRelatedRecordVersion("1").withExternalIdsHolder(new ExternalIdsHolder().withInstanceId(UUID.randomUUID().toString()));
RestAssured.given().spec(spec).body(parsedRecordDto).when().put(PARSED_RECORDS_URL + "/" + parsedRecordDto.getId()).then().statusCode(HttpStatus.SC_ACCEPTED);
String observeTopic = formatTopicName(kafkaConfig.getEnvId(), getDefaultNameSpace(), TENANT_ID, QMEventTypes.QM_RECORD_UPDATED.name());
kafkaCluster.observeValues(ObserveKeyValues.on(observeTopic, 1).observeFor(30, TimeUnit.SECONDS).build());
async.complete();
}
Aggregations