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