This is an automated email from the git hooks/post-receive script. New commit to branch feature/bug-on-save in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit ead4892801ffb21a0033ac982ab7c87ed495d1f7 Author: dcosse <cosse@codelutin.com> Date: Mon Oct 7 11:52:22 2019 +0200 refs #248 fix choices Adding/Removing process --- .../org/chorem/pollen/rest/api/v1/ChoiceApi.java | 19 +++++++++---- .../pollen/services/service/ChoiceService.java | 23 +++++++++++++++ .../pollen/services/service/QuestionService.java | 33 ++++------------------ 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java index 328f1027..f434bdc8 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ChoiceApi.java @@ -21,6 +21,8 @@ package org.chorem.pollen.rest.api.v1; * #L% */ +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.chorem.pollen.persistence.entity.Choice; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.Question; @@ -51,13 +53,15 @@ import java.util.List; @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class ChoiceApi { - + @Path("polls/{pollId}/questions/{questionId}/choices") @GET public List<ChoiceBean> getChoices(@Context ChoiceService choiceService, @PathParam("pollId") PollenEntityId<Poll> pollId, @PathParam("questionId") PollenEntityId<Question> questionId) { - + + Log log = LogFactory.getLog(ChoiceApi.class); + log.debug("getChoices" + " pollId:" + " questionId:" + questionId); List<ChoiceBean> choices = choiceService.getChoices(questionId.getEntityId()); return choices; @@ -69,7 +73,9 @@ public class ChoiceApi { @PathParam("pollId") PollenEntityId<Poll> pollId, @PathParam("questionId") PollenEntityId<Question> questionId, @PathParam("choiceId") PollenEntityId<Choice> choiceId) { - + + Log log = LogFactory.getLog(ChoiceApi.class); + log.debug("getChoice" + " pollId:" + " questionId:" + questionId); return choiceService.getChoice(questionId.getEntityId(), choiceId.getEntityId()); } @@ -82,7 +88,8 @@ public class ChoiceApi { @PathParam("pollId") PollenEntityId<Poll> pollId, @PathParam("questionId") PollenEntityId<Question> questionId, ChoiceBean choice) throws InvalidFormException { - + Log log = LogFactory.getLog(ChoiceApi.class); + log.debug("editChoice" + " pollId:" + " questionId:" + questionId); return choiceService.editChoice(questionId.getEntityId(), choice); } @@ -93,7 +100,9 @@ public class ChoiceApi { @PathParam("pollId") PollenEntityId<Poll> pollId, @PathParam("questionId") PollenEntityId<Question> questionId, @PathParam("choiceId")PollenEntityId<Choice> choiceId) throws InvalidFormException { - + + Log log = LogFactory.getLog(ChoiceApi.class); + log.debug("deleteChoice" + " pollId:" + " questionId:" + questionId); choiceService.deleteChoice(questionId.getEntityId(), choiceId.getEntityId()); } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java index 91b1a0c5..70d3abeb 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/ChoiceService.java @@ -36,6 +36,7 @@ import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.ReportResumeBean; import org.chorem.pollen.services.service.security.PollenPermissions; +import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Set; @@ -218,6 +219,28 @@ public class ChoiceService extends PollenServiceSupport { return result; } + + protected void saveChoices(Question question, List<ChoiceBean> choicesToSave) { + boolean questionExists = question.isPersisted(); + + List<Choice> existingChoices = questionExists ? getChoiceDao().findAll(question) : new ArrayList<>(); + + if (CollectionUtils.isNotEmpty(choicesToSave)) { + + for (ChoiceBean choice : choicesToSave) { + + Choice persistedChoice = saveChoice(question, choice); + existingChoices.remove(persistedChoice); + + } + } + + // the remaining choices have been deleted by the user and must be removed + if (CollectionUtils.isNotEmpty(existingChoices)) { + getChoiceDao().deleteAll(existingChoices); + } + + } protected Choice saveChoice(Question question, ChoiceBean choice) { diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/QuestionService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/QuestionService.java index 5c103be6..72ccc936 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/QuestionService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/QuestionService.java @@ -5,7 +5,6 @@ import org.apache.commons.collections4.CollectionUtils; import org.chorem.pollen.persistence.entity.Choice; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.PollenPrincipal; -import org.chorem.pollen.persistence.entity.PollenResource; import org.chorem.pollen.persistence.entity.Question; import org.chorem.pollen.persistence.entity.QuestionImpl; import org.chorem.pollen.persistence.entity.QuestionTopiaDao; @@ -155,26 +154,17 @@ public class QuestionService extends PollenServiceSupport { Question toSave; - List<Choice> existingChoices; - - if (questionExists) { // get existing question - toSave = getQuestion(poll, question.getEntityId()); - - existingChoices = getChoiceDao().findAll(toSave); - + } else { // create a new question toSave = questionDao.create(); - toSave.setPoll(poll); - - existingChoices = new ArrayList<>(); - + } toSave.setChoiceAddAllowed(question.isChoiceAddAllowed()); @@ -191,21 +181,10 @@ public class QuestionService extends PollenServiceSupport { String configToJson = getVoteCountingService().configToJson(question.getVoteCountingConfig()); toSave.setVoteCountingConfig(configToJson); - // -- choice -- // - - if (CollectionUtils.isNotEmpty(question.getChoices())) { - - ChoiceService choiceService = getChoiceService(); - - for (ChoiceBean choice : question.getChoices()) { - - choice.setChoiceOrder(existingChoices.size()); - Choice persistedChoice = choiceService.saveChoice(toSave, choice); - existingChoices.add(persistedChoice); - - } - } - + // -- choices -- // + ChoiceService choiceService = getChoiceService(); + choiceService.saveChoices(toSave, question.getChoices()); + return toSave; } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.