Author: fdesbois Date: 2012-04-02 11:29:44 +0200 (Mon, 02 Apr 2012) New Revision: 3223 Url: http://chorem.org/repositories/revision/pollen/3223 Log: - add comment author new column - improve pollAccount link with comment - add comment validation in action instead of xml Removed: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AddComment-addComment-validation.xml Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_2_6.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.zargo 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-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPollComments.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/AddComment.java branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/confirmDeletePollComment.jsp branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_2_6.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_2_6.java 2012-04-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_2_6.java 2012-04-02 09:29:44 UTC (rev 3223) @@ -82,8 +82,20 @@ // add foreign key indexes addForeignKeyIndexes(queries); + + // add author column for Comment + addCommentAuthorColumn(queries); } + + private void addCommentAuthorColumn(List<String> queries) { + queries.add("ALTER TABLE comment ADD COLUMN author VARCHAR(255);"); + + queries.add("UPDATE comment c SET c.author = (SELECT p.votingId FROM pollAccount p WHERE c.pollAccount = p.topiaId);"); + + queries.add("ALTER TABLE comment ALTER COLUMN author VARCHAR(255) NOT NULL;"); + } + private void addForeignKeyIndexes(List<String> queries) { queries.add("CREATE INDEX idx_PollAccount_pollsCreated ON poll(creator);"); Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.zargo =================================================================== (Binary files differ) 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-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java 2012-04-02 09:29:44 UTC (rev 3223) @@ -29,7 +29,6 @@ import org.chorem.pollen.business.persistence.CommentDAO; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.business.persistence.PollAccountDAO; import org.chorem.pollen.business.persistence.PollDAO; import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.services.PollenServiceSupport; @@ -52,31 +51,42 @@ Preconditions.checkNotNull(poll); PollDAO pollDAO = getDAO(Poll.class); - PollAccountDAO pollAccountDAO = getDAO(PollAccount.class); CommentDAO dao = getDAO(Comment.class); try { - // creates poll account - PollAccount pollAccountCreated = - pollAccountDAO.create(comment.getPollAccount()); - // get last version of the poll Poll pollToUpdate = pollDAO.findByTopiaId(poll.getTopiaId()); // creates the poll comment Comment commentCreated = dao.create( - Comment.PROPERTY_POLL_ACCOUNT, pollAccountCreated, Comment.PROPERTY_POLL, pollToUpdate, Comment.PROPERTY_POST_DATE, comment.getPostDate(), + Comment.PROPERTY_AUTHOR, comment.getAuthor(), Comment.PROPERTY_TEXT, comment.getText() ); // add it to poll pollToUpdate.addComment(commentCreated); - commitTransaction("Could not create comment " + comment.getText()); + // attach poll account if exists + PollAccount pollAccount = comment.getPollAccount(); + if (pollAccount.getTopiaId() != null) { + PollAccount pollAccountLoaded = getEntityById(PollAccount.class, pollAccount.getTopiaId()); + + // attach the userAccount + if (pollAccountLoaded.getUserAccount() == null && pollAccount.getUserAccount() != null) { + String userId = pollAccount.getUserAccount().getTopiaId(); + UserAccount userAccountLoaded = getEntityById(UserAccount.class, userId); + pollAccountLoaded.setUserAccount(userAccountLoaded); + } + + commentCreated.setPollAccount(pollAccountLoaded); + } + + commitTransaction("Could not create comment (" + comment.getAuthor() + ") " + comment.getText()); + // feed notification PollService pollService = newService(PollService.class); @@ -105,7 +115,6 @@ throw new PollCommentNotFound(); } - //FIXME Should we also delete the associated pollAccount ? dao.delete(comment); commitTransaction("Could not delete comment " + comment.getText()); @@ -115,7 +124,7 @@ } } - public Comment getNewComment(PollAccount account, String text) { + public Comment getNewComment(PollAccount account, String author, String text) { CommentDAO dao = getDAO(Comment.class); Comment result = newInstance(dao); @@ -123,49 +132,35 @@ if (account != null) { result.setPollAccount(account); } + result.setAuthor(author); result.setText(text); result.setPostDate(serviceContext.getCurrentTime()); return result; } - public PollAccount getNewPollAccount(UserAccount user, String votingId) { - PollAccountDAO dao = getDAO(PollAccount.class); - PollAccount result = newInstance(dao); - if (user != null) { - result.setVotingId(user.getDisplayName()); - result.setAccountId(serviceContext.createPollenUrlId()); - result.setEmail(user.getEmail()); - result.setUserAccount(user); - } - if (votingId != null) { - result.setVotingId(votingId); - } - return result; - } - public boolean isCanDeleteComment(Comment comment, + PollAccount pollAccount, UserAccount userAccount) { - boolean result = false; + boolean result; + PollAccount commentAccount = comment.getPollAccount(); + + // loggued if (userAccount != null) { - // loggued + boolean isAdmin = userAccount.isAdministrator(); + boolean isCommentAccount = userAccount.equals(commentAccount.getUserAccount()); - if (userAccount.isAdministrator()) { + result = isAdmin || isCommentAccount; - // administrator can always delete everything - result = true; + } else if (pollAccount != null) { - } else { + result = pollAccount.equals(commentAccount); - PollAccount pollAccount = comment.getPollAccount(); - if (userAccount.equals(pollAccount.getUserAccount())) { - result = true; - } - } + } else { + result = false; } - return result; } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPollComments.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPollComments.java 2012-04-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPollComments.java 2012-04-02 09:29:44 UTC (rev 3223) @@ -117,6 +117,7 @@ UserAccount userAccount) { Set<String> result = Sets.newHashSet(); boolean canDelete = pollService.isCanDeleteComment(comment, + null, userAccount); if (canDelete) { result.add("delete"); 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-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-04-02 09:29:44 UTC (rev 3223) @@ -39,6 +39,7 @@ import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.common.PollType; import org.chorem.pollen.common.VoteCountingType; +import org.chorem.pollen.services.exceptions.PollAccountNotFound; import org.chorem.pollen.services.exceptions.PollNotFoundException; import org.chorem.pollen.services.impl.PollCommentService; import org.chorem.pollen.services.impl.PollFeedService; @@ -70,6 +71,8 @@ private transient PollService pollService; private transient VoteService voteService; + + private transient PollCommentService pollCommentService; private Poll poll; @@ -87,7 +90,7 @@ private Vote vote; - private String commentName; + private String commentAuthor; private String voteSizeMessage; @@ -123,6 +126,13 @@ return voteService; } + protected PollCommentService getPollCommentService() { + if (pollCommentService == null) { + pollCommentService = newService(PollCommentService.class); + } + return pollCommentService; + } + @Override public void setParameters(Map<String, String[]> parameters) { this.parameters = parameters; @@ -248,19 +258,19 @@ return poll.getEndDate() != null && poll.getEndDate().before(now); } - public String getCommentName() { - return commentName; + public String getCommentAuthor() { + if (commentAuthor == null) { + // Use current pollAccount name for comment + commentAuthor = getPollAccount().getVotingId(); + } + return commentAuthor; } - public void setCommentName(String commentName) { - this.commentName = commentName; + public void setCommentAuthor(String commentAuthor) { + this.commentAuthor = commentAuthor; } - - public void resetCommentName() { - setCommentName(getPollAccount().getVotingId()); - } - public void prepareVotePage() throws Exception { + public String prepareVotePage() throws Exception { loadPoll(); @@ -269,7 +279,7 @@ DateFormat.SHORT, getLocale())); // Current poll account - pollAccount = getPollService().getPollAccountEditable(getAccountId(), getPollenUserAccount(), poll); + loadPollAccount(); // All votes // TODO no pagination for the moment, need to retrieve the correct page depends on current pollAccount @@ -309,8 +319,6 @@ creatorUser = getPollenUserAccount() != null && getPollenUserAccount().equals(poll.getCreator().getUserAccount()); - resetCommentName(); - if (log.isInfoEnabled()) { Date now = serviceContext.getCurrentTime(); log.info("pollChoiceOrVoteStarted = " + isPollChoiceOrVoteStarted()); @@ -319,6 +327,7 @@ log.info("accountFieldDisplayed = " + isAccountFieldDisplayed()); log.info("creatorUser = " + creatorUser); } + return INPUT; } public String getResultValue(Choice choice) { @@ -362,6 +371,10 @@ return getVoteService().isVoteAllowed(getPoll(), getPollAccount()); } + public boolean isDeleteCommentAllowed(Comment comment) { + return isCreatorUser() || getPollCommentService().isCanDeleteComment(comment, getPollAccount(), getPollenUserAccount()); + } + public String escapeLineBreak(String text) { return text; } @@ -370,6 +383,12 @@ return formatDate(new Date(Long.valueOf(choice.getName()))); } + protected void loadPollAccount() throws PollAccountNotFound { + + // Current poll account + pollAccount = getPollService().getPollAccountEditable(getAccountId(), getPollenUserAccount(), poll); + } + protected void loadPoll() throws PollNotFoundException { // Ensure uri for poll and pollAccount loading 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-04-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddComment.java 2012-04-02 09:29:44 UTC (rev 3223) @@ -23,11 +23,10 @@ */ package org.chorem.pollen.ui.actions.poll; -import com.google.common.base.Preconditions; +import com.opensymphony.xwork2.Preparable; import com.opensymphony.xwork2.interceptor.annotations.InputConfig; +import org.apache.commons.lang3.StringUtils; import org.chorem.pollen.business.persistence.Comment; -import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.services.impl.PollCommentService; /** * To add a poll comment. @@ -35,43 +34,50 @@ * @author tchemit <chemit@codelutin.com> * @since 1.2.6 */ -public class AddComment extends AbstractVoteAction { +public class AddComment extends AbstractVoteAction implements Preparable { private static final long serialVersionUID = 1L; + + private String commentText; - /** Text of the comment. */ - protected String commentText; + public void setCommentText(String commentText) { + this.commentText = commentText; + } public String getCommentText() { return commentText; } - public void setCommentText(String commentText) { - this.commentText = commentText; + @Override + public void prepare() throws Exception { + + loadPoll(); + loadPollAccount(); } - @InputConfig(methodName = "prepareVotePage") @Override - public String execute() throws Exception { + public void validate() { - loadPoll(); + if (StringUtils.isBlank(getCommentAuthor())) { + addFieldError("commentAuthor", _("pollen.error.comment.name.empty")); + } - Preconditions.checkNotNull(getCommentName()); - Preconditions.checkNotNull(getCommentText()); + if (StringUtils.isBlank(getCommentText())) { + addFieldError("commentText", _("pollen.error.comment.text.empty")); + } + } - PollCommentService service = newService(PollCommentService.class); + @InputConfig(methodName = "prepareVotePage") + @Override + public String execute() throws Exception { - // prepare a new poll account for the comment - PollAccount pollAccount = service.getNewPollAccount( - getPollenUserAccount(), getCommentName()); - // prepare a new comment - Comment comment = service.getNewComment(pollAccount, getCommentText()); + Comment comment = getPollCommentService().getNewComment( + getPollAccount(), getCommentAuthor(), getCommentText()); // create the comment - service.createComment(getPoll(), comment); + getPollCommentService().createComment(getPoll(), comment); return SUCCESS; } - } Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-04-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-04-02 09:29:44 UTC (rev 3223) @@ -53,7 +53,7 @@ pollen.common.choice.date=Date pollen.common.choice.image=Image pollen.common.choice.text=Label -pollen.common.commentName=Name +pollen.common.commentAuthor=Name pollen.common.commentText=Comment pollen.common.comments=Comments about this poll pollen.common.csvImport=CSV import Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-04-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-04-02 09:29:44 UTC (rev 3223) @@ -58,7 +58,7 @@ pollen.common.choice.date=Date pollen.common.choice.image=Image pollen.common.choice.text=Libellé -pollen.common.commentName=Nom +pollen.common.commentAuthor=Nom pollen.common.commentText=Commentaire pollen.common.comments=Commentaire à propos du sondage pollen.common.csvImport=Import CSV Deleted: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AddComment-addComment-validation.xml =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AddComment-addComment-validation.xml 2012-04-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AddComment-addComment-validation.xml 2012-04-02 09:29:44 UTC (rev 3223) @@ -1,46 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - #%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% - --> - - -<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" - "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd"> - -<validators> - - <field name="commentName"> - - <field-validator type="requiredstring"> - <message key="pollen.error.comment.name.empty"/> - </field-validator> - </field> - - <field name="commentText"> - - <field-validator type="requiredstring"> - <message key="pollen.error.comment.text.empty"/> - </field-validator> - </field> - -</validators> \ No newline at end of file Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/confirmDeletePollComment.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/confirmDeletePollComment.jsp 2012-04-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/confirmDeletePollComment.jsp 2012-04-02 09:29:44 UTC (rev 3223) @@ -33,8 +33,8 @@ <s:textfield key='comment.postDate' readonly="true" size="50" label="%{getText('pollen.common.postDate')}"/> - <s:textfield key='comment.pollAccount.votingId' readonly="true" size="50" - label="%{getText('pollen.common.commentName')}"/> + <s:textfield key='comment.author' readonly="true" size="50" + label="%{getText('pollen.common.commentAuthor')}"/> <s:textarea key='comment.text' readonly="true" rows="3" label="%{getText('pollen.common.commentText')}"/> Modified: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp =================================================================== --- branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp 2012-04-02 09:29:31 UTC (rev 3222) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/webapp/WEB-INF/jsp/poll/vote.jsp 2012-04-02 09:29:44 UTC (rev 3223) @@ -472,7 +472,7 @@ <s:if test="#status.even">class="even"</s:if> <s:else>class="odd"</s:else> > - <s:if test="creatorUser"> + <s:if test="isDeleteCommentAllowed(#comment)"> <span class="cmd"> <s:a href="#" onclick="return openDeleteCommentDialog('%{#comment.topiaId}');"> @@ -482,7 +482,7 @@ </s:a> </span> </s:if> - <span class="name"><s:property value='pollAccount.votingId'/></span> : + <span class="name"><s:property value='#comment.author'/></span> : <span class="date"> <s:property value='%{formatDateTime(postDate)}'/> </span> @@ -530,8 +530,8 @@ <div id="commentFormDiv"> <s:form id='addCommentForm' method="POST" namespace="/poll" validate="true"> - <s:textfield key="commentName" required="true" size="78" - label="%{getText('pollen.common.commentName')}"/> + <s:textfield key="commentAuthor" required="true" size="78" + label="%{getText('pollen.common.commentAuthor')}"/> <s:textarea key="commentText" required="true" value='' label="%{getText('pollen.common.commentText')}"/>