Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.swagger.v3.oas.annotations.Parameter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.support.CronExpression;
Expand Down Expand Up @@ -39,15 +40,13 @@ public class DataProcessingContextController {
@PutMapping(path = "/context/review")
@PreAuthorize("hasAnyRole('USER_PLATINE', 'USER_BACK_OFFICE', 'SCHEDULER')")
public ResponseEntity<Object> saveContext(
@Parameter(description = "Identifier of the partition", required = true) @RequestParam("partitionId") String partitionId,
@Parameter(description = "Allow reviewing") @RequestParam(value = "withReview", defaultValue = "false") Boolean withReview
){
try {
withReview = withReview != null && withReview; //False if null
dataProcessingContextApiPort.saveContext(partitionId, withReview);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
@Parameter(description = "Identifier of the partition", required = true)
@RequestParam("partitionId") String partitionId,
@Parameter(description = "Allow reviewing")
@RequestParam(value = "withReview", defaultValue = "false") Boolean withReview
) throws GenesisException {
withReview = withReview != null && withReview;
dataProcessingContextApiPort.saveContext(partitionId, withReview);
return ResponseEntity.ok().build();
}

Expand All @@ -56,14 +55,11 @@ public ResponseEntity<Object> saveContext(
@PreAuthorize("hasAnyRole('USER_PLATINE', 'USER_BACK_OFFICE', 'SCHEDULER')")
public ResponseEntity<Object> saveContextWithCollectionInstrumentId(
@PathVariable("collectionInstrumentId") String collectionInstrumentId,
@Parameter(description = "Allow reviewing") @RequestParam(value = "withReview", defaultValue = "false") Boolean withReview
){
try {
withReview = withReview != null && withReview; //False if null
dataProcessingContextApiPort.saveContextByCollectionInstrumentId(collectionInstrumentId, withReview);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
@Parameter(description = "Allow reviewing")
@RequestParam(value = "withReview", defaultValue = "false") Boolean withReview
) throws GenesisException {
withReview = withReview != null && withReview;
dataProcessingContextApiPort.saveContextByCollectionInstrumentId(collectionInstrumentId, withReview);
return ResponseEntity.ok().build();
}

Expand All @@ -73,28 +69,21 @@ public ResponseEntity<Object> saveContextWithCollectionInstrumentId(
@PreAuthorize("hasAnyRole('USER_BACK_OFFICE','SCHEDULER','USER_PLATINE')")
public ResponseEntity<Object> getReviewIndicatorByCollectionInstrumentId(
@PathVariable("collectionInstrumentId") String collectionInstrumentId
){
try {
boolean withReview = dataProcessingContextApiPort.getReviewByCollectionInstrumentId(collectionInstrumentId);
return ResponseEntity.ok(withReview);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
) throws GenesisException {
boolean withReview = dataProcessingContextApiPort.getReviewByCollectionInstrumentId(collectionInstrumentId);
return ResponseEntity.ok(withReview);
}

@Deprecated(forRemoval = true)
@Operation(summary = "Returns partition review indicator")
@GetMapping(path = "/context/review")
@PreAuthorize("hasAnyRole('USER_BACK_OFFICE','SCHEDULER','USER_PLATINE')")
public ResponseEntity<Object> getReviewIndicator(
@Parameter(description = "Identifier of the partition", required = true) @RequestParam("partitionId") String partitionId
){
try {
boolean withReview = dataProcessingContextApiPort.getReviewByPartitionId(partitionId);
return ResponseEntity.ok(withReview);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
@Parameter(description = "Identifier of the partition", required = true)
@RequestParam("partitionId") String partitionId
) throws GenesisException {
boolean withReview = dataProcessingContextApiPort.getReviewByPartitionId(partitionId);
return ResponseEntity.ok(withReview);
}

@Deprecated(forRemoval = true)
Expand All @@ -114,32 +103,33 @@ public ResponseEntity<Object> saveSchedule(
@Parameter(description = "(Encryption) output folder") @RequestParam(value = "encryptionOutputFolder",
defaultValue = "") String encryptionOutputFolder,
@Parameter(description = "(Encryption) Use signature system") @RequestParam(value = "useSignature", defaultValue = "false") boolean useSignature
) {
try {
//Check frequency
if(!CronExpression.isValidExpression(frequency)) {
log.warn("Returned error for wrong frequency : {}", frequency);
throw new GenesisException(400, "Wrong frequency syntax");
}
) throws GenesisException {

TrustParameters trustParameters = null;
if(useEncryption) {
trustParameters = new TrustParameters(
fileUtils.getKraftwerkOutFolder(partitionId),
encryptionOutputFolder,
encryptionVaultPath,
useSignature
);
}
dataProcessingContextApiPort.saveKraftwerkExecutionSchedule(
partitionId,
serviceToCall == null ? ServiceToCall.MAIN : serviceToCall,
frequency,
scheduleBeginDate, scheduleEndDate, trustParameters
//Check frequency
if (!CronExpression.isValidExpression(frequency)) {
log.warn("Returned error for wrong frequency : {}", frequency);
throw new GenesisException(HttpStatus.BAD_REQUEST, "Wrong frequency syntax");
}

TrustParameters trustParameters = null;
if (useEncryption) {
trustParameters = new TrustParameters(
fileUtils.getKraftwerkOutFolder(partitionId),
encryptionOutputFolder,
encryptionVaultPath,
useSignature
);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}

dataProcessingContextApiPort.saveKraftwerkExecutionSchedule(
partitionId,
serviceToCall == null ? ServiceToCall.MAIN : serviceToCall,
frequency,
scheduleBeginDate,
scheduleEndDate,
trustParameters
);

return ResponseEntity.ok().build();
}

Expand All @@ -160,12 +150,11 @@ public ResponseEntity<Object> saveScheduleWithCollectionInstrumentId(
@Parameter(description = "(Encryption) output folder") @RequestParam(value = "encryptionOutputFolder",
defaultValue = "") String encryptionOutputFolder,
@Parameter(description = "(Encryption) Use signature system") @RequestParam(value = "useSignature", defaultValue = "false") boolean useSignature
) {
try {
) throws GenesisException{
//Check frequency
if(!CronExpression.isValidExpression(frequency)) {
log.warn("Returned error for wrong frequency : {}", frequency);
throw new GenesisException(400, "Wrong frequency syntax");
throw new GenesisException(HttpStatus.BAD_REQUEST, "Wrong frequency syntax");
}

TrustParameters trustParameters = null;
Expand All @@ -183,9 +172,7 @@ public ResponseEntity<Object> saveScheduleWithCollectionInstrumentId(
frequency,
scheduleBeginDate, scheduleEndDate, trustParameters
);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}

return ResponseEntity.ok().build();
}

Expand Down Expand Up @@ -223,13 +210,11 @@ public ResponseEntity<Object> getAllSchedulesV2() {
public ResponseEntity<Object> setSurveyLastExecution(
@Parameter(description = "Survey name to call Kraftwerk on") @RequestBody String partitionId,
@Parameter(description = "Date to save as last execution date", example = "2024-01-01T12:00:00") @RequestParam("newDate") LocalDateTime newDate
) {
try {
dataProcessingContextApiPort.updateLastExecutionDate(partitionId, newDate);
log.info("{} last execution updated at {} !", partitionId, newDate);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
) throws GenesisException{

dataProcessingContextApiPort.updateLastExecutionDate(partitionId, newDate);
log.info("{} last execution updated at {} !", partitionId, newDate);

return ResponseEntity.ok().build();
}

Expand All @@ -239,13 +224,11 @@ public ResponseEntity<Object> setSurveyLastExecution(
public ResponseEntity<Object> setSurveyLastExecutionByCollectionInstrumentId(
@PathVariable("collectionInstrumentId") @RequestBody String collectionInstrumentId,
@Parameter(description = "Date to save as last execution date", example = "2024-01-01T12:00:00") @RequestParam("newDate") LocalDateTime newDate
) {
try {
dataProcessingContextApiPort.updateLastExecutionDateByCollectionInstrumentId(collectionInstrumentId, newDate);
log.info("{} last execution updated at {} !", collectionInstrumentId, newDate);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
) throws GenesisException{

dataProcessingContextApiPort.updateLastExecutionDateByCollectionInstrumentId(collectionInstrumentId, newDate);
log.info("{} last execution updated at {} !", collectionInstrumentId, newDate);

return ResponseEntity.ok().build();
}

Expand All @@ -255,12 +238,9 @@ public ResponseEntity<Object> setSurveyLastExecutionByCollectionInstrumentId(
@PreAuthorize("hasRole('USER_KRAFTWERK')")
public ResponseEntity<Object> deleteSchedules(
@Parameter(description = "Survey name of the schedule(s) to delete") @RequestParam("partitionId") String partitionId
){
try {
dataProcessingContextApiPort.deleteSchedules(partitionId);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
) throws GenesisException{

dataProcessingContextApiPort.deleteSchedules(partitionId);
log.info("Schedule deleted for survey {}", partitionId);
return ResponseEntity.ok().build();
}
Expand All @@ -270,25 +250,19 @@ public ResponseEntity<Object> deleteSchedules(
@PreAuthorize("hasRole('USER_KRAFTWERK')")
public ResponseEntity<Object> deleteSchedulesByCollectionInstrumentId(
@PathVariable("collectionInstrumentId") String collectionInstrumentId
){
try {
dataProcessingContextApiPort.deleteSchedulesByCollectionInstrumentId(collectionInstrumentId);
}catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
) throws GenesisException{

dataProcessingContextApiPort.deleteSchedulesByCollectionInstrumentId(collectionInstrumentId);
log.info("Schedule deleted for survey {}", collectionInstrumentId);
return ResponseEntity.ok().build();
}

@Operation(summary = "Delete expired schedules")
@DeleteMapping(path = "/context/schedules/expired-schedules")
@PreAuthorize("hasRole('SCHEDULER')")
public ResponseEntity<Object> deleteExpiredSchedules(){
try{
dataProcessingContextApiPort.deleteExpiredSchedules(fileUtils.getLogFolder());
} catch (GenesisException e){
return new ResponseEntity<>(e.getMessage(), HttpStatusCode.valueOf(e.getStatus()));
}
public ResponseEntity<Object> deleteExpiredSchedules() throws GenesisException{

dataProcessingContextApiPort.deleteExpiredSchedules(fileUtils.getLogFolder());
log.info("Expired schedules deleted");
return ResponseEntity.ok().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import fr.insee.bpm.metadata.model.MetadataModel;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.ports.api.QuestionnaireMetadataApiPort;
import fr.insee.genesis.exceptions.GenesisException;
import fr.insee.genesis.exceptions.QuestionnaireNotFoundException;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
Expand All @@ -24,12 +25,9 @@ public class QuestionnaireMetadataController {
public ResponseEntity<Object> getMetadata(
@RequestParam("questionnaireId") String questionnaireId,
@RequestParam("mode") Mode mode
){
try {
) throws QuestionnaireNotFoundException{

return ResponseEntity.ok().body(questionnaireMetadataApiPort.find(questionnaireId, mode));
} catch (GenesisException e) {
return ResponseEntity.status(e.getStatus()).body(e.getMessage());
}
}

@Operation(summary = "Save questionnaire metadata into database")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -52,18 +51,15 @@ public ResponseEntity<Object> getContextualVariables(
@PreAuthorize("hasAnyRole('USER_PLATINE','SCHEDULER')")
public ResponseEntity<Object> saveContextualVariables(
@RequestParam("questionnaireId") String questionnaireId
) {
try {
) throws GenesisException{
FileUtils fileUtils = new FileUtils(config);

String contextualFolderPath = fileUtils.getDataFolder(questionnaireId, "WEB", null) + Constants.CONTEXTUAL_FOLDER;

int fileCount = contextualVariableApiPort.saveContextualVariableFiles(questionnaireId, fileUtils,contextualFolderPath);

return ResponseEntity.ok("%d file(s) processed for questionnaire %s !".formatted(fileCount, questionnaireId));
} catch (GenesisException ge) {
return ResponseEntity.status(HttpStatusCode.valueOf(ge.getStatus())).body(ge.getMessage());
}

}

@Operation(summary = "Add contextual previous json file")
Expand All @@ -74,8 +70,8 @@ public ResponseEntity<Object> readContextualPreviousJson(
@RequestParam("mode") Mode mode,
@RequestParam(value = "sourceState", required = false) String sourceState,
@RequestParam(value = "jsonFileName") String jsonFileName
){
try {
) throws GenesisException{

FileUtils fileUtils = new FileUtils(config);

fileUtils.ensureContextualFolderExists(questionnaireId, mode);
Expand All @@ -86,15 +82,11 @@ public ResponseEntity<Object> readContextualPreviousJson(
jsonFileName
);
if (!jsonFileName.toLowerCase().endsWith(".json")) {
throw new GenesisException(400, "File must be a JSON file !");
throw new GenesisException(HttpStatus.BAD_REQUEST, "File must be a JSON file !");
}
contextualPreviousVariableApiPort.readContextualPreviousFile(questionnaireId.toUpperCase(), sourceState, filePath);
moveFile(questionnaireId, mode, fileUtils, filePath);
return ResponseEntity.ok("Contextual previous variable file %s saved !".formatted(filePath));
}catch (GenesisException ge){
return ResponseEntity.status(HttpStatusCode.valueOf(ge.getStatus())).body(ge.getMessage());
} catch (IOException ioe) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Erreur IO : " + ioe.getMessage()); }
}

@Operation(summary = "Add contextual external json file")
Expand All @@ -104,8 +96,7 @@ public ResponseEntity<Object> readContextualExternalJson(
@RequestParam("questionnaireId") String questionnaireId,
@RequestParam("mode") Mode mode,
@RequestParam(value = "jsonFileName") String jsonFileName
){
try {
) throws GenesisException{
FileUtils fileUtils = new FileUtils(config);

fileUtils.ensureContextualFolderExists(questionnaireId, mode);
Expand All @@ -116,23 +107,18 @@ public ResponseEntity<Object> readContextualExternalJson(
jsonFileName
);
if (!jsonFileName.toLowerCase().endsWith(".json")) {
throw new GenesisException(400, "File must be a JSON file !");
throw new GenesisException(HttpStatus.BAD_REQUEST, "File must be a JSON file !");
}
contextualExternalVariableApiPort.readContextualExternalFile(questionnaireId, filePath);
moveFile(questionnaireId, mode, fileUtils, filePath);
return ResponseEntity.ok("Contextual external variable file %s saved !".formatted(filePath));
}catch (GenesisException ge){
return ResponseEntity.status(HttpStatusCode.valueOf(ge.getStatus())).body(ge.getMessage());
} catch (IOException ioe) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Erreur IO : " + ioe.getMessage());
}
}

private static void moveFile(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()));
} catch (IOException _) {
throw new GenesisException(HttpStatus.INTERNAL_SERVER_ERROR, "Error while moving file to done");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ public ModeController(SurveyUnitApiPort surveyUnitService) {
@Operation(summary = "List sources/modes used for a given collection instrument (ex questionnaire)")
@GetMapping(path = "/by-questionnaire")
public ResponseEntity<List<Mode>> getModesByQuestionnaire(@RequestParam("collectionInstrumentId") String collectionInstrumentId) {
try {
List<Mode> modes = surveyUnitService.findModesByCollectionInstrumentId(collectionInstrumentId);
return ResponseEntity.ok(modes);
} catch (QuestionnaireNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Collections.emptyList());
}
}

@Operation(summary = "List sources/modes used for a given campaign")
Expand Down
Loading
Loading