22
33import fr .insee .genesis .configuration .Config ;
44import fr .insee .genesis .domain .model .surveyunit .Mode ;
5- import fr .insee .genesis .domain .ports .api .EditedExternalResponseApiPort ;
6- import fr .insee .genesis .domain .ports .api .EditedPreviousResponseApiPort ;
7- import fr .insee .genesis .domain .ports .api .EditedResponseApiPort ;
5+ import fr .insee .genesis .domain .ports .api .ContextualExternalVariableApiPort ;
6+ import fr .insee .genesis .domain .ports .api .ContextualPreviousVariableApiPort ;
7+ import fr .insee .genesis .domain .ports .api .ContextualVariableApiPort ;
88import fr .insee .genesis .exceptions .GenesisException ;
99import fr .insee .genesis .infrastructure .utils .FileUtils ;
1010import io .swagger .v3 .oas .annotations .Operation ;
1919import org .springframework .web .bind .annotation .RequestMapping ;
2020import org .springframework .web .bind .annotation .RequestParam ;
2121
22- import java .io .FileInputStream ;
23- import java .io .FileNotFoundException ;
2422import java .io .IOException ;
25- import java .io .InputStream ;
2623import java .nio .file .Path ;
2724
28- @ RequestMapping (path = "/edited " )
25+ @ RequestMapping (path = "/contextual-variables " )
2926@ Controller
3027@ Slf4j
3128@ AllArgsConstructor
32- public class EditedResponseController {
29+ public class ContextualVariableController {
3330
34- private final EditedPreviousResponseApiPort editedPreviousResponseApiPort ;
35- private final EditedExternalResponseApiPort editedExternalResponseApiPort ;
36- private final EditedResponseApiPort editedResponseApiPort ;
31+ private final ContextualPreviousVariableApiPort contextualPreviousVariableApiPort ;
32+ private final ContextualExternalVariableApiPort contextualExternalVariableApiPort ;
33+ private final ContextualVariableApiPort contextualVariableApiPort ;
3734 private final Config config ;
3835
39- @ Operation (summary = "Get edited variables (edited and previous)" )
36+ @ Operation (summary = "Get contextual variables (external and previous)" )
4037 @ GetMapping (path = "/" )
41- @ PreAuthorize ("hasRole ('USER_PLATINE')" )
42- public ResponseEntity <Object > getEditedResponses (
38+ @ PreAuthorize ("hasAnyRole ('USER_PLATINE','SCHEDULER ')" )
39+ public ResponseEntity <Object > getContextualVariables (
4340 @ RequestParam ("questionnaireId" ) String questionnaireId ,
4441 @ RequestParam ("interrogationId" ) String interrogationId
4542 ){
4643 return ResponseEntity .ok ().body (
47- editedResponseApiPort . getEditedResponse (questionnaireId , interrogationId )
44+ contextualVariableApiPort . getContextualVariable (questionnaireId , interrogationId )
4845 );
4946 }
5047
51- @ Operation (summary = "Add edited previous json file" )
48+ @ Operation (summary = "Save all contextual variables json files (external and previous)" )
49+ @ PostMapping (path = "/json" )
50+ @ PreAuthorize ("hasAnyRole('USER_PLATINE','SCHEDULER')" )
51+ public ResponseEntity <Object > saveContextualVariables (
52+ @ RequestParam ("questionnaireId" ) String questionnaireId
53+ ){
54+ try {
55+ FileUtils fileUtils = new FileUtils (config );
56+ int fileCount = contextualVariableApiPort .saveContextualVariableFiles (questionnaireId , fileUtils );
57+ return ResponseEntity .ok ("%d file(s) processed for questionnaire %s !" .formatted (fileCount , questionnaireId ));
58+ }catch (GenesisException ge ){
59+ return ResponseEntity .status (HttpStatusCode .valueOf (ge .getStatus ())).body (ge .getMessage ());
60+ }
61+ }
62+
63+ @ Operation (summary = "Add contextual previous json file" )
5264 @ PostMapping (path = "previous/json" )
5365 @ PreAuthorize ("hasAnyRole('USER_PLATINE','SCHEDULER','USER_BACK_OFFICE')" )
54- public ResponseEntity <Object > readEditedPreviousJson (
66+ public ResponseEntity <Object > readContextualPreviousJson (
5567 @ RequestParam ("questionnaireId" ) String questionnaireId ,
5668 @ RequestParam ("mode" ) Mode mode ,
5769 @ RequestParam (value = "sourceState" , required = false ) String sourceState ,
@@ -67,18 +79,18 @@ public ResponseEntity<Object> readEditedPreviousJson(
6779 if (!jsonFileName .toLowerCase ().endsWith (".json" )) {
6880 throw new GenesisException (400 , "File must be a JSON file !" );
6981 }
70- readEditedPreviousFile (questionnaireId .toUpperCase (), sourceState , filePath );
71- moveFiles (questionnaireId , mode , fileUtils , filePath );
72- return ResponseEntity .ok ("Edited previous variable file %s saved !" .formatted (filePath ));
82+ contextualPreviousVariableApiPort . readContextualPreviousFile (questionnaireId .toUpperCase (), sourceState , filePath );
83+ moveFile (questionnaireId , mode , fileUtils , filePath );
84+ return ResponseEntity .ok ("Contextual previous variable file %s saved !" .formatted (filePath ));
7385 }catch (GenesisException ge ){
7486 return ResponseEntity .status (HttpStatusCode .valueOf (ge .getStatus ())).body (ge .getMessage ());
7587 }
7688 }
7789
78- @ Operation (summary = "Add edited external json file" )
90+ @ Operation (summary = "Add contextual external json file" )
7991 @ PostMapping (path = "/external/json" )
8092 @ PreAuthorize ("hasAnyRole('USER_PLATINE','SCHEDULER','USER_BACK_OFFICE')" )
81- public ResponseEntity <Object > readEditedExternalJson (
93+ public ResponseEntity <Object > readContextualExternalJson (
8294 @ RequestParam ("questionnaireId" ) String questionnaireId ,
8395 @ RequestParam ("mode" ) Mode mode ,
8496 @ RequestParam (value = "jsonFileName" ) String jsonFileName
@@ -93,35 +105,15 @@ public ResponseEntity<Object> readEditedExternalJson(
93105 if (!jsonFileName .toLowerCase ().endsWith (".json" )) {
94106 throw new GenesisException (400 , "File must be a JSON file !" );
95107 }
96- readEditedExternalFile ( questionnaireId . toUpperCase () , filePath );
97- moveFiles (questionnaireId , mode , fileUtils , filePath );
98- return ResponseEntity .ok ("Edited external variable file %s saved !" .formatted (filePath ));
108+ contextualExternalVariableApiPort . readContextualExternalFile ( questionnaireId , filePath );
109+ moveFile (questionnaireId , mode , fileUtils , filePath );
110+ return ResponseEntity .ok ("Contextual external variable file %s saved !" .formatted (filePath ));
99111 }catch (GenesisException ge ){
100112 return ResponseEntity .status (HttpStatusCode .valueOf (ge .getStatus ())).body (ge .getMessage ());
101113 }
102114 }
103115
104- private void readEditedPreviousFile (String questionnaireId , String sourceState , String filePath ) throws GenesisException {
105- try (InputStream inputStream = new FileInputStream (filePath )) {
106- editedPreviousResponseApiPort .readEditedPreviousFile (inputStream , questionnaireId , sourceState );
107- } catch (FileNotFoundException e ) {
108- throw new GenesisException (404 , "File %s not found" .formatted (filePath ));
109- } catch (IOException e ) {
110- throw new GenesisException (500 , e .toString ());
111- }
112- }
113-
114- private void readEditedExternalFile (String questionnaireId , String filePath ) throws GenesisException {
115- try (InputStream inputStream = new FileInputStream (filePath )) {
116- editedExternalResponseApiPort .readEditedExternalFile (inputStream , questionnaireId );
117- } catch (FileNotFoundException e ) {
118- throw new GenesisException (404 , "File %s not found" .formatted (filePath ));
119- } catch (IOException e ) {
120- throw new GenesisException (500 , e .toString ());
121- }
122- }
123-
124- private static void moveFiles (String questionnaireId , Mode mode , FileUtils fileUtils , String filePath ) throws GenesisException {
116+ private static void moveFile (String questionnaireId , Mode mode , FileUtils fileUtils , String filePath ) throws GenesisException {
125117 try {
126118 fileUtils .moveFiles (Path .of (filePath ), fileUtils .getDoneFolder (questionnaireId , mode .getFolder ()));
127119 } catch (IOException e ) {
0 commit comments