Author: tchemit Date: 2012-02-22 15:25:23 +0100 (Wed, 22 Feb 2012) New Revision: 3138 Url: http://chorem.org/repositories/revision/pollen/3138 Log: add poll comment actions + service Added: 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/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/resources/org/chorem/pollen/ui/actions/poll/ 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-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.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-ui-struts2/src/main/resources/config/struts-poll.xml 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 Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java 2012-02-22 14:20:18 UTC (rev 3137) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java 2012-02-22 14:25:23 UTC (rev 3138) @@ -58,9 +58,8 @@ public <E extends TopiaEntity> List<E> getEntities(Class<E> entityType) { Preconditions.checkNotNull(entityType); try { - List<E> result = PollenDAOHelper.<E, TopiaDAO<E>>getDAO( - getTransaction(), - entityType).findAll(); + TopiaDAO<E> dao = getDAO(entityType); + List<E> result = dao.findAll(); return result; } catch (TopiaException eee) { throw new PollenTechnicalException( @@ -72,9 +71,8 @@ Preconditions.checkNotNull(entityType); Preconditions.checkArgument(StringUtils.isNotEmpty(id)); try { - E result = PollenDAOHelper.<E, TopiaDAO<E>>getDAO( - getTransaction(), - entityType).findByTopiaId(id); + TopiaDAO<E> dao = getDAO(entityType); + E result = dao.findByTopiaId(id); return result; } catch (TopiaException eee) { throw new PollenTechnicalException( 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-02-22 14:20:18 UTC (rev 3137) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java 2012-02-22 14:25:23 UTC (rev 3138) @@ -23,17 +23,22 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.base.Preconditions; import org.chorem.pollen.PollenTechnicalException; import org.chorem.pollen.business.persistence.Comment; import org.chorem.pollen.business.persistence.CommentDAO; -import org.chorem.pollen.business.persistence.CommentImpl; 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.entities.PollenDAOHelper; +import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.services.PollenServiceSupport; import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.web.struts2.FilterPagerUtil; +import java.util.List; + /** * Manage comments on a poll. * @@ -42,56 +47,138 @@ */ public class PollCommentService extends PollenServiceSupport { - public int getNbComments(Poll poll) { - try { - PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); + public Comment createComment(Poll poll, Comment comment) { - Poll pollFound = dao.findByTopiaId(poll.getTopiaId()); + Preconditions.checkNotNull(poll); - int result = pollFound.sizeComment(); - return result; - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } + PollDAO pollDAO = getDAO(Poll.class); + PollAccountDAO pollAccountDAO = getDAO(PollAccount.class); + CommentDAO dao = getDAO(Comment.class); - public Comment createComment(Poll poll, Comment comment) { try { - CommentDAO dao = PollenDAOHelper.getCommentDAO(getTransaction()); + // 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, poll, - Comment.PROPERTY_POST_DATE, serviceContext.getCurrentTime(), + Comment.PROPERTY_POLL_ACCOUNT, pollAccountCreated, + Comment.PROPERTY_POLL, pollToUpdate, + Comment.PROPERTY_POST_DATE, comment.getPostDate(), Comment.PROPERTY_TEXT, comment.getText() ); - PollDAO pollDAO = PollenDAOHelper.getPollDAO(getTransaction()); - - Poll pollToUpdate = pollDAO.findByTopiaId(poll.getTopiaId()); - + // add it to poll pollToUpdate.addComment(commentCreated); - pollDAO.update(pollToUpdate); return commentCreated; } catch (TopiaException e) { throw new PollenTechnicalException(e); } } - public void deleteComment(Comment comment) { + public void deleteComment(String commentId) { + + Preconditions.checkNotNull(commentId); + + CommentDAO dao = getDAO(Comment.class); + try { - CommentDAO dao = PollenDAOHelper.getCommentDAO(getTransaction()); + + Comment comment = dao.findByTopiaId(commentId); + + if (comment == null) { + throw new PollenTechnicalException( + "Poll comment with id [" + commentId + "] not found "); + } + + //FIXME Should we also delete the associated pollAccount ? dao.delete(comment); } catch (TopiaException e) { throw new PollenTechnicalException(e); } } - public Comment getNewComment(PollAccount account) { - Comment result = new CommentImpl(); + public Comment getNewComment(PollAccount account, String text) { + + CommentDAO dao = getDAO(Comment.class); + Comment result = newInstance(dao); + if (account != null) { result.setPollAccount(account); } + 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, + UserAccount userAccount) { + + boolean result = false; + + if (userAccount != null) { + + // loggued + + if (userAccount.isAdministrator()) { + + // administrator can always delete everything + result = true; + + } else { + + PollAccount pollAccount = comment.getPollAccount(); + if (userAccount.equals(pollAccount.getUserAccount())) { + result = true; + } + } + } + + return result; + } + + public List<Comment> getComments(FilterPagerUtil.FilterPagerBean pager, + String pollId) { + + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(pollId); + + CommentDAO dao = getDAO(Comment.class); + + try { + + TopiaQuery countQuery = dao.createQuery("e"). + addWhere("e." + Comment.PROPERTY_POLL + ".id", + TopiaQuery.Op.EQ, pollId); + long records = dao.countByQuery(countQuery); + pager.setRecords((int) records); + + TopiaQuery query = FilterPagerUtil.addPagerToQuery(countQuery, + pager); + List<Comment> result = dao.findAllByQuery(query); + return result; + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } } Added: 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 (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPollComments.java 2012-02-22 14:25:23 UTC (rev 3138) @@ -0,0 +1,111 @@ +/* + * #%L + * Pollen :: UI (strust2) + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * 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.json; + +import com.google.common.base.Preconditions; +import org.chorem.pollen.business.persistence.Comment; +import org.chorem.pollen.business.persistence.UserAccount; +import org.chorem.pollen.entities.PollenBinderHelper; +import org.chorem.pollen.services.impl.PollCommentService; +import org.nuiton.util.beans.Binder; + +import java.util.List; +import java.util.Map; + +/** + * Get paginated list of poll comments. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class GetPollComments extends AbstractJSONPaginedAction { + + private static final long serialVersionUID = 1L; + + protected transient Map<String, Object>[] comments; + + protected String pollId; + + public void setPollId(String pollId) { + this.pollId = pollId; + } + + public Map<String, Object>[] getComments() { + return comments; + } + + @Override + public Integer getRows() { + return pager.getPageSize(); + } + + @Override + public Integer getPage() { + return pager.getPageIndex(); + } + + @Override + public Integer getTotal() { + return pager.getPagesNumber(); + } + + @Override + public Integer getRecords() { + return pager.getRecords(); + } + + @Override + public String execute() throws Exception { + + Preconditions.checkNotNull(pollId); + + PollCommentService pollService = newService(PollCommentService.class); + + List<Comment> commentList = pollService.getComments(pager, pollId); + + UserAccount userAccount = getPollenSession().getUserAccount(); + + comments = new Map[commentList.size()]; + Binder<Comment, Comment> binder = + PollenBinderHelper.getSimpleTopiaBinder(Comment.class); + int index = 0; + for (Comment comment : commentList) { + + Map<String, Object> map = binder.obtainProperties( + comment, + Comment.PROPERTY_TEXT, + Comment.PROPERTY_POST_DATE + ); + map.put("name", comment.getPollAccount().getVotingId()); + map.put("id", comment.getTopiaId()); + boolean canDelete = pollService.isCanDeleteComment(comment, + userAccount); + map.put("canDelete", canDelete); + comments[index++] = map; + } + return SUCCESS; + } + + +} \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/json/GetPollComments.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: 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 (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddComment.java 2012-02-22 14:25:23 UTC (rev 3138) @@ -0,0 +1,98 @@ +/* + * #%L + * Pollen :: UI (strust2) + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * 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.Preconditions; +import org.chorem.pollen.business.persistence.Comment; +import org.chorem.pollen.business.persistence.Poll; +import org.chorem.pollen.business.persistence.PollAccount; +import org.chorem.pollen.services.impl.PollCommentService; +import org.chorem.pollen.ui.actions.PollenActionSupport; + +/** + * To add a poll comment. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class AddComment extends PollenActionSupport { + + private static final long serialVersionUID = 1L; + + protected String pollId; + + protected String name; + + protected String text; + + public String getPollId() { + return pollId; + } + + public void setPollId(String pollId) { + this.pollId = pollId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + @Override + public String execute() throws Exception { + + Preconditions.checkNotNull(pollId); + Preconditions.checkNotNull(text); + Preconditions.checkNotNull(name); + + PollCommentService service = newService(PollCommentService.class); + + Poll poll = service.getEntityById(Poll.class, pollId); + + // prepare a new poll account for the comment + PollAccount pollAccount = service.getNewPollAccount( + getPollenSession().getUserAccount(), name); + + // prepare a new comment + Comment comment = service.getNewComment(pollAccount, text); + + // create the comment + service.createComment(poll, comment); + + getTransaction().commitTransaction(); + + return SUCCESS; + } +} Property changes on: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AddComment.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: 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 (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteComment.java 2012-02-22 14:25:23 UTC (rev 3138) @@ -0,0 +1,59 @@ +/* + * #%L + * Pollen :: UI (strust2) + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * 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.Preconditions; +import org.chorem.pollen.services.impl.PollCommentService; +import org.chorem.pollen.ui.actions.PollenActionSupport; + +/** + * To delete a poll comment. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class DeleteComment extends PollenActionSupport { + + private static final long serialVersionUID = 1L; + + protected String pollId; + + protected String commentId; + + + @Override + public String execute() throws Exception { + + Preconditions.checkNotNull(pollId); + Preconditions.checkNotNull(commentId); + + PollCommentService service = newService(PollCommentService.class); + + service.deleteComment(commentId); + + getTransaction().commitTransaction(); + + return SUCCESS; + } +} Property changes on: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/DeleteComment.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-02-22 14:20:18 UTC (rev 3137) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/config/struts-poll.xml 2012-02-22 14:25:23 UTC (rev 3138) @@ -50,7 +50,7 @@ <action name="vote" class="org.chorem.pollen.ui.actions.poll.Vote"> <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> - <result>/WEB-INF/jsp/home.jsp</result> + <result>/WEB-INF/jsp/poll/vote.jsp</result> </action> <!-- clone poll --> @@ -104,7 +104,20 @@ <result>/WEB-INF/jsp/home.jsp</result> </action> + <!-- add a poll comment --> + <action name="addComment" + class="org.chorem.pollen.ui.actions.poll.AddComment"> + <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> + <result>/WEB-INF/jsp/poll/vote.jsp</result> + </action> + <!-- delete a poll comment --> + <action name="deleteComment" + class="org.chorem.pollen.ui.actions.poll.DeleteComment"> + <result name="input">/WEB-INF/jsp/poll/vote.jsp</result> + <result>/WEB-INF/jsp/poll/vote.jsp</result> + </action> + </package> 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-02-22 14:20:18 UTC (rev 3137) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-02-22 14:25:23 UTC (rev 3138) @@ -62,6 +62,8 @@ pollen.common.title=Title pollen.common.userSupport=User support pollen.common.voteCountingType=Counting type +pollen.error.comment.name.empty=Comment name mandatory +pollen.error.comment.text.empty=Comment text mandatory pollen.error.email.required=You must provide an email pollen.error.favoriteList.already.used=List name already used pollen.error.favoriteList.not.found=Favorite list not found 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-02-22 14:20:18 UTC (rev 3137) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-02-22 14:25:23 UTC (rev 3138) @@ -63,6 +63,8 @@ pollen.common.userSupport=Support utilisateur pollen.common.voteCountingType=Type de dépouillement pollen.common.x=Dépouillement +pollen.error.comment.name.empty=Nom du commentaire obligatoire +pollen.error.comment.text.empty=Texte du commentaire obligatoire pollen.error.email.required=Courriel obligatoire pollen.error.favoriteList.already.used=Nom de liste déjà utilisé pollen.error.favoriteList.not.found=Liste non trouvée Added: 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 (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AddComment-addComment-validation.xml 2012-02-22 14:25:23 UTC (rev 3138) @@ -0,0 +1,47 @@ +<?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 + "-//OpenSymphony Group//XWork Validator 1.0.2//EN" + "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> + +<validators> + + <field name="name"> + + <field-validator type="requiredstring"> + <message key="pollen.error.comment.name.empty"/> + </field-validator> + </field> + + <field name="text"> + + <field-validator type="requiredstring"> + <message key="pollen.error.comment.text.empty"/> + </field-validator> + </field> + +</validators> \ No newline at end of file Property changes on: branches/pollen-1.2.6-struts2/pollen-ui-struts2/src/main/resources/org/chorem/pollen/ui/actions/poll/AddComment-addComment-validation.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native