r3222 - in branches/pollen-1.2.6-struts2: pollen-services/src/main/java/org/chorem/pollen/bean pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/interceptors pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll
Author: fdesbois Date: 2012-04-02 11:29:31 +0200 (Mon, 02 Apr 2012) New Revision: 3222 Url: http://chorem.org/repositories/revision/pollen/3222 Log: - resolve issue with ChoiceType in createPoll prepare - resolve issue with image upload (no params before prepare). Use new FileUploadAware interface. Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/FileUploadAware.java Modified: 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/PollDateChoice.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java 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/interceptors/PollenFileUploadInterceptor.java 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/bean/ChoiceHelper.java =================================================================== --- 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) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/ChoiceHelper.java 2012-04-02 09:29:31 UTC (rev 3222) @@ -8,6 +8,9 @@ import java.util.Date; /** + * Used to manage all objects from {@link Choice} inheritance tree depends + * on {@link ChoiceType}. + * <p/> * Created: 30/03/12 * * @author fdesbois <desbois@codelutin.com> @@ -55,5 +58,21 @@ ChoiceType choiceType) { return Iterables.transform(choices, toValue(choiceType)); } + + public static String getValuePropertyName(ChoiceType choiceType) { + String result; + switch (choiceType) { + case DATE: + result = PollDateChoice.PROPERTY_DATE; + break; + case IMAGE: + result = PollImageChoice.PROPERTY_LOCATION; + break; + case TEXT: + default: + result = Choice.PROPERTY_NAME; + } + return result; + } } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollDateChoice.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollDateChoice.java 2012-03-30 10:32:15 UTC (rev 3221) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollDateChoice.java 2012-04-02 09:29:31 UTC (rev 3222) @@ -39,6 +39,8 @@ private static final long serialVersionUID = 1L; + public static final String PROPERTY_DATE = "date"; + protected Date date; public Date getDate() { Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java 2012-03-30 10:32:15 UTC (rev 3221) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java 2012-04-02 09:29:31 UTC (rev 3222) @@ -36,6 +36,8 @@ public class PollImageChoice extends ChoiceImpl { private static final long serialVersionUID = 1L; + + public static final String PROPERTY_LOCATION = "location"; protected String location; Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/FileUploadAware.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/FileUploadAware.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/FileUploadAware.java 2012-04-02 09:29:31 UTC (rev 3222) @@ -0,0 +1,18 @@ +package org.chorem.pollen.ui.actions; + +import java.io.File; +import java.util.List; + +/** + * Created: 30/03/12 + * + * @author fdesbois <desbois@codelutin.com> + */ +public interface FileUploadAware { + + void setFiles(String paramName, List<File> files); + + void setFileContentTypes(String paramName, List<String> contentTypes); + + void setFileNames(String paramName, List<String> fileNames); +} 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-30 10:32:15 UTC (rev 3221) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddChoice.java 2012-04-02 09:29:31 UTC (rev 3222) @@ -24,37 +24,94 @@ package org.chorem.pollen.ui.actions.poll; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import com.opensymphony.xwork2.Preparable; import com.opensymphony.xwork2.interceptor.annotations.InputConfig; import org.apache.commons.lang3.StringUtils; import org.chorem.pollen.bean.ChoiceHelper; +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 org.chorem.pollen.ui.actions.FileUploadAware; +import java.io.File; +import java.util.List; + /** * To add a poll comment. * * @author tchemit <chemit@codelutin.com> * @since 1.2.6 */ -public class AddChoice extends AbstractVoteAction implements Preparable { +public class AddChoice extends AbstractVoteAction implements Preparable, FileUploadAware { private static final long serialVersionUID = 1L; protected Choice choice; + private List<File> imageChoices; + + private List<String> imageChoiceContentTypes; + + private List<String> imageChoiceFileNames; + public Choice getChoice() { return choice; } @Override + public void setFiles(String paramName, List<File> files) { + this.imageChoices = Lists.newArrayList(files); + } + + @Override + public void setFileContentTypes(String paramName, List<String> contentTypes) { + this.imageChoiceContentTypes = Lists.newArrayList(contentTypes); + } + + @Override + public void setFileNames(String paramName, List<String> fileNames) { + this.imageChoiceFileNames = Lists.newArrayList(fileNames); + } + + public List<File> getImageChoices() { + if (imageChoices == null) { + imageChoices = Lists.newArrayList(); + } + return imageChoices; + } + + public List<String> getImageChoiceContentTypes() { + if (imageChoiceContentTypes == null) { + imageChoiceContentTypes = Lists.newArrayList(); + } + return imageChoiceContentTypes; + } + + public List<String> getImageChoiceFileNames() { + if (imageChoiceFileNames == null) { + imageChoiceFileNames = Lists.newArrayList(); + } + return imageChoiceFileNames; + } + + @Override public void prepare() throws PollNotFoundException { loadPoll(); ChoiceType choiceType = getPoll().getChoiceType(); choice = getPollService().getNewChoice(choiceType); + + // Push image choice data from uploaded file + if (ChoiceType.IMAGE == choiceType && getImageChoices().size() == 1) { + + String fileName = getImageChoiceFileNames().get(0); + String location = getImageChoices().get(0).getAbsolutePath(); + choice.setName(fileName); + ((PollImageChoice)choice).setLocation(location); + } } @Override @@ -62,14 +119,14 @@ ChoiceType choiceType = getPoll().getChoiceType(); - String typeKey = choiceType.name().toLowerCase(); + String propName = ChoiceHelper.getValuePropertyName(choiceType); Object value = ChoiceHelper.toValue(choice, choiceType); // -- Validate value notEmpty if (value == null || (value instanceof String && StringUtils.isBlank((String) value))) { String typeLabel = getText(choiceType.getI18nKey()); - addFieldError("choice." + typeKey, + addFieldError("choice." + propName, _("pollen.error.choice.empty", typeLabel)); } else { @@ -78,7 +135,7 @@ // -- Validate value notExists if (Iterables.contains(pollChoiceValues, value)) { - addFieldError("choice." + typeKey, + addFieldError("choice." + propName, _("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/CreatePoll.java =================================================================== --- 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) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/CreatePoll.java 2012-04-02 09:29:31 UTC (rev 3222) @@ -54,6 +54,7 @@ import org.chorem.pollen.common.VoteCountingType; import org.chorem.pollen.services.impl.PollService; import org.chorem.pollen.services.impl.PreventRuleService; +import org.chorem.pollen.ui.actions.FileUploadAware; import org.chorem.pollen.ui.actions.PollenActionSupport; import org.nuiton.util.StringUtil; @@ -73,7 +74,7 @@ * @author tchemit <chemit@codelutin.com> * @since 1.2.6 */ -public class CreatePoll extends PollenActionSupport implements Preparable, ParameterAware { +public class CreatePoll extends PollenActionSupport implements Preparable, ParameterAware, FileUploadAware { private static final long serialVersionUID = 1L; @@ -235,6 +236,21 @@ this.reminderHourCountdown = reminderHourCountdown; } + @Override + public void setFiles(String paramName, List<File> files) { + this.imageChoice = Lists.newArrayList(files); + } + + @Override + public void setFileContentTypes(String paramName, List<String> contentTypes) { + this.imageChoiceContentType = Lists.newArrayList(contentTypes); + } + + @Override + public void setFileNames(String paramName, List<String> fileNames) { + this.imageChoiceFileName = Lists.newArrayList(fileNames); + } + public List<File> getImageChoice() { if (imageChoice == null) { imageChoice = Lists.newArrayList(); @@ -242,10 +258,6 @@ return imageChoice; } - public void setImageChoice(int index, File imageChoice) { - addtoList(getImageChoice(), index, imageChoice); - } - public List<String> getImageChoiceContentType() { if (imageChoiceContentType == null) { imageChoiceContentType = Lists.newArrayList(); @@ -260,14 +272,6 @@ return imageChoiceFileName; } - public void setImageChoiceContentType(int index, String imageChoiceContentType) { - addtoList(getImageChoiceContentType(), index, imageChoiceContentType); - } - - public void setImageChoiceFileName(int index, String imageChoiceFileName) { - addtoList(getImageChoiceFileName(), index, imageChoiceFileName); - } - public boolean isInformationsError() { return informationsError; } @@ -333,10 +337,15 @@ imageChoiceTokenId = DisplayImageChoice.CHOICE_TOKEN + "_" + 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"); + ChoiceType pollChoiceType; + // Retrieve choiceType from parameters, the poll object will be updated after prepare + String choiceTypeParam = getNonEmptyParameterValue("poll.choiceType"); + if (choiceTypeParam == null) { + + // Default value will be initiliazed on poll instanciation + pollChoiceType = getPoll().getChoiceType(); + + } else { pollChoiceType = ChoiceType.valueOf(choiceTypeParam); } @@ -811,7 +820,7 @@ Matcher matcher = IMAGE_CHOICE_NAME_PATTERN.matcher(paramName); if (matcher.matches()) { - // found a text choice name + // found an image choice name String paramValue = getNonEmptyParameterValue(paramName); if (paramValue != null) { @@ -1114,7 +1123,7 @@ private String getNonEmptyParameterValue(String paramName) { String[] paramValues = parameters.get(paramName); String result = null; - if (paramValues.length == 1) { + if (paramValues != null && paramValues.length == 1) { String paramValue = paramValues[0]; if (StringUtils.isNotEmpty(paramValue)) { result = paramValue; Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/interceptors/PollenFileUploadInterceptor.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/interceptors/PollenFileUploadInterceptor.java 2012-03-30 10:32:15 UTC (rev 3221) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/interceptors/PollenFileUploadInterceptor.java 2012-04-02 09:29:31 UTC (rev 3222) @@ -31,6 +31,7 @@ import org.apache.struts2.ServletActionContext; import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper; import org.apache.struts2.interceptor.FileUploadInterceptor; +import org.chorem.pollen.ui.actions.FileUploadAware; import javax.servlet.http.HttpServletRequest; import java.io.File; @@ -142,11 +143,21 @@ } if (!acceptedFiles.isEmpty()) { - Map<String, Object> params = ac.getParameters(); - params.put(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()])); - params.put(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()])); - params.put(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()])); + if (action instanceof FileUploadAware) { + FileUploadAware fileUpload = (FileUploadAware) action; + + fileUpload.setFiles(inputName, acceptedFiles); + fileUpload.setFileContentTypes(contentTypeName, acceptedContentTypes); + fileUpload.setFileNames(fileNameName, acceptedFileNames); + + } else { + Map<String, Object> params = ac.getParameters(); + + params.put(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()])); + params.put(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()])); + params.put(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()])); + } } } } else { 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-30 10:32:15 UTC (rev 3221) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp 2012-04-02 09:29:31 UTC (rev 3222) @@ -433,31 +433,26 @@ <!-- Ajout de choix --> <s:if test="pollChoiceRunning"> <div id="choiceFormDiv"> - <s:form id="choiceForm" method="POST" namespace="/poll"> + <s:form id="choiceForm" method="POST" namespace="/poll" enctype="multipart/form-data"> <h4><s:text name="pollen.action.addChoice"/></h4> <s:if test="textType"> <s:textfield key="choice.name" - label="%{getText('pollen.common.choice')}" - required="true"/> + 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> + <s:elseif test="dateType"> + <sj:datepicker key="choice.date" + label="%{getText('pollen.common.choice')}" + changeMonth="true" changeYear="true" + timepicker="true" displayFormat="dd/mm/yy" + required="true"/> + </s:elseif> + <s:elseif test="imageType"> + <s:file key="choice.location" + label="%{getText('pollen.common.choice')}" + cssClass="nameField" + required="true"/> + </s:elseif> <br/> <s:textarea cols="30" key="choice.description" label="%{getText('pollen.common.description')}"/>
participants (1)
-
fdesbois@users.chorem.org