Skip to content
Merged
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 @@ -295,4 +295,61 @@ public BrAPIObservation updateBrAPIObservation(String dbId, BrAPIObservation obs
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 List<BrAPIObservation> 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 {
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();
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());
}
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);
}
}
Map<String, BrAPIObservation> processedObservations = processObservationsForCache(updatedObservations, program.getKey());
return programObservationCache.postThese(programId,processedObservations);
} catch (ApiException e) {
log.error("Error updating observation: " + Utilities.generateApiExceptionLogMessage(e), e);
throw e;
} catch (Exception e) {
log.error("Error updating observation: ", e);
throw new InternalServerException(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import org.breedinginsight.services.OntologyService;
import org.breedinginsight.services.exceptions.DoesNotExistException;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

@Prototype
Expand Down Expand Up @@ -114,14 +112,12 @@ public <U> List<U> brapiPut(List<U> members) throws ApiException, IllegalArgumen
if (experimentUtilities.isInvalidMemberListForClass(members, BrAPIObservation.class)) {
return new ArrayList<U>();
}
Map<String, BrAPIObservation> mutatedObservationByDbId = members.stream().collect(Collectors.toMap(
m -> ((BrAPIObservation) m).getObservationDbId(),
m -> ((BrAPIObservation) m)
));
return (List<U>) brAPIObservationDAO.updateBrAPIObservation(mutatedObservationByDbId, importContext.getProgram().getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and add this

Suggested change
return (List<U>) brAPIObservationDAO.updateBrAPIObservation(mutatedObservationByDbId, importContext.getProgram().getId());
Map<String, BrAPIObservation> mutatedObservationByDbId = members.stream().collect(Collectors.toMap(
m -> ((BrAPIObservation) m).getObservationDbId(),
m -> ((BrAPIObservation) m)
));
return (List<U>) brAPIObservationDAO.updateBrAPIObservation(mutatedObservationByDbId, importContext.getProgram().getId());


List<BrAPIObservation> updatedObservations = new ArrayList<>();
for (U member : members) {
BrAPIObservation observation = (BrAPIObservation) member;
Optional.ofNullable(brAPIObservationDAO.updateBrAPIObservation(observation.getObservationDbId(), observation, importContext.getProgram().getId())).ifPresent(updatedObservations::add);
}

return (List<U>) updatedObservations;
}

/**
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/breedinginsight/daos/cache/ProgramCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,26 @@ public List<R> post(UUID key, Callable<Map<String, R>> postMethod) throws Except
Map<String, R> response = null;
try {
response = postMethod.call();
return postThese(key, response);
} catch (Exception e) {
log.error("Error posting data and populating the cache", e);
invalidate(key);
throw e;
}
}

public List<R> postThese(UUID key, Map<String, R> toBePosted) throws Exception {
log.debug("posting for key: " + generateCacheKey(key));
try {
String cacheKey = generateCacheKey(key);
RMap<String, String> map = connection.getMap(cacheKey);
//temporarily populate the cache with the returned objects from the postMethod so they show in immediate cache requests
for(Map.Entry<String, R> obj : response.entrySet()) {
//temporarily populate the cache with the returned objects from the postMethod, so they show in immediate cache requests
for(Map.Entry<String, R> obj : toBePosted.entrySet()) {
map.put(obj.getKey(), gson.toJson(obj.getValue()));
}
populate(key);

return new ArrayList<>(response.values());
return new ArrayList<>(toBePosted.values());
} catch (Exception e) {
log.error("Error posting data and populating the cache", e);
invalidate(key);
Expand Down