|
40 | 40 | import javax.inject.Singleton; |
41 | 41 | import javax.validation.constraints.NotNull; |
42 | 42 | import java.util.*; |
| 43 | +import java.util.concurrent.Callable; |
43 | 44 | import java.util.stream.Collectors; |
44 | 45 |
|
45 | 46 | @Slf4j |
@@ -152,7 +153,37 @@ public List<BrAPIStudy> getStudiesByExperimentID(@NotNull UUID experimentID, Pro |
152 | 153 |
|
153 | 154 | public List<BrAPIStudy> createBrAPIStudies(List<BrAPIStudy> brAPIStudyList, UUID programId, ImportUpload upload) throws ApiException { |
154 | 155 | 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; |
156 | 187 | } |
157 | 188 |
|
158 | 189 | public List<BrAPIStudy> getStudiesByStudyDbId(Collection<String> studyDbIds, Program program) throws ApiException { |
|
0 commit comments