Author: fdesbois Date: 2012-03-27 21:09:51 +0200 (Tue, 27 Mar 2012) New Revision: 3212 Url: http://chorem.org/repositories/revision/pollen/3212 Log: - add PollUriConverter for parameter uriId that becomes a PollUri - add BooleanIntegerConverter for voteValue in case of NORMAL VoteCounting Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/BooleanIntegerConverter.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollUriConverter.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/business/ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/business/persistence/ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/business/persistence/VoteToChoice-conversion.properties branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AbstractPollUriIdAction-conversion.properties Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollUriIdAction.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/EditVote.java Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/BooleanIntegerConverter.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/BooleanIntegerConverter.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/BooleanIntegerConverter.java 2012-03-27 19:09:51 UTC (rev 3212) @@ -0,0 +1,49 @@ +package org.chorem.pollen.ui.actions; + +import org.apache.struts2.util.StrutsTypeConverter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + * Created: 26/03/12 + * + * @author fdesbois <desbois@codelutin.com> + * $Id$ + */ +public class BooleanIntegerConverter extends StrutsTypeConverter { + + private static final Logger log = LoggerFactory.getLogger(BooleanIntegerConverter.class); + + @Override + public Integer convertFromString(Map context, String[] values, Class toClass) { + Integer result; + if (values.length == 1) { + result = parseValue(values[0]); + + } else { + result = null; + } + return result; + } + + protected Integer parseValue(String value) { + Integer result; + if ("true".equals(value)) { + result = 1; + + } else if ("false".equals(value)) { + result = 0; + + } else { + result = Integer.parseInt(value); + } + return result; + } + + @Override + public String convertToString(Map context, Object object) { + return object == null ? "" : String.valueOf(object); + } +} Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollUriConverter.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollUriConverter.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollUriConverter.java 2012-03-27 19:09:51 UTC (rev 3212) @@ -0,0 +1,55 @@ +package org.chorem.pollen.ui.actions; + +import com.google.common.base.Preconditions; +import org.apache.struts2.util.StrutsTypeConverter; +import org.chorem.pollen.bean.PollUri; + +import java.util.Map; + +/** + * Created: 27/03/12 + * + * @author fdesbois <desbois@codelutin.com> + */ +public class PollUriConverter extends StrutsTypeConverter { + + private static PollUriConverter instance; + + public static PollUriConverter getInstance() { + if (instance == null) { + instance = new PollUriConverter(); + } + return instance; + } + + @Override + public PollUri convertFromString(Map context, String[] values, Class toClass) { + PollUri result; + if (values.length == 1) { + String value = values[0]; + result = PollUri.newPollUri(value); + + } else { + result = null; + } + return result; + } + + @Override + public String convertToString(Map context, Object o) { + String result; + if (o != null) { + result = ((PollUri)o).getUri(); + + } else { + result = ""; + } + return result; + } + + public static PollUri convertFromString(String[] values) { + Preconditions.checkNotNull(values); + PollUri result = getInstance().convertFromString(null, values, PollUri.class); + return result; + } +} Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollUriIdAction.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollUriIdAction.java 2012-03-27 11:28:43 UTC (rev 3211) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollUriIdAction.java 2012-03-27 19:09:51 UTC (rev 3212) @@ -24,8 +24,11 @@ package org.chorem.pollen.ui.actions.poll; import org.chorem.pollen.bean.PollUri; +import org.chorem.pollen.ui.actions.PollUriConverter; import org.chorem.pollen.ui.actions.PollenActionSupport; +import java.util.Map; + /** * Abstract action for all actions with a poll uri id. * @@ -34,23 +37,21 @@ * @since 1.2.6 */ public abstract class AbstractPollUriIdAction extends PollenActionSupport { + + public static final String PARAM_POLL_URI = "uriId"; private static final long serialVersionUID = 1L; private PollUri pollUri; - public final String getUriId() { - return pollUri != null ? pollUri.getUri() : null; + public final PollUri getUriId() { + return pollUri; } - public final void setUriId(String uriId) { - pollUri = PollUri.newPollUri(uriId); + public final void setUriId(PollUri pollUri) { + this.pollUri = pollUri; } - public void prepareUriId(String pollId, String accountId) { - pollUri = PollUri.newPollUri(pollId, accountId); - } - public final String getPollId() { return pollUri != null ? pollUri.getPollId() : null; } @@ -58,4 +59,11 @@ public final String getAccountId() { return pollUri != null ? pollUri.getAccountId() : null; } + + protected void preparePollUri(Map<String, String[]> parameters) { + if (pollUri == null) { + String[] values = parameters.get(PARAM_POLL_URI); + pollUri = PollUriConverter.convertFromString(values); + } + } } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-03-27 11:28:43 UTC (rev 3211) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-03-27 19:09:51 UTC (rev 3212) @@ -267,13 +267,8 @@ public void prepareVotePage() throws Exception { - // Ensure pollId for loading - if (getPollId() == null) { - log.debug("parameters= " + parameters); - String uriId = parameters.get("uriId")[0]; - log.debug("uriId= " + uriId); - setUriId(uriId); - } + // Ensure uri for poll and pollAccount loading + preparePollUri(parameters); loadPoll(); Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/EditVote.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/EditVote.java 2012-03-27 11:28:43 UTC (rev 3211) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/EditVote.java 2012-03-27 19:09:51 UTC (rev 3212) @@ -23,13 +23,14 @@ */ package org.chorem.pollen.ui.actions.poll; +import org.chorem.pollen.bean.PollUri; + /** * EditVote is a redirection to vote page. This action will simply update the * uri with {@code accountId} and {@code pollId}. * * @author fdesbois <fdesbois@codelutin.com> * @since 1.2.6 - * @see #prepareUriId(String, String) */ public class EditVote extends AbstractPollUriIdAction { @@ -50,7 +51,7 @@ @Override public String execute() throws Exception { - prepareUriId(pollId, accountId); + setUriId(PollUri.newPollUri(pollId, accountId)); return SUCCESS; } Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/business/persistence/VoteToChoice-conversion.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/business/persistence/VoteToChoice-conversion.properties (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/business/persistence/VoteToChoice-conversion.properties 2012-03-27 19:09:51 UTC (rev 3212) @@ -0,0 +1 @@ +voteValue=org.chorem.pollen.ui.actions.BooleanIntegerConverter \ No newline at end of file Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AbstractPollUriIdAction-conversion.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AbstractPollUriIdAction-conversion.properties (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AbstractPollUriIdAction-conversion.properties 2012-03-27 19:09:51 UTC (rev 3212) @@ -0,0 +1 @@ +uriId=org.chorem.pollen.ui.actions.PollUriConverter \ No newline at end of file