From 35f9ee861f26bf33e2965a5b7d988337098d4430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Demazi=C3=A8re?= Date: Wed, 30 Aug 2023 11:10:53 +0200 Subject: [PATCH] feat: add endpoint for questionnaireModelId simple fetch (#148) --- pom.xml | 8 +- .../api/configuration/KeycloakConfig.java | 1 + .../configuration/SecurityConfiguration.java | 1 + .../insee/queen/api/constants/Constants.java | 1 + .../QuestionnaireModelController.java | 144 +++++++++++------- 5 files changed, 96 insertions(+), 59 deletions(-) diff --git a/pom.xml b/pom.xml index 7a63ffed..717be75b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,10 +1,11 @@ - + 4.0.0 fr.insee.queen queen - 3.5.43 + 3.5.44 war Queen-Back-Office Back-office services for Queen @@ -13,7 +14,8 @@ org.springframework.boot spring-boot-starter-parent 2.3.1.RELEASE - + + diff --git a/src/main/java/fr/insee/queen/api/configuration/KeycloakConfig.java b/src/main/java/fr/insee/queen/api/configuration/KeycloakConfig.java index 613987a0..3c025682 100644 --- a/src/main/java/fr/insee/queen/api/configuration/KeycloakConfig.java +++ b/src/main/java/fr/insee/queen/api/configuration/KeycloakConfig.java @@ -125,6 +125,7 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers(HttpMethod.PUT, Constants.API_SURVEYUNIT_ID_COMMENT).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.GET, Constants.API_SURVEYUNIT_ID_STATEDATA).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.PUT, Constants.API_SURVEYUNIT_ID_STATEDATA).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) + .antMatchers(HttpMethod.POST, Constants.API_SURVEYUNITS_QUESTIONNAIRE_MODEL_ID).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.GET, Constants.API_SURVEYUNIT_ID_DEPOSITPROOF).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.GET, Constants.API_SURVEYUNIT_ID_PERSONALIZATION).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.PUT, Constants.API_SURVEYUNIT_ID_PERSONALIZATION).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) diff --git a/src/main/java/fr/insee/queen/api/configuration/SecurityConfiguration.java b/src/main/java/fr/insee/queen/api/configuration/SecurityConfiguration.java index 0fd088c8..4e04f110 100644 --- a/src/main/java/fr/insee/queen/api/configuration/SecurityConfiguration.java +++ b/src/main/java/fr/insee/queen/api/configuration/SecurityConfiguration.java @@ -119,6 +119,7 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers(HttpMethod.PUT, Constants.API_SURVEYUNIT_ID_DATA).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.GET, Constants.API_SURVEYUNIT_ID_COMMENT).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.PUT, Constants.API_SURVEYUNIT_ID_COMMENT).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) + .antMatchers(HttpMethod.POST, Constants.API_SURVEYUNITS_QUESTIONNAIRE_MODEL_ID).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.GET, Constants.API_SURVEYUNIT_ID_STATEDATA).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.PUT, Constants.API_SURVEYUNIT_ID_STATEDATA).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) .antMatchers(HttpMethod.GET, Constants.API_SURVEYUNIT_ID_DEPOSITPROOF).hasAnyRole(roleAdmin, roleWebClient, roleInterviewer, roleReviewer,roleReviewerAlternative) diff --git a/src/main/java/fr/insee/queen/api/constants/Constants.java b/src/main/java/fr/insee/queen/api/constants/Constants.java index 4d2d6efa..b5c00148 100644 --- a/src/main/java/fr/insee/queen/api/constants/Constants.java +++ b/src/main/java/fr/insee/queen/api/constants/Constants.java @@ -24,6 +24,7 @@ private Constants() { public static final String API_CAMPAIGN_ID_QUESTIONAIREID = "/api/campaign/{id}/questionnaire-id"; public static final String API_CAMPAIGN_ID_REQUIREDNOMENCLATURES = "/api/campaign/{id}/required-nomenclatures"; public static final String API_SURVEYUNITS_STATEDATA = "/api/survey-units/state-data"; + public static final String API_SURVEYUNITS_QUESTIONNAIRE_MODEL_ID = "/api/survey-units/questionnaire-model-id"; public static final String API_SURVEYUNITS = "/api/survey-units"; public static final String API_SURVEYUNIT_ID = "/api/survey-unit/{id}"; public static final String API_SURVEYUNIT_ID_DATA = "/api/survey-unit/{id}/data"; diff --git a/src/main/java/fr/insee/queen/api/controller/QuestionnaireModelController.java b/src/main/java/fr/insee/queen/api/controller/QuestionnaireModelController.java index 95758c1e..f3517b0d 100644 --- a/src/main/java/fr/insee/queen/api/controller/QuestionnaireModelController.java +++ b/src/main/java/fr/insee/queen/api/controller/QuestionnaireModelController.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; @@ -22,59 +23,65 @@ import fr.insee.queen.api.domain.Campaign; import fr.insee.queen.api.domain.QuestionnaireModel; +import fr.insee.queen.api.domain.SurveyUnit; import fr.insee.queen.api.dto.questionnairemodel.QuestionnaireIdDto; import fr.insee.queen.api.dto.questionnairemodel.QuestionnaireModelCreateDto; import fr.insee.queen.api.dto.questionnairemodel.QuestionnaireModelDto; +import fr.insee.queen.api.dto.surveyunit.SurveyUnitOkNokDto; +import fr.insee.queen.api.dto.surveyunit.SurveyUnitResponseDto; import fr.insee.queen.api.exception.NotFoundException; import fr.insee.queen.api.service.CampaignService; import fr.insee.queen.api.service.NomenclatureService; import fr.insee.queen.api.service.QuestionnaireModelService; -import fr.insee.queen.api.service.UtilsService; +import fr.insee.queen.api.service.SurveyUnitService; import io.swagger.annotations.ApiOperation; /** -* QuestionnaireModelController is the Controller using to manage {@link QuestionnaireModel} entity -* -* @author Claudel Benjamin -* -*/ + * QuestionnaireModelController is the Controller using to manage + * {@link QuestionnaireModel} entity + * + * @author Claudel Benjamin + * + */ @RestController @RequestMapping(path = "/api") public class QuestionnaireModelController { private static final Logger LOGGER = LoggerFactory.getLogger(QuestionnaireModelController.class); - + @Autowired - private UtilsService utilsService; - + private SurveyUnitService surveyUnitService; + /** - * The questionnaire model repository using to access to table 'questionnaire_model' in DB - */ + * The questionnaire model repository using to access to table + * 'questionnaire_model' in DB + */ @Autowired private QuestionnaireModelService questionnaireModelService; - + /** - * The nomenclature service using to access to table 'nomenclature' in DB - */ + * The nomenclature service using to access to table 'nomenclature' in DB + */ @Autowired private NomenclatureService nomenclatureService; - + /** - * The campaign repository using to access to table 'campaign' in DB - */ + * The campaign repository using to access to table 'campaign' in DB + */ @Autowired private CampaignService campaignService; - - + /** - * This method is using to get the questionnaireModel associated to a specific campaign - * - * @param id the id of campaign - * @return the {@link QuestionnaireModelDto} associated to the campaign - * @throws NotFoundException - */ + * This method is using to get the questionnaireModel associated to a specific + * campaign + * + * @param id the id of campaign + * @return the {@link QuestionnaireModelDto} associated to the campaign + * @throws NotFoundException + */ @ApiOperation(value = "Get questionnnaire model by campaign Id ") @GetMapping(path = "/campaign/{id}/questionnaires") - public ResponseEntity> getQuestionnaireModelByCampaignId(@PathVariable(value = "id") String id) { + public ResponseEntity> getQuestionnaireModelByCampaignId( + @PathVariable(value = "id") String id) { Optional campaignOptional = campaignService.findById(id); if (!campaignOptional.isPresent()) { LOGGER.error("GET questionnaire for campaign with id {} resulting in 404", id); @@ -93,14 +100,14 @@ public ResponseEntity> getQuestionnaireModelByCampai } /** - * This method is used to retrieve a questionnaireModel by Id - * - * @param id the id of questionnaire - * @return the {@link QuestionnaireModelDto} associated to the id - */ + * This method is used to retrieve a questionnaireModel by Id + * + * @param id the id of questionnaire + * @return the {@link QuestionnaireModelDto} associated to the id + */ @ApiOperation(value = "Get a questionnnaire model by Id ") @GetMapping(path = "/questionnaire/{id}") - public ResponseEntity getQuestionnaireModelById(@PathVariable(value = "id") String id){ + public ResponseEntity getQuestionnaireModelById(@PathVariable(value = "id") String id) { Optional questModel = questionnaireModelService.findById(id); if (!questModel.isPresent()) { LOGGER.error("GET questionnaire for id {} resulting in 404", id); @@ -111,17 +118,18 @@ public ResponseEntity getQuestionnaireModelById(@PathVari return new ResponseEntity<>(questMod, HttpStatus.OK); } } - + /** - * This method is used to retrieve a questionnaireModel by Id - * - * @param id the id of questionnaire - * @return the {@link QuestionnaireModelResponseDto} associated to the id - * @throws NotFoundException - */ + * This method is used to retrieve a questionnaireModel by Id + * + * @param id the id of questionnaire + * @return the {@link QuestionnaireModelResponseDto} associated to the id + * @throws NotFoundException + */ @ApiOperation(value = "Get questionnnaire id by campaign Id ") @GetMapping(path = "/campaign/{id}/questionnaire-id") - public ResponseEntity> getQuestionnaireModelIdByCampaignId(@PathVariable(value = "id") String id) { + public ResponseEntity> getQuestionnaireModelIdByCampaignId( + @PathVariable(value = "id") String id) { Optional campaignOptional = campaignService.findById(id); if (!campaignOptional.isPresent()) { LOGGER.error("GET questionnaire Id for campaign with id {} resulting in 404", id); @@ -138,33 +146,57 @@ public ResponseEntity> getQuestionnaireModelIdByCampaig return new ResponseEntity<>(resp, HttpStatus.OK); } } - + /** - * This method is using to post a new Questionnaire Model - * - * @param questionnaire to create - * @return {@link HttpStatus 400} if nomenclature is not found, else {@link HttpStatus 200} - * @throws ParseException - * @throws SQLException - * - */ + * This method is using to post a new Questionnaire Model + * + * @param questionnaire to create + * @return {@link HttpStatus 400} if nomenclature is not found, else + * {@link HttpStatus 200} + * @throws ParseException + * @throws SQLException + * + */ @ApiOperation(value = "Create a Questionnaire Model") @PostMapping(path = "/questionnaire-models") - public ResponseEntity createQuestionnaire(@RequestBody QuestionnaireModelCreateDto questionnaireModel, HttpServletRequest request) { + public ResponseEntity createQuestionnaire(@RequestBody QuestionnaireModelCreateDto questionnaireModel, + HttpServletRequest request) { - Optional questMod = questionnaireModelService.findDtoById(questionnaireModel.getIdQuestionnaireModel()); + Optional questMod = questionnaireModelService + .findDtoById(questionnaireModel.getIdQuestionnaireModel()); if (questMod.isPresent()) { - LOGGER.error("POST questionnaire with id {} resulting in 400 because it already exists", questionnaireModel.getIdQuestionnaireModel()); + LOGGER.error("POST questionnaire with id {} resulting in 400 because it already exists", + questionnaireModel.getIdQuestionnaireModel()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - if(!questionnaireModel.getRequiredNomenclatureIds().isEmpty() && - Boolean.FALSE.equals(nomenclatureService.checkIfNomenclatureExists(questionnaireModel.getRequiredNomenclatureIds()))) { - LOGGER.error("POST questionnaire with id {} resulting in 400 because a nomenclature does not exist", questionnaireModel.getIdQuestionnaireModel()); + if (!questionnaireModel.getRequiredNomenclatureIds().isEmpty() && + Boolean.FALSE.equals(nomenclatureService + .checkIfNomenclatureExists(questionnaireModel.getRequiredNomenclatureIds()))) { + LOGGER.error("POST questionnaire with id {} resulting in 400 because a nomenclature does not exist", + questionnaireModel.getIdQuestionnaireModel()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } questionnaireModelService.createQuestionnaire(questionnaireModel); LOGGER.info("POST campaign with id {} resulting in 200", questionnaireModel.getIdQuestionnaireModel()); return ResponseEntity.ok().build(); - + + } + + @ApiOperation(value = "Get questionnaireModelId for all survey-units defined in request body ") + @PostMapping(path = "/survey-units/questionnaire-model-id") + public ResponseEntity getQuestionnaireModelIdBySurveyUnits( + @RequestBody List lstSurveyUnitId, + HttpServletRequest request) { + List lstSurveyUnit = (List) surveyUnitService.findByIds(lstSurveyUnitId); + List surveyUnitsIds = lstSurveyUnit.stream().map(SurveyUnit::getId).collect(Collectors.toList()); + List surveyUnitsNOK = lstSurveyUnitId.stream() + .filter(su -> !surveyUnitsIds.contains(su)) + .map(su -> new SurveyUnitResponseDto(su)) + .collect(Collectors.toList()); + List surveyUnitsOK = lstSurveyUnit.stream() + .map(su -> new SurveyUnitResponseDto(su.getId(), su.getQuestionnaireModelId(), null, null, null, + null)) + .collect(Collectors.toList()); + return new ResponseEntity<>(new SurveyUnitOkNokDto(surveyUnitsOK, surveyUnitsNOK), HttpStatus.OK); } }