Author: fdesbois Date: 2012-04-26 12:02:23 +0200 (Thu, 26 Apr 2012) New Revision: 3319 Url: http://chorem.org/repositories/revision/pollen/3319 Log: fixes #536 : - missing # for choice date and image in JSP - use choice creators on poll loading (need specific instances depends on ChoiceType) - improve choice update Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java 2012-04-25 10:45:30 UTC (rev 3318) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java 2012-04-26 10:02:23 UTC (rev 3319) @@ -35,6 +35,8 @@ import org.chorem.pollen.business.persistence.PollAccountImpl; import org.chorem.pollen.business.persistence.VotingList; import org.chorem.pollen.business.persistence.VotingListImpl; +import org.chorem.pollen.common.ChoiceType; +import org.nuiton.topia.persistence.TopiaEntity; import java.util.Date; import java.util.List; @@ -68,6 +70,10 @@ } } + public static TopiaIdExtractor newTopiaIdExtractor() { + return new TopiaIdExtractor(); + } + public static TextChoiceCreator newTextChoiceCreator() { return new TextChoiceCreator(); } @@ -80,6 +86,25 @@ return new DateChoiceCreator(); } + public static Function<Choice, Choice> newChoiceCreator(ChoiceType choiceType) { + Function<Choice, Choice> result; + switch (choiceType) { + + case DATE: + result = newDateChoiceCreator(); + break; + + case IMAGE: + result = newImageChoiceCreator(); + break; + + default: + case TEXT: + result = newTextChoiceCreator(); + } + return result; + } + public static Function<VotingList, VotingList> newVotingListCreator( Function<PersonToList, PersonToList> persontoListCreator) { return new VotingListCreator(persontoListCreator); @@ -88,7 +113,15 @@ public static Function<PersonToList, PersonToList> newPersonToListCreator() { return new PersonToListCreator(); } + + public static class TopiaIdExtractor implements Function<TopiaEntity, String> { + @Override + public String apply(TopiaEntity input) { + return input.getTopiaId(); + } + } + public static class TextChoiceCreator implements Function<Choice, Choice> { @Override Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-04-25 10:45:30 UTC (rev 3318) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-04-26 10:02:23 UTC (rev 3319) @@ -23,9 +23,14 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.base.Function; import com.google.common.base.Objects; import com.google.common.base.Preconditions; +import com.google.common.base.Predicates; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -53,6 +58,7 @@ import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.common.PollType; import org.chorem.pollen.entities.PollenBinderHelper; +import org.chorem.pollen.services.PollenServiceFunctions; import org.chorem.pollen.services.PollenServiceSupport; import org.chorem.pollen.services.exceptions.PollAccountNotFound; import org.chorem.pollen.services.exceptions.PollChoiceNotFoundException; @@ -72,6 +78,8 @@ import java.net.URL; import java.util.Date; import java.util.List; +import java.util.Map; +import java.util.Set; public class PollService extends PollenServiceSupport { @@ -184,15 +192,23 @@ } // -- Choice -- // - ChoiceDAO choiceDAO = getDAO(Choice.class); - for (Choice choiceLoaded : source.getChoice()) { - Choice choiceEditable = newInstance(choiceDAO); - result.addChoice(choiceEditable); - // Do not keep choices topiaId, to simplify the update will delete old choices and create new ones - PollenBinderHelper.simpleCopy( - choiceLoaded, choiceEditable, false); - } + Function<Choice, Choice> choiceCreator = + PollenServiceFunctions.newChoiceCreator(source.getChoiceType()); + Iterable<Choice> choices = + Iterables.transform(source.getChoice(), choiceCreator); + // Ensure iteration with a new ArrayList instead of TransformingSequentialList + // from guava Lists#transform() that doesn't support add() method + result.setChoice(Lists.newArrayList(choices)); + +// for (Choice choiceLoaded : source.getChoice()) { +// Choice choiceEditable = newInstance(choiceDAO); +// result.addChoice(choiceEditable); +// // Do not keep choices topiaId, to simplify the update will delete old choices and create new ones +// PollenBinderHelper.simpleCopy( +// choiceLoaded, choiceEditable, false); +// } + // -- VotingList -- // VotingListDAO votingListDAO = getDAO(VotingList.class); PersonToListDAO personToListDAO = getDAO(PersonToList.class); @@ -341,11 +357,27 @@ // -- Choices -- // if (!voteStarted) { - - pollToUpdate.clearChoice(); + + // Retrieve previous existing choices + Map<String, Choice> choicesExist = Maps.uniqueIndex( + pollToUpdate.getChoice(), + PollenServiceFunctions.newTopiaIdExtractor() + ); + + // Save all choices from source poll + Set<String> choicesAdded = Sets.newHashSet(); for (Choice choice : poll.getChoice()) { - saveChoice(pollToUpdate, choice); + String choiceId = saveChoice(pollToUpdate, choice); + choicesAdded.add(choiceId); } + + // Delete all previous choices not keeped in source poll + Map<String, Choice> choicesToDelete = + Maps.filterKeys(choicesExist, + Predicates.not(Predicates.in(choicesAdded))); + for (Choice choice : choicesToDelete.values()) { + pollToUpdate.removeChoice(choice); + } } // -- PreventRules -- // @@ -724,7 +756,7 @@ commitTransaction("Can't create new choice [" + poll.getChoiceType() + "] for poll '" + pollId + "'"); } - protected void saveChoice(Poll poll, Choice choice) { + protected String saveChoice(Poll poll, Choice choice) { ChoiceType choiceType = poll.getChoiceType(); ChoiceDAO dao = getDAO(Choice.class); @@ -764,6 +796,8 @@ choiceLoaded.setValidate(choice.isValidate()); choiceLoaded.setName(choice.getName()); } + + return choiceLoaded.getTopiaId(); } public void deleteChoice(String pollId, String choiceId) Modified: trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp =================================================================== --- trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp 2012-04-25 10:45:30 UTC (rev 3318) +++ trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp 2012-04-26 10:02:23 UTC (rev 3319) @@ -97,7 +97,7 @@ theme="simple" value=''/> <sj:datepicker id='%{#prefix}.name' key="%{#prefix}.name" changeMonth="true" changeYear="true" labelSeparator="" theme="simple" label="" - timepicker="true" value="%{choice.date}" + timepicker="true" value="%{#choice.date}" displayFormat="%{getText('pollen.common.datePickerPattern')}" disabled="%{voteStarted}"/> - @@ -107,7 +107,7 @@ <div class="fleft"> <s:textarea cols="30" id="%{#prefix}.description" key="%{#prefix}.description" label='' theme="simple" - value="%{choice.description}" + value="%{#choice.description}" disabled="%{voteStarted}"/> </div> <s:if test="!voteStarted"> @@ -150,13 +150,13 @@ <%--Uploaded image--%> <s:hidden id="%{#prefix}.name" name="%{#prefix}.name" - value="%{choice.name}" label='' theme="simple"/> + value="%{#choice.name}" label='' theme="simple"/> <s:hidden id="%{#prefix}.location" name="%{#prefix}.location" - value="%{choice.location}" label='' theme="simple"/> + value="%{#choice.location}" label='' theme="simple"/> <s:label label='' theme="simple" cssClass="nameField" - value="%{choice.name}" readonly="true"/> + value="%{#choice.name}" readonly="true"/> </s:if> <s:else> <%--New image--%> @@ -169,7 +169,7 @@ </div> <div class="fleft"> <s:textarea cols="30" id="%{#prefix}.description" label='' theme="simple" - key="%{#prefix}.description" value="%{choice.description}" + key="%{#prefix}.description" value="%{#choice.description}" disabled="%{voteStarted}"/> </div> <s:if test="!voteStarted">