|
48 | 48 | import org.breedinginsight.services.OntologyService; |
49 | 49 | import org.breedinginsight.services.ProgramLocationService; |
50 | 50 | import org.breedinginsight.services.exceptions.DoesNotExistException; |
| 51 | +import org.breedinginsight.services.exceptions.UnprocessableEntityException; |
51 | 52 | import org.breedinginsight.utilities.Utilities; |
52 | 53 |
|
53 | 54 | import javax.inject.Inject; |
|
58 | 59 | import java.util.Objects; |
59 | 60 | import java.util.stream.Collectors; |
60 | 61 |
|
| 62 | +import static org.breedinginsight.brapps.importer.services.processors.experiment.ExperimentUtilities.PREEXISTING_EXPERIMENT_TITLE; |
| 63 | + |
61 | 64 | @Singleton |
62 | 65 | @Slf4j |
63 | 66 | public class CommitPendingImportObjectsStep { |
@@ -88,7 +91,7 @@ public CommitPendingImportObjectsStep(BrAPIListDAO brAPIListDAO, |
88 | 91 | } |
89 | 92 |
|
90 | 93 | // TODO: some common code between workflows here that could be broken out, removed append/update specific code |
91 | | - public void process(ProcessContext processContext, ProcessedData processedData) { |
| 94 | + public void process(ProcessContext processContext, ProcessedData processedData) throws UnprocessableEntityException { |
92 | 95 |
|
93 | 96 | PendingData pendingData = processContext.getPendingData(); |
94 | 97 | ImportContext importContext = processContext.getImportContext(); |
@@ -138,6 +141,24 @@ public void process(ProcessContext processContext, ProcessedData processedData) |
138 | 141 |
|
139 | 142 | AuthenticatedUser actingUser = new AuthenticatedUser(upload.getUpdatedByUser().getName(), new ArrayList<>(), upload.getUpdatedByUser().getId(), new ArrayList<>()); |
140 | 143 |
|
| 144 | + // TODO: Implement more robust solution either brapi server side or possibly redis SETNX client side |
| 145 | + // Do this check here, directly before creating new trials instead of in earlier step to minimize time window of race condition |
| 146 | + if (!newTrials.isEmpty()) { |
| 147 | + try { |
| 148 | + List<BrAPITrial> cachedTrials = brapiTrialDAO.getTrials(program.getId()); |
| 149 | + List<String> existingTrialNames = cachedTrials.stream().map(BrAPITrial::getTrialName).collect(Collectors.toList()); |
| 150 | + List<String> newTrialNames = newTrials.stream().map(t -> Utilities.removeProgramKey(t.getTrialName(), program.getKey())).collect(Collectors.toList()); |
| 151 | + log.debug("** Trials Duplicate Check: {} -> {}", existingTrialNames, newTrialNames); |
| 152 | + if (newTrialNames.stream().anyMatch(existingTrialNames::contains)) { |
| 153 | + log.debug("** New matches existing"); |
| 154 | + throw new UnprocessableEntityException(PREEXISTING_EXPERIMENT_TITLE); |
| 155 | + } |
| 156 | + } catch (ApiException e) { |
| 157 | + log.error("Error getting trials for duplicate name check", e); |
| 158 | + throw new InternalServerException(e.getMessage(), e); |
| 159 | + } |
| 160 | + } |
| 161 | + |
141 | 162 | try { |
142 | 163 | List<BrAPIListSummary> createdDatasets = new ArrayList<>(brAPIListDAO.createBrAPILists(newDatasetRequests, program.getId(), upload)); |
143 | 164 | createdDatasets.forEach(summary -> obsVarDatasetByName.get(summary.getListName()).getBrAPIObject().setListDbId(summary.getListDbId())); |
|
0 commit comments