r3434 - in trunk: pollen-services/src/main/java/org/chorem/pollen/services/impl pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/io pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll
Author: tchemit Date: 2012-06-10 22:47:46 +0200 (Sun, 10 Jun 2012) New Revision: 3434 Url: http://chorem.org/repositories/revision/pollen/3434 Log: fixes #585: Can not clone image-choice poll fixes #589: Can not save a poll (image choice) if there is a first validation error Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/io/GetPollImageChoice.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/LoadPoll.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/displayImageChoice.jsp Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-06-10 20:44:13 UTC (rev 3433) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-06-10 20:47:46 UTC (rev 3434) @@ -853,72 +853,109 @@ commitTransaction("Could not delete choice " + choice.getName()); } - public File getPollChoiceImageFile(String pollId, - String choiceId, - boolean thumb) { + /** + * Obtain the location of an image choice. + * + * @param pollId the id of the poll containing the image choice + * @param choiceId the id of the choice to render + * @return the location of image + * @since 1.4 + */ + public File getPollChoiceImageFile(String pollId, String choiceId) { File imageDirectory = getConfiguration().getImageDirectory(); File pollDirectory = new File(imageDirectory, pollId); - if (thumb) { - choiceId = THUMB_PREFIX + choiceId; - } File result = new File(pollDirectory, choiceId); return result; } +// public File getPollChoiceImageFile(String pollId, +// String choiceId, +// boolean thumb) { +// File imageDirectory = getConfiguration().getImageDirectory(); +// File pollDirectory = new File(imageDirectory, pollId); +// +// if (thumb) { +// choiceId = THUMB_PREFIX + choiceId; +// } +// File result = new File(pollDirectory, choiceId); +// +// return result; +// } /** + * given the location of an image, gets his name as a thumb image. + * + * @param imagePath location of the image + * @return location of the thumb of a given image + * @since 1.4 + */ + public File getImageThumbFile(File imagePath) { + + File imageDirectory = imagePath.getParentFile(); + + File result = new File(imageDirectory, + THUMB_PREFIX + imagePath.getName()); + + return result; + } + + /** * Sauvegarde des images d'un choix de type image. * * @param poll le sondage contenant le choix * @param choice le choix à sauvegarder. - * @throws IOException si un problème IO lors de la copie ou génération de la miniature + * @throws IOException si un problème IO lors de la copie ou + * génération de la miniature */ public void saveImages(Poll poll, PollImageChoice choice) throws IOException { String pollId = poll.getPollId(); String name = choice.getName(); - generateThumb(pollId, name, choice); - } + File pollChoiceImage = getPollChoiceImageFile(pollId, name); - public void generateThumb(String pollId, - String name, - PollImageChoice choice) throws IOException { + // copy image to correct directory - File pollChoiceImage = getPollChoiceImageFile(pollId, name, false); - File pollChoiceImageThumb = getPollChoiceImageFile(pollId, name, true); + String location = choice.getLocation(); - if (choice != null) { + FileUtils.copyFile(new File(location), pollChoiceImage); - // means must copy it + // generate thumb + generateThumbIfNeeded(pollChoiceImage); + } + public File generateThumbIfNeeded(File pollChoiceImage) throws IOException { - String location = choice.getLocation(); + File pollChoiceImageThumb = getImageThumbFile(pollChoiceImage); - FileUtils.copyFile(new File(location), pollChoiceImage); - } - int width = 100; - ImageIcon ii = new ImageIcon(pollChoiceImage.getAbsolutePath()); - Image image = ii.getImage(); - double imageRatio = (double) image.getHeight(null) - / (double) image.getWidth(null); - int height = (int) (width * imageRatio); + if (!pollChoiceImageThumb.exists()) { - BufferedImage thumbImage = new BufferedImage(width, height, - BufferedImage.TYPE_INT_RGB); - Graphics2D graphics2D = thumbImage.createGraphics(); - graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, - RenderingHints.VALUE_INTERPOLATION_BILINEAR); - graphics2D.drawImage(image, 0, 0, width, height, null); + int width = 100; + ImageIcon ii = new ImageIcon(pollChoiceImage.getAbsolutePath()); + Image image = ii.getImage(); + double imageRatio = (double) image.getHeight(null) + / (double) image.getWidth(null); + int height = (int) (width * imageRatio); - ImageIO.write(thumbImage, "jpg", pollChoiceImageThumb); + BufferedImage thumbImage = new BufferedImage( + width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics2D = thumbImage.createGraphics(); + graphics2D.setRenderingHint( + RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BILINEAR); + graphics2D.drawImage(image, 0, 0, width, height, null); - if (log.isDebugEnabled()) { - log.debug("Thumbnail created: " + pollChoiceImageThumb.getName() + " (size=" - + pollChoiceImageThumb.length() + ")"); + ImageIO.write(thumbImage, "jpg", pollChoiceImageThumb); + + if (log.isDebugEnabled()) { + log.debug("Thumbnail created: " + + pollChoiceImageThumb.getName() + " (size=" + + pollChoiceImageThumb.length() + ")"); + } } + return pollChoiceImageThumb; } public PersonToList getNewPersonToList(PollAccount pollAccount) { Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/io/GetPollImageChoice.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/io/GetPollImageChoice.java 2012-06-10 20:44:13 UTC (rev 3433) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/io/GetPollImageChoice.java 2012-06-10 20:47:46 UTC (rev 3434) @@ -23,7 +23,7 @@ package org.chorem.pollen.ui.actions.io; import com.google.common.base.Preconditions; -import org.chorem.pollen.services.impl.PollService; +import org.apache.commons.lang3.StringUtils; import org.chorem.pollen.ui.actions.PollenActionSupport; import javax.activation.MimetypesFileTypeMap; @@ -41,6 +41,8 @@ private static final long serialVersionUID = 1L; + protected String choiceTokenId; + protected String pollId; protected String choiceId; @@ -51,6 +53,10 @@ protected String contentType; + public void setChoiceTokenId(String choiceTokenId) { + this.choiceTokenId = choiceTokenId; + } + public void setPollId(String pollId) { this.pollId = pollId; } @@ -74,11 +80,27 @@ @Override public String execute() throws Exception { - Preconditions.checkNotNull(pollId); - Preconditions.checkNotNull(choiceId); + File file; - File file = getPollService().getPollChoiceImageFile(pollId, choiceId, thumb); + if (StringUtils.isNotBlank(choiceTokenId)) { + // use location from session + file = getPollenSession().consumeDynamicData(choiceTokenId); + } else { + + // using pollId / choiceId to find image to render + + Preconditions.checkNotNull(pollId); + Preconditions.checkNotNull(choiceId); + + file = getPollService().getPollChoiceImageFile(pollId, choiceId); + + if (thumb) { + + file = getPollService().getImageThumbFile(file); + } + } + MimetypesFileTypeMap mimes = new MimetypesFileTypeMap(); contentType = mimes.getContentType(file); Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java 2012-06-10 20:44:13 UTC (rev 3433) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java 2012-06-10 20:47:46 UTC (rev 3434) @@ -28,6 +28,7 @@ import com.google.common.collect.Maps; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.chorem.pollen.bean.PollImageChoice; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.PersonToList; import org.chorem.pollen.business.persistence.Poll; @@ -38,9 +39,12 @@ import org.chorem.pollen.common.PollType; import org.chorem.pollen.common.VoteCountingType; import org.chorem.pollen.services.PollenServiceFunctions; +import org.chorem.pollen.services.impl.PollService; import org.chorem.pollen.ui.actions.PageSkin; import org.chorem.pollen.ui.actions.PollenActionSupport; +import java.io.File; +import java.io.IOException; import java.util.Collection; import java.util.List; import java.util.Map; @@ -229,7 +233,7 @@ } public boolean isEdit() { - return StringUtils.isNotEmpty(getPoll().getTopiaId()); + return StringUtils.isNotBlank(getPoll().getTopiaId()); } public void prepareFormPage() throws Exception { @@ -292,13 +296,12 @@ protected void loadChoicesAndvotingLists(Poll poll, Collection<Choice> pollChoices, Collection<VotingList> pollVotinLists, - boolean fillLists) { + boolean fillLists) throws IOException { Preconditions.checkNotNull(poll); Preconditions.checkNotNull(poll.getChoiceType()); Preconditions.checkNotNull(poll.getPollType()); - if (CollectionUtils.isNotEmpty(pollChoices)) { //push back choices @@ -313,6 +316,44 @@ break; case IMAGE: getImageChoices().addAll(pollChoices); + + // if images are not still saved in a poll, then thumb + // does not exists, must create a temporary one + for (Choice choice : pollChoices) { + PollImageChoice imageChoice = (PollImageChoice) choice; + String choiceId = imageChoice.getTopiaId(); + File imageChoiceFile; + + PollService service = getPollService(); + if (StringUtils.isBlank(choiceId)) { + + // new choice, must create the thumb + imageChoiceFile = + new File(imageChoice.getLocation()); + + if (imageChoiceFile.exists()) { + + service.generateThumbIfNeeded(imageChoiceFile); + } + } else { + + // already persisted choice, nothing to do + imageChoiceFile = service.getPollChoiceImageFile( + poll.getPollId(), choice.getName()); + + } + + File thumbFile = service.getImageThumbFile( + imageChoiceFile); + + // keep in session the location of this thumb (do not + // want to expose the full path location in url) + getPollenSession().putDynamicData( + "imagechoicesThumb_" + choice.getName(), + thumbFile); + } + + break; } } Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/LoadPoll.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/LoadPoll.java 2012-06-10 20:44:13 UTC (rev 3433) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/LoadPoll.java 2012-06-10 20:47:46 UTC (rev 3434) @@ -22,13 +22,20 @@ */ package org.chorem.pollen.ui.actions.poll; +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.bean.PollImageChoice; import org.chorem.pollen.bean.PollUri; import org.chorem.pollen.business.persistence.Choice; 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.common.ChoiceType; +import org.chorem.pollen.services.impl.PollService; import org.chorem.pollen.services.impl.PreventRuleService; +import java.io.File; import java.util.List; /** @@ -45,6 +52,9 @@ private static final long serialVersionUID = 1L; + /** Logger. */ + private static final Log log = LogFactory.getLog(LoadPoll.class); + private PollUri pollUri; private boolean clone; @@ -68,22 +78,55 @@ UserAccount userAccount = getPollenUserAccount(); String pollUid = pollUri == null ? null : pollUri.getPollId(); - poll = getPollService().getPollEditable(pollUid, userAccount, clone); + PollService service = getPollService(); + poll = service.getPollEditable(pollUid, userAccount, clone); + if (poll.isClosed()) { addFlashWarning(_("pollen.warning.poll.is.closed.so.read.only")); } List<Choice> pollChoices = poll.getChoice(); + + if (clone) { + + if (ChoiceType.IMAGE == poll.getChoiceType()) { + + // recopy images to tmp + + File tmpDir = getConfiguration().getTemporaryDirectory(); + for (Choice choice : pollChoices) { + String choiceName = choice.getName(); + + // image from poll to clone + File imageChoiceFile = service.getPollChoiceImageFile( + pollUid, + choiceName + ); + + // new image + File newImageChoiceFile = File.createTempFile( + choiceName, null, tmpDir); + + if (log.isInfoEnabled()) { + log.info("Copy image from " + imageChoiceFile + + " to " + newImageChoiceFile); + } + FileUtils.copyFile(imageChoiceFile, + newImageChoiceFile); + + ((PollImageChoice) choice).setLocation( + newImageChoiceFile.getAbsolutePath()); + } + } + } + List<VotingList> pollVotingLists = poll.getVotingList(); loadChoicesAndvotingLists(poll, pollChoices, pollVotingLists, !isVoteStarted()); - //TODO-tchemit comment me Simplyf this! -// if (poll.getMaxChoiceNb() > 0) { -// setLimitChoice(true); -// } + setLimitChoice(poll.getMaxChoiceNb() > 0); PreventRule reminder = poll.getPreventRuleByScope( Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java 2012-06-10 20:44:13 UTC (rev 3433) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java 2012-06-10 20:47:46 UTC (rev 3434) @@ -30,6 +30,7 @@ import com.opensymphony.xwork2.interceptor.annotations.InputConfig; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -131,17 +132,6 @@ prepareFormPage(); String pollUid = getNonEmptyParameterValue("poll.pollId"); - //TODO-tchemit comment me No ! inverse tests otherwise make possible to change owner of a poll! :( - // Retrieve userAccount to attach to the poll -// UserAccount userAccount = getPollenUserAccount(); -// if (userAccount == null) { -// -// // from parameter userId if defined during update loading -// String userId = getNonEmptyParameterValue("userId"); -// if (userId != null) { -// userAccount = getPollService().getEntityById(UserAccount.class, userId); -// } -// } String userId = getNonEmptyParameterValue("userId"); if (StringUtils.isBlank(userId)) { @@ -154,8 +144,8 @@ UserAccount userAccount = null; if (StringUtils.isNotBlank(userId)) { // load use account to use - userAccount = - getPollService().getEntityById(UserAccount.class, userId); + userAccount = getPollService().getEntityById( + UserAccount.class, userId); } // get a copy (or a fresh new poll) @@ -184,17 +174,9 @@ choices = buildImageChoices(); break; } - //TODO-tchemit comment me No the nullite tests is not possible : if pollType is null not possible ? PollType pollType; String pollTypeParam = getNonEmptyParameterValue("poll.pollType"); pollType = PollType.valueOf(pollTypeParam); -// if (pollTypeParam == null) { -// pollType = poll.getPollType(); -// -// } else { -// pollType = PollType.valueOf(pollTypeParam); -// } - switch (pollType) { case FREE: @@ -221,13 +203,44 @@ } @Override - public String input() { + public String input() throws Exception { Collection<Choice> pollChoices = isVoteStarted() ? poll.getChoice() : choices.values(); Collection<VotingList> pollVotingLists = isVoteStarted() ? poll.getVotingList() : votingLists.values(); + + if (ChoiceType.IMAGE == poll.getChoiceType()) { + + // recopy images for new choices, the one uploaded will be + // destroyed, by the upload interceptor + + File tmpDir = getConfiguration().getTemporaryDirectory(); + + for (Choice choice : pollChoices) { + + String choiceId = choice.getTopiaId(); + + if (StringUtils.isBlank(choiceId)) { + + PollImageChoice imageChoice = (PollImageChoice) choice; + + File uploadedImage = new File(imageChoice.getLocation()); + File copyImage = File.createTempFile( + uploadedImage.getName(), null, tmpDir); + + if (log.isInfoEnabled()) { + log.info("Copy image from " + uploadedImage + + " to " + copyImage); + } + FileUtils.copyFile(uploadedImage, copyImage); + + imageChoice.setLocation(copyImage.getAbsolutePath()); + } + } + } + loadChoicesAndvotingLists(poll, pollChoices, pollVotingLists, true); return INPUT; } @@ -251,13 +264,6 @@ // Clear previous collections to save those from the form poll.clearChoice(); poll.clearVotingList(); - //TODO-tchemit comment me this variables, works directly on choices -// Map<Integer, Choice> orderedChoices = choices; -// -// for (Integer index : orderedChoices.keySet()) { -// Choice choice = orderedChoices.get(index); -// poll.addChoice(choice); -// } for (Integer index : choices.keySet()) { Choice choice = choices.get(index); poll.addChoice(choice); Modified: trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp =================================================================== --- trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp 2012-06-10 20:44:13 UTC (rev 3433) +++ trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/createPoll_choices.jsp 2012-06-10 20:47:46 UTC (rev 3434) @@ -165,10 +165,10 @@ <s:hidden id="%{#prefix}.location" name="%{#prefix}.location" value="%{#choice.location}" label='' theme="simple"/> <s:url id="imageUrl" namespace="/io" action="getPollChoiceImage" - escapeAmp="false"> - <s:param name="choiceId" value="%{#choice.name}"/> - <s:param name="pollId" value="poll.pollId"/> - <s:param name="thumb" value="true"/> + escapeAmp="true"> + <s:param name="choiceTokenId"> + imagechoicesThumb_<s:property value='name'/> + </s:param> </s:url> <img name="<s:property value="#prefix"/>.thumb" alt="<s:property value='name'/>" @@ -181,11 +181,7 @@ <s:else> <%--New image--%> <s:url id="imageUrl" namespace="/io" action="getPollChoiceImage" - escapeAmp="false" value="/img/7ter.jpg"> - <s:param name="choiceId" value="%{#choice.name}"/> - <s:param name="pollId" value="poll.pollId"/> - <s:param name="thumb" value="true"/> - </s:url> + escapeAmp="false" value="/img/7ter.jpg"/> <img name="<s:property value="#prefix"/>.thumb" alt="<s:text name='pollen.image.not.loaded'/>" title="<s:text name='pollen.image.not.loaded'/>" Modified: trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/displayImageChoice.jsp =================================================================== --- trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/displayImageChoice.jsp 2012-06-10 20:44:13 UTC (rev 3433) +++ trunk/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/displayImageChoice.jsp 2012-06-10 20:47:46 UTC (rev 3434) @@ -48,11 +48,7 @@ <%--<s:else>--%> <%--New image--%> <s:url id="imageUrl" namespace="/io" action="getPollChoiceImage" - escapeAmp="false" value="/img/7ter.jpg"> - <s:param name="choiceId" value="%{#choice.name}"/> - <s:param name="pollId" value="poll.pollId"/> - <s:param name="thumb" value="true"/> - </s:url> + escapeAmp="false" value="/img/7ter.jpg"/> <img name="<s:property value="#prefix"/>.thumb" alt="<s:text name='pollen.image.not.loaded'/>" title="<s:text name='pollen.image.not.loaded'/>"
participants (1)
-
tchemit@users.chorem.org