-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: codelists endpoints #343
Open
laurentC35
wants to merge
41
commits into
main
Choose a base branch
from
feat/codelists-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
8186af5
chore(model): create codeList model
laurentC35 67b09b8
chore(model): create utils function to convert model
laurentC35 44712ff
feat: create new Service to manage codeLists
laurentC35 caa406b
feat: create CodesList controller
laurentC35 1db67d3
ref: update controller numbering
laurentC35 b398abf
test: handle new feature (codesList)
laurentC35 a2c161f
bump: 4.12.0-SNAPSHOT
laurentC35 cab9cbc
fix: add empty parent to code
laurentC35 822d04e
bump: 4.12.0-SNAPSHOT.1
laurentC35 da58832
ref: rename to CodesListService
laurentC35 5ea6e27
ref: improve method with stream
laurentC35 049423d
ref: simplify impl
laurentC35 2848a2f
wip: create VariableService
laurentC35 4135c28
ref: remove cast to QuestionType
laurentC35 855524f
ref: move PoguesModelUtils
laurentC35 ced4e84
ref: move utils function to CodeList class
laurentC35 ee74e87
ref: create utils class according to Poges-Model
laurentC35 ce4faa8
ref: simplify variableService
laurentC35 b5f8fdc
ref: remove VariableService
laurentC35 678e3a7
fix: variableName generation
laurentC35 0ea1ace
test: perform new tests
laurentC35 158bfed
bump; 4.12.0-SNAPSHOT.2
laurentC35 7f15bc8
ref: split codes
laurentC35 c163028
test: add test for heart method
laurentC35 9f66a54
bump: 4.12.0-SNAPSHOT.3
laurentC35 eb3ab7a
ref: simplify variables generation
laurentC35 a14574e
fix: mapping with response collected variables Id
laurentC35 4d673d4
test: check variable factory
laurentC35 e7acd31
fix: update table condition
laurentC35 a62e57b
feat: remove clarification Question for question with updatedCodeList
laurentC35 704fda2
bump: 4.12.0-SNAPSHOT.4
laurentC35 2ee1efa
fix: naming convention (keep only allow char for variable name)
laurentC35 7d691bf
fix: getNeededCollectedVariablesInQuestionnaire method (add arbitrary…
laurentC35 5f8b1a7
bump: 4.12.0-SNAPSHOT.5
laurentC35 5ce4ed9
fix: name of MultipleChoice variables
laurentC35 cef2818
ref: Exception to handle CodeList custom message
laurentC35 540e2f3
test: fix test with exception
laurentC35 132ac5f
chore: add content type
laurentC35 2e70108
bump: 4.12.0-SNAPSHOT.6
laurentC35 461da5e
feat: return list of updatedQuestion
laurentC35 4c36dcd
Merge branch 'main' into feat/codelists-endpoints
laurentC35 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/fr/insee/pogues/exception/CodesListException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package fr.insee.pogues.exception; | ||
|
||
import fr.insee.pogues.webservice.error.ApiMessage; | ||
import fr.insee.pogues.webservice.error.CodesListMessage; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class CodesListException extends PoguesException { | ||
|
||
private List<String> questionIds; | ||
|
||
public CodesListException(int status, String message, String details, List<String> questionIds) { | ||
super(status, message, details); | ||
this.questionIds = questionIds; | ||
} | ||
|
||
@Override | ||
public ApiMessage toApiMessage(){ | ||
return new CodesListMessage(this.getStatus(), this.getMessage(),this.getDetails(), questionIds); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/fr/insee/pogues/exception/GenericException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package fr.insee.pogues.exception; | ||
|
||
import fr.insee.pogues.webservice.error.ApiMessage; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public abstract class GenericException extends Exception { | ||
|
||
private String details; | ||
|
||
public GenericException(String message, String details){ | ||
super(message); | ||
this.details = details; | ||
} | ||
|
||
public ApiMessage toApiMessage() { | ||
return new ApiMessage(500, this.getMessage(), details); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/fr/insee/pogues/exception/PoguesException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package fr.insee.pogues.exception; | ||
|
||
import fr.insee.pogues.webservice.error.ApiMessage; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PoguesException extends GenericException { | ||
|
||
private int status; | ||
private String details; | ||
|
||
/** | ||
* | ||
* @param status | ||
* @param message | ||
* @param details | ||
*/ | ||
public PoguesException(int status, String message, String details) { | ||
super(message, details); | ||
this.status = status; | ||
this.details = details; | ||
} | ||
|
||
@Override | ||
public ApiMessage toApiMessage(){ | ||
return new ApiMessage(this.status, this.getMessage(), this.details); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/fr/insee/pogues/metadata/client/DDIASClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
src/main/java/fr/insee/pogues/service/CodesListService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package fr.insee.pogues.service; | ||
|
||
import fr.insee.pogues.exception.CodesListException; | ||
import fr.insee.pogues.model.*; | ||
import fr.insee.pogues.persistence.service.QuestionnairesService; | ||
import fr.insee.pogues.utils.PoguesDeserializer; | ||
import fr.insee.pogues.utils.PoguesSerializer; | ||
import fr.insee.pogues.webservice.model.dtd.codeList.CodesList; | ||
import fr.insee.pogues.exception.PoguesException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
import static fr.insee.pogues.utils.CodesListConverter.*; | ||
import static fr.insee.pogues.utils.json.JSONFunctions.jsonStringtoJsonNode; | ||
import static fr.insee.pogues.utils.model.CodesList.getListOfQuestionIdWhereCodesListIsUsed; | ||
import static fr.insee.pogues.utils.model.CodesList.getListOfQuestionWhereCodesListIsUsed; | ||
import static fr.insee.pogues.utils.model.Variables.getNeededCollectedVariablesInQuestionnaire; | ||
import static fr.insee.pogues.utils.model.question.Common.removeClarificationQuestion; | ||
import static fr.insee.pogues.utils.model.question.MultipleChoice.updateMultipleChoiceQuestionAccordingToCodeList; | ||
import static fr.insee.pogues.utils.model.question.Table.updateTableQuestionAccordingToCodeList; | ||
|
||
@Service | ||
@Slf4j | ||
public class CodesListService { | ||
private QuestionnairesService questionnairesService; | ||
|
||
@Autowired | ||
public CodesListService(QuestionnairesService questionnairesService){ | ||
this.questionnairesService = questionnairesService; | ||
} | ||
|
||
/** | ||
* | ||
* @param questionnaireId | ||
* @param idCodesList | ||
* @param codesList | ||
* @return the list of question's id updated (or null if created) | ||
* @throws Exception | ||
*/ | ||
public List<String> updateOrAddCodeListToQuestionnaire(String questionnaireId, String idCodesList, CodesList codesList) throws Exception { | ||
Questionnaire questionnaire = retrieveQuestionnaireWithId(questionnaireId); | ||
List<String> updatedQuestionIds = updateOrAddCodeListToQuestionnaire(questionnaire, idCodesList, codesList); | ||
updateQuestionnaireInDataBase(questionnaire); | ||
return updatedQuestionIds; | ||
} | ||
/** | ||
* | ||
* @param questionnaire | ||
* @param idCodesList | ||
* @param codesList | ||
* @return the list of question's id updated (or null if created) | ||
* @throws Exception | ||
*/ | ||
public List<String> updateOrAddCodeListToQuestionnaire(Questionnaire questionnaire, String idCodesList, CodesList codesList) { | ||
List<fr.insee.pogues.model.CodeList> codesLists = questionnaire.getCodeLists().getCodeList(); | ||
boolean created = updateOrAddCodeListDTD(codesLists, idCodesList, codesList); | ||
if(!created) return updateQuestionAndVariablesAccordingToCodesList(questionnaire, idCodesList); | ||
return null; | ||
} | ||
|
||
public void deleteCodeListOfQuestionnaireWithId(String questionnaireId, String codesListId) throws Exception { | ||
Questionnaire questionnaire = retrieveQuestionnaireWithId(questionnaireId); | ||
deleteCodeListOfQuestionnaire(questionnaire, codesListId); | ||
updateQuestionnaireInDataBase(questionnaire); | ||
} | ||
|
||
public void deleteCodeListOfQuestionnaire(Questionnaire questionnaire, String codesListId) throws Exception { | ||
List<String> questionIds = getListOfQuestionIdWhereCodesListIsUsed(questionnaire, codesListId); | ||
if(!questionIds.isEmpty()){ | ||
throw new CodesListException(400, String.format("CodesList with id %s is required.", codesListId), "", questionIds); | ||
|
||
} | ||
List<fr.insee.pogues.model.CodeList> codesLists = questionnaire.getCodeLists().getCodeList(); | ||
removeCodeListDTD(codesLists, codesListId); | ||
} | ||
|
||
private Questionnaire retrieveQuestionnaireWithId(String id) throws Exception { | ||
return PoguesDeserializer.questionnaireToJavaObject(questionnairesService.getQuestionnaireByID(id)); | ||
} | ||
|
||
private void updateQuestionnaireInDataBase(Questionnaire questionnaire) throws Exception { | ||
questionnairesService.updateQuestionnaire( | ||
questionnaire.getId(), | ||
jsonStringtoJsonNode(PoguesSerializer.questionnaireJavaToString(questionnaire))); | ||
} | ||
|
||
/** | ||
* | ||
* @param existingCodeLists | ||
* @param idCodesList | ||
* @param codesListDtdToUpdate | ||
* @return true if new codeList is created | ||
*/ | ||
boolean updateOrAddCodeListDTD(List<CodeList> existingCodeLists, String idCodesList, CodesList codesListDtdToUpdate) { | ||
if(existingCodeLists.stream().noneMatch(codeList -> Objects.equals(idCodesList, codeList.getId()))){ | ||
addCodeListDTD(existingCodeLists, codesListDtdToUpdate); | ||
return true; | ||
} | ||
codesListDtdToUpdate.setId(idCodesList); | ||
replaceElementInListAccordingCondition( | ||
existingCodeLists, | ||
codeList -> Objects.equals(idCodesList, codeList.getId()), | ||
codesListDtdToUpdate, | ||
codesList -> convertFromCodeListDTDtoCodeListModel(codesListDtdToUpdate)); | ||
return false; | ||
} | ||
|
||
|
||
void addCodeListDTD(List<CodeList> existingCodeLists, CodesList codesListDtdToAdd){ | ||
existingCodeLists.add(convertFromCodeListDTDtoCodeListModel(codesListDtdToAdd)); | ||
} | ||
|
||
void removeCodeListDTD(List<CodeList> existingCodeLists, String id) throws CodesListException { | ||
boolean deleted = existingCodeLists.removeIf(codeList -> id.equals(codeList.getId())); | ||
if(!deleted) throw new CodesListException(404, "Not found", String.format("CodesList with id %s doesn't exist in questionnaire", id),null); | ||
} | ||
|
||
List<String> updateQuestionAndVariablesAccordingToCodesList(Questionnaire questionnaire, String updatedCodeListId){ | ||
// Retrieve updatedCodeList in questionnaire | ||
CodeList codeList = questionnaire.getCodeLists().getCodeList().stream().filter(cL -> updatedCodeListId.equals(cL.getId())).findFirst().get(); | ||
// Just retrieve MULTIPLE_CHOICE and TABLE questions | ||
List<QuestionType> questionsToModify = getListOfQuestionWhereCodesListIsUsed(questionnaire, updatedCodeListId); | ||
// Clear Clarification question for concerned question | ||
questionsToModify.forEach(question -> removeClarificationQuestion(question)); | ||
// modify Multiple and Table question and get there new Variables | ||
List<QuestionType> multipleAndTableQuestion = questionsToModify.stream() | ||
.filter(questionType -> { | ||
QuestionTypeEnum questionTypeEnum = questionType.getQuestionType(); | ||
return QuestionTypeEnum.MULTIPLE_CHOICE.equals(questionTypeEnum) || QuestionTypeEnum.TABLE.equals(questionTypeEnum);}) | ||
.toList(); | ||
List<VariableType> variables = multipleAndTableQuestion.stream() | ||
.map(questionType -> { | ||
QuestionTypeEnum questionTypeEnum = questionType.getQuestionType(); | ||
if(QuestionTypeEnum.MULTIPLE_CHOICE.equals(questionTypeEnum)) return updateMultipleChoiceQuestionAccordingToCodeList(questionType, codeList); | ||
return updateTableQuestionAccordingToCodeList(questionType, codeList, questionnaire.getCodeLists().getCodeList()); | ||
}) | ||
.flatMap(Collection::stream) | ||
.toList(); | ||
// add new variables to questionnaire | ||
questionnaire.getVariables().getVariable().addAll(variables); | ||
// Delete variables that are not referenced in response | ||
List<String> neededCollectedVariables = getNeededCollectedVariablesInQuestionnaire(questionnaire); | ||
questionnaire.getVariables().getVariable().removeIf(variableType -> !neededCollectedVariables.contains(variableType.getId())); | ||
return questionsToModify.stream().map(question -> question.getId()).toList(); | ||
} | ||
|
||
private <T,G> void replaceElementInListAccordingCondition(List<T> elements, Predicate<T> conditionFunction, G newElement, Function<G,T> factory){ | ||
for (int i = 0; i < elements.size(); i++) { | ||
T element = elements.get(i); | ||
if (conditionFunction.test(element)) { | ||
elements.set(i, factory.apply(newElement)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plus besoin du
@Autowired
comme tu utilises déjà l'injection par constructeur. tu eux remove