Skip to content

Commit

Permalink
feat: add endpoint for questionnaireModelId simple fetch (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDmz authored Aug 30, 2023
1 parent 5823fb7 commit 35f9ee8
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 59 deletions.
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>fr.insee.queen</groupId>
<artifactId>queen</artifactId>
<version>3.5.43</version>
<version>3.5.44</version>
<packaging>war</packaging>
<name>Queen-Back-Office</name>
<description>Back-office services for Queen</description>
Expand All @@ -13,7 +14,8 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
<relativePath />
<!-- lookup parent from repository -->
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/fr/insee/queen/api/constants/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<List<QuestionnaireModelDto>> getQuestionnaireModelByCampaignId(@PathVariable(value = "id") String id) {
public ResponseEntity<List<QuestionnaireModelDto>> getQuestionnaireModelByCampaignId(
@PathVariable(value = "id") String id) {
Optional<Campaign> campaignOptional = campaignService.findById(id);
if (!campaignOptional.isPresent()) {
LOGGER.error("GET questionnaire for campaign with id {} resulting in 404", id);
Expand All @@ -93,14 +100,14 @@ public ResponseEntity<List<QuestionnaireModelDto>> 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<QuestionnaireModelDto> getQuestionnaireModelById(@PathVariable(value = "id") String id){
public ResponseEntity<QuestionnaireModelDto> getQuestionnaireModelById(@PathVariable(value = "id") String id) {
Optional<QuestionnaireModel> questModel = questionnaireModelService.findById(id);
if (!questModel.isPresent()) {
LOGGER.error("GET questionnaire for id {} resulting in 404", id);
Expand All @@ -111,17 +118,18 @@ public ResponseEntity<QuestionnaireModelDto> 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<List<QuestionnaireIdDto>> getQuestionnaireModelIdByCampaignId(@PathVariable(value = "id") String id) {
public ResponseEntity<List<QuestionnaireIdDto>> getQuestionnaireModelIdByCampaignId(
@PathVariable(value = "id") String id) {
Optional<Campaign> campaignOptional = campaignService.findById(id);
if (!campaignOptional.isPresent()) {
LOGGER.error("GET questionnaire Id for campaign with id {} resulting in 404", id);
Expand All @@ -138,33 +146,57 @@ public ResponseEntity<List<QuestionnaireIdDto>> 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<Object> createQuestionnaire(@RequestBody QuestionnaireModelCreateDto questionnaireModel, HttpServletRequest request) {
public ResponseEntity<Object> createQuestionnaire(@RequestBody QuestionnaireModelCreateDto questionnaireModel,
HttpServletRequest request) {

Optional<QuestionnaireModelDto> questMod = questionnaireModelService.findDtoById(questionnaireModel.getIdQuestionnaireModel());
Optional<QuestionnaireModelDto> 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<SurveyUnitOkNokDto> getQuestionnaireModelIdBySurveyUnits(
@RequestBody List<String> lstSurveyUnitId,
HttpServletRequest request) {
List<SurveyUnit> lstSurveyUnit = (List<SurveyUnit>) surveyUnitService.findByIds(lstSurveyUnitId);
List<String> surveyUnitsIds = lstSurveyUnit.stream().map(SurveyUnit::getId).collect(Collectors.toList());
List<SurveyUnitResponseDto> surveyUnitsNOK = lstSurveyUnitId.stream()
.filter(su -> !surveyUnitsIds.contains(su))
.map(su -> new SurveyUnitResponseDto(su))
.collect(Collectors.toList());
List<SurveyUnitResponseDto> 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);
}
}

0 comments on commit 35f9ee8

Please sign in to comment.