r3203 - branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll
Author: fdesbois Date: 2012-03-23 18:15:45 +0100 (Fri, 23 Mar 2012) New Revision: 3203 Url: http://chorem.org/repositories/revision/pollen/3203 Log: Use new methods for flags in Poll and improve code organization 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/AddComment.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteComment.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ModifyVote.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-03-23 08:56:10 UTC (rev 3202) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-03-23 17:15:45 UTC (rev 3203) @@ -62,64 +62,61 @@ /** Logger. */ private static final Log log = LogFactory.getLog(AbstractVoteAction.class); - protected Poll poll; + /** + * Keep this service as property to avoid new instance for each loop in + * {@link #getResultValue(Choice)} + */ + private transient PollResultsService pollResultsService; - protected boolean feedFileExisting; + private Poll poll; - protected boolean creatorUser; + private boolean feedFileExisting; - protected boolean pollChoiceOrVoteStarted; + private boolean creatorUser; - protected boolean accountFieldDisplayed; + private PollAccount pollAccount; - protected boolean pollChoiceRunning; + private boolean alreadyVoted; - protected boolean pollRunning; + private String voteCountingTypeHelp; - protected boolean alreadyVoted; + private String accountName; + + private String commentName; - private boolean anonymousVote; + private String voteSizeMessage; - protected String voteCountingTypeHelp; - - protected String defaultCommentName; - - protected String creatorName; - - protected String commentName; - - protected String voteSizeMessage; - - private PollAccount pollAccount; - private List<PollResult> results; private List<Comment> comments; @Override - public final String getPageLogo() { + public String getPageLogo() { return "Vote"; } - public final Poll getPoll() { - return poll; + private PollResultsService getPollResultsService() { + if (pollResultsService == null) { + pollResultsService = newService(PollResultsService.class); + } + return pollResultsService; } - public PollAccount getPollAccount() { - if (pollAccount == null) { - PollService service = newService(PollService.class); - pollAccount = service.getNewPollAccount( - getPollenUserAccount()); - } - return pollAccount; + public Poll getPoll() { + return poll; } - public final boolean isFeedFileExisting() { + public boolean isFeedFileExisting() { return feedFileExisting; } + public PollAccount getPollAccount() { + return pollAccount; + } + public boolean isPollChoiceOrVoteStarted() { - return pollChoiceOrVoteStarted; + Date now = serviceContext.getCurrentTime(); + return poll.isAddChoiceStarted(now) || poll.isStarted(now); } public boolean isAlreadyVoted() { @@ -127,21 +124,17 @@ } public boolean isAnonymousVote() { - return anonymousVote; + return poll.isAnonymous(); } - public final String getVoteCountingTypeHelp() { + public String getVoteCountingTypeHelp() { return voteCountingTypeHelp; } - public final String getDefaultCommentName() { - return defaultCommentName; + public String getAccountName() { + return accountName; } - public final String getCommentName() { - return commentName; - } - public List<PollResult> getResults() { return results; } @@ -150,194 +143,155 @@ return comments; } - public void prepareVotePage() throws Exception { + public boolean isCreatorUser() { + return creatorUser; + } - loadPoll(); + public boolean isAccountFieldDisplayed() { + return !poll.isAnonymous() || isRestrictedPoll() || isGroupPoll(); + } - setDateFormat(DateFormat.getDateTimeInstance( - DateFormat.SHORT, - DateFormat.SHORT, getLocale())); + public boolean isPollChoiceRunning() { + Date now = serviceContext.getCurrentTime(); + return poll.isAddChoiceRunning(now); + } - if (poll != null) { + public boolean isPollRunning() { + Date now = serviceContext.getCurrentTime(); + return poll.isRunning(now); + } - loadPollAccount(); - loadPollResults(); - loadPollComments(); + public String getCreatorName() { + return poll.getCreator().getAccountId(); + } - PollFeedService pollFeedService = newService(PollFeedService.class); + public String getVoteSizeMessage() { + return voteSizeMessage; + } - feedFileExisting = pollFeedService.isFeedExists(poll); + public boolean isDescNull(Choice choice) { + return StringUtils.isEmpty(choice.getDescription()); + } - VoteCountingType voteCountingType = poll.getVoteCountingType(); - switch (voteCountingType) { + public boolean isFreePoll() { + return poll.getPollType() == PollType.FREE; + } - case NORMAL: - voteCountingTypeHelp = - _("pollen.common.voteCountingTypeHelp.normal"); - break; - case PERCENTAGE: - voteCountingTypeHelp = - _("pollen.common.voteCountingTypeHelp.percentage"); - break; - case CONDORCET: - voteCountingTypeHelp = - _("pollen.common.voteCountingTypeHelp.condorcet"); - break; - case NUMBER: - voteCountingTypeHelp = - _("pollen.common.voteCountingTypeHelp.number"); - break; - } + public boolean isRestrictedPoll() { + return poll.getPollType() == PollType.RESTRICTED; + } - // set default comment name - UserAccount userAccount = getPollenUserAccount(); - if (userAccount == null) { - defaultCommentName = ""; - } else { - defaultCommentName = userAccount.getDisplayName(); - } + public boolean isGroupPoll() { + return poll.getPollType() == PollType.GROUP; + } - Date currentTime = serviceContext.getCurrentTime(); + public boolean isTextType() { + return poll.getChoiceType() == ChoiceType.TEXT; + } - accountFieldDisplayed = !poll.isAnonymous() || isRestrictedPoll() || isGroupPoll(); + public boolean isDateType() { + return poll.getChoiceType() == ChoiceType.DATE; + } - boolean choicestarted = poll.getBeginChoiceDate() == null || poll.getBeginChoiceDate().before(currentTime); - boolean choiceended = poll.getEndChoiceDate() != null && poll.getEndChoiceDate().before(currentTime); - boolean pollChoiceStarted = choicestarted && !choiceended; + public boolean isImageType() { + return poll.getChoiceType() == ChoiceType.IMAGE; + } - //TODO Move this is poll entity - pollChoiceOrVoteStarted = pollChoiceStarted || isPollStarted(); + public boolean isNormalVoteCounting() { + return poll.getVoteCountingType() == VoteCountingType.NORMAL; + } - pollChoiceRunning = poll.isChoiceAddAllowed() && pollChoiceStarted; + public boolean isPercentageVoteCounting() { + return poll.getVoteCountingType() == VoteCountingType.PERCENTAGE; + } - pollRunning = !poll.isClosed() && isPollStarted() && !isPollFinished(); + public boolean isCondorcetVoteCounting() { + return poll.getVoteCountingType() == VoteCountingType.CONDORCET; + } - //TODO Deal the case of the not loggued poll (using the pollAccountId - creatorUser = getPollenUserAccount() != null && - getPollenUserAccount().equals(poll.getCreator().getUserAccount()); - - if (log.isInfoEnabled()) { - log.info("pollChoiceOrVoteStarted = " + pollChoiceOrVoteStarted); - log.info("pollChoiceRunning = " + pollChoiceRunning); - log.info("pollRunning = " + pollRunning); - log.info("accountFieldDisplayed = " + accountFieldDisplayed); - log.info("creatorUser = " + creatorUser); - } - creatorName = poll.getCreator().getVotingId(); - -// voteSizeMessage=XXX; - } + public boolean isNumberVoteCounting() { + return poll.getVoteCountingType() == VoteCountingType.NUMBER; } - private transient PollResultsService pollResultsService; - - private PollResultsService getPollResultsService() { - if (pollResultsService == null) { - pollResultsService = newService(PollResultsService.class); - } - return pollResultsService; + public boolean isPollStarted() { + Date now = serviceContext.getCurrentTime(); + return poll.getBeginDate() == null || poll.getBeginDate().before(now); } - public String getResultValue(Choice choice) { - - String val = getPollResultsService().getResultValue(choice, results); - return val; + public boolean isPollFinished() { + Date now = serviceContext.getCurrentTime(); + return poll.getEndDate() != null && poll.getEndDate().before(now); } - protected void loadPollComments() { - - PollCommentService service = newService(PollCommentService.class); - - comments = service.getAllComments(poll.getPollId()); + public String getCommentName() { + return commentName; } - protected void loadPollResults() throws PollNotFoundException { - - PollResultsService service = getPollResultsService(); - - PollResultList pollResultList = service.getResults(poll.getPollId()); - - results = pollResultList.getPollResults(); - - if (log.isDebugEnabled()) { - for (PollResult res : results) { - log.debug(res.getName() + ": " + res.getValue() - + ", (voteCounting=" + res.getVoteCountingType() - + ", byGroup=" + res.isByGroup() + ")"); - } - } + public void setCommentName(String commentName) { + this.commentName = commentName; } + + public void resetCommentName() { + setCommentName(getAccountName()); + } - protected void loadPollAccount() throws PollNotFoundException { + public void prepareVotePage() throws Exception { - PollService service = newService(PollService.class); + loadPoll(); - //// Contrôle et définition du votingId - alreadyVoted = false; - boolean modifAllowed = false; - boolean restrictedListsForbidden = false; + setDateFormat(DateFormat.getDateTimeInstance( + DateFormat.SHORT, + DateFormat.SHORT, getLocale())); - // The calcul of alreadyVoted will be needed for no double votingId - // Carefull, not correct for an anonymous vote - VoteService voteService = newService(VoteService.class); + loadPollAccount(); + loadPollResults(); + loadPollComments(); - // Suppression des espaces pouvant provoquer un double vote - String votingId = getPollAccount().getVotingId().trim(); - getPollAccount().setVotingId(votingId); + PollFeedService pollFeedService = newService(PollFeedService.class); + feedFileExisting = pollFeedService.isFeedExists(poll); - alreadyVoted = voteService.hasAlreadyVoted(votingId, poll); + VoteCountingType voteCountingType = poll.getVoteCountingType(); + switch (voteCountingType) { - // Check for restricted poll - if (!alreadyVoted && !isFreePoll()) { + case NORMAL: + voteCountingTypeHelp = + _("pollen.common.voteCountingTypeHelp.normal"); + break; + case PERCENTAGE: + voteCountingTypeHelp = + _("pollen.common.voteCountingTypeHelp.percentage"); + break; + case CONDORCET: + voteCountingTypeHelp = + _("pollen.common.voteCountingTypeHelp.condorcet"); + break; + case NUMBER: + voteCountingTypeHelp = + _("pollen.common.voteCountingTypeHelp.number"); + break; + } - if (StringUtils.isNotEmpty(getAccountId())) { + //TODO Deal the case of the not loggued poll (using the pollAccountId + creatorUser = getPollenUserAccount() != null && + getPollenUserAccount().equals(poll.getCreator().getUserAccount()); - // The accountUId must be valid for the poll - PollAccount restrictedAccount = - service.getRestrictedAccount(getPollId(), getAccountId()); + resetCommentName(); - if (restrictedAccount != null) { - // PollAccount is replaced by the good account from db - pollAccount = restrictedAccount; - - // Refresh alreadyVoted value depends on pollAccount -// alreadyVoted = pollAccount.isHasVoted(); - - } else { - // Existing account is null is forbidden for a restricted poll - restrictedListsForbidden = true; - } - - } else { - // Not allowed to vote without an accountUId - restrictedListsForbidden = true; - } - - if (restrictedListsForbidden) { - addActionError(_("pollen.error.user.restrictedListsForbidden")); - } + if (log.isInfoEnabled()) { + log.info("pollChoiceOrVoteStarted = " + isPollChoiceOrVoteStarted()); + log.info("pollChoiceRunning = " + isPollChoiceRunning()); + log.info("pollRunning = " + isPollRunning()); + log.info("accountFieldDisplayed = " + isAccountFieldDisplayed()); + log.info("creatorUser = " + creatorUser); } + } - // Génération d'un identifiant de vote (si le sondage est libre et anonyme) - if (poll.isAnonymous()) { - anonymousVote = true; - if (isFreePoll()) { - getPollAccount().setVotingId( - "anonymous" + serviceContext.createPollenUrlId()); - } - } + public String getResultValue(Choice choice) { - modifAllowed = isModifAllowed(getPollAccount().getVotingId()); - if (alreadyVoted && !modifAllowed) { - addActionError(_("pollen.error.user.alreadyVoted", - getPollAccount().getVotingId())); - } + String val = getPollResultsService().getResultValue(choice, results); + return val; } - public String escapeLineBreak(String text) { - return text; - } - /** * Retourne si le choix fait partie du vote (s'il a été renseigné par le * votant). @@ -372,7 +326,7 @@ Vote vote = poll.getVoteByTopiaId(voteId); // can only modify a vote if poll is running. - if (vote != null && pollRunning) { + if (vote != null && isPollRunning()) { PollAccount votePollAccount = vote.getPollAccount(); // si le votant du vote correspond au votant actuel (pollAccountId) @@ -393,30 +347,14 @@ return result; } - public boolean isCreatorUser() { - return creatorUser; + public String escapeLineBreak(String text) { + return text; } - public boolean isAccountFieldDisplayed() { - return accountFieldDisplayed; + public String formatChoiceNameAsDate(Choice choice) { + return formatDate(new Date(Long.valueOf(choice.getName()))); } - public boolean isPollChoiceRunning() { - return pollChoiceRunning; - } - - public boolean isPollRunning() { - return pollRunning; - } - - public String getCreatorName() { - return creatorName; - } - - public String getVoteSizeMessage() { - return voteSizeMessage; - } - protected void loadPoll() throws PollNotFoundException { PollService service = newService(PollService.class); @@ -425,77 +363,112 @@ poll = service.getPollByPollId(pollId); } - if (poll == null) { - addActionError(_("pollen.error.poll.notfound")); - } else { - if (poll.isClosed()) { - addActionMessage(_("pollen.information.pollClosed")); - } else if (!isPollStarted()) { - addActionMessage(_("pollen.information.pollNotStarted")); - } else if (isPollFinished()) { - addActionMessage(_("pollen.information.pollFinished")); - } - if (isPollChoiceRunning()) { - addActionMessage(_("pollen.information.pollChoiceRunning")); - } + + if (poll.isClosed()) { + addActionMessage(_("pollen.information.pollClosed")); + } else if (!isPollStarted()) { + addActionMessage(_("pollen.information.pollNotStarted")); + } else if (isPollFinished()) { + addActionMessage(_("pollen.information.pollFinished")); } + if (isPollChoiceRunning()) { + addActionMessage(_("pollen.information.pollChoiceRunning")); + } } - public boolean isDescNull(Choice choice) { - return StringUtils.isEmpty(choice.getDescription()); - } + protected void loadPollAccount() throws PollNotFoundException { - public boolean isFreePoll() { - return poll.getPollType() == PollType.FREE; - } + PollService service = newService(PollService.class); - public boolean isRestrictedPoll() { - return poll.getPollType() == PollType.RESTRICTED; - } + //// Contrôle et définition du votingId + alreadyVoted = false; + boolean modifAllowed = false; + boolean restrictedListsForbidden = false; - public boolean isGroupPoll() { - return poll.getPollType() == PollType.GROUP; - } + // The calcul of alreadyVoted will be needed for no double votingId + // Carefull, not correct for an anonymous vote + VoteService voteService = newService(VoteService.class); - public boolean isTextType() { - return poll.getChoiceType() == ChoiceType.TEXT; - } + // Suppression des espaces pouvant provoquer un double vote + pollAccount = service.getNewPollAccount(getPollenUserAccount()); + String votingId = pollAccount.getVotingId().trim(); + pollAccount.setVotingId(votingId); - public boolean isDateType() { - return poll.getChoiceType() == ChoiceType.DATE; - } + alreadyVoted = voteService.hasAlreadyVoted(votingId, poll); - public boolean isImageType() { - return poll.getChoiceType() == ChoiceType.IMAGE; - } + // Check for restricted poll + if (!alreadyVoted && !isFreePoll()) { - public boolean isNormalVoteCounting() { - return poll.getVoteCountingType() == VoteCountingType.NORMAL; - } + if (StringUtils.isNotEmpty(getAccountId())) { - public boolean isPercentageVoteCounting() { - return poll.getVoteCountingType() == VoteCountingType.PERCENTAGE; - } + // The accountUId must be valid for the poll + PollAccount restrictedAccount = + service.getRestrictedAccount(getPollId(), getAccountId()); - public boolean isCondorcetVoteCounting() { - return poll.getVoteCountingType() == VoteCountingType.CONDORCET; - } + if (restrictedAccount != null) { + // PollAccount is replaced by the good account from db + pollAccount = restrictedAccount; - public boolean isNumberVoteCounting() { - return poll.getVoteCountingType() == VoteCountingType.NUMBER; - } + // Refresh alreadyVoted value depends on pollAccount +// alreadyVoted = pollAccount.isHasVoted(); - public boolean isPollStarted() { - Date now = serviceContext.getCurrentTime(); - return poll.getBeginDate() == null || poll.getBeginDate().before(now); + } else { + // Existing account is null is forbidden for a restricted poll + restrictedListsForbidden = true; + } + + } else { + // Not allowed to vote without an accountUId + restrictedListsForbidden = true; + } + + if (restrictedListsForbidden) { + addActionError(_("pollen.error.user.restrictedListsForbidden")); + } + } + + // Génération d'un identifiant de vote (si le sondage est libre et anonyme) + if (poll.isAnonymous() && isFreePoll()) { + getPollAccount().setVotingId( + "anonymous" + serviceContext.createPollenUrlId()); + } + + modifAllowed = isModifAllowed(getPollAccount().getVotingId()); + if (alreadyVoted && !modifAllowed) { + addActionError(_("pollen.error.user.alreadyVoted", + getPollAccount().getVotingId())); + } + + // Initialize accountName + UserAccount userAccount = getPollenUserAccount(); + if (userAccount != null) { + accountName = userAccount.getDisplayName(); + } else { + accountName = getPollAccount().getAccountId(); + } } - public boolean isPollFinished() { - Date now = serviceContext.getCurrentTime(); - return poll.getEndDate() != null && poll.getEndDate().before(now); + protected void loadPollComments() { + + PollCommentService service = newService(PollCommentService.class); + + comments = service.getAllComments(poll.getPollId()); } - public String formatChoiceNameAsDate(Choice choice) { - return formatDate(new Date(Long.valueOf(choice.getName()))); + protected void loadPollResults() throws PollNotFoundException { + + PollResultsService service = getPollResultsService(); + + PollResultList pollResultList = service.getResults(poll.getPollId()); + + results = pollResultList.getPollResults(); + + if (log.isDebugEnabled()) { + for (PollResult res : results) { + log.debug(res.getName() + ": " + res.getValue() + + ", (voteCounting=" + res.getVoteCountingType() + + ", byGroup=" + res.isByGroup() + ")"); + } + } } } \ No newline at end of file Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddComment.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddComment.java 2012-03-23 08:56:10 UTC (rev 3202) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddComment.java 2012-03-23 17:15:45 UTC (rev 3203) @@ -42,10 +42,6 @@ /** Text of the comment. */ protected String commentText; - public void setCommentName(String commentName) { - this.commentName = commentName; - } - public String getCommentText() { return commentText; } @@ -60,23 +56,22 @@ loadPoll(); - Preconditions.checkNotNull(commentName); - Preconditions.checkNotNull(commentText); + Preconditions.checkNotNull(getCommentName()); + Preconditions.checkNotNull(getCommentText()); PollCommentService service = newService(PollCommentService.class); // prepare a new poll account for the comment PollAccount pollAccount = service.getNewPollAccount( - getPollenUserAccount(), commentName); + getPollenUserAccount(), getCommentName()); // prepare a new comment - Comment comment = service.getNewComment(pollAccount, commentText); + Comment comment = service.getNewComment(pollAccount, getCommentText()); // create the comment - service.createComment(poll, comment); + service.createComment(getPoll(), comment); - // let's use back default comment name - commentName = getDefaultCommentName(); + resetCommentName(); return SUCCESS; } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteComment.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteComment.java 2012-03-23 08:56:10 UTC (rev 3202) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteComment.java 2012-03-23 17:15:45 UTC (rev 3203) @@ -64,8 +64,7 @@ service.deleteComment(commentId); - commentName = getDefaultCommentName(); - + resetCommentName(); return SUCCESS; } } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ModifyVote.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ModifyVote.java 2012-03-23 08:56:10 UTC (rev 3202) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ModifyVote.java 2012-03-23 17:15:45 UTC (rev 3203) @@ -37,14 +37,8 @@ @Override public String execute() throws Exception { - loadPoll(); - - if (poll == null) { - - } - return SUCCESS; } } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java 2012-03-23 08:56:10 UTC (rev 3202) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java 2012-03-23 17:15:45 UTC (rev 3203) @@ -54,6 +54,5 @@ public void prepare() throws Exception { prepareVotePage(); - commentName = getDefaultCommentName(); } }
participants (1)
-
fdesbois@users.chorem.org