r3228 - in branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main: java/org/chorem/pollen/ui/actions/poll resources/config
Author: fdesbois Date: 2012-04-02 18:42:38 +0200 (Mon, 02 Apr 2012) New Revision: 3228 Url: http://chorem.org/repositories/revision/pollen/3228 Log: Begin EditPoll action Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/CreatePoll.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/EditPoll.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/config/struts-poll.xml Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/CreatePoll.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/CreatePoll.java 2012-04-02 16:42:32 UTC (rev 3227) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/CreatePoll.java 2012-04-02 16:42:38 UTC (rev 3228) @@ -138,7 +138,7 @@ * All the parameters send by request used to build back choices of the * poll. */ - private Map<String, String[]> parameters; + protected Map<String, String[]> parameters; private boolean informationsError; @@ -311,7 +311,16 @@ } return result; } + + protected int getNbChoices() { + //TODO tchemit-2012-03-05 use a default value from configuration + return 5; + } + protected int getDefaultNbVotingLists() { + return 1; + } + @Override public void prepare() throws Exception { @@ -322,9 +331,8 @@ choiceTypes = decorateToName(ChoiceType.values()); voteCountingTypes = decorateToName(VoteCountingType.values()); - //TODO tchemit-2012-03-05 use a default value from configuration - nbTextChoices = nbDateChoices = nbImageChoices = 5; - nbVotingLists = 1; + nbTextChoices = nbDateChoices = nbImageChoices = getNbChoices(); + nbVotingLists = getDefaultNbVotingLists(); String tokenSuffix = getServiceContext().createPollenUrlId(); @@ -358,15 +366,15 @@ switch (pollChoiceType) { case TEXT: - choices = buildTextChoices(5); + choices = buildTextChoices(nbTextChoices); tokenId = textChoiceTokenId; break; case DATE: - choices = buildDateChoices(5); + choices = buildDateChoices(nbDateChoices); tokenId = dateChoiceTokenId; break; case IMAGE: - choices = buildImageChoices(5); + choices = buildImageChoices(nbImageChoices); tokenId = imageChoiceTokenId; break; } @@ -376,7 +384,7 @@ votingListTokenId = DisplayVotingList.VOTING_LIST_TOKEN + "_" + tokenSuffix; - Map<Integer, VotingList> votingLists = buildVotingLists(1); + Map<Integer, VotingList> votingLists = buildVotingLists(nbVotingLists); getPollenSession().putDynamicData(votingListTokenId, votingLists); } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/EditPoll.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/EditPoll.java 2012-04-02 16:42:32 UTC (rev 3227) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/EditPoll.java 2012-04-02 16:42:38 UTC (rev 3228) @@ -23,13 +23,220 @@ */ package org.chorem.pollen.ui.actions.poll; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import org.chorem.pollen.bean.PollImageChoice; +import org.chorem.pollen.bean.PollUri; +import org.chorem.pollen.business.persistence.Choice; +import org.chorem.pollen.business.persistence.PersonToList; +import org.chorem.pollen.business.persistence.Poll; +import org.chorem.pollen.business.persistence.PollAccount; +import org.chorem.pollen.business.persistence.VotingList; +import org.chorem.pollen.common.ChoiceType; +import org.chorem.pollen.common.PollType; +import org.chorem.pollen.ui.actions.PollUriConverter; + +import java.util.List; +import java.util.Map; + /** - * Display a poll. + * Edit a poll. * * @author tchemit <chemit@codelutin.com> + * @author fdesbois <desbois@codelutin.com> * @since 1.2.6 */ -public class EditPoll extends AbstractPollUriIdAction { +public class EditPoll extends CreatePoll { - private static final long serialVersionUID = 1L; + private static final String PREFIX_CHOICE = "%sChoice_%d"; + + private static final String PREFIX_VOTING_LIST = "votingList_%d"; + + private static final String PREFIX_PERSON_TO_LIST = "personToList_%d_%d"; + + protected PollUri pollUri; + + public final PollUri getUriId() { + return pollUri; + } + + public final void setUriId(PollUri pollUri) { + this.pollUri = pollUri; + } + + protected void preparePollUri(Map<String, String[]> parameters) { + if (pollUri == null) { + String[] values = parameters.get("uriId"); + pollUri = PollUriConverter.convertFromString(values); + } + } + + protected boolean needTopiaId() { + return true; + } + + public boolean isVoteStarted() { + return getPoll().sizeVote() > 0; + } + + @Override + public Poll getPoll() { + if (poll == null) { + poll = getPollService().getPollByPollId(pollUri.getPollId()); + if (!needTopiaId()) { + poll.setTopiaId(null); + } + } + return poll; + } + + @Override + public void prepare() throws Exception { + + preparePollUri(parameters); + + // -- Choice -- + ChoiceType choiceType = getPoll().getChoiceType(); + prepareParams(getPoll().getChoice(), withChoicePrefix(choiceType)); + + // -- VotingList -- + if (getPoll().getPollType() != PollType.FREE) { + prepareParams(getPoll().getVotingList(), withVotingListPrefix()); + } + + super.prepare(); + } + + @Override + public String execute() throws Exception { + +// getPollService().updatePoll(poll): + + // remove all stuff from session + getPollenSession().clearDynamicData(); + + addActionMessage(_("pollen.info.poll.created")); + return SUCCESS; + } + + private Function<Choice, String> withChoicePrefix(final ChoiceType choiceType) { + final String choiceTypeName = choiceType.name().toLowerCase(); + return new IndexedFunction<Choice, String>() { + + @Override + protected String apply(Choice input, int index) { + + String prefix = String.format(PREFIX_CHOICE, choiceTypeName, index); + if (needTopiaId()) { + putParameter(prefix, Choice.TOPIA_ID, input.getTopiaId()); + } + putParameter(prefix, Choice.PROPERTY_NAME, input.getName()); + putParameter(prefix, Choice.PROPERTY_DESCRIPTION, input.getDescription()); + + if (choiceType == ChoiceType.IMAGE) { + putParameter(prefix, PollImageChoice.PROPERTY_LOCATION, input.getName()); + } + + return prefix; + } + }; + } + + private Function<VotingList, String> withVotingListPrefix() { + return new IndexedFunction<VotingList, String>() { + + @Override + protected String apply(VotingList input, int index) { + + String prefix = String.format(PREFIX_VOTING_LIST, index); + if (needTopiaId()) { + putParameter(prefix, VotingList.TOPIA_ID, input.getTopiaId()); + } + putParameter(prefix, VotingList.PROPERTY_NAME, input.getName()); + putParameter(prefix, VotingList.PROPERTY_WEIGHT, String.valueOf(input.getWeight())); + + prepareParams(input.getPollAccountPersonToList(), withPersonToListPrefix(index)); + + return prefix; + } + }; + } + + private Function<PersonToList, String> withPersonToListPrefix(final int votingListIndex) { + return new IndexedFunction<PersonToList, String>() { + + @Override + protected String apply(PersonToList input, int index) { + + PollAccount pollAccount = input.getPollAccount(); + + String prefix = String.format(PREFIX_PERSON_TO_LIST, votingListIndex, index); + if (needTopiaId()) { + putParameter(prefix, PersonToList.TOPIA_ID, input.getTopiaId()); + } + putParameter(prefix, PersonToList.PROPERTY_WEIGHT, String.valueOf(input.getWeight())); + putParameter(prefix, PollAccount.PROPERTY_ACCOUNT_ID, pollAccount.getAccountId()); + putParameter(prefix, PollAccount.PROPERTY_VOTING_ID, pollAccount.getVotingId()); + putParameter(prefix, PollAccount.PROPERTY_EMAIL, pollAccount.getEmail()); + + return prefix; + } + }; + } + + /** + * Prepare the {@code source} for parameters using the {@code function} to + * push necessary data. + * + * @param source List of elements + * @param function Function used to push element data using {@link #putParameter(String, String, String)} + * @param <T> Type of data + * @see #putParameter(String, String, String) + */ + private <T> void prepareParams(List<T> source, Function<T, String> function) { + ImmutableList.copyOf(Lists.transform(source, function)); + } + + /** + * Put some data in the {@code parameters} with name built using + * {@code prefix} and {@code property}. The {@code value} will be put for + * this parameter. + * + * @param prefix Prefix to use for the parameter name + * @param property Name of the {@code property} to push in parameters + * @param value Value of this {@code property} + */ + private void putParameter(String prefix, String property, String value) { + parameters.put(prefix + "." + property, new String[]{value}); + } + + /** + * Guava {@link Function} that keeps index on each {@link Function#apply(Object)} call. + * You just have to implement {@link #apply(Object, int)} to have the current + * index of the {@code input} value. + * + * @param <F> Type of data where function will be applied on + * @param <T> Resulting type + */ + private abstract static class IndexedFunction<F, T> implements Function<F, T> { + + private int index; + + @Override + public T apply(F input) { + T result = apply(input, index); + index++; + return result; + } + + /** + * Called during {@link #apply(Object)} method with current {@code index}. + * + * @param input current value + * @param index current index + * @return the result of the function applied on the {@code input} object + */ + protected abstract T apply(F input, int index); + } } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/config/struts-poll.xml =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/config/struts-poll.xml 2012-04-02 16:42:32 UTC (rev 3227) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/config/struts-poll.xml 2012-04-02 16:42:38 UTC (rev 3228) @@ -93,7 +93,7 @@ <action name="modification/*" class="org.chorem.pollen.ui.actions.poll.EditPoll"> <param name="uriId">{1}</param> - <result name="input">/WEB-INF/jsp/poll/edit.jsp</result> + <result name="input">/WEB-INF/jsp/poll/create.jsp</result> <result>/WEB-INF/jsp/poll/resume.jsp</result> </action>
participants (1)
-
fdesbois@users.chorem.org