r3236 - in branches/pollen-1.2.6-struts2: pollen-services/src/main/java/org/chorem/pollen/services pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll pollen-ui-struts2/src/main/resources pollen-ui-struts2/src/main/resources/config pollen-ui-struts2/src/main/webapp/WEB-INF/jsp pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll pollen-ui-struts2/src/main/webapp/js
Author: tchemit Date: 2012-04-06 01:47:56 +0200 (Fri, 06 Apr 2012) New Revision: 3236 Url: http://chorem.org/repositories/revision/pollen/3236 Log: rethink the poll form (use less ajax request), need to be continued (validation not finished) Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/js/createPoll.js Removed: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/choiceHelper.jsp branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/votingListHelper.jsp Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DisplayPersonToList.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DisplayVotingList.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/LoadPoll.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.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/struts.xml branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/PersonToList.jsp branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/VotingList.jsp branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/create.jsp Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java 2012-04-05 23:47:56 UTC (rev 3236) @@ -0,0 +1,263 @@ +/* + * #%L + * Pollen :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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.services; + +import com.google.common.base.Function; +import com.google.common.collect.Lists; +import org.chorem.pollen.bean.PollDateChoice; +import org.chorem.pollen.bean.PollImageChoice; +import org.chorem.pollen.business.persistence.Choice; +import org.chorem.pollen.business.persistence.ChoiceImpl; +import org.chorem.pollen.business.persistence.PersonToList; +import org.chorem.pollen.business.persistence.PersonToListImpl; +import org.chorem.pollen.business.persistence.PollAccount; +import org.chorem.pollen.business.persistence.PollAccountImpl; +import org.chorem.pollen.business.persistence.VotingList; +import org.chorem.pollen.business.persistence.VotingListImpl; + +import java.util.Date; +import java.util.List; + +/** + * Usefull functions for the service (and higher) layers. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class PollenServiceFunctions { + + protected PollenServiceFunctions() { + // no instance for helper class + } + + public static void fillChoiceList(List<Choice> choiceList, int max, + Function<Choice, Choice> function) { + while (choiceList.size() < max) { + Choice newChoice = function.apply(null); + choiceList.add(newChoice); + } + } + + public static void fillVotingList(VotingList votingList, + Function<PersonToList, PersonToList> persontoListCreator, + int max) { + while (votingList.sizePollAccountPersonToList() < max) { + PersonToList newPersontoList = persontoListCreator.apply(null); + votingList.addPollAccountPersonToList(newPersontoList); + } + } + + public static TextChoiceCreator newTextChoiceCreator(boolean clone) { + return new TextChoiceCreator(clone); + } + + public static ImageChoiceCreator newImageChoiceCreator(boolean clone) { + return new ImageChoiceCreator(clone); + } + + public static DateChoiceCreator newDateChoiceCreator(boolean clone) { + return new DateChoiceCreator(clone); + } + + public static Function<VotingList, VotingList> newVotingListCreator( + boolean clone, + PollenServiceContext serviceContext, + Function<PersonToList, PersonToList> persontoListCreator) { + return new VotingListCreator(clone, serviceContext, persontoListCreator); + } + + public static Function<PersonToList, PersonToList> newPersonToListCreator( + boolean clone, + PollenServiceContext serviceContext) { + return new PersonToListCreator(clone, serviceContext); + } + + public static class TextChoiceCreator implements Function<Choice, Choice> { + + + protected final boolean clone; + + protected TextChoiceCreator(boolean clone) { + this.clone = clone; + } + + @Override + public Choice apply(Choice input) { + Choice result = new ChoiceImpl(); + if (input != null) { + if (clone) { + // do not copy topia id + } else { + result.setTopiaId(input.getTopiaId()); + } + result.setName(input.getName()); + result.setDescription(input.getDescription()); + } + return result; + } + } + + public static class ImageChoiceCreator implements Function<Choice, Choice> { + + protected final boolean clone; + + protected ImageChoiceCreator(boolean clone) { + this.clone = clone; + } + + + @Override + public Choice apply(Choice input) { + PollImageChoice result = new PollImageChoice(); + if (input != null) { + if (clone) { + // do not copy topia id + } else { + result.setTopiaId(input.getTopiaId()); + } + result.setName(input.getName()); + result.setDescription(input.getDescription()); + result.setLocation(input.getName()); + } + return result; + } + } + + public static class DateChoiceCreator implements Function<Choice, Choice> { + + protected final boolean clone; + + protected DateChoiceCreator(boolean clone) { + this.clone = clone; + } + + @Override + public Choice apply(Choice input) { + PollDateChoice result = new PollDateChoice(); + if (input != null) { + if (clone) { + // do not copy topia id + } else { + result.setTopiaId(input.getTopiaId()); + } + + result.setName(input.getName()); + result.setDescription(input.getDescription()); + result.setDate(new Date(Long.valueOf(input.getName()))); + } + return result; + } + } + + public static class VotingListCreator implements Function<VotingList, VotingList> { + + protected final boolean clone; + + protected final PollenServiceContext serviceContext; + + protected final Function<PersonToList, PersonToList> persontoListCreator; + + protected VotingListCreator(boolean clone, + PollenServiceContext serviceContext, + Function<PersonToList, PersonToList> persontoListCreator) { + this.clone = clone; + this.serviceContext = serviceContext; + this.persontoListCreator = persontoListCreator; + } + + @Override + public VotingList apply(VotingList input) { + VotingList result = new VotingListImpl(); + result.setPollAccountPersonToList( + Lists.<PersonToList>newArrayList()); + if (input == null) { + + // new votingList + result.setWeight(1); + } else { + + if (clone) { + // do not copy topia id + } else { + result.setTopiaId(input.getTopiaId()); + } + result.setName(input.getName()); + result.setWeight(input.getWeight()); + + if (!input.isPollAccountPersonToListEmpty()) { + + // copy person to lists + result.addAllPollAccountPersonToList(Lists.transform( + input.getPollAccountPersonToList(), + persontoListCreator)); + } + } + return result; + } + } + + public static class PersonToListCreator implements Function<PersonToList, PersonToList> { + + protected final boolean clone; + + protected final PollenServiceContext serviceContext; + + protected PersonToListCreator(boolean clone, PollenServiceContext serviceContext) { + this.clone = clone; + this.serviceContext = serviceContext; + } + + @Override + public PersonToList apply(PersonToList input) { + PersonToList result = new PersonToListImpl(); + if (input != null) { + if (clone) { + // do not copy topia id + } else { + result.setTopiaId(input.getTopiaId()); + } + result.setWeight(input.getWeight()); + PollAccount pollAccount = input.getPollAccount(); + if (pollAccount != null) { + PollAccount pollAccount2 = new PollAccountImpl(); + String accountId; + if (clone) { + // do not copy topia id + // generate a new accountId + accountId = serviceContext.createPollenUrlId(); + } else { + pollAccount2.setTopiaId(pollAccount.getTopiaId()); + accountId = pollAccount.getAccountId(); + } + pollAccount2.setAccountId(accountId); + pollAccount2.setVotingId(pollAccount.getVotingId()); + result.setPollAccount(pollAccount2); + } + } else { + result.setWeight(1); + } + return result; + } + } +} Property changes on: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceFunctions.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java 2012-04-05 23:47:56 UTC (rev 3236) @@ -1,41 +1,40 @@ +/* + * #%L + * Pollen :: UI (strust2) + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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.google.common.base.Predicate; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.common.collect.Sets; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.struts2.interceptor.ParameterAware; -import org.chorem.pollen.bean.PollDateChoice; -import org.chorem.pollen.bean.PollImageChoice; -import org.chorem.pollen.business.persistence.Choice; -import org.chorem.pollen.business.persistence.ChoiceImpl; -import org.chorem.pollen.business.persistence.PersonToList; -import org.chorem.pollen.business.persistence.PersonToListImpl; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.business.persistence.PollAccountImpl; -import org.chorem.pollen.business.persistence.VotingList; -import org.chorem.pollen.business.persistence.VotingListImpl; import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.common.I18nAble; import org.chorem.pollen.common.PollType; import org.chorem.pollen.common.VoteCountingType; import org.chorem.pollen.services.impl.PollService; -import org.chorem.pollen.ui.actions.FileUploadAware; import org.chorem.pollen.ui.actions.PollenActionSupport; -import java.io.File; -import java.text.ParseException; -import java.util.Collections; -import java.util.Date; -import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Created: 04/04/12 @@ -43,27 +42,12 @@ * @author fdesbois <desbois@codelutin.com> * $Id$ */ -public abstract class AbstractPollForm extends PollenActionSupport implements ParameterAware, FileUploadAware { +public abstract class AbstractPollForm extends PollenActionSupport { private static final long serialVersionUID = 1L; - /** Logger. */ - private static final Log log = LogFactory.getLog(AbstractPollForm.class); + protected Poll poll; - private static final Pattern TEXT_CHOICE_NAME_PATTERN = - Pattern.compile("textChoice_(\\d+)\\.name"); - - private static final Pattern DATE_CHOICE_NAME_PATTERN = - Pattern.compile("dateChoice_(\\d+)\\.name"); - - private static final Pattern IMAGE_CHOICE_NAME_PATTERN = - Pattern.compile("imageChoice_(\\d+)\\.name"); - - private static final Pattern VOTING_LIST_NAME_PATTERN = - Pattern.compile("votingList_(\\d+)\\.name"); - - private Poll poll; - private Map<String, String> pollTypes; private Map<String, String> voteCountingTypes; @@ -78,47 +62,27 @@ private int reminderHourCountdown = 2; - private String textChoiceTokenId; +// private String textChoiceTokenId; +// +// private String dateChoiceTokenId; +// +// private String imageChoiceTokenId; +// +// private String votingListTokenId; - private String dateChoiceTokenId; +// private int nbTextChoices; +// +// private int nbDateChoices; +// +// private int nbImageChoices; +// +// private int nbVotingLists; - private String imageChoiceTokenId; +// private String actionLabel; - private String votingListTokenId; - - private int nbTextChoices; - - private int nbDateChoices; - - private int nbImageChoices; - - private int nbVotingLists; - - /** Uploaded images (for choice type poll). */ - private List<File> imageChoice; - - /** Uploaded images content type (for choice type poll). */ - private List<String> imageChoiceContentType; - - /** Uploaded images name (from use desktop) (for choice type poll). */ - private List<String> imageChoiceFileName; - - private String actionLabel; - private transient PollService pollService; - /** - * All the parameters send by request used to build back choices of the - * poll. - */ - protected Map<String, String[]> parameters; - protected boolean informationsError; - - protected boolean optionsError; - - protected abstract Poll initPoll(); - @Override public String getPageLogo() { return "Creation"; @@ -132,44 +96,49 @@ } public Poll getPoll() { - if (poll == null) { - poll = initPoll(); - } return poll; } - public int getNbTextChoices() { - return nbTextChoices; - } +// public int getNbTextChoices() { +// return nbTextChoices; +// } +// +// public String getTextChoiceTokenId() { +// return textChoiceTokenId; +// } +// +// public int getNbDateChoices() { +// return nbDateChoices; +// } +// +// public String getDateChoiceTokenId() { +// return dateChoiceTokenId; +// } +// +// public int getNbImageChoices() { +// return nbImageChoices; +// } +// +// public String getImageChoiceTokenId() { +// return imageChoiceTokenId; +// } +// +// public int getNbVotingLists() { +// return nbVotingLists; +// } +// +// public String getVotingListTokenId() { +// return votingListTokenId; +// } - public String getTextChoiceTokenId() { - return textChoiceTokenId; + public boolean isInformationsError() { + return false; } - public int getNbDateChoices() { - return nbDateChoices; + public boolean isOptionsError() { + return false; } - public String getDateChoiceTokenId() { - return dateChoiceTokenId; - } - - public int getNbImageChoices() { - return nbImageChoices; - } - - public String getImageChoiceTokenId() { - return imageChoiceTokenId; - } - - public int getNbVotingLists() { - return nbVotingLists; - } - - public String getVotingListTokenId() { - return votingListTokenId; - } - public Map<String, String> getPollTypes() { return pollTypes; } @@ -214,42 +183,6 @@ 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(); - } - return imageChoice; - } - - public List<String> getImageChoiceContentType() { - if (imageChoiceContentType == null) { - imageChoiceContentType = Lists.newArrayList(); - } - return imageChoiceContentType; - } - - public List<String> getImageChoiceFileName() { - if (imageChoiceFileName == null) { - imageChoiceFileName = Lists.newArrayList(); - } - return imageChoiceFileName; - } - public boolean isFreePoll() { PollType pollType = poll.getPollType(); return pollType == PollType.FREE; @@ -268,41 +201,25 @@ return getPollService().getPollEditUrl(poll); } - public boolean isInformationsError() { - return informationsError; - } - - public boolean isOptionsError() { - return optionsError; - } - public int getSelectedTab() { - int result; - if (isInformationsError()) { - result = 0; - } else { - if (isOptionsError()) { - result = 1; - } else { - result = 0; - } - } + int result = 0; return result; } public String getActionLabel() { - return actionLabel; + return isEdit() ? _("pollen.action.editPoll") : + _("pollen.action.createPoll"); } public boolean isVoteStarted() { - return isEdit() && getPoll().sizeVote() > 0; + return isEdit() && poll.sizeVote() > 0; } - + public boolean isCreatorUserAccountDefined() { - PollAccount creator = getPoll().getCreator(); + PollAccount creator = poll.getCreator(); return creator.getUserAccount() != null; } - + protected int getNbChoices() { //TODO tchemit-2012-03-05 use a default value from configuration return 5; @@ -321,473 +238,70 @@ // remove all stuff from session getPollenSession().clearDynamicData(); - actionLabel = isEdit() ? _("pollen.action.editPoll") : _("pollen.action.createPoll"); +// actionLabel = isEdit() ? _("pollen.action.editPoll") : +// _("pollen.action.createPoll"); pollTypes = decorateToName(PollType.values()); choiceTypes = decorateToName(ChoiceType.values()); voteCountingTypes = decorateToName(VoteCountingType.values()); - nbTextChoices = nbDateChoices = nbImageChoices = getNbChoices(); - nbVotingLists = getDefaultNbVotingLists(); - - String tokenSuffix = getServiceContext().createPollenUrlId(); - - textChoiceTokenId = DisplayTextChoice.CHOICE_TOKEN + "_" + - tokenSuffix; - - dateChoiceTokenId = DisplayDateChoice.CHOICE_TOKEN + "_" + - tokenSuffix; - - imageChoiceTokenId = DisplayImageChoice.CHOICE_TOKEN + "_" + - tokenSuffix; - - 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); - } - - if (log.isInfoEnabled()) { - log.info("choice type " + pollChoiceType); - } - - Map<Integer, Choice> choices = null; - String tokenId = null; - - switch (pollChoiceType) { - case TEXT: - choices = buildTextChoices(nbTextChoices); - tokenId = textChoiceTokenId; - break; - case DATE: - choices = buildDateChoices(nbDateChoices); - tokenId = dateChoiceTokenId; - break; - case IMAGE: - choices = buildImageChoices(nbImageChoices); - tokenId = imageChoiceTokenId; - break; - } - getPollenSession().putDynamicData(tokenId, choices); - - // load votingLists - - votingListTokenId = DisplayVotingList.VOTING_LIST_TOKEN + "_" + - tokenSuffix; - Map<Integer, VotingList> votingLists = buildVotingLists(nbVotingLists); - getPollenSession().putDynamicData(votingListTokenId, votingLists); +// nbTextChoices = nbDateChoices = nbImageChoices = getNbChoices(); +// nbVotingLists = getDefaultNbVotingLists(); +// +// String tokenSuffix = getServiceContext().createPollenUrlId(); +// +// textChoiceTokenId = DisplayTextChoice.CHOICE_TOKEN + "_" + +// tokenSuffix; +// +// dateChoiceTokenId = DisplayDateChoice.CHOICE_TOKEN + "_" + +// tokenSuffix; +// +// imageChoiceTokenId = DisplayImageChoice.CHOICE_TOKEN + "_" + +// tokenSuffix; +// +// 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); +// } +// +// if (log.isInfoEnabled()) { +// log.info("choice type " + pollChoiceType); +// } +// +// Map<Integer, Choice> choices = null; +// String tokenId = null; +// +// switch (pollChoiceType) { +// case TEXT: +// choices = buildTextChoices(nbTextChoices); +// tokenId = textChoiceTokenId; +// break; +// case DATE: +// choices = buildDateChoices(nbDateChoices); +// tokenId = dateChoiceTokenId; +// break; +// case IMAGE: +// choices = buildImageChoices(nbImageChoices); +// tokenId = imageChoiceTokenId; +// break; +// } +//// getPollenSession().putDynamicData(tokenId, choices); +// +// // load votingLists +// +// votingListTokenId = DisplayVotingList.VOTING_LIST_TOKEN + "_" + +// tokenSuffix; +// Map<Integer, VotingList> votingLists = buildVotingLists(nbVotingLists); +// getPollenSession().putDynamicData(votingListTokenId, votingLists); } - @Override - public void setParameters(Map<String, String[]> parameters) { - this.parameters = parameters; - } - - protected Map<Integer, Choice> buildTextChoices(int nbDefault) { - Map<Integer, Choice> result = Maps.newTreeMap(); - - int maxNumber = 0; - - for (String paramName : parameters.keySet()) { - - Matcher matcher = TEXT_CHOICE_NAME_PATTERN.matcher(paramName); - if (matcher.matches()) { - - // found a text choice name - - String paramValue = getNonEmptyParameterValue(paramName); - if (paramValue != null) { - - // can keep this none empty text choice name - - Integer choiceNumber = Integer.valueOf(matcher.group(1)); - if (choiceNumber > maxNumber) { - maxNumber = choiceNumber; - } - Choice choice = new ChoiceImpl(); - createChoice(choice, "textChoice_" + choiceNumber, - paramValue); - result.put(choiceNumber, choice); - } - } - } - result = reindexMap(result, maxNumber); - - int size = result.size(); - nbTextChoices = Math.max(nbDefault, size); - log.info("nbTextChoices (from request) = " + size); - logChoice(result); - return result; - } - - protected Map<Integer, Choice> buildDateChoices(int nbDefault) { - Map<Integer, Choice> result = Maps.newTreeMap(); - - int maxNumber = 0; - for (String paramName : parameters.keySet()) { - - Matcher matcher = DATE_CHOICE_NAME_PATTERN.matcher(paramName); - if (matcher.matches()) { - - // found a text choice name - - String paramValue = getNonEmptyParameterValue(paramName); - if (paramValue != null) { - - // can keep this none empty text choice name - - Integer choiceNumber = Integer.valueOf(matcher.group(1)); - if (choiceNumber > maxNumber) { - maxNumber = choiceNumber; - } - PollDateChoice choice = new PollDateChoice(); - createDateChoice(choice, - "dateChoice_" + choiceNumber, - paramValue); - result.put(choiceNumber, choice); - } - } - } - result = reindexMap(result, maxNumber); - - int size = result.size(); - nbDateChoices = Math.max(nbDefault, size); - log.info("nbDateChoices (from request) = " + size); - logChoice(result); - return result; - } - - protected Map<Integer, Choice> buildImageChoices(int nbDefault) { - Map<Integer, Choice> result = Maps.newTreeMap(); - - // push back in parameters stuff from uploaded files - int index = 0; - - for (String fileName : getImageChoiceFileName()) { - if (fileName != null) { - parameters.put("imageChoice_" + index + ".name", - new String[]{fileName}); - parameters.put( - "imageChoice_" + index + ".location", - new String[]{getImageChoice().get(index).getAbsolutePath()}); - } - index++; - } - - int maxNumber = 0; - for (String paramName : parameters.keySet()) { - - Matcher matcher = IMAGE_CHOICE_NAME_PATTERN.matcher(paramName); - if (matcher.matches()) { - - // found an image choice name - - String paramValue = getNonEmptyParameterValue(paramName); - if (paramValue != null) { - - // can keep this none empty text choice name - - Integer choiceNumber = Integer.valueOf(matcher.group(1)); - if (choiceNumber > maxNumber) { - maxNumber = choiceNumber; - } - PollImageChoice choice = new PollImageChoice(); - createImageChoice(choice, - "imageChoice_" + choiceNumber, - paramValue); - - result.put(choiceNumber, choice); - } - } - } - result = reindexMap(result, maxNumber); - int size = result.size(); - nbImageChoices = Math.max(nbDefault, size); - log.info("nbImageChoices (from request) = " + size); - logChoice(result); - return result; - } - - protected Map<Integer, VotingList> buildVotingLists(int nbDefault) { - Map<Integer, VotingList> result = Maps.newTreeMap(); - - int maxNumber = 0; - - // get all votingList_ parameters - Set<String> votingListParameterNames = Sets.filter( - parameters.keySet(), - new StringStartWithPredicate("votingList_")); - - for (String paramName : votingListParameterNames) { - - Matcher matcher = VOTING_LIST_NAME_PATTERN.matcher(paramName); - - if (matcher.matches()) { - - // found a voting list name - - int votingListNumber = buildVotingList(paramName, - matcher, - result - ); - maxNumber = Math.max(maxNumber, votingListNumber); - } - } - - result = reindexMap(result, maxNumber); - - int size = result.size(); - nbVotingLists = Math.max(nbDefault, size); - log.info("nbVotingList (from request) = " + size); - - // add personToList maps to session (but just now, since votingList - // could have been reindex) - for (Map.Entry<Integer, VotingList> entry : result.entrySet()) { - Integer votingListNumber = entry.getKey(); - VotingList votingList = entry.getValue(); - - if (!votingList.isPollAccountPersonToListEmpty()) { - List<PersonToList> personToList = - votingList.getPollAccountPersonToList(); - - Map<Integer, PersonToList> personToListMap = Maps.newTreeMap(); - int index = 0; - for (PersonToList toList : personToList) { - personToListMap.put(index++, toList); - } - - String token = DisplayPersonToList.getPersonToListTokenId( - votingListTokenId, votingListNumber); - if (log.isInfoEnabled()) { - log.info("Add " + token + " with " + - personToListMap.size() + " personToList"); - } - getPollenSession().putDynamicData(token, personToListMap); - } - } - - return result; - } - - private double getDoubleValue(String parameterName) { - String parameterValue = getNonEmptyParameterValue(parameterName); - double result = 0; - if (StringUtils.isNotEmpty(parameterValue)) { - - try { - result = Double.valueOf(parameterValue); - } catch (NumberFormatException e) { - //bad conversion, will be treated later - if (log.isDebugEnabled()) { - log.debug("Bad double conversion from param [" + - parameterName + "] : " + parameterValue); - } - } - } - return result; - } - - private int buildVotingList(String votingListParameterName, - Matcher votingListMatcher, - Map<Integer, VotingList> result) { - - String paramValue = getNonEmptyParameterValue(votingListParameterName); - int votingListNumber = Integer.valueOf(votingListMatcher.group(1)); - - VotingList votingList = new VotingListImpl(); - - votingList.setName(paramValue); - - String prefix = "votingList_" + votingListNumber; - - double weight = getDoubleValue(prefix + ".weight"); - votingList.setWeight(weight); - - String topiaId = getNonEmptyParameterValue(prefix + ".topiaId"); - votingList.setTopiaId(topiaId); - - result.put(votingListNumber, votingList); - - String personToListPrefix = "personToList_" + votingListNumber + "_"; - - // get all personToList parameters - Set<String> votingListParameterNames = Sets.filter( - parameters.keySet(), new StringStartWithPredicate(personToListPrefix)); - - Pattern personToListNamePattern = Pattern.compile( - personToListPrefix + "(\\d+)\\.votingId"); - - Map<Integer, PersonToList> personToLists = Maps.newTreeMap(); - - int maxPersonToListNumber = 0; - - // let's build personToList list - for (String personToListNameParameter : votingListParameterNames) { - - Matcher matcher = personToListNamePattern.matcher( - personToListNameParameter); - - if (matcher.matches()) { - - int personToListNumber = buildPersonToList( - personToListPrefix, - personToListNameParameter, - matcher, - votingListNumber, - personToLists); - - maxPersonToListNumber = Math.max(maxPersonToListNumber, personToListNumber); - } - } - - personToLists = reindexMap(personToLists, maxPersonToListNumber); - - for (PersonToList personToList : personToLists.values()) { - votingList.addPollAccountPersonToList(personToList); - } - - return votingListNumber; - } - - private int buildPersonToList(String personToListPrefix, - String paramName, - Matcher personToListMatcher, - int votingListNumber, - Map<Integer, PersonToList> result) { - - String paramValue = getNonEmptyParameterValue(paramName); - - int personToListNumber = 0; - - if (paramValue != null) { - - // found a PersonToList none empty name, keep it - - personToListNumber = Integer.valueOf(personToListMatcher.group(1)); - - PersonToList personToList = new PersonToListImpl(); - - PollAccount account = new PollAccountImpl(); - personToList.setPollAccount(account); - - account.setVotingId(paramValue); - - String prefix = personToListPrefix + personToListNumber; - - double weight = getDoubleValue(prefix + ".weight"); - personToList.setWeight(weight); - - String topiaId = getNonEmptyParameterValue(prefix + ".topiaId"); - personToList.setTopiaId(topiaId); - - String email = getNonEmptyParameterValue(prefix + ".email"); - account.setEmail(email); - - String accountId = getNonEmptyParameterValue(prefix + ".accountId"); - account.setAccountId(accountId); - - result.put(personToListNumber, personToList); - } - return personToListNumber; - } - - private Choice createImageChoice(PollImageChoice choice, - String prefix, - String name) { - createChoice(choice, prefix, name); - String locationName = prefix + ".location"; - String location = getNonEmptyParameterValue(locationName); - choice.setLocation(location); - if (log.isInfoEnabled()) { - log.info("image location [" + name + "] =" + location); - } - return choice; - } - - private Choice createDateChoice(PollDateChoice choice, - String prefix, - String name) { - createChoice(choice, prefix, name); - if (StringUtils.isNotEmpty(name)) { - Date date = null; - try { - date = parseDateTime(name); - } catch (ParseException e) { - if (log.isErrorEnabled()) { - log.error("Unparseable date " + name, e); - } - } - choice.setDate(date); - } - return choice; - } - - private Choice createChoice(Choice choice, String prefix, String name) { - String descriptionName = prefix + ".description"; - String topiaIdName = prefix + ".topiaId"; - String description = getNonEmptyParameterValue(descriptionName); - String topiaId = getNonEmptyParameterValue(topiaIdName); - choice.setName(name); - choice.setDescription(description); - choice.setTopiaId(topiaId); - return choice; - } - - private void logChoice(Map<Integer, Choice> result) { - for (Map.Entry<Integer, Choice> e : result.entrySet()) { - Integer choiceId = e.getKey(); - Choice choice = e.getValue(); - if (log.isInfoEnabled()) { - log.info("Choice [" + choiceId + "] = " + - choice.getName() + " -- " + - choice.getDescription()); - } - } - } - - private <T> Map<Integer, T> reindexMap(Map<Integer, T> result, int maxNumber) { - Map<Integer, T> result2; - - if (maxNumber != result.size() - 1) { - - // means there is a hole inside the result (a empty choice was - // submitted) - - // le'ts remove this - List<Integer> numbers = Lists.newArrayList(result.keySet()); - - Collections.sort(numbers); - - result2 = Maps.newTreeMap(); - int i = 0; - for (Integer number : numbers) { - T choice = result.get(number); - result2.put(i++, choice); - } - } else { - result2 = result; - } - return result2; - } - - protected String getNonEmptyParameterValue(String paramName) { - String[] paramValues = parameters.get(paramName); - String result = null; - if (paramValues != null && paramValues.length == 1) { - String paramValue = paramValues[0]; - if (StringUtils.isNotEmpty(paramValue)) { - result = paramValue; - } - } - return result; - } - private <E extends Enum<E> & I18nAble> Map<String, String> decorateToName(E... values) { Map<String, String> result = Maps.newLinkedHashMap(); for (E value : values) { @@ -795,17 +309,4 @@ } return result; } - - private static class StringStartWithPredicate implements Predicate<String> { - private final String prefix; - - public StringStartWithPredicate(String prefix) { - this.prefix = prefix; - } - - @Override - public boolean apply(String input) { - return input.startsWith(prefix); - } - } } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DisplayPersonToList.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DisplayPersonToList.java 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DisplayPersonToList.java 2012-04-05 23:47:56 UTC (rev 3236) @@ -23,16 +23,12 @@ */ package org.chorem.pollen.ui.actions.poll; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.business.persistence.PersonToList; import org.chorem.pollen.services.impl.PollService; import org.chorem.pollen.ui.actions.PollenActionSupport; -import java.util.Map; - /** * To display a personToList (use at a poll creation). * @@ -64,8 +60,8 @@ return personToListTokenId + "_error_" + personToListNumber; } - /** Where to find dynamic data (says choices in this case). */ - protected String tokenId; + /** Type of the voting list. */ + protected String votingListType; /** Number of the vontingList container. */ protected int votingListNumber; @@ -75,14 +71,6 @@ protected PersonToList personToList; - public String getTokenId() { - return tokenId; - } - - public void setTokenId(String tokenId) { - this.tokenId = tokenId; - } - public int getVotingListNumber() { return votingListNumber; } @@ -91,6 +79,14 @@ this.votingListNumber = votingListNumber; } + public String getVotingListType() { + return votingListType; + } + + public void setVotingListType(String votingListType) { + this.votingListType = votingListType; + } + public int getPersonToListNumber() { return personToListNumber; } @@ -105,49 +101,26 @@ @Override public String execute() throws Exception { - if (StringUtils.isNotEmpty(tokenId)) { - Map<Integer, PersonToList> personToLists = - getPollenSession().getDynamicData(tokenId); - - if (personToLists != null) { - - // get choices from datas - personToList = personToLists.get(personToListNumber); - - if (personToList != null) { - - // remove this choice from session - personToLists.remove(personToListNumber); - } - } - - if (MapUtils.isEmpty(personToLists)) { - - // remove it from session - getPollenSession().removeDynamicData(tokenId); - } - } - if (personToList == null) { personToList = newService(PollService.class).getNewPersonToList(null); } - // consume personToList errors - String token = getPersonToListErrorTokenId(tokenId, personToListNumber); - Map<String, String> errors = getPollenSession().getDynamicData(token); - if (MapUtils.isNotEmpty(errors)) { - // transmit them as field errors - for (Map.Entry<String, String> e : errors.entrySet()) { - String fieldName = e.getKey(); - String errorMessage = e.getValue(); - if (log.isInfoEnabled()) { - log.info("Transmit error on " + fieldName + " [" + errorMessage + "]"); - } - addFieldError(fieldName, errorMessage); - } - } - getPollenSession().removeDynamicData(token); +// // consume personToList errors +// String token = getPersonToListErrorTokenId(tokenId, personToListNumber); +// Map<String, String> errors = getPollenSession().getDynamicData(token); +// if (MapUtils.isNotEmpty(errors)) { +// // transmit them as field errors +// for (Map.Entry<String, String> e : errors.entrySet()) { +// String fieldName = e.getKey(); +// String errorMessage = e.getValue(); +// if (log.isInfoEnabled()) { +// log.info("Transmit error on " + fieldName + " [" + errorMessage + "]"); +// } +// addFieldError(fieldName, errorMessage); +// } +// } +// getPollenSession().removeDynamicData(token); return SUCCESS; } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DisplayVotingList.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DisplayVotingList.java 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DisplayVotingList.java 2012-04-05 23:47:56 UTC (rev 3236) @@ -23,16 +23,12 @@ */ package org.chorem.pollen.ui.actions.poll; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import com.google.common.base.Function; +import org.chorem.pollen.business.persistence.PersonToList; import org.chorem.pollen.business.persistence.VotingList; -import org.chorem.pollen.business.persistence.VotingListImpl; +import org.chorem.pollen.services.PollenServiceFunctions; import org.chorem.pollen.ui.actions.PollenActionSupport; -import java.util.Map; - /** * To display a voting list of a poll (use at a poll creation). * @@ -45,42 +41,45 @@ public static final String VOTING_LIST_TOKEN = "votingList"; - /** Logger. */ - private static final Log log = LogFactory.getLog(DisplayVotingList.class); +// /** Logger. */ +// private static final Log log = LogFactory.getLog(DisplayVotingList.class); public static String getVotingListErrorTokenId(String votingListTokenId, int votingListNumber) { return votingListTokenId + "_" + votingListNumber + "_errors"; } - /** Where to find dynamic data (says choices in this case). */ - protected String tokenId; +// /** Where to find dynamic data (says choices in this case). */ +// protected String tokenId; + /** Type of the voting list. */ + protected String votingListType; + /** Number of person to display. */ protected int votingListNumber; - /** Count of personToList for this votingList. */ - protected int nbPersonToLists; +// /** Count of personToList for this votingList. */ +// protected int nbPersonToLists; /** The voting list ot display. */ protected VotingList votingList; - public String getPersonToListTokenId() { - return DisplayPersonToList.getPersonToListTokenId(tokenId, votingListNumber); - } +// public String getPersonToListTokenId() { +// return DisplayPersonToList.getPersonToListTokenId(tokenId, votingListNumber); +// } +// +// public int getNbPersonToLists() { +// return nbPersonToLists; +// } - public int getNbPersonToLists() { - return nbPersonToLists; - } +// public String getTokenId() { +// return tokenId; +// } +// +// public void setTokenId(String tokenId) { +// this.tokenId = tokenId; +// } - public String getTokenId() { - return tokenId; - } - - public void setTokenId(String tokenId) { - this.tokenId = tokenId; - } - public int getVotingListNumber() { return votingListNumber; } @@ -93,58 +92,76 @@ return votingList; } + public String getVotingListType() { + return votingListType; + } + + public void setVotingListType(String votingListType) { + this.votingListType = votingListType; + } + @Override public String execute() throws Exception { - if (StringUtils.isNotEmpty(tokenId)) { - Map<Integer, VotingList> votingLists = - getPollenSession().getDynamicData(tokenId); + Function<PersonToList, PersonToList> persontoListCreator = + PollenServiceFunctions.newPersonToListCreator( + false, serviceContext); + Function<VotingList, VotingList> votingListCreator = + PollenServiceFunctions.newVotingListCreator( + false, serviceContext, persontoListCreator); - if (votingLists != null) { +// if (StringUtils.isNotEmpty(tokenId)) { +// +// Map<Integer, VotingList> votingLists = +// getPollenSession().getDynamicData(tokenId); +// +// if (votingLists != null) { +// +// // get choices from datas +// votingList = votingLists.get(votingListNumber); +// +// if (votingList != null) { +// +// // remove this choice from session +// votingLists.remove(votingListNumber); +// } +// } +// +// if (MapUtils.isEmpty(votingLists)) { +// +// // remove it from session +// getPollenSession().removeDynamicData(tokenId); +// } +// } - // get choices from datas - votingList = votingLists.get(votingListNumber); + votingList = votingListCreator.apply(null); +// if (votingList == null) { +// - if (votingList != null) { +// nbPersonToLists = 5; +// } +// else { +// nbPersonToLists = votingList.sizePollAccountPersonToList(); +// } - // remove this choice from session - votingLists.remove(votingListNumber); - } - } + PollenServiceFunctions.fillVotingList(votingList, + persontoListCreator, 5); - if (MapUtils.isEmpty(votingLists)) { - - // remove it from session - getPollenSession().removeDynamicData(tokenId); - } - } - - if (votingList == null) { - votingList = new VotingListImpl(); - votingList.setWeight(1); - - //TODO tchemit use a default value from configuration - nbPersonToLists = 5; - } else { - nbPersonToLists = votingList.sizePollAccountPersonToList(); - } - nbPersonToLists = Math.max(5, nbPersonToLists); - - // consume votingList errors - String token = getVotingListErrorTokenId(tokenId, votingListNumber); - Map<String, String> errors = getPollenSession().getDynamicData(token); - if (MapUtils.isNotEmpty(errors)) { - // transmit them as field errors - for (Map.Entry<String, String> e : errors.entrySet()) { - String fieldName = e.getKey(); - String errorMessage = e.getValue(); - if (log.isInfoEnabled()) { - log.info("Transmit error on " + fieldName + " [" + errorMessage + "]"); - } - addFieldError(fieldName, errorMessage); - } - } - getPollenSession().removeDynamicData(token); +// // consume votingList errors +// String token = getVotingListErrorTokenId(tokenId, votingListNumber); +// Map<String, String> errors = getPollenSession().getDynamicData(token); +// if (MapUtils.isNotEmpty(errors)) { +// // transmit them as field errors +// for (Map.Entry<String, String> e : errors.entrySet()) { +// String fieldName = e.getKey(); +// String errorMessage = e.getValue(); +// if (log.isInfoEnabled()) { +// log.info("Transmit error on " + fieldName + " [" + errorMessage + "]"); +// } +// addFieldError(fieldName, errorMessage); +// } +// } +// getPollenSession().removeDynamicData(token); return SUCCESS; } } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/LoadPoll.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/LoadPoll.java 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/LoadPoll.java 2012-04-05 23:47:56 UTC (rev 3236) @@ -1,19 +1,39 @@ +/* + * #%L + * Pollen :: UI (strust2) + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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.google.common.base.Function; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; -import com.opensymphony.xwork2.Preparable; -import org.chorem.pollen.bean.PollImageChoice; +import org.apache.commons.collections.CollectionUtils; 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.UserAccount; import org.chorem.pollen.business.persistence.VotingList; -import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.common.PollType; +import org.chorem.pollen.services.PollenServiceFunctions; import java.util.List; @@ -22,18 +42,29 @@ * * @author fdesbois <desbois@codelutin.com> */ -public class LoadPoll extends AbstractPollForm implements Preparable { +public class LoadPoll extends AbstractPollForm { - private static final String PREFIX_CHOICE = "%sChoice_%d"; + private static final long serialVersionUID = 1L; - private static final String PREFIX_VOTING_LIST = "votingList_%d"; - - private static final String PREFIX_PERSON_TO_LIST = "personToList_%d_%d"; - private PollUri pollUri; private boolean clone; + /** Text choices. */ + private List<Choice> textChoices; + + /** Image choices. */ + private List<Choice> imageChoices; + + /** Date choices. */ + private List<Choice> dateChoices; + + /** restricted Voting lists. */ + private List<VotingList> restrictedVotingList; + + /** Group voting lists. */ + private List<VotingList> groupVotingList; + public PollUri getUriId() { return pollUri; } @@ -50,168 +81,145 @@ this.clone = clone; } - @Override - protected Poll initPoll() { - Poll result; - if (pollUri != null) { - result = getPollService().getPollByPollId(pollUri.getPollId()); - - if (clone) { - result.setTopiaId(null); - result.setPollId(serviceContext.createPollenUrlId()); + public List<Choice> getTextChoices() { + return textChoices; + } - // Reset creator but keep userAccount link - PollAccount creator = result.getCreator(); - creator.setTopiaId(null); - creator.setAccountId(serviceContext.createPollenUrlId()); - } - - } else { - UserAccount userAccount = getPollenUserAccount(); - result = getPollService().getNewPoll(userAccount); - } - return result; + public List<Choice> getImageChoices() { + return imageChoices; } - @Override - public void prepare() throws Exception { - - if (pollUri != null) { + public List<Choice> getDateChoices() { + return dateChoices; + } - // -- Choice -- // - ChoiceType choiceType = getPoll().getChoiceType(); - prepareParams(getPoll().getChoice(), withChoicePrefix(choiceType)); + public List<VotingList> getRestrictedVotingList() { + return restrictedVotingList; + } - // -- VotingList -- // - if (getPoll().getPollType() != PollType.FREE) { - prepareParams(getPoll().getVotingList(), withVotingListPrefix()); - } - } - - prepareFormPage(); + public List<VotingList> getGroupVotingList() { + return groupVotingList; } - private Function<Choice, String> withChoicePrefix(final ChoiceType choiceType) { - final String choiceTypeName = choiceType.name().toLowerCase(); - return new IndexedFunction<Choice, String>() { + @Override + public String execute() throws Exception { - @Override - protected String apply(Choice input, int index) { + prepareFormPage(); - String prefix = String.format(PREFIX_CHOICE, choiceTypeName, index); - if (!clone) { - putParameter(prefix, Choice.TOPIA_ID, input.getTopiaId()); - } - putParameter(prefix, Choice.PROPERTY_NAME, input.getName()); - putParameter(prefix, Choice.PROPERTY_DESCRIPTION, input.getDescription()); + textChoices = Lists.newArrayList(); + imageChoices = Lists.newArrayList(); + dateChoices = Lists.newArrayList(); + restrictedVotingList = Lists.newArrayList(); + groupVotingList = Lists.newArrayList(); - if (choiceType == ChoiceType.IMAGE) { - putParameter(prefix, PollImageChoice.PROPERTY_LOCATION, input.getName()); - } + Function<PersonToList, PersonToList> persontoListCreator = + PollenServiceFunctions.newPersonToListCreator(clone, serviceContext); + Function<VotingList, VotingList> votingListCreator = + PollenServiceFunctions.newVotingListCreator(clone, + serviceContext, + persontoListCreator); - return prefix; - } - }; - } + Function<Choice, Choice> textChoiceCreator = + PollenServiceFunctions.newTextChoiceCreator(clone); + Function<Choice, Choice> dateChoiceCreator = + PollenServiceFunctions.newDateChoiceCreator(clone); + Function<Choice, Choice> imageChoiceCreator = + PollenServiceFunctions.newImageChoiceCreator(clone); - private Function<VotingList, String> withVotingListPrefix() { - return new IndexedFunction<VotingList, String>() { + if (pollUri == null) { - @Override - protected String apply(VotingList input, int index) { + // create a new poll - String prefix = String.format(PREFIX_VOTING_LIST, index); - if (!clone) { - putParameter(prefix, VotingList.TOPIA_ID, input.getTopiaId()); - } - putParameter(prefix, VotingList.PROPERTY_NAME, input.getName()); - putParameter(prefix, VotingList.PROPERTY_WEIGHT, String.valueOf(input.getWeight())); + UserAccount userAccount = getPollenUserAccount(); + poll = getPollService().getNewPoll(userAccount); + } else { - prepareParams(input.getPollAccountPersonToList(), withPersonToListPrefix(index)); + // update or clone an existing poll - return prefix; + poll = getPollService().getPollByPollId(pollUri.getPollId()); + + if (clone) { + + poll.setTopiaId(null); + poll.setPollId(serviceContext.createPollenUrlId()); + + // Reset creator but keep userAccount link + PollAccount creator = poll.getCreator(); + creator.setTopiaId(null); + creator.setAccountId(serviceContext.createPollenUrlId()); } - }; - } - private Function<PersonToList, String> withPersonToListPrefix(final int votingListIndex) { - return new IndexedFunction<PersonToList, String>() { + // -- Choice -- // + if (!poll.isChoiceEmpty()) { - @Override - protected String apply(PersonToList input, int index) { + // reload choices - PollAccount pollAccount = input.getPollAccount(); + switch (poll.getChoiceType()) { - String prefix = String.format(PREFIX_PERSON_TO_LIST, votingListIndex, index); - String accountId; - if (!clone) { - putParameter(prefix, PersonToList.TOPIA_ID, input.getTopiaId()); - accountId = pollAccount.getAccountId(); - } else { - accountId = serviceContext.createPollenUrlId(); + case TEXT: + textChoices.addAll(Lists.transform(poll.getChoice(), textChoiceCreator)); + break; + case DATE: + dateChoices.addAll(Lists.transform(poll.getChoice(), dateChoiceCreator)); + break; + case IMAGE: + imageChoices.addAll(Lists.transform(poll.getChoice(), imageChoiceCreator)); + break; } - putParameter(prefix, PersonToList.PROPERTY_WEIGHT, String.valueOf(input.getWeight())); - putParameter(prefix, PollAccount.PROPERTY_ACCOUNT_ID, accountId); - putParameter(prefix, PollAccount.PROPERTY_VOTING_ID, pollAccount.getVotingId()); - putParameter(prefix, PollAccount.PROPERTY_EMAIL, pollAccount.getEmail()); + } - return prefix; + // -- VotingList -- // + if (poll.getPollType() != PollType.FREE) { + + // reload voting lists + if (!poll.isVotingListEmpty()) { + switch (poll.getPollType()) { + + case FREE: + // nothing to load + break; + case RESTRICTED: + restrictedVotingList.addAll(Lists.transform( + poll.getVotingList(), votingListCreator)); + break; + case GROUP: + groupVotingList.addAll(Lists.transform( + poll.getVotingList(), votingListCreator)); + break; + } + + } } - }; - } + } - /** - * 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)); - } + if (!isVoteStarted()) { - /** - * 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}); - } + // can edit choices and voting lists, so fill them - /** - * 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> { + int defaultMaxChoices = 5; + PollenServiceFunctions.fillChoiceList(textChoices, defaultMaxChoices, textChoiceCreator); + PollenServiceFunctions.fillChoiceList(dateChoices, defaultMaxChoices, dateChoiceCreator); + PollenServiceFunctions.fillChoiceList(imageChoices, defaultMaxChoices, imageChoiceCreator); - private int index; - - @Override - public T apply(F input) { - T result = apply(input, index); - index++; - return result; + int defaultMaxVoting = 5; + if (CollectionUtils.isEmpty(restrictedVotingList)) { + restrictedVotingList.add(votingListCreator.apply(null)); + } + for (VotingList votingList : restrictedVotingList) { + PollenServiceFunctions.fillVotingList(votingList, + persontoListCreator, + defaultMaxVoting); + } + if (CollectionUtils.isEmpty(groupVotingList)) { + groupVotingList.add(votingListCreator.apply(null)); + } + for (VotingList votingList : groupVotingList) { + PollenServiceFunctions.fillVotingList(votingList, + persontoListCreator, + defaultMaxVoting); + } } + return SUCCESS; + } - /** - * 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/java/org/chorem/pollen/ui/actions/poll/SavePoll.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java 2012-04-05 23:47:56 UTC (rev 3236) @@ -23,29 +23,51 @@ */ package org.chorem.pollen.ui.actions.poll; +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.opensymphony.xwork2.Preparable; +import com.opensymphony.xwork2.interceptor.annotations.InputConfig; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; 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; +import org.chorem.pollen.business.persistence.ChoiceImpl; import org.chorem.pollen.business.persistence.PersonToList; +import org.chorem.pollen.business.persistence.PersonToListImpl; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; +import org.chorem.pollen.business.persistence.PollAccountImpl; import org.chorem.pollen.business.persistence.PreventRule; import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.business.persistence.VotingList; +import org.chorem.pollen.business.persistence.VotingListImpl; import org.chorem.pollen.common.ChoiceType; +import org.chorem.pollen.common.PollType; +import org.chorem.pollen.services.PollenServiceFunctions; import org.chorem.pollen.services.impl.PollService; import org.chorem.pollen.services.impl.PreventRuleService; import org.chorem.pollen.services.impl.UserService; +import org.chorem.pollen.ui.actions.FileUploadAware; import org.nuiton.util.StringUtil; +import java.io.File; +import java.text.ParseException; +import java.util.Collections; +import java.util.Date; +import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Creates a new poll. @@ -53,46 +75,244 @@ * @author tchemit <chemit@codelutin.com> * @since 1.2.6 */ -public class SavePoll extends AbstractPollForm implements Preparable { +public class SavePoll extends AbstractPollForm implements Preparable, ParameterAware, FileUploadAware { private static final long serialVersionUID = 1L; /** Logger. */ private static final Log log = LogFactory.getLog(SavePoll.class); - + + private static final Pattern TEXT_CHOICE_NAME_PATTERN = + Pattern.compile("textChoice_(\\d+)\\.name"); + + private static final Pattern DATE_CHOICE_NAME_PATTERN = + Pattern.compile("dateChoice_(\\d+)\\.name"); + + private static final Pattern IMAGE_CHOICE_NAME_PATTERN = + Pattern.compile("imageChoice_(\\d+)\\.name"); + +// private static final Pattern VOTING_LIST_NAME_PATTERN = +// Pattern.compile("votingList_(\\d+)\\.name"); + private String userId; - - private String pollId; + /** Uploaded images (for choice type poll). */ + private List<File> imageChoice; + + /** Uploaded images content type (for choice type poll). */ + private List<String> imageChoiceContentType; + + /** Uploaded images name (from use desktop) (for choice type poll). */ + private List<String> imageChoiceFileName; + + protected boolean informationsError; + + protected boolean optionsError; + + /** + * All the parameters send by request used to build back choices of the + * poll. + */ + protected Map<String, String[]> parameters; + + /** + * Indexed choices retreive from parameters for the choiceType selected in + * form. (Other choices are lost). + */ + private Map<Integer, Choice> choices; + + /** + * Indexed voting lists retreive from parameters the pollType selected in + * form . (Other voting lists are lost). + */ + private Map<Integer, VotingList> votingLists; + + /** Text choices. */ + private List<Choice> textChoices; + + /** Image choices. */ + private List<Choice> imageChoices; + + /** Date choices. */ + private List<Choice> dateChoices; + + /** restricted Voting lists. */ + private List<VotingList> restrictedVotingList; + + /** Group voting lists. */ + private List<VotingList> groupVotingList; + public void setUserId(String userId) { this.userId = userId; } + public List<Choice> getTextChoices() { + return textChoices; + } + + public List<Choice> getImageChoices() { + return imageChoices; + } + + public List<Choice> getDateChoices() { + return dateChoices; + } + + public List<VotingList> getRestrictedVotingList() { + return restrictedVotingList; + } + + public List<VotingList> getGroupVotingList() { + return groupVotingList; + } + @Override - protected Poll initPoll() { - Poll result; + public void prepare() throws Exception { + + prepareFormPage(); + + String pollId = getNonEmptyParameterValue("poll.topiaId"); + if (pollId != null) { - result = getPollService().getEntityById(Poll.class, pollId); - + poll = getPollService().getEntityById(Poll.class, pollId); + } else { UserAccount userAccount = getPollenUserAccount(); - result = getPollService().getNewPoll(userAccount); + poll = getPollService().getNewPoll(userAccount); } - return result; + + // Retrieve choiceType from parameters, the poll object will be updated after prepare + String choiceTypeParam = getNonEmptyParameterValue("poll.choiceType"); + ChoiceType pollChoiceType = ChoiceType.valueOf(choiceTypeParam); + poll.setChoiceType(pollChoiceType); + + // build poll choices + + switch (pollChoiceType) { + + case TEXT: + + choices = buildTextChoices(); + break; + case DATE: + + choices = buildDateChoices(); + break; + case IMAGE: + + choices = buildImageChoices(); + break; + } + + String pollTypeParam = getNonEmptyParameterValue("poll.pollType"); + PollType pollType = PollType.valueOf(pollTypeParam); + poll.setPollType(pollType); + + switch (pollType) { + + case FREE: + + // empty voting list + votingLists = Maps.newTreeMap(); + + break; + case RESTRICTED: + + // restricted voting list + votingLists = buildVotingLists(pollType); + break; + case GROUP: + + // group voting lists + votingLists = buildVotingLists(pollType); + + break; + } } @Override - public void prepare() throws Exception { + public String input() { - pollId = getNonEmptyParameterValue("poll.topiaId"); + //push back choices - prepareFormPage(); + textChoices = Lists.newArrayList(); + imageChoices = Lists.newArrayList(); + dateChoices = Lists.newArrayList(); + + switch (poll.getChoiceType()) { + + case TEXT: + textChoices.addAll(choices.values()); + break; + case DATE: + dateChoices.addAll(choices.values()); + break; + case IMAGE: + imageChoices.addAll(choices.values()); + break; + } + + // push back voting lists + + restrictedVotingList = Lists.newArrayList(); + groupVotingList = Lists.newArrayList(); + + switch (poll.getPollType()) { + + case FREE: + // not voting lists + break; + case RESTRICTED: + restrictedVotingList.addAll(votingLists.values()); + break; + case GROUP: + groupVotingList.addAll(votingLists.values()); + break; + } + + // refill lists + + Function<PersonToList, PersonToList> persontoListCreator = + PollenServiceFunctions.newPersonToListCreator(false, serviceContext); + Function<VotingList, VotingList> votingListCreator = + PollenServiceFunctions.newVotingListCreator(false, + serviceContext, + persontoListCreator); + + Function<Choice, Choice> textChoiceCreator = + PollenServiceFunctions.newTextChoiceCreator(false); + Function<Choice, Choice> dateChoiceCreator = + PollenServiceFunctions.newDateChoiceCreator(false); + Function<Choice, Choice> imageChoiceCreator = + PollenServiceFunctions.newImageChoiceCreator(false); + + int defaultMaxChoices = 5; + PollenServiceFunctions.fillChoiceList(textChoices, defaultMaxChoices, textChoiceCreator); + PollenServiceFunctions.fillChoiceList(dateChoices, defaultMaxChoices, dateChoiceCreator); + PollenServiceFunctions.fillChoiceList(imageChoices, defaultMaxChoices, imageChoiceCreator); + + int defaultMaxVoting = 5; + if (CollectionUtils.isEmpty(restrictedVotingList)) { + restrictedVotingList.add(votingListCreator.apply(null)); + } + for (VotingList votingList : restrictedVotingList) { + PollenServiceFunctions.fillVotingList(votingList, + persontoListCreator, + defaultMaxVoting); + } + if (CollectionUtils.isEmpty(groupVotingList)) { + groupVotingList.add(votingListCreator.apply(null)); + } + for (VotingList votingList : groupVotingList) { + PollenServiceFunctions.fillVotingList(votingList, + persontoListCreator, + defaultMaxVoting); + } + return INPUT; } @Override public void validate() { - - Poll poll = getPoll(); informationsError = false; @@ -101,11 +321,9 @@ addFieldError("poll.title", _("pollen.error.poll.required.title")); } - // validate choices - Map<Integer, Choice> orderedChoices = getOrderedChoices(); - if (MapUtils.isEmpty(orderedChoices)) { + if (MapUtils.isEmpty(choices)) { // poll must have at least one choice addFieldError("poll.choices", @@ -113,13 +331,15 @@ } else { ChoiceType choiceType = poll.getChoiceType(); - int inputChoicesSize = orderedChoices.size(); + int inputChoicesSize = choices.size(); Set<Object> choiceValues = - Sets.newHashSet(ChoiceHelper.toValues(orderedChoices.values(), choiceType)); + Sets.newHashSet(ChoiceHelper.toValues(choices.values(), + choiceType)); if (inputChoicesSize > choiceValues.size()) { - addFieldError("poll.choices", - _("pollen.error.poll.detected.duplicate.choice.name")); + addFieldError( + "poll.choices", + _("pollen.error.poll.detected.duplicate.choice.name")); } } @@ -132,7 +352,7 @@ // nothing to validate } else { - Map<Integer, VotingList> votingLists = getOrderedVotingLists(); +// Map<Integer, VotingList> votingLists = getOrderedVotingLists(); Set<String> groups = Sets.newHashSet(); Set<String> voters = Sets.newHashSet(); @@ -157,25 +377,25 @@ } @Override + @InputConfig(methodName = "input") public String execute() throws Exception { - Poll poll = getPoll(); - // Clear previous collections to save those from the form poll.clearChoice(); poll.clearPreventRule(); poll.clearVotingList(); - + // Load userAccount from the form if (!isCreatorUserAccountDefined() && StringUtils.isNotBlank(userId)) { UserService userService = newService(UserService.class); - UserAccount userAccount = userService.getEntityById(UserAccount.class, userId); + UserAccount userAccount = userService.getEntityById( + UserAccount.class, userId); poll.getCreator().setUserAccount(userAccount); } - Map<Integer, Choice> orderedChoices = getOrderedChoices(); + Map<Integer, Choice> orderedChoices = choices; for (Integer index : orderedChoices.keySet()) { Choice choice = orderedChoices.get(index); @@ -184,7 +404,7 @@ if (!isFreePoll()) { - Map<Integer, VotingList> votingLists = getOrderedVotingLists(); +// Map<Integer, VotingList> votingLists = getOrderedVotingLists(); for (Integer index : votingLists.keySet()) { VotingList votingList = votingLists.get(index); poll.addVotingList(votingList); @@ -228,29 +448,90 @@ return SUCCESS; } - protected Map<Integer, Choice> getOrderedChoices() { - Map<Integer, Choice> result = null; - switch (getPoll().getChoiceType()) { - case TEXT: - result = getPollenSession().getDynamicData(getTextChoiceTokenId()); - break; - case DATE: - result = getPollenSession().getDynamicData(getDateChoiceTokenId()); - break; - case IMAGE: - result = getPollenSession().getDynamicData(getImageChoiceTokenId()); - break; + @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); + } + + @Override + public void setParameters(Map<String, String[]> parameters) { + this.parameters = parameters; + } + + public List<File> getImageChoice() { + return imageChoice; + } + + public List<String> getImageChoiceContentType() { + if (imageChoiceContentType == null) { + imageChoiceContentType = Lists.newArrayList(); } - return result; + return imageChoiceContentType; } - protected Map<Integer, VotingList> getOrderedVotingLists() { + public List<String> getImageChoiceFileName() { + if (imageChoiceFileName == null) { + imageChoiceFileName = Lists.newArrayList(); + } + return imageChoiceFileName; + } - Map<Integer, VotingList> result = getPollenSession().getDynamicData( - getVotingListTokenId()); + public boolean isInformationsError() { + return informationsError; + } + + public boolean isOptionsError() { + return optionsError; + } + + public int getSelectedTab() { + int result; + if (isInformationsError()) { + result = 0; + } else { + if (isOptionsError()) { + result = 1; + } else { + result = 0; + } + } return result; } +// protected Map<Integer, Choice> getOrderedChoices() { +// Map<Integer, Choice> result = null; +//// switch (getPoll().getChoiceType()) { +//// case TEXT: +//// result = getPollenSession().getDynamicData(getTextChoiceTokenId()); +//// break; +//// case DATE: +//// result = getPollenSession().getDynamicData(getDateChoiceTokenId()); +//// break; +//// case IMAGE: +//// result = getPollenSession().getDynamicData(getImageChoiceTokenId()); +//// break; +//// } +// return result; +// } + +// protected Map<Integer, VotingList> getOrderedVotingLists() { +// +// Map<Integer, VotingList> result = null; +//// result = getPollenSession().getDynamicData( +//// getVotingListTokenId()); +// return result; +// } + protected boolean validateVotingList(int votingListNumber, VotingList votingList, Set<String> groups, @@ -308,41 +589,41 @@ // check there is at least one voter - String persontoListToken = DisplayPersonToList.getPersonToListTokenId( - getVotingListTokenId(), votingListNumber); - - Map<Integer, PersonToList> personToLists = - getPollenSession().getDynamicData(persontoListToken); - - if (MapUtils.isEmpty(personToLists)) { - - // no personToList found for unique votingList 0 - errors.put(votingListErrorPrefix, - _("pollen.error.poll.required.one.personToList")); - } else { - - // check no doublon on voter names - // check no doublon on voter emails - - for (Map.Entry<Integer, PersonToList> entry : - personToLists.entrySet()) { - - result |= validatePersonList(entry.getKey(), entry.getValue(), - voters, emails); - } - } - - if (MapUtils.isNotEmpty(errors)) { - - // keep errors to display them in correct action - String errorToken = DisplayVotingList.getVotingListErrorTokenId( - getVotingListTokenId(), votingListNumber); - if (log.isInfoEnabled()) { - log.info("Add " + errors.size() + " errors to " + errorToken); - } - getPollenSession().putDynamicData(errorToken, errors); - result = true; - } +// String persontoListToken = DisplayPersonToList.getPersonToListTokenId( +// getVotingListTokenId(), votingListNumber); +// +// Map<Integer, PersonToList> personToLists = +// getPollenSession().getDynamicData(persontoListToken); +// +// if (MapUtils.isEmpty(personToLists)) { +// +// // no personToList found for unique votingList 0 +// errors.put(votingListErrorPrefix, +// _("pollen.error.poll.required.one.personToList")); +// } else { +// +// // check no doublon on voter names +// // check no doublon on voter emails +// +// for (Map.Entry<Integer, PersonToList> entry : +// personToLists.entrySet()) { +// +// result |= validatePersonList(entry.getKey(), entry.getValue(), +// voters, emails); +// } +// } +// +// if (MapUtils.isNotEmpty(errors)) { +// +// // keep errors to display them in correct action +// String errorToken = DisplayVotingList.getVotingListErrorTokenId( +// getVotingListTokenId(), votingListNumber); +// if (log.isInfoEnabled()) { +// log.info("Add " + errors.size() + " errors to " + errorToken); +// } +// getPollenSession().putDynamicData(errorToken, errors); +// result = true; +// } return result; } @@ -412,15 +693,436 @@ } if (MapUtils.isNotEmpty(errors)) { - String errorToken = - DisplayPersonToList.getPersonToListErrorTokenId( - getVotingListTokenId(), 0, personToListNumber); +// String errorToken = +// DisplayPersonToList.getPersonToListErrorTokenId( +// getVotingListTokenId(), 0, personToListNumber); +// if (log.isInfoEnabled()) { +// log.info("Add " + errors.size() + " errors to " + errorToken); +// } +// getPollenSession().putDynamicData(errorToken, errors); + result = true; + } + return result; + } + + protected Map<Integer, Choice> buildTextChoices() { + Map<Integer, Choice> result = Maps.newTreeMap(); + + int maxNumber = 0; + + for (String paramName : parameters.keySet()) { + + Matcher matcher = TEXT_CHOICE_NAME_PATTERN.matcher(paramName); + if (matcher.matches()) { + + // found a text choice name + + String paramValue = getNonEmptyParameterValue(paramName); + if (paramValue != null) { + + // can keep this none empty text choice name + + Integer choiceNumber = Integer.valueOf(matcher.group(1)); + if (choiceNumber > maxNumber) { + maxNumber = choiceNumber; + } + Choice choice = new ChoiceImpl(); + createChoice(choice, "textChoice_" + choiceNumber, + paramValue); + result.put(choiceNumber, choice); + } + } + } + result = reindexMap(result, maxNumber); + + int size = result.size(); +// nbTextChoices = Math.max(nbDefault, size); + log.info("nbTextChoices (from request) = " + size); + logChoice(result); + return result; + } + + protected Map<Integer, Choice> buildDateChoices() { + Map<Integer, Choice> result = Maps.newTreeMap(); + + int maxNumber = 0; + for (String paramName : parameters.keySet()) { + + Matcher matcher = DATE_CHOICE_NAME_PATTERN.matcher(paramName); + if (matcher.matches()) { + + // found a text choice name + + String paramValue = getNonEmptyParameterValue(paramName); + if (paramValue != null) { + + // can keep this none empty text choice name + + Integer choiceNumber = Integer.valueOf(matcher.group(1)); + if (choiceNumber > maxNumber) { + maxNumber = choiceNumber; + } + PollDateChoice choice = new PollDateChoice(); + createDateChoice(choice, + "dateChoice_" + choiceNumber, + paramValue); + result.put(choiceNumber, choice); + } + } + } + result = reindexMap(result, maxNumber); + + int size = result.size(); +// nbDateChoices = Math.max(nbDefault, size); + log.info("nbDateChoices (from request) = " + size); + logChoice(result); + return result; + } + + protected Map<Integer, Choice> buildImageChoices() { + Map<Integer, Choice> result = Maps.newTreeMap(); + + // push back in parameters stuff from uploaded files + int index = 0; + + for (String fileName : getImageChoiceFileName()) { + if (fileName != null) { + parameters.put("imageChoice_" + index + ".name", + new String[]{fileName}); + parameters.put( + "imageChoice_" + index + ".location", + new String[]{getImageChoice().get(index).getAbsolutePath()}); + } + index++; + } + + int maxNumber = 0; + for (String paramName : parameters.keySet()) { + + Matcher matcher = IMAGE_CHOICE_NAME_PATTERN.matcher(paramName); + if (matcher.matches()) { + + // found an image choice name + + String paramValue = getNonEmptyParameterValue(paramName); + if (paramValue != null) { + + // can keep this none empty text choice name + + Integer choiceNumber = Integer.valueOf(matcher.group(1)); + if (choiceNumber > maxNumber) { + maxNumber = choiceNumber; + } + PollImageChoice choice = new PollImageChoice(); + createImageChoice(choice, + "imageChoice_" + choiceNumber, + paramValue); + + result.put(choiceNumber, choice); + } + } + } + result = reindexMap(result, maxNumber); + int size = result.size(); +// nbImageChoices = Math.max(nbDefault, size); + log.info("nbImageChoices (from request) = " + size); + logChoice(result); + return result; + } + + protected Map<Integer, VotingList> buildVotingLists(PollType pollType) { + Map<Integer, VotingList> result = Maps.newTreeMap(); + + int maxNumber = 0; + + String votingListPrefix = "votingList" + pollType.name(); + + // get all votingList_ parameters + Set<String> votingListParameterNames = Sets.filter( + parameters.keySet(), + new StringStartWithPredicate(votingListPrefix)); + + Pattern votingListPattern = Pattern.compile( + "(" + votingListPrefix + ")_(\\d+)\\.name"); + + for (String paramName : votingListParameterNames) { + + Matcher matcher = votingListPattern.matcher(paramName); + + if (matcher.matches()) { + + // found a voting list name + + int votingListNumber = Integer.valueOf(matcher.group(2)); + + votingListPrefix += "_" + votingListNumber; + buildVotingList(paramName, + votingListPrefix, + votingListNumber, + result + ); + maxNumber = Math.max(maxNumber, votingListNumber); + } + } + + result = reindexMap(result, maxNumber); + + int size = result.size(); +// nbVotingLists = Math.max(nbDefault, size); + log.info("nbVotingList [" + pollType + "] (from request) = " + size); + + // add personToList maps to session (but just now, since votingList + // could have been reindex) + for (Map.Entry<Integer, VotingList> entry : result.entrySet()) { + Integer votingListNumber = entry.getKey(); + VotingList votingList = entry.getValue(); + + if (!votingList.isPollAccountPersonToListEmpty()) { + List<PersonToList> personToList = + votingList.getPollAccountPersonToList(); + + Map<Integer, PersonToList> personToListMap = Maps.newTreeMap(); + int index = 0; + for (PersonToList toList : personToList) { + personToListMap.put(index++, toList); + } + +// String token = DisplayPersonToList.getPersonToListTokenId( +// votingListTokenId, votingListNumber); +// if (log.isInfoEnabled()) { +// log.info("Add " + token + " with " + +// personToListMap.size() + " personToList"); +// } +// getPollenSession().putDynamicData(token, personToListMap); + } + } + + return result; + } + + private double getDoubleValue(String parameterName) { + String parameterValue = getNonEmptyParameterValue(parameterName); + double result = 0; + if (StringUtils.isNotEmpty(parameterValue)) { + + try { + result = Double.valueOf(parameterValue); + } catch (NumberFormatException e) { + //bad conversion, will be treated later + if (log.isDebugEnabled()) { + log.debug("Bad double conversion from param [" + + parameterName + "] : " + parameterValue); + } + } + } + return result; + } + + private int buildVotingList(String votingListParameterName, + String votingListPrefix, + int votingListNumber, + Map<Integer, VotingList> result) { + + String paramValue = getNonEmptyParameterValue(votingListParameterName); + + VotingList votingList = new VotingListImpl(); + + votingList.setName(paramValue); + + double weight = getDoubleValue(votingListPrefix + ".weight"); + votingList.setWeight(weight); + + String topiaId = getNonEmptyParameterValue(votingListPrefix + ".topiaId"); + votingList.setTopiaId(topiaId); + + result.put(votingListNumber, votingList); + + String personToListPrefix = votingListPrefix + "PersonToList_"; + + // get all personToList parameters + Set<String> votingListParameterNames = Sets.filter( + parameters.keySet(), + new StringStartWithPredicate(personToListPrefix)); + + Pattern personToListNamePattern = Pattern.compile( + personToListPrefix + "(\\d+)\\.votingId"); + + Map<Integer, PersonToList> personToLists = Maps.newTreeMap(); + + int maxPersonToListNumber = 0; + + // let's build personToList list + for (String personToListNameParameter : votingListParameterNames) { + + Matcher matcher = personToListNamePattern.matcher( + personToListNameParameter); + + if (matcher.matches()) { + + int personToListNumber = buildPersonToList( + personToListPrefix, + personToListNameParameter, + matcher, + votingListNumber, + personToLists); + + maxPersonToListNumber = Math.max(maxPersonToListNumber, + personToListNumber); + } + } + + personToLists = reindexMap(personToLists, maxPersonToListNumber); + + for (PersonToList personToList : personToLists.values()) { + votingList.addPollAccountPersonToList(personToList); + } + + return votingListNumber; + } + + private int buildPersonToList(String personToListPrefix, + String paramName, + Matcher personToListMatcher, + int votingListNumber, + Map<Integer, PersonToList> result) { + + String paramValue = getNonEmptyParameterValue(paramName); + + int personToListNumber = 0; + + if (paramValue != null) { + + // found a PersonToList none empty name, keep it + + personToListNumber = Integer.valueOf(personToListMatcher.group(1)); + + PersonToList personToList = new PersonToListImpl(); + + PollAccount account = new PollAccountImpl(); + personToList.setPollAccount(account); + + account.setVotingId(paramValue); + + String prefix = personToListPrefix + personToListNumber; + + double weight = getDoubleValue(prefix + ".weight"); + personToList.setWeight(weight); + + String topiaId = getNonEmptyParameterValue(prefix + ".topiaId"); + personToList.setTopiaId(topiaId); + + String email = getNonEmptyParameterValue(prefix + ".email"); + account.setEmail(email); + + String accountId = getNonEmptyParameterValue(prefix + ".accountId"); + account.setAccountId(accountId); + + result.put(personToListNumber, personToList); + } + return personToListNumber; + } + + private Choice createImageChoice(PollImageChoice choice, + String prefix, + String name) { + createChoice(choice, prefix, name); + String locationName = prefix + ".location"; + String location = getNonEmptyParameterValue(locationName); + choice.setLocation(location); + if (log.isInfoEnabled()) { + log.info("image location [" + name + "] =" + location); + } + return choice; + } + + private Choice createDateChoice(PollDateChoice choice, + String prefix, + String name) { + createChoice(choice, prefix, name); + if (StringUtils.isNotEmpty(name)) { + Date date = null; + try { + date = parseDateTime(name); + } catch (ParseException e) { + if (log.isErrorEnabled()) { + log.error("Unparseable date " + name, e); + } + } + choice.setDate(date); + } + return choice; + } + + private Choice createChoice(Choice choice, String prefix, String name) { + String descriptionName = prefix + ".description"; + String topiaIdName = prefix + ".topiaId"; + String description = getNonEmptyParameterValue(descriptionName); + String topiaId = getNonEmptyParameterValue(topiaIdName); + choice.setName(name); + choice.setDescription(description); + choice.setTopiaId(topiaId); + return choice; + } + + private void logChoice(Map<Integer, Choice> result) { + for (Map.Entry<Integer, Choice> e : result.entrySet()) { + Integer choiceId = e.getKey(); + Choice choice = e.getValue(); if (log.isInfoEnabled()) { - log.info("Add " + errors.size() + " errors to " + errorToken); + log.info("Choice [" + choiceId + "] = " + + choice.getName() + " -- " + + choice.getDescription()); } - getPollenSession().putDynamicData(errorToken, errors); - result = true; } + } + + protected <T> Map<Integer, T> reindexMap(Map<Integer, T> result, int maxNumber) { + Map<Integer, T> result2; + + if (maxNumber != result.size() - 1) { + + // means there is a hole inside the result (a empty choice was + // submitted) + + // le'ts remove this + List<Integer> numbers = Lists.newArrayList(result.keySet()); + + Collections.sort(numbers); + + result2 = Maps.newTreeMap(); + int i = 0; + for (Integer number : numbers) { + T choice = result.get(number); + result2.put(i++, choice); + } + } else { + result2 = result; + } + return result2; + } + + protected String getNonEmptyParameterValue(String paramName) { + String[] paramValues = parameters.get(paramName); + String result = null; + if (paramValues != null && paramValues.length == 1) { + String paramValue = paramValues[0]; + if (StringUtils.isNotEmpty(paramValue)) { + result = paramValue; + } + } return result; } + + private static class StringStartWithPredicate implements Predicate<String> { + private final String prefix; + + public StringStartWithPredicate(String prefix) { + this.prefix = prefix; + } + + @Override + public boolean apply(String input) { + return input.startsWith(prefix); + } + } } Property changes on: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native 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-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/config/struts-poll.xml 2012-04-05 23:47:56 UTC (rev 3236) @@ -42,47 +42,15 @@ </result-types> <!-- create poll --> - <action name="create" - class="org.chorem.pollen.ui.actions.poll.LoadPoll"> + <action name="create" class="org.chorem.pollen.ui.actions.poll.LoadPoll"> <result>/WEB-INF/jsp/poll/create.jsp</result> </action> <!-- display * --> - <action name="display*" - class="org.chorem.pollen.ui.actions.poll.Display{1}"> + <action name="display*" class="org.chorem.pollen.ui.actions.poll.Display{1}"> <result>/WEB-INF/jsp/poll/{1}.jsp</result> </action> - <!--<!– display text choice –>--> - <!--<action name="displayTextChoice"--> - <!--class="org.chorem.pollen.ui.actions.poll.DisplayTextChoice">--> - <!--<result>/WEB-INF/jsp/poll/TextChoice.jsp</result>--> - <!--</action>--> - - <!--<!– display date choice –>--> - <!--<action name="displayDateChoice"--> - <!--class="org.chorem.pollen.ui.actions.poll.DisplayDateChoice">--> - <!--<result>/WEB-INF/jsp/poll/DateChoice.jsp</result>--> - <!--</action>--> - - <!--<!– display image choice –>--> - <!--<action name="displayImageChoice"--> - <!--class="org.chorem.pollen.ui.actions.poll.DisplayImageChoice">--> - <!--<result>/WEB-INF/jsp/poll/ImageChoice.jsp</result>--> - <!--</action>--> - - <!--<!– display a votingList –>--> - <!--<action name="displayVotingList"--> - <!--class="org.chorem.pollen.ui.actions.poll.DisplayVotingList">--> - <!--<result>/WEB-INF/jsp/poll/VotingList.jsp</result>--> - <!--</action>--> - - <!--<!– display a personToList –>--> - <!--<action name="displayPersonToList"--> - <!--class="org.chorem.pollen.ui.actions.poll.DisplayPersonToList">--> - <!--<result>/WEB-INF/jsp/poll/PersonToList.jsp</result>--> - <!--</action>--> - <!-- edit poll --> <action name="modification/*" class="org.chorem.pollen.ui.actions.poll.LoadPoll"> @@ -99,8 +67,7 @@ </action> <!-- save poll --> - <action name="save" - class="org.chorem.pollen.ui.actions.poll.SavePoll"> + <action name="save" class="org.chorem.pollen.ui.actions.poll.SavePoll"> <result name="input">/WEB-INF/jsp/poll/create.jsp</result> <result>/WEB-INF/jsp/poll/resume.jsp</result> </action> @@ -253,7 +220,7 @@ </action> <!-- Load in session a personList to be imported into a voting list --> - <action name="displayPersonListToVotingList" + <action name="importPersonListToVotingList" class="org.chorem.pollen.ui.actions.poll.ImportPersonListToVotingList"> <result type="json"/> </action> Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/struts.xml =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/struts.xml 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/struts.xml 2012-04-05 23:47:56 UTC (rev 3236) @@ -43,7 +43,7 @@ <constant name="struts.ui.theme" value="css_xhtml"/> <constant name="struts.multipart.maxSize" value="209715200"/> <constant name="struts.enable.SlashesInActionNames" value="true"/> - <constant name="struts.devMode" value="true"/> + <constant name="struts.devMode" value="false"/> <!--Performance tuning--> <!--see http://struts.apache.org/2.2.3/docs/performance-tuning.html--> Deleted: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/choiceHelper.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/choiceHelper.jsp 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/choiceHelper.jsp 2012-04-05 23:47:56 UTC (rev 3236) @@ -1,191 +0,0 @@ -<%-- - #%L - Pollen :: UI (strust2) - - $Id$ - $HeadURL$ - %% - Copyright (C) 2009 - 2012 CodeLutin - %% - 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% - --%> -<%@ page language="java" contentType="text/html" pageEncoding="utf-8" %> -<%@ taglib prefix="s" uri="/struts-tags" %> -<%@ taglib prefix="sj" uri="/struts-jquery-tags" %> - -<script type="text/javascript"> - - var choiceText = '<s:text name="pollen.common.choice"/>'; - - function getFirstChoice(containerId) { - var result = 65535; - - getAllChoices(containerId).each(function () { - var currentChoiceNumber = $(this).data('choiceNumber'); - if (currentChoiceNumber < result) { - result = currentChoiceNumber; - } - }); - return result; - } - - function getLastChoice(containerId) { - var result = 0; - getAllChoices(containerId).each(function () { - var currentChoiceNumber = $(this).data('choiceNumber'); - if (currentChoiceNumber > result) { - result = currentChoiceNumber; - } - }); - return result; - } - - function getAllChoices(containerId) { - return $("div[id^='" + containerId + "_']"); - } - - function updateUpdownActions(containerId) { - - var firstChoice = getFirstChoice(containerId); - var lastChoice = getLastChoice(containerId); - - getAllChoices(containerId).each(function (index) { - - var currentChoiceNumber = $(this).data('choiceNumber'); - - var labelWidget = $('#' + containerId + "_label_" + currentChoiceNumber); - labelWidget.html(choiceText + ' ' + (index+1)); - - /*var upWidget = $('#' + containerId + "_up_" + currentChoiceNumber); - if (firstChoice == currentChoiceNumber) { - // hide up - upWidget.addClass('hidden'); - } else { - //show up - upWidget.removeClass('hidden'); - } - - var downWidget = $('#' + containerId + "_down_" + currentChoiceNumber); - if (lastChoice == currentChoiceNumber) { - // hide down - downWidget.addClass('hidden'); - } else { - //show down - downWidget.removeClass('hidden'); - }*/ - }); - - } - - function loadChoice(containerId, url, choiceNumber, nbChoices, tokenId) { - if (choiceNumber == -1) { - - // to add a new choice - // get new choiceNumber - - choiceNumber = 1 + getLastChoice(containerId); - } - $.ajax( - { - url:url, - data:{ tokenId:tokenId, choiceNumber:choiceNumber }, - async:false, - dataType:"html", - success:function (data, textStatus) { - var container = $("#" + containerId); - container.append(data); - // store choiceNumber and choiceType - var choice = $("#" + containerId + "_" + choiceNumber); - choice.data('choiceType', containerId); - choice.data('choiceNumber', choiceNumber); - if (choiceNumber == nbChoices) { - - updateUpdownActions(containerId); - // end it can show it - var classes = container.attr("class"); - if (classes.indexOf("thischoice") > -1) { - // show it - container.removeClass('hidden'); - } - } - if (!tokenId) { - updateUpdownActions(containerId); - } - } - }); - } - - function addTextChoice(choiceNumber, nbChoices, tokenId) { - loadChoice( - 'choicesTEXT', - '<s:url namespace="/poll" action="displayTextChoice"/>', - choiceNumber, nbChoices, tokenId - ); - } - - function addDateChoice(choiceNumber, nbChoices, tokenId) { - loadChoice( - 'choicesDATE', - '<s:url namespace="/poll" action="displayDateChoice"/>', - choiceNumber, nbChoices, tokenId - ); - } - - function addImageChoice(choiceNumber, nbChoices, tokenId) { - loadChoice( - 'choicesIMAGE', - '<s:url namespace="/poll" action="displayImageChoice"/>', - choiceNumber, nbChoices, tokenId - ); - } - - function addNewChoice() { - var type = $('[name="poll.choiceType"][checked="checked"]').val(); - if ("TEXT" == type) { - addTextChoice(-1, -1); - } else if ("DATE" == type) { - addDateChoice(-1, -1); - } else if ("IMAGE" == type) { - addImageChoice(-1, -1); - } - return false; - } - - function deleteChoice(choiceId) { - var choice = $('#' + choiceId); - var containerId = choice.data('choiceType'); - choice.remove(); - updateUpdownActions(containerId); - return false; - } - - function upChoice(choiceId) { - var choice = $('#' + choiceId); - var containerId = choice.data('choiceType'); - var choiceNumber = choice.data('choiceNumber'); - console.info("will up [" + containerId + "-" + choiceNumber + "]"); - updateUpdownActions(containerId); - return false; - } - - function downChoice(choiceId) { - var choice = $('#' + choiceId); - var containerId = choice.data('choiceType'); - var choiceNumber = choice.data('choiceNumber'); - console.info("will down [" + containerId + "-" + choiceNumber + "]"); - updateUpdownActions(containerId); - return false; - } -</script> \ No newline at end of file Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/PersonToList.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/PersonToList.jsp 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/PersonToList.jsp 2012-04-05 23:47:56 UTC (rev 3236) @@ -24,36 +24,37 @@ <%@ page language="java" contentType="text/html" pageEncoding="utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> -<s:set name="prefix">personToList_<s:property - value="votingListNumber"/>_<s:property value="personToListNumber"/> +<s:set name="personPrefix">votingList<s:property value="votingListType"/>_<s:property value="votingListNumber"/>PersonToList_<s:property value="personToListNumber"/> </s:set> -<s:set id='deleteTitle'> +<s:set name="personToList" value="%{personToList}"/> + +<s:set id='deletePersonTitle'> <s:text name="pollen.action.pollPersonToListDelete"/> </s:set> -<s:div id='%{#prefix}'> +<s:div id='%{#personPrefix}' cssClass="personToList"> <s:fielderror/> - <s:hidden key='%{#prefix}.topiaId' value='%{personToList.topiaId}' label=''/> - <s:hidden key='%{#prefix}.accountId' value='%{personToList.pollAccount.accountId}' label=''/> + <s:hidden key='%{#personPrefix}.topiaId' value='%{#personToList.topiaId}' label=''/> + <s:hidden key='%{#personPrefix}.accountId' value='%{#personToList.pollAccount.accountId}' label=''/> <div class="fleft choiceName"> - <s:label for="%{#prefix}.votingId" id="%{#prefix}_label" theme="simple" + <s:label for="%{#personPrefix}.votingId" id="%{#personPrefix}_label" theme="simple" value=''/> - <s:textfield cssClass="nameField" id='%{#prefix}.votingId' - key="%{#prefix}.votingId" label='' theme="simple" - value="%{personToList.pollAccount.votingId}"/> + <s:textfield cssClass="nameField" id='%{#personPrefix}.votingId' + key="%{#personPrefix}.votingId" label='' theme="simple" + value="%{#personToList.pollAccount.votingId}"/> - - <s:label for="%{#prefix}.email" key="pollen.common.email" theme="simple"/> - <s:textfield cols="30" id="%{#prefix}.email" key="%{#prefix}.email" + <s:label for="%{#personPrefix}.email" key="pollen.common.email" theme="simple"/> + <s:textfield cols="30" id="%{#personPrefix}.email" key="%{#personPrefix}.email" label='' theme="simple" size="30" - value="%{personToList.pollAccount.email}"/> + value="%{#personToList.pollAccount.email}"/> - - <s:label for="%{#prefix}.weight" key="pollen.common.weight" theme="simple"/> - <s:textfield id="%{#prefix}.weight" key="%{#prefix}.weight" size="1" - label='' theme="simple" value="%{personToList.weight}"/> + <s:label for="%{#personPrefix}.weight" key="pollen.common.weight" theme="simple"/> + <s:textfield id="%{#personPrefix}.weight" key="%{#personPrefix}.weight" size="1" + label='' theme="simple" value="%{#personToList.weight}"/> </div> <div class="fright"> - <s:a href='#' onclick="return deletePersonToList('%{#prefix}')"> - <image alt='<s:property value="deleteTitle"/>' - title='<s:property value="deleteTitle"/>' + <s:a href='#' onclick="return deletePersonToList('%{#personPrefix}')"> + <image alt='<s:property value="%{#deletePersonTitle}"/>' + title='<s:property value="%{#deletePersonTitle}"/>' src="<s:url value='/img/delete.png'/>"></image> </s:a> </div> Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/VotingList.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/VotingList.jsp 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/VotingList.jsp 2012-04-05 23:47:56 UTC (rev 3236) @@ -24,67 +24,115 @@ <%@ page language="java" contentType="text/html" pageEncoding="utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> -<s:set name="prefix">votingList_<s:property value="votingListNumber"/></s:set> -<s:set id='deleteTitle'> +<s:set name="prefix">votingList<s:property value="votingListType"/>_<s:property + value="votingListNumber"/></s:set> +<s:set name="votingList" value="%{votingList}"/> +<s:set name="votingListType" value="%{votingListType}"/> + +<s:set id='deleteVotingListTitle'> <s:text name="pollen.action.pollVotingListDelete"/> </s:set> -<script type="text/javascript"> - jQuery(document).ready(function () { - // personToList loading - <s:iterator begin="1" end="nbPersonToLists" status="status"> - addPersonToList(<s:property value="votingListNumber"/>, <s:property value='%{#status.index}'/>, <s:property value="nbPersonToLists - 1"/>, '<s:property value="personToListTokenId" />'); - </s:iterator> - }); -</script> -<s:div id='votingLists_%{votingListNumber}'> +<s:div id='%{#prefix}' cssClass="votingList"> <fieldset class="ui-widget-content ui-corner-all"> <s:fielderror/> - <div class="groupPoll"> - <div class="fleft choiceName"> - <s:label for="%{#prefix}.name" theme="simple" - id="votingLists_%{votingListNumber}_label"/> - <s:textfield id='%{#prefix}.name' key="%{#prefix}.name" label='' - theme="simple" value="%{votingList.name}"/> - - - <s:label for="%{#prefix}.weight" key="pollen.common.weight" - theme="simple"/> - <s:textfield id="%{#prefix}.weight" key="%{#prefix}.weight" - value="%{votingList.weight}" - size="1" label='' theme="simple"/> - </div> - <div id='<s:property value="%{#prefix}"/>_actions' class="fright"> - <s:a href='#' - onclick="return deleteVotingList('votingLists_%{votingListNumber}')"> - <image alt='<s:property value="deleteTitle"/>' - title='<s:property value="deleteTitle"/>' - src="<s:url value='/img/delete.png'/>"></image> - </s:a> - </div> - <hr/> - </div> + <s:if test='%{#votingListType == "GROUP"}'> + <%--<div>--%> + <div class="fleft choiceName"> + <s:label for="%{#prefix}.name" theme="simple" id="%{#prefix}_label"/> + <s:textfield id='%{#prefix}.name' key="%{#prefix}.name" label='' + theme="simple" value="%{#votingList.name}"/> + - + <s:label for="%{#prefix}.weight" key="pollen.common.weight" + theme="simple"/> + <s:textfield id="%{#prefix}.weight" key="%{#prefix}.weight" + value="%{#votingList.weight}" + size="1" label='' theme="simple"/> + </div> + <div id='<s:property value="%{#prefix}"/>_actions' class="fright"> + <s:a href='#' onclick="return deleteVotingList('%{#prefix}')"> + <image alt='<s:property value="deleteVotingListTitle"/>' + title='<s:property value="deleteVotingListTitle"/>' + src="<s:url value='/img/delete.png'/>"></image> + </s:a> + </div> + <hr/> + <%--</div>--%> + </s:if> + <s:else> + <%--Readd the name even if it is empty to detect voting list--%> + <s:hidden key='%{#prefix}.name' label='' value="%{#votingList.name}"/> + <s:hidden key='%{#prefix}.weight' label='' value="%{#votingList.weight}"/> + </s:else> <div class="cleanBoth"></div> - <s:hidden key='%{#prefix}.topiaId' value='%{votingList.topiaId}' label=''/> + <s:hidden key='%{#prefix}.topiaId' value='%{#votingList.topiaId}' label=''/> - <s:div id='personToList_%{votingListNumber}'> - <%--Where to load personToList--%> + <s:div id='%{#prefix}PersonToList'> + <s:iterator value="%{#votingList.pollAccountPersonToList}" + status="pstatus" var="personToList"> + + <s:set name="personToListNumber"><s:property value="%{#pstatus.index}"/></s:set> + <s:set name="personPrefix"><s:property value="%{#prefix}"/>PersonToList_<s:property value="%{#personToListNumber}"/></s:set> + <%@include file="PersonToList.jsp" %> + + <%--<s:div id='%{#personPrefix}' cssClass="personToList">--%> + <%--<s:fielderror/>--%> + <%--<s:hidden key='%{#personPrefix}.topiaId'--%> + <%--value='%{#personToList.topiaId}' label=''/>--%> + <%--<s:hidden key='%{#personPrefix}.accountId'--%> + <%--value='%{#personToList.pollAccount.accountId}'--%> + <%--label=''/>--%> + <%--<div class="fleft choiceName">--%> + <%--<s:label for="%{#personPrefix}.votingId"--%> + <%--id="%{#personPrefix}_label" theme="simple"--%> + <%--value=''/>--%> + <%--<s:textfield cssClass="nameField"--%> + <%--id='%{#personPrefix}.votingId'--%> + <%--key="%{#personPrefix}.votingId" label=''--%> + <%--theme="simple"--%> + <%--value="%{#personToList.pollAccount.votingId}"/>--%> + <%-----%> + <%--<s:label for="%{#personPrefix}.email"--%> + <%--key="pollen.common.email" theme="simple"/>--%> + <%--<s:textfield cols="30" id="%{#personPrefix}.email"--%> + <%--key="%{#personPrefix}.email"--%> + <%--label='' theme="simple" size="30"--%> + <%--value="%{personToList.pollAccount.email}"/>--%> + <%-----%> + <%--<s:label for="%{#personPrefix}.weight"--%> + <%--key="pollen.common.weight" theme="simple"/>--%> + <%--<s:textfield id="%{#personPrefix}.weight"--%> + <%--key="%{#personPrefix}.weight" size="1"--%> + <%--label='' theme="simple"--%> + <%--value="%{#personToList.weight}"/>--%> + <%--</div>--%> + <%--<div class="fright">--%> + <%--<s:a href='#'--%> + <%--onclick="return deletePersonToList('%{#personPrefix}')">--%> + <%--<image alt='<s:property value="deletePersonTitle"/>'--%> + <%--title='<s:property value="deletePersonTitle"/>'--%> + <%--src="<s:url value='/img/delete.png'/>"></image>--%> + <%--</s:a>--%> + <%--</div>--%> + + <%--<div class="cleanBoth"></div>--%> + <%--</s:div>--%> + </s:iterator> </s:div> <hr/> <div class="cleanBoth" align="center"> <s:submit id='%{#prefix}_addPersonToList' name='%{#prefix}_addPersonToList' key="pollen.action.addPersonToList" theme="simple" - onclick='return addNewPersonToList("%{votingListNumber}");'/> + onclick='return addNewPersonToList("%{votingListType}","%{#prefix}PersonToList","%{votingListNumber}");'/> <s:if test="userLoggued"> <s:submit id='%{#prefix}_addPersonToList' theme="simple" name='%{#prefix}_addPersonToList' key="pollen.action.addPersonListFromVotingList" onclick='return selectPersonListToAddToVotingList("%{votingListNumber}");'/> </s:if> - </div> - </fieldset> <br/> </s:div> Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/create.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/create.jsp 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/create.jsp 2012-04-05 23:47:56 UTC (rev 3236) @@ -27,157 +27,38 @@ <link rel="stylesheet" type="text/css" href="<s:url value='/css/pollCreation.css'/>"/> -<%@include file="/WEB-INF/jsp/choiceHelper.jsp" %> -<%@include file="/WEB-INF/jsp/votingListHelper.jsp" %> - -<s:url id='errorImg' value='/img/exclamation.png'/> - <script type="text/javascript"> - function selectPersonListToAddToVotingList(votingListNumber) { - $('.ui-dialog-title').html('<s:text name="pollen.title.selectPersonListToAddVotingList"/>') - var dialog = $("#selectPersonListDialog"); - var url = "<s:url action='selectPersonListToAddToVotingList' namespace='/poll'/>"; - url += '?'+ $.param({votingListNumber:votingListNumber}); - dialog.load(url); - dialog.dialog('open'); - return false; - } + $(document).data( + { + selectPersonListToAddToVotingListTitle:'<s:text name="pollen.title.selectPersonListToAddVotingList"/>', + selectPersonListToCreateNewVotingListTitle:'<s:text name="pollen.title.selectPersonListToCreateVotingList"/>', - function selectPersonListToCreateNewVotingList() { - $('.ui-dialog-title').html('<s:text name="pollen.title.selectPersonListToCreateVotingList"/>') - var dialog = $("#selectPersonListDialog"); - var url = "<s:url action='selectPersonListToCreateNewVotingList' namespace='/poll'/>"; - dialog.load(url); - dialog.dialog('open'); - return false; - } + votingListText:'<s:text name="pollen.common.votingList"/>', + personToListText:'<s:text name="pollen.common.personToList"/>', + choiceText:'<s:text name="pollen.common.choice"/>', - jQuery(document).ready(function () { - $('[name="poll.choiceAddAllowed"]').change(function (event) { - var val = $(this).prop('checked'); - if (val) { - $('#addChoiceAddAllowedPanel').show(); - } else { - $('#addChoiceAddAllowedPanel').hide(); - } - }); + displayVotingListUrl:'<s:url namespace="/poll" action="displayVotingList"/>', + displayPersonToListUrl:'<s:url namespace="/poll" action="displayPersonToList"/>', + importPersonListToVotingListUrl:'<s:url namespace="/poll" action="importPersonListToVotingList"/>', + displayImageChoiceUrl:'<s:url namespace="/poll" action="displayImageChoice"/>', + displayDateChoiceUrl:'<s:url namespace="/poll" action="displayDateChoice"/>', + displayTextChoiceUrl:'<s:url namespace="/poll" action="displayTextChoice"/>', + selectPersonListToAddToVotingListUrl:'<s:url action="selectPersonListToAddToVotingList" namespace="/poll"/>', + selectPersonListToCreateNewVotingListUrl:'<s:url action="selectPersonListToCreateNewVotingList" namespace="/poll"/>', - $('[name="limitChoice"]').change(function (event) { - var val = $(this).prop('checked'); - if (val) { - $('#maxChoiceNbPanel').show(); - } else { - $('#maxChoiceNbPanel').hide(); - } - }); - - $('[name="reminder"]').change(function (event) { - var val = $(this).prop('checked'); - if (val) { - $('#reminderPanel').show(); - } else { - $('#reminderPanel').hide(); - } - }); - - $('[name="poll.anonymous"]').change(function (event) { - var val = $(this).prop('checked'); - $('[name="poll.anonymousVoteAllowed"]').prop('checked', val); - }); - $('[name="poll.continuousResults"]').change(function (event) { - var val = $(this).prop('checked'); - $('[name="poll.publicResults"]').prop('checked', val); - }); - - function changeChoiceType(type) { - $('.choices').addClass('hidden'); - $('.choices').removeClass('thischoice'); - $('#choices' + type).removeClass('hidden'); - $('#choices' + type).addClass('thischoice'); - $('[name="poll.choiceType"]').attr('checked', false); - $('[name="poll.choiceType"][value="' + type + '"]').attr('checked', 'checked'); + voteCountingType:'<s:property value="%{poll.voteCountingType.name()}"/>', + choiceType:'<s:property value="%{poll.choiceType.name()}"/>', + pollType:'<s:property value="%{poll.pollType.name()}"/>' } + ); +</script> - function changePollType(type, loading) { +<script type="text/javascript" + src='<s:url value="/js/createPoll.js"/>'></script> - if (!loading) { +<s:url id='errorImg' value='/img/exclamation.png'/> - // remove extra votingLists - var containerId = "votingLists"; - var lastVotingList = getLastVotingList(containerId); - while (lastVotingList > -1) { - deleteVotingList(containerId + "_" + lastVotingList); - lastVotingList -= 1; - } - } - - if (type == 'FREE') { - - // hide everything - $('.restrictedPoll').addClass("hidden"); - $('.groupPoll').addClass("hidden"); - - } else if (type == 'RESTRICTED') { - - if (!loading) { - addNewVotingList(containerId); - } - - // show restricted sutff - $('.restrictedPoll').removeClass("hidden"); - // remove all group stuff - $('.groupPoll').addClass("hidden"); - - } else if (type == 'GROUP') { - - if (!loading) { - addNewVotingList(containerId); - } - - $('.restrictedPoll').removeClass("hidden"); - $('.groupPoll').removeClass("hidden"); - } - } - - $('[name="poll.choiceType"]').change(function (event) { - changeChoiceType($(this).val()); - }); - - $('[name="poll.pollType"]').change(function (event) { - changePollType($(this).val(), false); - }); - - $('[name="poll.voteCountingType"][value="<s:property value="%{poll.voteCountingType.name()}"/>"]').attr('checked', 'checked'); - - changeChoiceType('<s:property value="%{poll.choiceType.name()}"/>'); - - // before loading all choices let's hide everything, it will be shown later - $('.choices').addClass('hidden'); - - // text choices loading - <s:iterator begin="1" end="nbTextChoices" status="status"> - addTextChoice(<s:property value='%{#status.index}'/>, <s:property value="nbTextChoices - 1"/>, '<s:property value="textChoiceTokenId" />'); - </s:iterator> - - // date choices loading - <s:iterator begin="1" end="nbDateChoices" status="status"> - addDateChoice(<s:property value='%{#status.index}'/>, <s:property value="nbDateChoices - 1"/>, '<s:property value="dateChoiceTokenId" />'); - </s:iterator> - - // image choices loading - <s:iterator begin="1" end="nbImageChoices" status="status"> - addImageChoice(<s:property value='%{#status.index}'/>, <s:property value="nbImageChoices - 1"/>, '<s:property value="imageChoiceTokenId" />'); - </s:iterator> - - // votingLists loading - <s:iterator begin="1" end="nbVotingLists" status="status"> - addVotingList(<s:property value='%{#status.index}'/>, <s:property value="nbVotingLists - 1"/>, '<s:property value="votingListTokenId" />'); - </s:iterator> - - changePollType('<s:property value="%{poll.pollType.name()}"/>', true); - }); -</script> <title><s:text name="pollen.title.createPoll"/></title> <h1 class="title${pageLogo}"><s:text name="pollen.title.createPoll"/></h1> @@ -185,7 +66,7 @@ <s:form method="POST" id="registerForm" namespace="/poll" enctype="multipart/form-data"> - <s:hidden key="poll.topiaId"/> + <s:hidden key="poll.topiaId" label=""/> <sj:tabbedpanel id="formTabs" selectedTab="%{selectedTab}"> @@ -201,7 +82,7 @@ required="true" size="80"/> <s:textarea key="poll.description" rows="3" cols="54" label="%{getText('pollen.common.description')}"/> - <s:hidden key="poll.pollId"/> + <s:hidden key="poll.pollId" label=""/> </fieldset> <br/> @@ -213,27 +94,8 @@ </fieldset> <br/> - <fieldset> - <legend><s:text name="pollen.fieldset.poll.choices"/></legend> - <s:radio key='poll.choiceType' list="choiceTypes" label='' - theme="simple" disabled="%{voteStarted}"/> - <hr/> - <s:fielderror fieldName="poll.choices"/> + <%@include file="createPoll_choices.jsp" %> - <div id="choicesTEXT" class="choices"> - <%--Where to load text choices --%> - </div> - <div id="choicesDATE" class="choices"> - <%--Where to load date choices --%> - </div> - <div id="choicesIMAGE" class="choices"> - <%--Where to load image choices --%> - </div> - <hr/> - <s:submit key="pollen.action.addChoice" align="center" - onclick="return addNewChoice();" disabled="%{voteStarted}"/> - </fieldset> - </div> <div id="toptions"> @@ -244,8 +106,8 @@ label="%{getText('pollen.common.login')}"/> <s:textfield key="poll.creator.email" size="80" label="%{getText('pollen.common.email')}"/> - <s:hidden key="poll.creator.accountId"/> - <s:hidden key="poll.creator.topiaId"/> + <s:hidden key="poll.creator.accountId" label=""/> + <s:hidden key="poll.creator.topiaId" label=""/> <s:if test="creatorUserAccountDefined"> <s:hidden name="userId" value="%{poll.creator.userAccount.topiaId}"/> </s:if> @@ -257,15 +119,18 @@ <legend><s:text name="pollen.fieldset.poll.options"/></legend> <s:checkbox key="poll.anonymous" - label="%{getText('pollen.common.pollOption.anonymous')}" disabled="%{voteStarted}"/> + label="%{getText('pollen.common.pollOption.anonymous')}" + disabled="%{voteStarted}"/> <s:checkbox key="poll.anonymousVoteAllowed" - label="%{getText('pollen.common.pollOption.anonymousVoteAllowed')}" disabled="%{voteStarted}"/> + label="%{getText('pollen.common.pollOption.anonymousVoteAllowed')}" + disabled="%{voteStarted}"/> <s:checkbox key="poll.continuousResults" label="%{getText('pollen.common.pollOption.continuousResults')}"/> <s:checkbox key="poll.publicResults" label="%{getText('pollen.common.pollOption.publicResults')}"/> <s:checkbox key="poll.choiceAddAllowed" - label="%{getText('pollen.common.pollOption.choiceAddAllowed')}" disabled="%{voteStarted}"/> + label="%{getText('pollen.common.pollOption.choiceAddAllowed')}" + disabled="%{voteStarted}"/> <div id='addChoiceAddAllowedPanel' class="hidden"> <sj:datepicker key="poll.beginChoiceDate" label="%{getText('pollen.common.beginChoiceDate')}" @@ -275,7 +140,8 @@ displayFormat="dd/mm/yy"/> </div> <s:checkbox key="limitChoice" - label="%{getText('pollen.common.pollOption.limitChoice')}" disabled="%{voteStarted}"/> + label="%{getText('pollen.common.pollOption.limitChoice')}" + disabled="%{voteStarted}"/> <div id='maxChoiceNbPanel' class="hidden"> <s:textfield key="poll.maxChoiceNb" label="%{getText('pollen.common.pollOption.maxChoiceNb')}"/> @@ -295,28 +161,52 @@ <legend><s:text name="pollen.fieldset.poll.general"/></legend> <sj:datepicker key="poll.beginDate" displayFormat="dd/mm/yy" - label="%{getText('pollen.common.beginDate')}" disabled="%{voteStarted}"/> + label="%{getText('pollen.common.beginDate')}" + disabled="%{voteStarted}"/> <sj:datepicker key="poll.endDate" displayFormat="dd/mm/yy" label="%{getText('pollen.common.endDate')}"/> </fieldset> <br/> + + <%--<%@include file="createPoll_votingLists.jsp" %>--%> + <fieldset> <legend><s:text name="pollen.common.pollType"/></legend> <s:radio key='poll.pollType' list="pollTypes" - label='%{getText("pollen.common.pollType")}' disabled="%{voteStarted}"/> + label='%{getText("pollen.common.pollType")}' + disabled="%{voteStarted}"/> <hr/> - <div class="restrictedPoll"> - <div id="votingLists"> - <%--Where to load voting lists --%> + + <div id="pollTypeFREE" class="pollType"> + <%--Nothing to show for a free poll--%> + </div> + + <div id="pollTypeRESTRICTED" class="pollType"> + <s:iterator value="restrictedVotingList" status="status" var="votingList"> + <s:set name="votingListNumber"><s:property value="%{#status.index}"/></s:set> + <s:set name="votingListType">RESTRICTED</s:set> + <%@include file="VotingList.jsp" %> + </s:iterator> + </div> + + <div id="pollTypeGROUP" class="pollType"> + <div id="votingListGROUP"> + <s:iterator value="groupVotingList" status="status" var="votingList"> + <s:set name="votingListNumber"><s:property value="%{#status.index}"/></s:set> + <s:set name="votingListType">GROUP</s:set> + <%@include file="VotingList.jsp" %> + </s:iterator> </div> - <div class="groupPoll" align="center"> + <div align="center"> <s:submit key="pollen.action.addVotingList" theme="simple" - onclick="return addNewVotingList();" disabled="%{voteStarted}"/> + onclick="return addNewVotingList();" + disabled="%{voteStarted}"/> <s:if test="userLoggued"> <s:submit key="pollen.action.addVotingListFromPersonList" theme="simple" - onclick="return selectPersonListToCreateNewVotingList();" disabled="%{voteStarted}"/> + onclick="return selectPersonListToCreateNewVotingList();" + disabled="%{voteStarted}"/> </s:if> </div> </div> @@ -326,7 +216,7 @@ </sj:tabbedpanel> <br/> - <s:submit action="save" key="pollen.action.savePoll" value="%{actionLabel}" align="center"/> + <s:submit action="save" value="%{actionLabel}" align="center"/> </s:form> <script type="text/javascript"> Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp 2012-04-05 23:47:56 UTC (rev 3236) @@ -0,0 +1,188 @@ +<%-- + #%L + Pollen :: UI (strust2) + + $Id$ + $HeadURL$ + %% + Copyright (C) 2009 - 2012 CodeLutin + %% + 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% + --%> +<%@ page language="java" contentType="text/html" pageEncoding="utf-8" %> +<%@ taglib prefix="s" uri="/struts-tags" %> +<%@ taglib prefix="sj" uri="/struts-jquery-tags" %> +<fieldset> + <legend><s:text name="pollen.fieldset.poll.choices"/></legend> + <s:radio key='poll.choiceType' list="choiceTypes" label='' + theme="simple" disabled="%{voteStarted}"/> + <hr/> + <s:fielderror fieldName="poll.choices"/> + + <s:set id='deleteTitle'><s:text name="pollen.action.pollChoiceDelete"/></s:set> + <s:set id='upTitle'><s:text name="pollen.action.pollChoiceUp"/></s:set> + <s:set id='downTitle'><s:text name="pollen.action.pollChoiceDown"/></s:set> + + <div id="choicesTEXT" class="choices"> + <s:iterator value="textChoices" status="status" var="choice"> + <s:set name="choiceNumber"><s:property value="%{#status.index}"/></s:set> + <s:set name="prefix">textChoice_<s:property value="%{#choiceNumber}"/></s:set> + <div id='choicesTEXT_<s:property value="choiceNumber"/>'> + <s:hidden key='%{#prefix}.topiaId' value='%{choice.topiaId}' label=''/> + <div class="fleft choiceName"> + <s:label for="%{#prefix}.name" id="choicesTEXT_label_%{#choiceNumber}" + theme="simple" value=''/> + <s:textfield cssClass="nameField" id='%{#prefix}.name' key="%{#prefix}.name" + label='' theme="simple" value="%{#choice.name}"/> + - + <s:label for="%{#prefix}.description" key="pollen.common.description" + theme="simple"/> + <%--/div> + <div class="fleft"--%> + <s:textarea cols="30" id="%{#prefix}.description" label='' theme="simple" + key="%{#prefix}.description" value="%{#choice.description}"/> + </div> + <div class="fright"> + <s:a href='#' onclick="return deleteChoice('choicesTEXT_%{#choiceNumber}')"> + <image alt='<s:property value="deleteTitle"/>' + title='<s:property value="deleteTitle"/>' + src="<s:url value='/img/delete.png'/>"></image> + </s:a> + <%--s:a id='choicesTEXT_down_%{choiceNumber}' + cssClass="hidden" href='#' + onclick="return downChoice('choicesTEXT_%{choiceNumber}')"> + <image alt='<s:property value="downTitle"/>' + title='<s:property value="downTitle"/>' + src="<s:url value='/img/1downarrow.png'/>"></image> + </s:a> + <s:a id='choicesTEXT_up_%{choiceNumber}' href='#' cssClass="hidden" + onclick="return upChoice('choicesTEXT_%{choiceNumber}')"> + <image alt='<s:property value="upTitle"/>' + title='<s:property value="upTitle"/>' + src="<s:url value='/img/1uparrow.png'/>"></image> + </s:a--%> + </div> + <div class="cleanBoth"></div> + </div> + </s:iterator> + </div> + <div id="choicesDATE" class="choices"> + <s:iterator value="dateChoices" status="status" var="choice"> + <s:set name="choiceNumber"><s:property value="%{#status.index}"/></s:set> + <s:set name="prefix">dateChoice_<s:property value="%{#choiceNumber}"/></s:set> + <div id='choicesDATE_<s:property value="choiceNumber"/>'> + <s:hidden key='%{#prefix}.topiaId' id='%{#prefix}.topiaId' + value='%{choice.topiaId}' label='' theme="simple" /> + <div class="fleft choiceName"> + <s:label for="%{#prefix}.name" id="choicesDATE_label_%{choiceNumber}" + theme="simple" value=''/> + <sj:datepicker id='%{#prefix}.name' key="%{#prefix}.name" changeMonth="true" + changeYear="true" labelSeparator="" theme="simple" label="" + timepicker="true" value="%{choice.date}" displayFormat="dd/mm/yy"/> + - + <s:label for="%{#prefix}.description" key="pollen.common.description" + theme="simple"/> + </div> + <div class="fleft"> + <s:textarea cols="30" id="%{#prefix}.description" + key="%{#prefix}.description" label='' theme="simple" + value="%{choice.description}"/> + </div> + <div class="fright"> + <s:a href='#' onclick="return deleteChoice('choicesDATE_%{choiceNumber}')"> + <image alt='<s:property value="deleteTitle"/>' + title='<s:property value="deleteTitle"/>' + src="<s:url value='/img/delete.png'/>"></image> + </s:a> + <%--s:a id='choicesDATE_down_%{choiceNumber}' + cssClass="hidden" href='#' + onclick="return downChoice('choicesDATE_%{choiceNumber}')"> + <image alt='<s:property value="downTitle"/>' + title='<s:property value="downTitle"/>' + src="<s:url value='/img/1downarrow.png'/>"></image> + </s:a> + <s:a id='choicesDATE_up_%{choiceNumber}' href='#' cssClass="hidden" + onclick="return upChoice('choicesDATE_%{choiceNumber}')"> + <image alt='<s:property value="upTitle"/>' + title='<s:property value="upTitle"/>' + src="<s:url value='/img/1uparrow.png'/>"></image> + </s:a--%> + </div> + <div class="cleanBoth"></div> + </div> + </s:iterator> + </div> + <div id="choicesIMAGE" class="choices"> + <s:iterator value="imageChoices" status="status" var="choice"> + <s:set name="choiceNumber"><s:property value="%{#status.index}"/></s:set> + <s:set name="prefix">imageChoice_<s:property value="%{#choiceNumber}"/></s:set> + <div id='choicesIMAGE_<s:property value="choiceNumber"/>'> + <s:hidden key='%{#prefix}.topiaId' value='%{choice.topiaId}' label=''/> + <div class="fleft choiceName"> + <s:label for="%{#prefix}.name" id="choicesIMAGE_label_%{choiceNumber}" + theme="simple" value=''/> + <s:if test="choice.name != ''"> + + <%--Uploaded image--%> + <s:hidden id="%{#prefix}.name" name="%{#prefix}.name" + value="%{choice.name}" label='' theme="simple"/> + + <s:hidden id="%{#prefix}.location" name="%{#prefix}.location" + value="%{choice.location}" label='' theme="simple"/> + + <s:label label='' theme="simple" cssClass="nameField" + value="%{choice.name}" readonly="true"/> + </s:if> + <s:else> + <%--New image--%> + <s:file key='%{#prefix2}' label='' theme="simple" cssClass="nameField"/> + </s:else> + - + <s:label for="%{#prefix}.description" key="pollen.common.description" + theme="simple"/> + </div> + <div class="fleft"> + <s:textarea cols="30" id="%{#prefix}.description" label='' theme="simple" + key="%{#prefix}.description" value="%{choice.description}"/> + </div> + <div class="fright"> + <s:a href='#' onclick="return deleteChoice('choicesIMAGE_%{choiceNumber}')"> + <image alt='<s:property value="deleteTitle"/>' + title='<s:property value="deleteTitle"/>' + src="<s:url value='/img/delete.png'/>"></image> + </s:a> + <%--s:a id='choicesIMAGE_down_%{choiceNumber}' + cssClass="hidden" href='#' + onclick="return downChoice('choicesIMAGE_%{choiceNumber}')"> + <image alt='<s:property value="downTitle"/>' + title='<s:property value="downTitle"/>' + src="<s:url value='/img/1downarrow.png'/>"></image> + </s:a> + <s:a id='choicesIMAGE_up_%{choiceNumber}' href='#' cssClass="hidden" + onclick="return upChoice('choicesIMAGE_%{choiceNumber}')"> + <image alt='<s:property value="upTitle"/>' + title='<s:property value="upTitle"/>' + src="<s:url value='/img/1uparrow.png'/>"></image> + </s:a--%> + </div> + + <div class="cleanBoth"></div> + </div> + </s:iterator> + </div> + <hr/> + <s:submit key="pollen.action.addChoice" align="center" + onclick="return addNewChoice();" disabled="%{voteStarted}"/> +</fieldset> \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/votingListHelper.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/votingListHelper.jsp 2012-04-05 07:50:52 UTC (rev 3235) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/votingListHelper.jsp 2012-04-05 23:47:56 UTC (rev 3236) @@ -1,256 +0,0 @@ -<%-- - #%L - Pollen :: UI (strust2) - - $Id$ - $HeadURL$ - %% - Copyright (C) 2009 - 2012 CodeLutin - %% - 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% - --%> -<%@ page language="java" contentType="text/html" pageEncoding="utf-8" %> -<%@ taglib prefix="s" uri="/struts-tags" %> -<%@ taglib prefix="sj" uri="/struts-jquery-tags" %> - -<script type="text/javascript"> - -var votingListText = '<s:text name="pollen.common.votingList"/>'; -var personToListText = '<s:text name="pollen.common.personToList"/>'; - -function getFirstVotingList(containerId) { - var result = 65535; - getAllVotingLists(containerId).each(function () { - var current = $(this).data('votingListNumber'); - if (current < result) { - result = current; - } - }); - return result; -} - -function getLastVotingList(containerId) { - var result = 0; - getAllVotingLists(containerId).each(function () { - var current = $(this).data('votingListNumber'); - if (current > result) { - result = current; - } - }); - return result; -} - -function getAllVotingLists(containerId) { - return $("div[id^='" + containerId + "_']"); -} - -function updateUpdownVotingListActions(containerId) { - getAllVotingLists(containerId).each(function (index) { - var currentVotingListNumber = $(this).data('votingListNumber'); - var labelWidget = $('#' + containerId + "_" + currentVotingListNumber + "_label"); - labelWidget.html(votingListText + ' ' + (index + 1)); - }); -} - -function loadVotingList(containerId, votingListNumber, nbVotingLists, tokenId) { - if (votingListNumber == -1) { - // to add a new votingList, get the new available votingListNumber - votingListNumber = 1 + getLastVotingList(containerId); - } - $.ajax( - { - url:'<s:url namespace="/poll" action="displayVotingList"/>', - data:{ tokenId:tokenId, votingListNumber:votingListNumber }, - async:false, - dataType:"html", - success:function (data, textStatus) { - var container = $("#" + containerId); - container.append(data); - // store votingListNumber - var votingList = $("#" + containerId + "_" + votingListNumber); - votingList.data('votingListNumber', votingListNumber); - votingList.data('votingListContainerId', containerId); - if (votingListNumber == nbVotingLists || !tokenId) { - updateUpdownVotingListActions(containerId); - } - } - }); -} - -function addVotingList(votingListNumber, nbVotingLists, tokenId) { - loadVotingList('votingLists', votingListNumber, nbVotingLists, tokenId); -} - -function addNewVotingList() { - addVotingList(-1, -1); - return false; -} - -function deleteVotingList(votingListId) { - var votingList = $('#' + votingListId); - var containerId = votingList.data('votingListContainerId'); - votingList.remove(); - updateUpdownVotingListActions(containerId); - return false; -} - -function getFirstPersonToList(containerId) { - var result = 65535; - getAllPersonToLists(containerId).each(function () { - var current = $(this).data('personToListNumber'); - if (current < result) { - result = current; - } - }); - return result; -} - -function getLastPersonToList(containerId) { - var result = 0; - getAllPersonToLists(containerId).each(function () { - var current = $(this).data('personToListNumber'); - if (current > result) { - result = current; - } - }); - return result; -} - -function getAllPersonToLists(containerId) { - return $("div[id^='" + containerId + "_']"); -} - -function getAllPersonToListNumbers(containerId) { - var result = []; - getAllPersonToLists(containerId).each(function () { - var current = $(this).data('personToListNumber'); - result.push(current); - }); - return result; -} - -function getFirstEmptyPersonToList(containerId) { - var result = -1; - var numbers = getAllPersonToListNumbers(containerId); - for (var n in numbers) { - var nameInput = $('[name="' + containerId + '_' + n + '.votingId"]'); - var emailInput = $('[name="' + containerId + '_' + n + '.email"]'); - - var name = nameInput.val(); - var email = emailInput.val(); - if (name == '' && email == '') { - result = n; - break; - } - } - return result; -} - -function updateUpdownPersonToListActions(containerId) { - getAllPersonToLists(containerId).each(function (index) { - var currentPersonToListNumber = $(this).data('personToListNumber'); - var labelWidget = $('#' + containerId + "_" + currentPersonToListNumber + "_label"); - labelWidget.html(personToListText + ' ' + (index + 1)); - }); -} - -function loadPersonToList(containerId, votingListNumber, personToListNumber, nbPersonToLists, tokenId) { - if (personToListNumber == -1) { - // to add a new personToList, get the new available personToListNumber - personToListNumber = 1 + getLastPersonToList(containerId); - } - $.ajax( - { - url:'<s:url namespace="/poll" action="displayPersonToList"/>', - data:{ - tokenId:tokenId, - votingListNumber:votingListNumber, - personToListNumber:personToListNumber - }, - async:false, - dataType:"html", - success:function (data, textStatus) { - var container = $("#" + containerId); - container.append(data); - // store personToListNumber - var personToList = $("#" + containerId + "_" + personToListNumber); - personToList.data('votingListNumber', votingListNumber); - personToList.data('personToListNumber', personToListNumber); - personToList.data('personToListContainerId', containerId); - if (personToListNumber == nbPersonToLists || !tokenId) { - updateUpdownPersonToListActions(containerId); - } - } - }); -} - -function addPersonToList(votingListNumber, personToListNumber, nbPersonToLists, tokenId) { - loadPersonToList( - 'personToList_' + votingListNumber, - votingListNumber, personToListNumber, nbPersonToLists, tokenId - ); -} - -function addNewPersonToList(votingListNumber) { - addPersonToList(votingListNumber, -1, -1); - return false; -} - -function deletePersonToList(personToListId) { - var personToList = $('#' + personToListId); - var containerId = personToList.data('personToListContainerId'); - personToList.remove(); - updateUpdownPersonToListActions(containerId); - return false; -} - - -function loadPersonListToVotingList(votingListNumber, personToListNumber, personListId) { - - $.ajax( - { - url:'<s:url namespace="/poll" action="displayPersonListToVotingList"/>', - data:{ - votingListNumber:votingListNumber, - personToListNumber:personToListNumber, - personListId:personListId }, - async:false, - dataType:"json", - success:function (data, textStatus) { - var token = data.tokenId; - var nbPersonToLists = parseInt(data.nbPersonToLists); - var personToListNumber = parseInt(data.personToListNumber); - var maxPersonToListNumber = parseInt(personToListNumber) + nbPersonToLists - 1; - console.info(">> token = " + token); - console.info(">> personListId = " + personListId); - console.info(">> incoming votingListNumber = " + votingListNumber); - console.info(">> nb personToList to import = " + nbPersonToLists); - console.info(">> first personToListNumber = " + personToListNumber); - console.info(">> last PersonToListNumber = " + maxPersonToListNumber); - - var index = personToListNumber; - while (index <= maxPersonToListNumber) { - console.info("Will load personToList = " + index + " / " + maxPersonToListNumber); - addPersonToList(votingListNumber, - index, - maxPersonToListNumber, - token - ); - index += 1; - } - } - }); -} -</script> \ No newline at end of file Added: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/js/createPoll.js =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/js/createPoll.js (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/js/createPoll.js 2012-04-05 23:47:56 UTC (rev 3236) @@ -0,0 +1,591 @@ +/* + * #%L + * Pollen :: UI (strust2) + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +var datas = $(document).data(); + +function selectPersonListToAddToVotingList(votingListNumber) { + $('.ui-dialog-title').html(datas['selectPersonListToAddToVotingListTitle']); + var dialog = $("#selectPersonListDialog"); + var url = datas['selectPersonListToAddToVotingListUrl']; + url += '?' + $.param({votingListNumber:votingListNumber}); + dialog.load(url); + dialog.dialog('open'); + return false; +} + +function selectPersonListToCreateNewVotingList() { + $('.ui-dialog-title').html(datas['selectPersonListToCreateNewVotingListTitle']); + var dialog = $("#selectPersonListDialog"); + var url = datas['selectPersonListToCreateNewVotingListUrl']; + dialog.load(url); + dialog.dialog('open'); + return false; +} + + +//---------------------------------------------------------------------------- +//-- Voting List methods +//---------------------------------------------------------------------------- + +/*function getFirstVotingList(containerId) { + var result = 65535; + getAllVotingLists(containerId).each(function () { + var current = $(this).data('votingListNumber'); + if (current < result) { + result = current; + } + }); + return result; + }*/ + +function getLastVotingList(containerId) { + var result = 0; + getAllVotingLists(containerId).each(function () { + var current = $(this).data('votingListNumber'); + if (current > result) { + result = current; + } + }); + return result; +} + +function getAllVotingLists(containerId) { + return $("div[id^='" + containerId + "_'].votingList"); +} + +function prepareVotingLists(containerId) { + + getAllVotingLists(containerId).each(function (index) { + + var votingListId = containerId + "_" + index; + var votingList = $("#" + votingListId); + votingList.data('votingListNumber', index); + votingList.data('votingListContainerId', containerId); + + preparePersonToLists(index, votingListId + 'PersonToList'); + + }); + updateUpdownVotingListActions(containerId); +} + +function updateUpdownVotingListActions(containerId) { + getAllVotingLists(containerId).each(function (index) { + var currentVotingListNumber = $(this).data('votingListNumber'); + var labelWidget = $('#' + containerId + "_" + currentVotingListNumber + "_label"); + labelWidget.html(datas['votingListText'] + ' ' + (index + 1)); + }); +} + +function addNewVotingList() { + + var containerId = 'votingListGROUP'; + var votingListNumber = 1 + getLastVotingList(containerId); + + $.ajax({ + url:datas['displayVotingListUrl'], + data:{ + votingListType:'GROUP', + votingListNumber:votingListNumber + }, + async:false, + dataType:"html", + success:function (data) { + var container = $("#" + containerId); + container.append(data); + // store votingListNumber + var votingListId = containerId + "_" + votingListNumber; + var votingList = $("#" + votingListId); + votingList.data('votingListNumber', votingListNumber); + votingList.data('votingListContainerId', containerId); + preparePersonToLists(votingListNumber, votingListId + 'PersonToList'); + updateUpdownVotingListActions(containerId); + } + }); + return false; +} + +function deleteVotingList(votingListId) { + var votingList = $('#' + votingListId); + var containerId = votingList.data('votingListContainerId'); + votingList.remove(); + updateUpdownVotingListActions(containerId); + return false; +} + + +//---------------------------------------------------------------------------- +//-- PersonToList methods +//---------------------------------------------------------------------------- + +/*function getFirstPersonToList(containerId) { + var result = 65535; + getAllPersonToLists(containerId).each(function () { + var current = $(this).data('personToListNumber'); + if (current < result) { + result = current; + } + }); + return result; + }*/ + +function getLastPersonToList(containerId) { + var result = 0; + getAllPersonToLists(containerId).each(function () { + var current = $(this).data('personToListNumber'); + if (current > result) { + result = current; + } + }); + return result; +} + +function getAllPersonToLists(containerId) { + return $("div[id^='" + containerId + "_'].personToList"); +} + +function getAllPersonToListNumbers(containerId) { + var result = []; + getAllPersonToLists(containerId).each(function () { + var current = $(this).data('personToListNumber'); + result.push(current); + }); + return result; +} + +/*function getFirstEmptyPersonToList(containerId) { + var result = -1; + var numbers = getAllPersonToListNumbers(containerId); + for (var n in numbers) { + var nameInput = $('[name="' + containerId + '_' + n + '.votingId"]'); + var emailInput = $('[name="' + containerId + '_' + n + '.email"]'); + + var name = nameInput.val(); + var email = emailInput.val(); + if (name == '' && email == '') { + result = n; + break; + } + } + return result; + }*/ + +function preparePersonToLists(votingListNumber, containerId) { + getAllPersonToLists(containerId).each(function (index) { + + var personToListId = containerId + "_" + index; + var personToList = $("#" + personToListId); + + personToList.data('votingListNumber', votingListNumber); + personToList.data('personToListNumber', index); + personToList.data('personToListContainerId', containerId); + }); + updateUpdownPersonToListActions(containerId); +} +function updateUpdownPersonToListActions(containerId) { + getAllPersonToLists(containerId).each(function (index) { + var currentPersonToListNumber = $(this).data('personToListNumber'); + var labelWidget = $('#' + containerId + "_" + currentPersonToListNumber + "_label"); + labelWidget.html(datas['personToListText'] + ' ' + (index + 1)); + }); +} + +function addNewPersonToList(votingListType, containerId, votingListNumber) { + + var personToListNumber = 1 + getLastPersonToList(containerId); + $.ajax({ + url:datas['displayPersonToListUrl'], + data:{ + votingListNumber:votingListNumber, + votingListType:votingListType, + personToListNumber:personToListNumber + }, + async:false, + dataType:"html", + success:function (data) { + var container = $("#" + containerId); + container.append(data); + // store personToListNumber + var personToList = $("#" + containerId + "_" + personToListNumber); + personToList.data('votingListNumber', votingListNumber); + personToList.data('personToListNumber', personToListNumber); + personToList.data('personToListContainerId', containerId); + updateUpdownPersonToListActions(containerId); + } + }); + return false; +} + +function addPersonToList(votingListType, containerId, votingListNumber, personToListNumber, nbPersonToLists, tokenId) { + loadPersonToList( + votingListType, + containerId, + votingListNumber, personToListNumber, nbPersonToLists, tokenId + ); +} + +function loadPersonToList(votingListType, containerId, votingListNumber, personToListNumber, nbPersonToLists, tokenId) { + if (personToListNumber == -1) { + // to add a new personToList, get the new available personToListNumber + personToListNumber = 1 + getLastPersonToList(containerId); + } + $.ajax( + { + url:datas['displayPersonToListUrl'], + data:{ + tokenId:tokenId, + votingListNumber:votingListNumber, + votingListType:votingListType, + personToListNumber:personToListNumber + }, + async:false, + dataType:"html", + success:function (data) { + var container = $("#" + containerId); + container.append(data); + // store personToListNumber + var personToList = $("#" + containerId + "_" + personToListNumber); + personToList.data('votingListNumber', votingListNumber); + personToList.data('personToListNumber', personToListNumber); + personToList.data('personToListContainerId', containerId); + if (personToListNumber == nbPersonToLists || !tokenId) { + updateUpdownPersonToListActions(containerId); + } + } + }); +} + +function deletePersonToList(personToListId) { + var personToList = $('#' + personToListId); + var containerId = personToList.data('personToListContainerId'); + personToList.remove(); + updateUpdownPersonToListActions(containerId); + return false; +} + +function loadPersonListToVotingList(votingListNumber, personToListNumber, personListId) { + + $.ajax( + { + url:datas['importPersonListToVotingListUrl'], + data:{ + votingListNumber:votingListNumber, + personToListNumber:personToListNumber, + personListId:personListId }, + async:false, + dataType:"json", + success:function (data) { + var token = data.tokenId; + var nbPersonToLists = parseInt(data['nbPersonToLists']); + var personToListNumber = parseInt(data.personToListNumber); + var maxPersonToListNumber = parseInt(personToListNumber) + nbPersonToLists - 1; + console.info(">> token = " + token); + console.info(">> personListId = " + personListId); + console.info(">> incoming votingListNumber = " + votingListNumber); + console.info(">> nb personToList to import = " + nbPersonToLists); + console.info(">> first personToListNumber = " + personToListNumber); + console.info(">> last PersonToListNumber = " + maxPersonToListNumber); + + var index = personToListNumber; + while (index <= maxPersonToListNumber) { + console.info("Will load personToList = " + index + " / " + maxPersonToListNumber); + addPersonToList(votingListNumber, + index, + maxPersonToListNumber, + token + ); + index += 1; + } + } + }); +} + + +//---------------------------------------------------------------------------- +//-- Choice methods +//---------------------------------------------------------------------------- + +/*function getFirstChoice(containerId) { + var result = 65535; + + getAllChoices(containerId).each(function () { + var currentChoiceNumber = $(this).data('choiceNumber'); + if (currentChoiceNumber < result) { + result = currentChoiceNumber; + } + }); + return result; + }*/ + +function getLastChoice(containerId) { + var result = 0; + getAllChoices(containerId).each(function () { + var currentChoiceNumber = $(this).data('choiceNumber'); + if (currentChoiceNumber > result) { + result = currentChoiceNumber; + } + }); + return result; +} + +function getAllChoices(containerId) { + return $("div[id^='" + containerId + "_']"); +} + +function prepareChoices(containerId) { + + getAllChoices(containerId).each(function (index) { + + var choice = $("#" + containerId + "_" + index); + choice.data('choiceType', containerId); + choice.data('choiceNumber', index); + }); + + if (containerId == 'choicesDATE') { + + getAllChoices(containerId).each(function (index) { + var choice = $("#" + containerId + "_" + index + " script"); + $('[id="wwctrl_dateChoice_' + index + '.name"] input').detach().insertBefore(choice); + $('[id="wwctrl_dateChoice_' + index + '.name"] button').detach().insertBefore(choice); + + }); + + $('#choicesDATE .wwgrp').remove(); + } + updateUpdownActions(containerId); +} + +function updateUpdownActions(containerId) { + +// var firstChoice = getFirstChoice(containerId); +// var lastChoice = getLastChoice(containerId); + + getAllChoices(containerId).each(function (index) { + + var currentChoiceNumber = $(this).data('choiceNumber'); + + var labelWidget = $('#' + containerId + "_label_" + currentChoiceNumber); + labelWidget.html(datas['choiceText'] + ' ' + (index + 1)); + + /*var upWidget = $('#' + containerId + "_up_" + currentChoiceNumber); + if (firstChoice == currentChoiceNumber) { + // hide up + upWidget.addClass('hidden'); + } else { + //show up + upWidget.removeClass('hidden'); + } + + var downWidget = $('#' + containerId + "_down_" + currentChoiceNumber); + if (lastChoice == currentChoiceNumber) { + // hide down + downWidget.addClass('hidden'); + } else { + //show down + downWidget.removeClass('hidden'); + }*/ + }); + +} + +function loadChoice(containerId, url, choiceNumber, nbChoices, tokenId) { + if (choiceNumber == -1) { + + // to add a new choice + // get new choiceNumber + + choiceNumber = 1 + getLastChoice(containerId); + } + $.ajax( + { + url:url, + data:{ tokenId:tokenId, choiceNumber:choiceNumber }, + async:false, + dataType:"html", + success:function (data) { + var container = $("#" + containerId); + container.append(data); + // store choiceNumber and choiceType + var choice = $("#" + containerId + "_" + choiceNumber); + choice.data('choiceType', containerId); + choice.data('choiceNumber', choiceNumber); + if (choiceNumber == nbChoices) { + + updateUpdownActions(containerId); + // end it can show it + var classes = container.attr("class"); + if (classes.indexOf("thischoice") > -1) { + // show it + container.removeClass('hidden'); + } + } + if (!tokenId) { + updateUpdownActions(containerId); + } + } + }); +} + +function addTextChoice(choiceNumber, nbChoices, tokenId) { + loadChoice( + 'choicesTEXT', + datas['displayTextChoiceUrl'], + choiceNumber, nbChoices, tokenId + ); +} + +function addDateChoice(choiceNumber, nbChoices, tokenId) { + loadChoice( + 'choicesDATE', + datas['displayDateChoiceUrl'], + choiceNumber, nbChoices, tokenId + ); +} + +function addImageChoice(choiceNumber, nbChoices, tokenId) { + loadChoice('choicesIMAGE', + datas['displayImageChoiceUrl'], + choiceNumber, nbChoices, tokenId + ); +} + +function addNewChoice() { + var type = $('[name="poll.choiceType"][checked="checked"]').val(); + if ("TEXT" == type) { + addTextChoice(-1, -1); + } else if ("DATE" == type) { + addDateChoice(-1, -1); + } else if ("IMAGE" == type) { + addImageChoice(-1, -1); + } + return false; +} + +function deleteChoice(choiceId) { + var choice = $('#' + choiceId); + var containerId = choice.data('choiceType'); + choice.remove(); + updateUpdownActions(containerId); + return false; +} + +/*function upChoice(choiceId) { + var choice = $('#' + choiceId); + var containerId = choice.data('choiceType'); + var choiceNumber = choice.data('choiceNumber'); + console.info("will up [" + containerId + "-" + choiceNumber + "]"); + updateUpdownActions(containerId); + return false; + } + + function downChoice(choiceId) { + var choice = $('#' + choiceId); + var containerId = choice.data('choiceType'); + var choiceNumber = choice.data('choiceNumber'); + console.info("will down [" + containerId + "-" + choiceNumber + "]"); + updateUpdownActions(containerId); + return false; + }*/ + +jQuery(document).ready(function () { + + function changePollType(type) { + + $('.pollType').addClass('hidden'); + $('.pollType').removeClass('thispoll'); + $('#pollType' + type).removeClass('hidden'); + $('#pollType' + type).addClass('thispoll'); + + $('[name="poll.pollType"]').attr('checked', false); + $('[name="poll.pollType"][value="' + type + '"]').attr('checked', 'checked'); + } + + function changeChoiceType(type) { + $('.choices').addClass('hidden'); + $('.choices').removeClass('thischoice'); + $('#choices' + type).removeClass('hidden'); + $('#choices' + type).addClass('thischoice'); + $('[name="poll.choiceType"]').attr('checked', false); + $('[name="poll.choiceType"][value="' + type + '"]').attr('checked', 'checked'); + } + + $('[name="poll.choiceAddAllowed"]').change(function () { + var val = $(this).prop('checked'); + if (val) { + $('#addChoiceAddAllowedPanel').show(); + } else { + $('#addChoiceAddAllowedPanel').hide(); + } + }); + + $('[name="limitChoice"]').change(function () { + var val = $(this).prop('checked'); + if (val) { + $('#maxChoiceNbPanel').show(); + } else { + $('#maxChoiceNbPanel').hide(); + } + }); + + $('[name="reminder"]').change(function () { + var val = $(this).prop('checked'); + if (val) { + $('#reminderPanel').show(); + } else { + $('#reminderPanel').hide(); + } + }); + + $('[name="poll.anonymous"]').change(function () { + var val = $(this).prop('checked'); + $('[name="poll.anonymousVoteAllowed"]').prop('checked', val); + }); + $('[name="poll.continuousResults"]').change(function () { + var val = $(this).prop('checked'); + $('[name="poll.publicResults"]').prop('checked', val); + }); + + $('[name="poll.choiceType"]').change(function () { + changeChoiceType($(this).val()); + }); + + $('[name="poll.pollType"]').change(function () { + changePollType($(this).val()); + }); + + $('[name="poll.voteCountingType"][value="' + datas['voteCountingType'] + '"]').attr('checked', 'checked'); + + // before loading all choices let's hide everything, it will be shown later + $('.choices').addClass('hidden'); + + prepareChoices('choicesTEXT'); + prepareChoices('choicesDATE'); + prepareChoices('choicesIMAGE'); + prepareVotingLists('votingListRESTRICTED'); + prepareVotingLists('votingListGROUP'); + + changeChoiceType(datas['choiceType']); + + + changePollType(datas['pollType']); + +}); \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/js/createPoll.js ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.chorem.org