Author: fdesbois Date: 2012-03-29 17:13:30 +0200 (Thu, 29 Mar 2012) New Revision: 3219 Url: http://chorem.org/repositories/revision/pollen/3219 Log: - add AddChoice action (validation redirection failed) - missing addChoiceAllowed flag save in createPoll - resolve issue on AddComment (validation doesn't work) Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddChoice.java Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.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/AddComment.java 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/i18n/pollen-ui-struts2_en_GB.properties branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-03-28 16:40:09 UTC (rev 3218) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-03-29 15:13:30 UTC (rev 3219) @@ -124,6 +124,7 @@ result.setAnonymous(poll.isAnonymous()); result.setAnonymousVoteAllowed(poll.isAnonymousVoteAllowed()); + result.setChoiceAddAllowed(poll.isChoiceAddAllowed()); result.setBeginChoiceDate(poll.getBeginChoiceDate()); result.setBeginDate(poll.getBeginDate()); result.setChoiceType(poll.getChoiceType()); @@ -211,40 +212,42 @@ // create choices - ChoiceType choiceType = result.getChoiceType(); - - ChoiceDAO choiceDAO = getDAO(Choice.class); - +// ChoiceType choiceType = result.getChoiceType(); +// +// ChoiceDAO choiceDAO = getDAO(Choice.class); +// for (Choice choice : poll.getChoice()) { - Choice choiceCreated = create(choiceDAO); - result.addChoice(choiceCreated); - - if (choiceType == ChoiceType.IMAGE) { - - // copy image where it belong and generate the thumb - - PollImageChoice imageChoice = (PollImageChoice) choice; - imageChoice.toChoice(choiceCreated); - try { - saveImages(result, imageChoice); - } catch (IOException e) { - throw new PollenTechnicalException( - "Could not create image choice", e); - } - } else if (choiceType == ChoiceType.DATE) { - - // date choice - - PollDateChoice dateChoice = (PollDateChoice) choice; - dateChoice.toChoice(choiceCreated); - } else { - - // text choice - choiceCreated.setDescription(choice.getDescription()); - choiceCreated.setValidate(choice.isValidate()); - choiceCreated.setName(choice.getName()); - } + addChoice(result, choice); +// +// Choice choiceCreated = create(choiceDAO); +// result.addChoice(choiceCreated); +// +// if (choiceType == ChoiceType.IMAGE) { +// +// // copy image where it belong and generate the thumb +// +// PollImageChoice imageChoice = (PollImageChoice) choice; +// imageChoice.toChoice(choiceCreated); +// try { +// saveImages(result, imageChoice); +// } catch (IOException e) { +// throw new PollenTechnicalException( +// "Could not create image choice", e); +// } +// } else if (choiceType == ChoiceType.DATE) { +// +// // date choice +// +// PollDateChoice dateChoice = (PollDateChoice) choice; +// dateChoice.toChoice(choiceCreated); +// } else { +// +// // text choice +// choiceCreated.setDescription(choice.getDescription()); +// choiceCreated.setValidate(choice.isValidate()); +// choiceCreated.setName(choice.getName()); +// } } } @@ -556,7 +559,79 @@ commitTransaction("Could not close poll " + poll.getTitle()); } + + public Choice getNewChoice(ChoiceType choiceType) { + Choice result; + switch (choiceType) { + + case DATE: + result = new PollDateChoice(); + break; + + case IMAGE: + result = new PollImageChoice(); + break; + + case TEXT: + default: + ChoiceDAO dao = getDAO(Choice.class); + result = newInstance(dao); + } + return result; + } + + public void createChoice(String pollId, Choice choice) throws PollNotFoundException { + + Preconditions.checkNotNull(pollId); + Preconditions.checkNotNull(choice); + + Poll poll = getPollByPollId(pollId); + + if (poll == null) { + throw new PollNotFoundException(); + } + + addChoice(poll, choice); + + commitTransaction("Can't create new choice [" + poll.getChoiceType() + "] for poll '" + pollId + "'"); + } + + protected void addChoice(Poll poll, Choice choice) { + + ChoiceType choiceType = poll.getChoiceType(); + ChoiceDAO dao = getDAO(Choice.class); + Choice choiceCreated = create(dao); + poll.addChoice(choiceCreated); + if (choiceType == ChoiceType.IMAGE) { + + // copy image where it belong and generate the thumb + + PollImageChoice imageChoice = (PollImageChoice) choice; + imageChoice.toChoice(choiceCreated); + try { + saveImages(poll, imageChoice); + } catch (IOException e) { + throw new PollenTechnicalException( + "Could not create image choice", e); + } + + } else if (choiceType == ChoiceType.DATE) { + + // date choice + + PollDateChoice dateChoice = (PollDateChoice) choice; + dateChoice.toChoice(choiceCreated); + + } else { + + // text choice + choiceCreated.setDescription(choice.getDescription()); + choiceCreated.setValidate(choice.isValidate()); + choiceCreated.setName(choice.getName()); + } + } + public void deleteChoice(String pollId, String choiceId) throws PollNotFoundException, PollChoiceNotFoundException { 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-28 16:40:09 UTC (rev 3218) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-03-29 15:13:30 UTC (rev 3219) @@ -262,9 +262,6 @@ public void prepareVotePage() throws Exception { - // Ensure uri for poll and pollAccount loading - preparePollUri(parameters); - loadPoll(); setDateFormat(DateFormat.getDateTimeInstance( @@ -374,6 +371,9 @@ } protected void loadPoll() throws PollNotFoundException { + + // Ensure uri for poll and pollAccount loading + preparePollUri(parameters); String pollId = getPollId(); if (StringUtils.isNotEmpty(pollId)) { Added: 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 (rev 0) +++ 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:30 UTC (rev 3219) @@ -0,0 +1,112 @@ +/* + * #%L + * Pollen :: UI (strust2) + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.ui.actions.poll; + +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.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. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class AddChoice extends AbstractVoteAction implements Preparable { + + private static final long serialVersionUID = 1L; + + protected Choice choice; + + public Choice getChoice() { + return choice; + } + + @Override + public void prepare() throws PollNotFoundException { + + loadPoll(); + + ChoiceType choiceType = getPoll().getChoiceType(); + choice = getPollService().getNewChoice(choiceType); + } + + @Override + public void validate() { + + ChoiceType choiceType = getPoll().getChoiceType(); + switch (choiceType) { + + case DATE: + validateChoiceDate(); + break; + + case IMAGE: + validateChoiceImage(); + break; + + case TEXT: + default: + validateChoice(); + } + } + + @InputConfig(methodName = "prepareVotePage") + @Override + public String execute() throws Exception { + + getPollService().createChoice(getPollId(), choice); + + 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/AddComment.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddComment.java 2012-03-28 16:40:09 UTC (rev 3218) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddComment.java 2012-03-29 15:13:30 UTC (rev 3219) @@ -71,7 +71,6 @@ // create the comment service.createComment(getPoll(), comment); - resetCommentName(); return SUCCESS; } 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-03-28 16:40:09 UTC (rev 3218) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/config/struts-poll.xml 2012-03-29 15:13:30 UTC (rev 3219) @@ -97,21 +97,6 @@ <result>/WEB-INF/jsp/poll/resume.jsp</result> </action> - <!-- vote poll --> - <action name="vote/*" method="execute" - class="org.chorem.pollen.ui.actions.poll.VoteForPoll"> - <param name="uriId">{1}</param> - <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> - <result type="redirectToVote"/> - </action> - - <!-- vote poll (input) --> - <action name="votefor/*" method="input" - class="org.chorem.pollen.ui.actions.poll.VoteForPoll"> - <param name="uriId">{1}</param> - <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> - </action> - <!-- confirm clone poll --> <action name="confirmClonePoll/*" class="org.chorem.pollen.ui.actions.poll.ConfirmPollAction"> @@ -182,9 +167,33 @@ <result>/WEB-INF/jsp/poll/confirmDeletePollComment.jsp</result> </action> + <!-- vote poll --> + <action name="vote/*" method="execute" + class="org.chorem.pollen.ui.actions.poll.VoteForPoll"> + <param name="uriId">{1}</param> + <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> + <result type="redirectToVote"/> + </action> + + <!-- vote poll (input) --> + <action name="votefor/*" method="input" + class="org.chorem.pollen.ui.actions.poll.VoteForPoll"> + <param name="uriId">{1}</param> + <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> + </action> + + <!-- add a choice --> + <action name="addChoice/*" + class="org.chorem.pollen.ui.actions.poll.AddChoice"> + <param name="uriId">{1}</param> + <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> + <result type="redirectToVote"/> + </action> + <!-- add a poll comment --> - <action name="addComment" + <action name="addComment/*" class="org.chorem.pollen.ui.actions.poll.AddComment"> + <param name="uriId">{1}</param> <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> <result type="redirectToVote"/> </action> Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-03-28 16:40:09 UTC (rev 3218) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-03-29 15:13:30 UTC (rev 3219) @@ -50,6 +50,9 @@ pollen.common.chartType-pie3d=Pie 3D pollen.common.chartType-ring=Ring pollen.common.choice=Choice +pollen.common.choice.date=Date +pollen.common.choice.image=Image +pollen.common.choice.text=Label pollen.common.commentName=Name pollen.common.commentText=Comment pollen.common.comments=Comments about this poll @@ -116,6 +119,7 @@ pollen.common.votingList=Group pollen.common.weight=Weight pollen.error.accountNotFound= +pollen.error.choice.empty=%s mandatory pollen.error.comment.name.empty=Comment name mandatory pollen.error.comment.text.empty=Comment text mandatory pollen.error.email.required=You must provide an email Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-03-28 16:40:09 UTC (rev 3218) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-03-29 15:13:30 UTC (rev 3219) @@ -55,6 +55,9 @@ pollen.common.chartType-pie3d=Camembert 3D pollen.common.chartType-ring=Anneau pollen.common.choice=Choix +pollen.common.choice.date=Date +pollen.common.choice.image=Image +pollen.common.choice.text=Libellé pollen.common.commentName=Nom pollen.common.commentText=Commentaire pollen.common.comments=Commentaire à propos du sondage @@ -123,6 +126,7 @@ pollen.common.weight=Poids pollen.common.x=Dépouillement pollen.error.accountNotFound= +pollen.error.choice.empty=%s obligatoire pollen.error.comment.name.empty=Nom du commentaire obligatoire pollen.error.comment.text.empty=Texte du commentaire obligatoire pollen.error.email.required=Courriel obligatoire Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp 2012-03-28 16:40:09 UTC (rev 3218) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp 2012-03-29 15:13:30 UTC (rev 3219) @@ -423,14 +423,51 @@ <%--<t:label for="anonymousVote"/>--%> <br/> </s:if> - <s:submit action="vote/%{uriId}" key="pollen.action.pollVote"/> + <s:submit action="vote/%{uriId}" key="pollen.action.pollVote" align="center"/> <%--<input id="submitVote" t:type="Submit" t:value="${message:submitVote}"/>--%> </div> </s:if> </s:form> </div> + +<!-- Ajout de choix --> +<s:if test="pollChoiceRunning"> + <div id="choiceFormDiv"> + <s:form id="choiceForm" method="POST" namespace="/poll" validate="true"> + <h4><s:text name="pollen.action.addChoice"/></h4> + <s:if test="textType"> + <s:textfield key="choice.name" + label="%{getText('pollen.common.choice')}" + required="true"/> + </s:if> + <s:if test="dateType"> + <s:label id="choice.date-label" for="choice.date" key="pollen.common.choice" theme="simple"/> + <br/> + <s:fielderror fieldName="choice.date"/> + <br/> + <sj:datepicker id="choice.date" name="choice.date" changeMonth="true" + changeYear="true" labelSeparator="" theme="simple" label="" + timepicker="true" displayFormat="dd/mm/yy" required="true"/> + <br/> + </s:if> + <s:if test="imageType"> + <s:label id="choice.location-label" for="choice.location" key="pollen.common.choice" theme="simple"/> + <br/> + <s:fielderror fieldName="choice.location"/> + <br/> + <s:file id="choice.location" name="" label="choice.location" theme="simple" cssClass="nameField" required="true"/> + <br/> + </s:if> + <br/> + <s:textarea cols="30" key="choice.description" + label="%{getText('pollen.common.description')}"/> + <br/> + <s:submit action="addChoice/%{uriId}" key="pollen.action.addChoice" align="center"/> + </s:form> + </div> +</s:if> + <!-- Ajout de commentaires --> - <h3><s:text name="pollen.common.comments"/></h3> <div id="commentZone"> @@ -497,16 +534,15 @@ <div id="commentFormDiv"> - <s:form id='addCommentForm' method="POST" namespace="/poll"> - <s:hidden key="uriId" label=''/> + <s:form id='addCommentForm' method="POST" namespace="/poll" validate="true"> <s:textfield key="commentName" required="true" size="78" label="%{getText('pollen.common.commentName')}"/> <s:textarea key="commentText" required="true" value='' label="%{getText('pollen.common.commentText')}"/> <div class="cleanBoth"> - <s:submit action="addComment" key="pollen.action.addComment" - align="left"/> + <s:submit action="addComment/%{uriId}" key="pollen.action.addComment" + align="center"/> </div> </s:form>