-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEditedResponseController.java
More file actions
131 lines (119 loc) · 5.96 KB
/
Copy pathEditedResponseController.java
File metadata and controls
131 lines (119 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package fr.insee.genesis.controller.rest.responses;
import fr.insee.genesis.configuration.Config;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.ports.api.EditedExternalResponseApiPort;
import fr.insee.genesis.domain.ports.api.EditedPreviousResponseApiPort;
import fr.insee.genesis.domain.ports.api.EditedResponseApiPort;
import fr.insee.genesis.exceptions.GenesisException;
import fr.insee.genesis.infrastructure.utils.FileUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
@RequestMapping(path = "/edited")
@Controller
@Slf4j
@AllArgsConstructor
public class EditedResponseController {
private final EditedPreviousResponseApiPort editedPreviousResponseApiPort;
private final EditedExternalResponseApiPort editedExternalResponseApiPort;
private final EditedResponseApiPort editedResponseApiPort;
private final Config config;
@Operation(summary = "Get edited variables (edited and previous)")
@GetMapping(path = "/")
@PreAuthorize("hasRole('USER_PLATINE')")
public ResponseEntity<Object> getEditedResponses(
@RequestParam("questionnaireId") String questionnaireId,
@RequestParam("interrogationId") String interrogationId
){
return ResponseEntity.ok().body(
editedResponseApiPort.getEditedResponse(questionnaireId, interrogationId)
);
}
@Operation(summary = "Add edited previous json file")
@PostMapping(path = "previous/json")
@PreAuthorize("hasAnyRole('USER_PLATINE','SCHEDULER','USER_BACK_OFFICE')")
public ResponseEntity<Object> readEditedPreviousJson(
@RequestParam("questionnaireId") String questionnaireId,
@RequestParam("mode") Mode mode,
@RequestParam(value = "sourceState", required = false) String sourceState,
@RequestParam(value = "jsonFileName") String jsonFileName
){
try {
FileUtils fileUtils = new FileUtils(config);
String filePath = "%s/%s".formatted(
fileUtils.getDataFolder(questionnaireId, mode.getFolder(), null),
jsonFileName
);
if (!jsonFileName.toLowerCase().endsWith(".json")) {
throw new GenesisException(400, "File must be a JSON file !");
}
readEditedPreviousFile(questionnaireId, sourceState, filePath);
moveFiles(questionnaireId, mode, fileUtils, filePath);
return ResponseEntity.ok("Edited previous variable file %s saved !".formatted(filePath));
}catch (GenesisException ge){
return ResponseEntity.status(HttpStatusCode.valueOf(ge.getStatus())).body(ge.getMessage());
}
}
@Operation(summary = "Add edited external json file")
@PostMapping(path = "/external/json")
@PreAuthorize("hasAnyRole('USER_PLATINE','SCHEDULER','USER_BACK_OFFICE')")
public ResponseEntity<Object> readEditedExternalJson(
@RequestParam("questionnaireId") String questionnaireId,
@RequestParam("mode") Mode mode,
@RequestParam(value = "jsonFileName") String jsonFileName
){
try {
FileUtils fileUtils = new FileUtils(config);
String filePath = "%s/%s".formatted(
fileUtils.getDataFolder(questionnaireId, mode.getFolder(), null),
jsonFileName
);
if (!jsonFileName.toLowerCase().endsWith(".json")) {
throw new GenesisException(400, "File must be a JSON file !");
}
readEditedExternalFile(questionnaireId, filePath);
moveFiles(questionnaireId, mode, fileUtils, filePath);
return ResponseEntity.ok("Edited external variable file %s saved !".formatted(filePath));
}catch (GenesisException ge){
return ResponseEntity.status(HttpStatusCode.valueOf(ge.getStatus())).body(ge.getMessage());
}
}
private void readEditedPreviousFile(String questionnaireId, String sourceState, String filePath) throws GenesisException {
try (InputStream inputStream = new FileInputStream(filePath)) {
editedPreviousResponseApiPort.readEditedPreviousFile(inputStream, questionnaireId, sourceState);
} catch (FileNotFoundException e) {
throw new GenesisException(404, "File %s not found".formatted(filePath));
} catch (IOException e) {
throw new GenesisException(500, e.toString());
}
}
private void readEditedExternalFile(String questionnaireId, String filePath) throws GenesisException {
try (InputStream inputStream = new FileInputStream(filePath)) {
editedExternalResponseApiPort.readEditedExternalFile(inputStream, questionnaireId);
} catch (FileNotFoundException e) {
throw new GenesisException(404, "File %s not found".formatted(filePath));
} catch (IOException e) {
throw new GenesisException(500, e.toString());
}
}
private static void moveFiles(String questionnaireId, Mode mode, FileUtils fileUtils, String filePath) throws GenesisException {
try {
fileUtils.moveFiles(Path.of(filePath), fileUtils.getDoneFolder(questionnaireId, mode.getFolder()));
} catch (IOException e) {
throw new GenesisException(500, "Error while moving file to done : %s".formatted(e.toString()));
}
}
}