1111import fr .insee .genesis .controller .dto .SurveyUnitInputDto ;
1212import fr .insee .genesis .controller .dto .SurveyUnitQualityToolDto ;
1313import fr .insee .genesis .controller .dto .SurveyUnitSimplified ;
14- import fr .insee .genesis .controller .services .MetadataService ;
1514import fr .insee .genesis .controller .rest .CommonApiResponse ;
15+ import fr .insee .genesis .controller .services .MetadataService ;
1616import fr .insee .genesis .controller .sources .xml .LunaticXmlCampaign ;
1717import fr .insee .genesis .controller .sources .xml .LunaticXmlDataParser ;
1818import fr .insee .genesis .controller .sources .xml .LunaticXmlDataSequentialParser ;
5858import java .time .LocalDateTime ;
5959import java .util .ArrayList ;
6060import java .util .List ;
61+ import java .util .Set ;
6162import java .util .stream .Stream ;
6263
6364@ RequestMapping (path = "/responses" )
@@ -77,7 +78,6 @@ public class ResponseController implements CommonApiResponse {
7778 private final AuthUtils authUtils ;
7879 private final MetadataService metadataService ;
7980
80-
8181 public ResponseController (SurveyUnitApiPort surveyUnitService ,
8282 SurveyUnitQualityService surveyUnitQualityService ,
8383 FileUtils fileUtils ,
@@ -105,7 +105,7 @@ public ResponseEntity<Object> saveResponsesFromXmlFile(@RequestParam("pathLunati
105105 VariablesMap variablesMap ;
106106 if (withDDI ) {
107107 //Parse DDI
108- log .info (String . format ( "Try to read DDI file : %s " , metadataFilePath ) );
108+ log .info ("Try to read DDI file : {} " , metadataFilePath );
109109 try {
110110 variablesMap =
111111 DDIReader .getMetadataFromDDI (Path .of (metadataFilePath ).toFile ().toURI ().toURL ().toString (),
@@ -115,12 +115,12 @@ public ResponseEntity<Object> saveResponsesFromXmlFile(@RequestParam("pathLunati
115115 }
116116 }else {
117117 //Parse Lunatic
118- log .info (String . format ( "Try to read lunatic file : %s " , metadataFilePath ) );
118+ log .info ("Try to read lunatic file : {} " , metadataFilePath );
119119
120120 variablesMap = LunaticReader .getMetadataFromLunatic (new FileInputStream (metadataFilePath )).getVariables ();
121121 }
122122
123- log .info (String . format ( "Try to read Xml file : %s " , xmlFile ) );
123+ log .info ("Try to read Xml file : {} " , xmlFile );
124124 Path filepath = Paths .get (xmlFile );
125125
126126 if (getFileSizeInMB (filepath ) <= Constants .MAX_FILE_SIZE_UNTIL_SEQUENTIAL ) {
@@ -324,9 +324,18 @@ public ResponseEntity<List<SurveyUnitSimplified>> getLatestForInterrogationList(
324324 public ResponseEntity <Object > saveEditedVariables (
325325 @ RequestBody SurveyUnitInputDto surveyUnitInputDto
326326 ){
327+ //Code quality : we need to put all that logic out of this controller
327328 //Parse metadata
328329 //Try to look for DDI first, if no DDI found looks for lunatic components
329330 List <GenesisError > errors = new ArrayList <>();
331+ //We need to retrieve campaignId
332+ Set <String > campaignIds = surveyUnitService .findCampaignIdsFrom (surveyUnitInputDto );
333+ if (campaignIds .size () != 1 ){
334+ return ResponseEntity .status (500 ).body ("Impossible to assign one campaignId to that response" );
335+ }
336+ // If the size is equal to 1 we get this campaignId
337+ String campaignId = campaignIds .iterator ().next ();
338+ surveyUnitInputDto .setCampaignId (campaignId );
330339 VariablesMap variablesMap = metadataService .readMetadatas (surveyUnitInputDto .getCampaignId (),
331340 surveyUnitInputDto .getMode ().getModeName (), fileUtils , errors );
332341 if (variablesMap == null ){
@@ -373,6 +382,8 @@ public ResponseEntity<Object> saveEditedVariables(
373382 return ResponseEntity .ok (SUCCESS_MESSAGE );
374383 }
375384
385+
386+
376387 //Utilities
377388 /**
378389 * Process a campaign with a specific mode
@@ -489,7 +500,6 @@ private ResponseEntity<Object> processXmlFileSequentially(Path filepath, Mode mo
489500
490501 log .info ("Saved {} survey units updates from Xml file {}" , suCount , filepath .getFileName ());
491502 }
492-
493503 return ResponseEntity .ok ().build ();
494504 }
495505
0 commit comments