Author: fdesbois Date: 2012-03-30 12:32:15 +0200 (Fri, 30 Mar 2012) New Revision: 3221 Url: http://chorem.org/repositories/revision/pollen/3221 Log: - resolve issue on create poll with choiceType date and image - create ChoiceHelper - improve AddChoice validation Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/ChoiceHelper.java Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddChoice.java 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/VoteForPoll.java Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/ChoiceHelper.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/ChoiceHelper.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/ChoiceHelper.java 2012-03-30 10:32:15 UTC (rev 3221) @@ -0,0 +1,59 @@ +package org.chorem.pollen.bean; + +import com.google.common.base.Function; +import com.google.common.collect.Iterables; +import org.chorem.pollen.business.persistence.Choice; +import org.chorem.pollen.common.ChoiceType; + +import java.util.Date; + +/** + * Created: 30/03/12 + * + * @author fdesbois <desbois@codelutin.com> + */ +public final class ChoiceHelper { + + private ChoiceHelper() { + } + + public static Object toValue(Choice choice, ChoiceType choiceType) { + Object result; + switch (choiceType) { + case DATE: + if (choice instanceof PollDateChoice) { + result = ((PollDateChoice)choice).getDate(); + } else { + result = new Date(Long.parseLong(choice.getName())); + } + break; + case IMAGE: + if (choice instanceof PollImageChoice) { + result = ((PollImageChoice)choice).getLocation(); + } else { + result = choice.getName(); + } + break; + case TEXT: + default: + result = choice.getName(); + } + return result; + } + + public static Function<Choice, Object> toValue(final ChoiceType type) { + return new Function<Choice, Object>() { + + @Override + public Object apply(Choice input) { + return toValue(input, type); + } + }; + } + + public static Iterable<Object> toValues(Iterable<Choice> choices, + ChoiceType choiceType) { + return Iterables.transform(choices, toValue(choiceType)); + } + +} Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddChoice.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddChoice.java 2012-03-29 15:13:38 UTC (rev 3220) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddChoice.java 2012-03-30 10:32:15 UTC (rev 3221) @@ -23,17 +23,15 @@ */ package org.chorem.pollen.ui.actions.poll; +import com.google.common.collect.Iterables; import com.opensymphony.xwork2.Preparable; import com.opensymphony.xwork2.interceptor.annotations.InputConfig; import org.apache.commons.lang3.StringUtils; -import org.chorem.pollen.bean.PollDateChoice; -import org.chorem.pollen.bean.PollImageChoice; +import org.chorem.pollen.bean.ChoiceHelper; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.services.exceptions.PollNotFoundException; -import java.util.Date; - /** * To add a poll comment. * @@ -63,19 +61,26 @@ public void validate() { ChoiceType choiceType = getPoll().getChoiceType(); - switch (choiceType) { - case DATE: - validateChoiceDate(); - break; + String typeKey = choiceType.name().toLowerCase(); + + Object value = ChoiceHelper.toValue(choice, choiceType); - case IMAGE: - validateChoiceImage(); - break; + // -- Validate value notEmpty + if (value == null || (value instanceof String && StringUtils.isBlank((String) value))) { + String typeLabel = getText(choiceType.getI18nKey()); + addFieldError("choice." + typeKey, + _("pollen.error.choice.empty", typeLabel)); + } else { - case TEXT: - default: - validateChoice(); + // Retrieve existing values to check if the new choice not already exists + Iterable<Object> pollChoiceValues = ChoiceHelper.toValues(getPoll().getChoice(), choiceType); + + // -- Validate value notExists + if (Iterables.contains(pollChoiceValues, value)) { + addFieldError("choice." + typeKey, + _("pollen.error.poll.detected.duplicate.choice.name")); + } } } @@ -88,25 +93,4 @@ return SUCCESS; } - protected void validateChoiceDate() { - Date date = ((PollDateChoice) choice).getDate(); - if (date == null) { - addFieldError("choice.date", _("pollen.error.choice.empty", _("pollen.common.choice.date"))); - } - } - - protected void validateChoiceImage() { - String location = ((PollImageChoice) choice).getLocation(); - if (StringUtils.isBlank(location)) { - addFieldError("choice.image", _("pollen.error.choice.empty", _("pollen.common.choice.image"))); - } - } - - protected void validateChoice() { - String name = choice.getName(); - if (StringUtils.isBlank(name)) { - addFieldError("choice.text", _("pollen.error.choice.empty", _("pollen.common.choice.text"))); - } - } - } 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-03-29 15:13:38 UTC (rev 3220) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/CreatePoll.java 2012-03-30 10:32:15 UTC (rev 3221) @@ -34,6 +34,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.interceptor.ParameterAware; +import org.chorem.pollen.bean.ChoiceHelper; import org.chorem.pollen.bean.PollDateChoice; import org.chorem.pollen.bean.PollImageChoice; import org.chorem.pollen.business.persistence.Choice; @@ -333,6 +334,11 @@ tokenSuffix; ChoiceType pollChoiceType = getPoll().getChoiceType(); + if (pollChoiceType == null) { + // Retrieve choiceType from parameters, the poll object will be updated after prepare + String choiceTypeParam = getNonEmptyParameterValue("poll.choiceType"); + pollChoiceType = ChoiceType.valueOf(choiceTypeParam); + } if (log.isInfoEnabled()) { log.info("choice type " + pollChoiceType); @@ -387,19 +393,29 @@ addFieldError("poll.choices", _("pollen.error.poll.required.one.choice")); } else { +// +// //TODO tchemit improve this (from different cases text-date-image) +// // check there is no choice with same name +// boolean duplicateNameDetected = false; +// Set<String> names = Sets.newHashSet(); +// for (Choice choice : orderedChoices.values()) { +// String choiceName = choice.getName(); +// if (!names.add(choiceName)) { +// duplicateNameDetected = true; +// break; +// } +// } +// if (duplicateNameDetected) { +// addFieldError("poll.choices", +// _("pollen.error.poll.detected.duplicate.choice.name")); +// } - //TODO tchemit improve this (from different cases text-date-image) - // check there is no choice with same name - boolean duplicateNameDetected = false; - Set<String> names = Sets.newHashSet(); - for (Choice choice : orderedChoices.values()) { - String choiceName = choice.getName(); - if (!names.add(choiceName)) { - duplicateNameDetected = true; - break; - } - } - if (duplicateNameDetected) { + ChoiceType choiceType = poll.getChoiceType(); + int inputChoicesSize = orderedChoices.size(); + Set<Object> choiceValues = + Sets.newHashSet(ChoiceHelper.toValues(orderedChoices.values(), choiceType)); + + if (inputChoicesSize > choiceValues.size()) { addFieldError("poll.choices", _("pollen.error.poll.detected.duplicate.choice.name")); } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java 2012-03-29 15:13:38 UTC (rev 3220) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java 2012-03-30 10:32:15 UTC (rev 3221) @@ -47,10 +47,9 @@ private static final long serialVersionUID = 1L; @Override - public String input() throws Exception { + public void prepare() throws Exception { - //TODO DO me! - return INPUT; + prepareVotePage(); } @Override @@ -117,10 +116,4 @@ } return SUCCESS; } - - @Override - public void prepare() throws Exception { - - prepareVotePage(); - } }