Author: fdesbois Date: 2012-04-12 12:32:21 +0200 (Thu, 12 Apr 2012) New Revision: 3250 Url: http://chorem.org/repositories/revision/pollen/3250 Log: - create new object PollUrl that use PollUri to avoid floating String for email urls - missing reset for limitChoice - ensure keeping existing account on update poll for restricted votingLists Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollUrl.java Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenNotifierWorker.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PreventRuleService.java 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/AbstractVoteAction.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollUrl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollUrl.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollUrl.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -0,0 +1,63 @@ +package org.chorem.pollen.bean; + +/** + * Created: 12/04/12 + * + * @author fdesbois <desbois@codelutin.com> + */ +public class PollUrl { + + private String baseUrl; + + private PollUri pollUri; + + /** + * Create a new PollUrl with {@code baseUrl} which is the application url + * for this url, and the {@code pollUri} to identify the poll. + * + * @param baseUrl Base url from the application for this url + * @param pollUri Uri that identify the poll and the account + * @return a new PollUrl + */ + public static PollUrl newPollUrl(String baseUrl, PollUri pollUri) { + PollUrl result = new PollUrl(); + result.baseUrl = baseUrl; + result.pollUri = pollUri; + return result; + } + + /** + * Create a new PollUrl which is a copy of the given {@code pollUrl} with + * an other {@code accountId} for {@link PollUri}. + * + * @param pollUrl PollUrl to copy + * @param accountId New accountId to use for copy + * @return the new PollUrl + */ + public static PollUrl newPollUrl(PollUrl pollUrl, String accountId) { + PollUrl result = new PollUrl(); + result.baseUrl = pollUrl.getBaseUrl(); + String pollId = pollUrl.getPollUri().getPollId(); + result.pollUri = PollUri.newPollUri(pollId, accountId); + return result; + } + + private PollUrl() { } + + public String getBaseUrl() { + return baseUrl; + } + + public PollUri getPollUri() { + return pollUri; + } + + public String getUrl() { + return getBaseUrl() + getPollUri().getUri(); + } + + @Override + public String toString() { + return getUrl(); + } +} Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenNotifierWorker.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenNotifierWorker.java 2012-04-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenNotifierWorker.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -25,6 +25,7 @@ import com.google.common.base.Preconditions; import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.bean.PollUrl; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PreventRule; import org.chorem.pollen.services.impl.PollService; @@ -89,7 +90,7 @@ for (Poll poll : polls) { - String pollVoteUrl = pollService.getPollVoteUrl(poll); + PollUrl pollVoteUrl = pollService.getPollVoteUrl(poll); preventRuleService.onPollToRemind(poll, pollVoteUrl); Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java 2012-04-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -33,10 +33,10 @@ import org.apache.commons.mail.SimpleEmail; import org.chorem.pollen.PollenConfiguration; import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.bean.PollUrl; 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.common.PollType; import org.chorem.pollen.services.PollenServiceSupport; import org.nuiton.util.StringUtil; @@ -72,7 +72,7 @@ } } - public void onVoteAdded(Poll poll, String voteURL, String modifURL) { + public void onVoteAdded(Poll poll, PollUrl voteURL, PollUrl modifURL) { URL url = serviceContext.getApplicationURL(); @@ -102,7 +102,7 @@ } } - public void onPollCreated(Poll poll, String voteURL, String modifURL) { + public void onPollCreated(Poll poll, PollUrl voteURL, PollUrl modifURL) { URL url = serviceContext.getApplicationURL(); @@ -115,14 +115,10 @@ // Mail au créateur if (StringUtils.isNotEmpty(poll.getCreator().getEmail())) { - String voteURL2 = voteURL; - if (poll.getPollType() != PollType.FREE) { - voteURL2 += ":" + poll.getCreator().getAccountId(); - } String subject = l_(locale, "pollen.email.creatorEmail.subject", pollTitle); String content = l_(locale, "pollen.email.creatorEmail.content", - pollTitle, voteURL2, modifURL); + pollTitle, voteURL, modifURL); PollEmail pollEmail = createPollEmail( poll.getCreator().getEmail(), subject, content); @@ -140,8 +136,7 @@ List<PollEmail> emails = Lists.newArrayList(); for (PollAccount account : votingPollAccounts) { String accountId = account.getAccountId(); - String accountVoteURL = - voteURL + ":" + accountId; + PollUrl accountVoteURL = PollUrl.newPollUrl(voteURL, accountId); String content = l_(locale, "pollen.email.votingEmail.content", @@ -158,7 +153,7 @@ } } - public void onPollReminder(Poll poll, String voteURL) { + public void onPollReminder(Poll poll, PollUrl voteURL) { URL url = serviceContext.getApplicationURL(); @@ -177,8 +172,8 @@ List<PollEmail> emails = Lists.newArrayList(); for (PollAccount account : votingPollAccounts) { - String accountVoteURL = - voteURL + ":" + account.getAccountId(); + PollUrl accountVoteURL = + PollUrl.newPollUrl(voteURL, account.getAccountId()); String content = l_(locale, "pollen.email.reminderEmail.content", Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java 2012-04-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -25,6 +25,7 @@ import com.google.common.base.Preconditions; import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.bean.PollUrl; import org.chorem.pollen.business.persistence.Comment; import org.chorem.pollen.business.persistence.CommentDAO; import org.chorem.pollen.business.persistence.Poll; @@ -91,7 +92,7 @@ PollService pollService = newService(PollService.class); - String pollVoteUrl = pollService.getPollVoteUrl(poll); + PollUrl pollVoteUrl = pollService.getPollVoteUrl(poll); PollFeedService pollFeedService = newService(PollFeedService.class); pollFeedService.onAddComment(commentCreated, pollVoteUrl); Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java 2012-04-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollFeedService.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -36,6 +36,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.bean.PollUrl; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.Comment; import org.chorem.pollen.business.persistence.Poll; @@ -81,7 +82,7 @@ return result; } - public void onPollCreated(Poll poll, String pollVoteUrl) { + public void onPollCreated(Poll poll, PollUrl pollVoteUrl) { File file = getFeedLocation(poll); String title = l_(getLocale(), "pollen.feed.pollTitle", poll.getTitle()); @@ -91,7 +92,7 @@ SyndFeed feed = new SyndFeedImpl(); feed.setFeedType("atom_1.0"); feed.setTitle(title); - feed.setLink(pollVoteUrl); + feed.setLink(pollVoteUrl.getUrl()); feed.setDescription(description); Writer writer = new FileWriter(file); @@ -115,7 +116,7 @@ } } - public void onAddComment(Comment comment, String pollVoteUrl) { + public void onAddComment(Comment comment, PollUrl pollVoteUrl) { String title = _("pollen.feed.addCommentTitle", comment.getAuthor()); @@ -128,8 +129,8 @@ public void onAddVote(Vote vote, boolean anonymousVote, - String pollResult - , String pollVoteUrl) { + String pollResult, + PollUrl pollVoteUrl) { String userId; if (anonymousVote) { @@ -145,7 +146,7 @@ addFeedEntry(vote.getPoll(), title, content, pollVoteUrl); } - public void onAddChoice(Choice choice, String pollVoteUrl) { + public void onAddChoice(Choice choice, PollUrl pollVoteUrl) { String title = _("pollen.feed.addChoiceTitle", choice.getName()); @@ -185,7 +186,7 @@ protected void addFeedEntry(Poll poll, String title, String content, - String pollVoteUrl) { + PollUrl pollVoteUrl) { //TODO tchemitshould remove this, may migrates when going to version 2.0 //TODO pass on all polls from database and creates the feed if not found. @@ -205,7 +206,7 @@ SyndContent description; entry = new SyndEntryImpl(); entry.setTitle(title); - entry.setLink(pollVoteUrl); + entry.setLink(pollVoteUrl.getUrl()); entry.setPublishedDate(new Date()); description = new SyndContentImpl(); description.setType("text/plain"); Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-04-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -34,6 +34,7 @@ import org.chorem.pollen.bean.PollDateChoice; import org.chorem.pollen.bean.PollImageChoice; import org.chorem.pollen.bean.PollUri; +import org.chorem.pollen.bean.PollUrl; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.ChoiceDAO; import org.chorem.pollen.business.persistence.PersonToList; @@ -241,10 +242,13 @@ Poll result = create(pollDAO); // check max number choice authorized (can not be more than - if (poll.getMaxChoiceNb() < 1 - || poll.getMaxChoiceNb() > poll.sizeChoice()) { - poll.setMaxChoiceNb(poll.sizeChoice()); - } + // XXX-fdesbois-2012-04-11 : don't know why the maxNbChoice must be + // forced to the choice size, it seems useless. The case maxNbChoice = 0 + // must be available (equivalent to undefined) +// if (poll.getMaxChoiceNb() < 1 +// || poll.getMaxChoiceNb() > poll.sizeChoice()) { +// poll.setMaxChoiceNb(poll.sizeChoice()); +// } result.setAnonymous(poll.isAnonymous()); result.setAnonymousVoteAllowed(poll.isAnonymousVoteAllowed()); @@ -285,20 +289,26 @@ } // -- VotingList -- // - for (VotingList votingList : poll.getVotingList()) { - saveVotingList(result, votingList); + try { + for (VotingList votingList : poll.getVotingList()) { + saveVotingList(result, votingList); + } + } catch (TopiaException e) { + throw new PollenTechnicalException("Can't save votingLists", e); } commitTransaction("Could not create poll " + poll.getTitle()); - String pollVoteUrl = getPollVoteUrl(poll); - String pollEditUrl = getPollEditUrl(poll); + PollUrl pollVoteUrl = getPollVoteUrl(poll); + PollUrl pollEditUrl = getPollEditUrl(poll); // email notification EmailService emailService = newService(EmailService.class); emailService.onPollCreated(result, pollVoteUrl, pollEditUrl); // feed notification + // XXX-fdesbois-2012-04-12 : if the feed is the same for everybody maybe + // not publish restricted poll that can't be accessed by everybody PollFeedService pollFeedService = newService(PollFeedService.class); pollFeedService.onPollCreated(result, pollVoteUrl); @@ -344,35 +354,41 @@ if (!voteStarted) { pollToUpdate.clearVotingList(); - for (VotingList votingList : poll.getVotingList()) { - saveVotingList(pollToUpdate, votingList); + try { + for (VotingList votingList : poll.getVotingList()) { + saveVotingList(pollToUpdate, votingList); + } + } catch (TopiaException e) { + throw new PollenTechnicalException("Can't save votingLists", e); } } commitTransaction("Could not update poll [" + poll.getTopiaId() + "]"); } - public String getPollVoteUrl(Poll poll) { + public PollUrl getPollVoteUrl(Poll poll) { URL applicationUrl = serviceContext.getApplicationURL(); - StringBuilder url = new StringBuilder(applicationUrl.toString()); - url.append("/poll/votefor/"); - - if (PollType.FREE == poll.getPollType()) { - url.append(poll.getPollId()); - - } else { - // Restricted poll is secure, the creator could access to it when - // using the adminId - url.append(poll.getAdminId()); + String baseUrl = applicationUrl + "/poll/votefor/"; + PollUri pollUri = PollUri.newPollUri(poll.getPollId()); + + if (poll.getPollType() != PollType.FREE) { + String creatorId = poll.getCreator().getAccountId(); + pollUri.setAccountId(creatorId); } - return url.toString(); + + PollUrl result = PollUrl.newPollUrl(baseUrl, pollUri); + return result; } - public String getPollEditUrl(Poll poll) { + public PollUrl getPollEditUrl(Poll poll) { URL applicationUrl = serviceContext.getApplicationURL(); - StringBuilder url = new StringBuilder(applicationUrl.toString()); - url.append("/poll/modification/").append(poll.getAdminId()); - return url.toString(); + String baseUrl = applicationUrl + "/poll/modification/"; + + String creatorId = poll.getCreator().getAccountId(); + PollUri pollUri = PollUri.newPollUri(poll.getPollId(), creatorId); + + PollUrl result = PollUrl.newPollUrl(baseUrl, pollUri); + return result; } public List<Poll> getPolls(TopiaFilterPagerUtil.FilterPagerBean pager) { @@ -889,8 +905,8 @@ // Send notification if necessary PreventRuleService preventRuleService = newService(PreventRuleService.class); - String pollVoteUrl = getPollVoteUrl(pollToUpdate); - String modifUrl = getPollEditUrl(pollToUpdate); + PollUrl pollVoteUrl = getPollVoteUrl(pollToUpdate); + PollUrl modifUrl = getPollEditUrl(pollToUpdate); preventRuleService.onVoteAdded(poll, pollVoteUrl, modifUrl); } @@ -910,7 +926,7 @@ return result; } - protected void saveVotingList(Poll poll, VotingList votingList) { + protected void saveVotingList(Poll poll, VotingList votingList) throws TopiaException { VotingListDAO votingListDAO = getDAO(VotingList.class); PersonToListDAO personToListDAO = getDAO(PersonToList.class); @@ -946,9 +962,18 @@ // The model doesn't have any composition for this relation, // the link must be set in both objects personToListLoaded.setVotingList(result); - - pollAccountLoaded = create(pollAccountDAO); - pollAccountLoaded.setAccountId(pollAccount.getAccountId()); + + // FIXME-fdesbois-2012-04-12 : find a better way to ensure accountId + String accountId = pollAccount.getAccountId(); + if (accountId == null) { + accountId = serviceContext.createPollenUrlId(); + } + + pollAccountLoaded = pollAccountDAO.findByAccountId(accountId); + if (pollAccountLoaded == null) { + pollAccountLoaded = create(pollAccountDAO); + pollAccountLoaded.setAccountId(accountId); + } personToListLoaded.setPollAccount(pollAccountLoaded); } else { Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PreventRuleService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PreventRuleService.java 2012-04-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PreventRuleService.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -26,6 +26,7 @@ import com.google.common.base.Preconditions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.bean.PollUrl; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PreventRule; import org.chorem.pollen.services.PollenServiceSupport; @@ -63,7 +64,7 @@ return rule; } - public void onVoteAdded(Poll poll, String pollVoteUrl, String modifURL) { + public void onVoteAdded(Poll poll, PollUrl pollVoteUrl, PollUrl modifURL) { if (poll.getEndDate() != null) { @@ -77,7 +78,7 @@ } } - public void onPollToRemind(Poll poll, String pollVoteUrl) { + public void onPollToRemind(Poll poll, PollUrl pollVoteUrl) { // Parcours des preventRules de chaque sondage // envoi d'un email si endDate-nowDate <= preventRuleSensibility @@ -109,8 +110,8 @@ protected void onVoteAdded(Poll poll, PreventRule preventRule, - String pollVoteUrl, - String modifURL) { + PollUrl pollVoteUrl, + PollUrl modifURL) { // send to creator a email to say hey a new vote was added! @@ -130,7 +131,7 @@ protected void onPollToRemind(Poll poll, PreventRule preventRule, - String pollVoteUrl, + PollUrl pollVoteUrl, int timeValue) { // send to all voting a email to say hey we need to vote 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-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -203,11 +203,11 @@ } public String getPollVoteUrl() { - return getPollService().getPollVoteUrl(poll); + return getPollService().getPollVoteUrl(poll).getUrl(); } public String getPollEditUrl() { - return getPollService().getPollEditUrl(poll); + return getPollService().getPollEditUrl(poll).getUrl(); } public int getSelectedTab() { Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-04-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -85,6 +85,8 @@ private PollAccount pollAccount; private boolean alreadyVoted; + + private boolean voteAllowed; private List<Vote> votes; @@ -285,8 +287,10 @@ // TODO no pagination for the moment, need to retrieve the correct page depends on current pollAccount votes = poll.getVote(); + voteAllowed = getVoteService().isVoteAllowed(poll, pollAccount); + // Current vote - if (isVoteAllowed()) { + if (voteAllowed) { vote = getVoteService().getVoteEditable(poll, pollAccount); } @@ -370,7 +374,7 @@ } public boolean isVoteAllowed() { - return getVoteService().isVoteAllowed(getPoll(), getPollAccount()); + return voteAllowed; } public boolean isDeleteCommentAllowed(Comment comment) { 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-12 07:54:38 UTC (rev 3249) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/SavePoll.java 2012-04-12 10:32:21 UTC (rev 3250) @@ -263,6 +263,11 @@ } } } + + // Reset maxChoiceNb if limitChoice is unchecked + if (!isLimitChoice()) { + poll.setMaxChoiceNb(0); + } poll.clearPreventRule();