Skip to content

Commit 88a4cd5

Browse files
committed
[BI-1720] - cache studies on create
1 parent 024f4e6 commit 88a4cd5

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/main/java/org/breedinginsight/brapps/importer/daos/BrAPIStudyDAO.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import javax.inject.Singleton;
4141
import javax.validation.constraints.NotNull;
4242
import java.util.*;
43+
import java.util.concurrent.Callable;
4344
import java.util.stream.Collectors;
4445

4546
@Slf4j
@@ -152,7 +153,37 @@ public List<BrAPIStudy> getStudiesByExperimentID(@NotNull UUID experimentID, Pro
152153

153154
public List<BrAPIStudy> createBrAPIStudies(List<BrAPIStudy> brAPIStudyList, UUID programId, ImportUpload upload) throws ApiException {
154155
StudiesApi api = brAPIEndpointProvider.get(programDAO.getCoreClient(programId), StudiesApi.class);
155-
return brAPIDAOUtil.post(brAPIStudyList, upload, api::studiesPost, importDAO::update);
156+
List<BrAPIStudy> createdStudies = new ArrayList<>();
157+
try {
158+
if (!brAPIStudyList.isEmpty()) {
159+
Callable<Map<String, BrAPIStudy>> postCallback = () -> {
160+
List<BrAPIStudy> postedStudies = brAPIDAOUtil
161+
.post(brAPIStudyList, upload, api::studiesPost, importDAO::update);
162+
return studyById(postedStudies);
163+
};
164+
createdStudies.addAll(programStudyCache.post(programId, postCallback));
165+
}
166+
167+
return createdStudies;
168+
} catch (Exception e) {
169+
throw new InternalServerException("Unknown error has occurred: " + e.getMessage(), e);
170+
}
171+
}
172+
173+
/**
174+
* @return Map - Key = BI external reference ID, Value = BrAPIStudy
175+
* */
176+
private Map<String, BrAPIStudy> studyById(List<BrAPIStudy> studies) {
177+
Map<String, BrAPIStudy> studyById = new HashMap<>();
178+
for (BrAPIStudy study: studies) {
179+
BrAPIExternalReference xref = study
180+
.getExternalReferences()
181+
.stream()
182+
.filter(reference -> String.format("%s/%s", referenceSource, ExternalReferenceSource.STUDIES).equalsIgnoreCase(reference.getReferenceSource()))
183+
.findFirst().orElseThrow(() -> new IllegalStateException("No BI external reference found"));
184+
studyById.put(xref.getReferenceID(), study);
185+
}
186+
return studyById;
156187
}
157188

158189
public List<BrAPIStudy> getStudiesByStudyDbId(Collection<String> studyDbIds, Program program) throws ApiException {

0 commit comments

Comments
 (0)