Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,57 @@ public BrAPIObservation updateBrAPIObservation(String dbId, BrAPIObservation obs
ObservationsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), ObservationsApi.class);
var program = programDAO.fetchOneById(programId);
try {

Callable<Map<String, BrAPIObservation>> postFunction = () -> {

ApiResponse<BrAPIObservationSingleResponse> response = api.observationsObservationDbIdPut(dbId, observation);
if (response == null)
{
if (response == null) {
throw new ApiException("Response is null", 0, null, null);
}
BrAPIObservationSingleResponse body = response.getBody();
if (body == null) {
throw new ApiException("Response is missing body", 0, response.getHeaders(), null);
}
BrAPIObservation updatedObservation = body.getResult();
if (updatedObservation == null) {
throw new ApiException("Response body is missing result", 0, response.getHeaders(), response.getBody().toString());
}
return processObservationsForCache(List.of(updatedObservation), program.getKey());
};
// returns ListArray<> = postFunction.call().values()
return programObservationCache.post(programId, postFunction).get(0);
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e));
throw new InternalServerException("Unknown error has occurred: " + e.getMessage(), e);
} catch (Exception e) {
throw new InternalServerException("Unknown error has occurred: " + e.getMessage(), e);
}
}

// This method overloads updateBrAPIObservation(String dbId, BrAPIObservation observation, UUID programId)
// It was added to increase efficiency. It insures that ProgramCache<BrAPIObservation>.populate() is called only once
// not once per observation.
public void updateBrAPIObservation(Map<String, BrAPIObservation> mutatedObservationByDbId, UUID programId) throws ApiException {
ObservationsApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), ObservationsApi.class);
var program = programDAO.fetchOneById(programId);

List <BrAPIObservation> updatedObservations = new ArrayList<>();
try {
Map<String, BrAPIObservation> updatedObservationsByDbId = new HashMap<>();
for (Map.Entry<String, BrAPIObservation> entry : mutatedObservationByDbId.entrySet()) {
String dbId = entry.getKey();
BrAPIObservation observation = entry.getValue();
if (observation == null) {
throw new Exception("Null observation");
}

ApiResponse<BrAPIObservationSingleResponse> response = null;
try {
response = api.observationsObservationDbIdPut(dbId, observation);
} catch (ApiException e) {
throw new RuntimeException(e);
}
if (response == null) {
throw new ApiException("Response is null", 0, null, null);
}
BrAPIObservationSingleResponse body = response.getBody();
Expand All @@ -264,14 +311,29 @@ public BrAPIObservation updateBrAPIObservation(String dbId, BrAPIObservation obs
if (updatedObservation == null) {
throw new ApiException("Response body is missing result", 0, response.getHeaders(), response.getBody().toString());
}
return processObservationsForCache(List.of(updatedObservation), program.getKey());
};
return programObservationCache.post(programId, postFunction).get(0);
} catch (ApiException e) {
log.error(Utilities.generateApiExceptionLogMessage(e));
throw new InternalServerException("Unknown error has occurred: " + e.getMessage(), e);
} catch (Exception e) {
throw new InternalServerException("Unknown error has occurred: " + e.getMessage(), e);
}
updatedObservations.add(updatedObservation);

if (!Objects.equals(observation.getValue(), updatedObservation.getValue())
|| !Objects.equals(observation.getObservationTimeStamp(), updatedObservation.getObservationTimeStamp())) {
String message;
if (!Objects.equals(observation.getValue(), updatedObservation.getValue())) {
message = String.format("Updated observation, %s, from BrAPI service does not match requested update %s.", updatedObservation.getValue(), observation.getValue());
} else {
message = String.format("Updated observation timestamp, %s, from BrAPI service does not match requested update timestamp %s.", updatedObservation.getObservationTimeStamp(), observation.getObservationTimeStamp());
}
throw new Exception(message);
}
}

} catch (ApiException e) {
log.error("Error updating observation: " + Utilities.generateApiExceptionLogMessage(e), e);
throw new InternalServerException("Error saving experiment import", e);
} catch (Exception e) {
log.error("Error updating observation: ", e);
throw new InternalServerException(e.getMessage(), e);
}
processObservationsForCache(updatedObservations, program.getKey());
programObservationCache.populate(programId);
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,16 @@ public void validateDependencies(Map<Integer, PendingImport> mappedBrAPIImport)
}

@Override
public void postBrapiData(Map<Integer, PendingImport> mappedBrAPIImport, Program program, ImportUpload upload) {
public void postBrapiData(Map<Integer, PendingImport> mappedBrAPIImport, Program program, ImportUpload upload) {
log.debug("starting post of experiment data to BrAPI server");

List<BrAPITrial> newTrials = ProcessorData.getNewObjects(this.trialByNameNoScope);
Map<String, BrAPITrial> mutatedTrialsById = ProcessorData
.getMutationsByObjectId(trialByNameNoScope, BrAPITrial::getTrialDbId);

//getMutationsByObjectId() will return a HashMap of just the mutated Observations,
// key = BrAPIObservation::getObservationDbId
// value = mutated BrAPIObservation
Map<String, BrAPIObservation> mutatedObservationByDbId = ProcessorData
.getMutationsByObjectId(observationByHash, BrAPIObservation::getObservationDbId);

Expand Down Expand Up @@ -428,36 +431,16 @@ public void postBrapiData(Map<Integer, PendingImport> mappedBrAPIImport, Program
throw new InternalServerException(e.getMessage(), e);
}
});

mutatedObservationByDbId.forEach((id, observation) -> {
try {
if (observation == null) {
throw new Exception("Null observation");
}
BrAPIObservation updatedObs = brAPIObservationDAO.updateBrAPIObservation(id, observation, program.getId());

if (updatedObs == null) {
throw new Exception("Null updated observation");
}

if (!Objects.equals(observation.getValue(), updatedObs.getValue())
|| !Objects.equals(observation.getObservationTimeStamp(), updatedObs.getObservationTimeStamp())) {
String message;
if(!Objects.equals(observation.getValue(), updatedObs.getValue())) {
message = String.format("Updated observation, %s, from BrAPI service does not match requested update %s.", updatedObs.getValue(), observation.getValue());
} else {
message = String.format("Updated observation timestamp, %s, from BrAPI service does not match requested update timestamp %s.", updatedObs.getObservationTimeStamp(), observation.getObservationTimeStamp());
}
throw new Exception(message);
}
} catch (ApiException e) {
try {
brAPIObservationDAO.updateBrAPIObservation( mutatedObservationByDbId, program.getId() );
} catch (ApiException e) {
log.error("Error updating observation: " + Utilities.generateApiExceptionLogMessage(e), e);
throw new InternalServerException("Error saving experiment import", e);
} catch (Exception e) {
log.error("Error updating observation: ", e);
throw new InternalServerException(e.getMessage(), e);
}
});

log.debug("experiment import complete");
}

Expand Down