|
57 | 57 | import java.nio.file.Paths; |
58 | 58 | import java.time.LocalDateTime; |
59 | 59 | import java.util.ArrayList; |
| 60 | +import java.util.HashSet; |
60 | 61 | import java.util.List; |
| 62 | +import java.util.Set; |
61 | 63 | import java.util.stream.Stream; |
62 | 64 |
|
63 | 65 | @RequestMapping(path = "/responses" ) |
@@ -324,9 +326,18 @@ public ResponseEntity<List<SurveyUnitSimplified>> getLatestForInterrogationList( |
324 | 326 | public ResponseEntity<Object> saveEditedVariables( |
325 | 327 | @RequestBody SurveyUnitInputDto surveyUnitInputDto |
326 | 328 | ){ |
| 329 | + //Code quality : we need to put all that logic out of this controller |
327 | 330 | //Parse metadata |
328 | 331 | //Try to look for DDI first, if no DDI found looks for lunatic components |
329 | 332 | List<GenesisError> errors = new ArrayList<>(); |
| 333 | + //We need to retrieve campaignId |
| 334 | + HashSet<String> campaignIds = findCampaignId(surveyUnitInputDto); |
| 335 | + if (campaignIds.size() != 1){ |
| 336 | + return ResponseEntity.status(500).body("Impossible to assign one campaignId to that response"); |
| 337 | + } |
| 338 | + // If the size is equal to 1 we get this campaignId |
| 339 | + String campaignId = campaignIds.iterator().next(); |
| 340 | + surveyUnitInputDto.setCampaignId(campaignId); |
330 | 341 | VariablesMap variablesMap = metadataService.readMetadatas(surveyUnitInputDto.getCampaignId(), |
331 | 342 | surveyUnitInputDto.getMode().getModeName(), fileUtils, errors); |
332 | 343 | if(variablesMap == null){ |
@@ -373,6 +384,15 @@ public ResponseEntity<Object> saveEditedVariables( |
373 | 384 | return ResponseEntity.ok(SUCCESS_MESSAGE); |
374 | 385 | } |
375 | 386 |
|
| 387 | + private HashSet<String> findCampaignId(SurveyUnitInputDto surveyUnitInputDto) { |
| 388 | + List<SurveyUnitModel> responses = surveyUnitService.findByIdsInterrogationAndQuestionnaire(surveyUnitInputDto.getInterrogationId(), surveyUnitInputDto.getQuestionnaireId()); |
| 389 | + HashSet<String> campaignIds = new HashSet<>(); |
| 390 | + for(SurveyUnitModel response : responses){ |
| 391 | + campaignIds.add(response.getCampaignId()); |
| 392 | + } |
| 393 | + return campaignIds; |
| 394 | + } |
| 395 | + |
376 | 396 | //Utilities |
377 | 397 | /** |
378 | 398 | * Process a campaign with a specific mode |
@@ -489,7 +509,6 @@ private ResponseEntity<Object> processXmlFileSequentially(Path filepath, Mode mo |
489 | 509 |
|
490 | 510 | log.info("Saved {} survey units updates from Xml file {}", suCount, filepath.getFileName()); |
491 | 511 | } |
492 | | - |
493 | 512 | return ResponseEntity.ok().build(); |
494 | 513 | } |
495 | 514 |
|
|
0 commit comments