From 529bebd226c5f8d51770463044d098962f5332bb Mon Sep 17 00:00:00 2001 From: ajsteadly Date: Mon, 20 Mar 2017 20:03:53 -0300 Subject: [PATCH] Comments and votes now deleted when the associated post is deleted. --- .../compute/feedback/FeedbackService.java | 5 ++++ .../feedback/FeedbackServiceInterface.java | 9 ++++++- .../piccritic/compute/post/PostService.java | 21 +++++++++++++-- .../compute/post/PostServiceInterface.java | 6 ++++- .../compute/post/PostServiceTest.java | 27 +++++++++++++++++-- .../database/feedback/JPAVoteConnector.java | 8 ++++++ .../database/feedback/VoteConnector.java | 9 +++++++ .../com/piccritic/website/post/ViewPost.java | 4 ++- 8 files changed, 82 insertions(+), 7 deletions(-) diff --git a/compute/src/main/java/com/piccritic/compute/feedback/FeedbackService.java b/compute/src/main/java/com/piccritic/compute/feedback/FeedbackService.java index 428317c..42deff4 100644 --- a/compute/src/main/java/com/piccritic/compute/feedback/FeedbackService.java +++ b/compute/src/main/java/com/piccritic/compute/feedback/FeedbackService.java @@ -298,4 +298,9 @@ public Rating updateRating(Rating rating) throws RatingException { public Rating selectRating(Long id) { return rc.selectRating(id); } + + @Override + public List getVotes(Comment comment) { + return vc.getVotes(comment); + } } diff --git a/compute/src/main/java/com/piccritic/compute/feedback/FeedbackServiceInterface.java b/compute/src/main/java/com/piccritic/compute/feedback/FeedbackServiceInterface.java index deae1f8..2f38494 100644 --- a/compute/src/main/java/com/piccritic/compute/feedback/FeedbackServiceInterface.java +++ b/compute/src/main/java/com/piccritic/compute/feedback/FeedbackServiceInterface.java @@ -39,7 +39,7 @@ public interface FeedbackServiceInterface { /** * Gets all comments associated with the post * @param post - * @return An ArrayList of comments for the given post + * @return An List of comments for the given post * @throws PostException */ List getComments(Post post) throws PostException; @@ -95,6 +95,13 @@ public interface FeedbackServiceInterface { * @return score of given comment */ int getScore(Comment comment); + + /** + * Returns all votes associated with comment + * @param comment + * @return + */ + List getVotes(Comment comment); /** * Method to calculate total reputation given by comments for a user diff --git a/compute/src/main/java/com/piccritic/compute/post/PostService.java b/compute/src/main/java/com/piccritic/compute/post/PostService.java index 1a8a503..8919cac 100644 --- a/compute/src/main/java/com/piccritic/compute/post/PostService.java +++ b/compute/src/main/java/com/piccritic/compute/post/PostService.java @@ -12,6 +12,12 @@ import org.hibernate.Hibernate; +import com.piccritic.compute.feedback.FeedbackService; +import com.piccritic.compute.feedback.FeedbackServiceInterface; +import com.piccritic.database.feedback.Comment; +import com.piccritic.database.feedback.CommentException; +import com.piccritic.database.feedback.Vote; +import com.piccritic.database.feedback.VoteException; import com.piccritic.database.post.Album; import com.piccritic.database.post.AlbumException; import com.piccritic.database.post.JPAPostConnector; @@ -32,6 +38,7 @@ public class PostService implements PostServiceInterface { public static final String USERS_DIR = "users"; static PostConnector pc = new JPAPostConnector(); + private FeedbackServiceInterface fs = FeedbackService.createService(); public File getImageFile(String handle) { Path p0 = Paths.get(USERS_DIR, handle); @@ -82,15 +89,25 @@ public Post createPost(Post post) throws PostException, AlbumException{ return pc.updatePost(post) ; } - public boolean deletePost(Post post) throws PostException{ + public boolean deletePost(Post post) throws PostException, CommentException, VoteException { if (post == null) { throw new PostException("Cannot delete null post"); } + + List comments = fs.getComments(post); + for (Comment comment : comments) { + List votes = fs.getVotes(comment); + for (Vote vote : votes) { + fs.deleteVote(vote); + } + fs.deleteComment(comment); + } + File image = new File(post.getPath()); if (image.exists()) { image.delete(); } - + return pc.deletePost(post); } diff --git a/compute/src/main/java/com/piccritic/compute/post/PostServiceInterface.java b/compute/src/main/java/com/piccritic/compute/post/PostServiceInterface.java index f1b5e7f..448df32 100644 --- a/compute/src/main/java/com/piccritic/compute/post/PostServiceInterface.java +++ b/compute/src/main/java/com/piccritic/compute/post/PostServiceInterface.java @@ -3,6 +3,8 @@ import java.io.File; import java.util.List; +import com.piccritic.database.feedback.CommentException; +import com.piccritic.database.feedback.VoteException; import com.piccritic.database.post.Album; import com.piccritic.database.post.AlbumException; import com.piccritic.database.post.Post; @@ -45,8 +47,10 @@ public interface PostServiceInterface { * @param post * to delete * @return Post object deleted + * @throws CommentException + * @throws VoteException */ - public boolean deletePost(Post post) throws PostException; + public boolean deletePost(Post post) throws PostException, CommentException, VoteException; /** * Returns a user's default album diff --git a/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java b/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java index 331662e..c65be53 100644 --- a/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java +++ b/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java @@ -12,6 +12,11 @@ import org.junit.Before; import org.junit.Test; +import com.piccritic.compute.feedback.FeedbackService; +import com.piccritic.compute.feedback.FeedbackServiceInterface; +import com.piccritic.database.feedback.Comment; +import com.piccritic.database.feedback.CommentException; +import com.piccritic.database.feedback.VoteException; import com.piccritic.database.license.AttributionLicense; import com.piccritic.database.post.Album; import com.piccritic.database.post.AlbumException; @@ -33,9 +38,13 @@ public class PostServiceTest { private Set postSet; private Critic critic; private Set albumSet; + private Comment comment; + private Set criticComments = new HashSet(); + private Set postComments = new HashSet(); private JPAUserConnector uc = new JPAUserConnector(); private JPAPostConnector pc = new JPAPostConnector(); private PostService ps = new PostService(); + private FeedbackServiceInterface fs = FeedbackService.createService(); @Before public void setup() throws Exception{ @@ -45,12 +54,14 @@ public void setup() throws Exception{ postSet.add(post); critic = new Critic(); albumSet = new HashSet(); + comment = new Comment(); critic.setFirstName("firstName"); critic.setLastName("lastName"); critic.setJoinDate(new Date(0)); critic.setLicense(new AttributionLicense()); critic.setHandle("handle"); + critic.setComments(criticComments); albumSet.add(album); album.setName("albumName"); @@ -63,8 +74,16 @@ public void setup() throws Exception{ post.setUploadDate(null); post.setLicense(new AttributionLicense()); post.setAlbum(album); + post.setComments(postComments); - uc.insertCritic(critic, "hash"); + postComments.add(comment); + comment.setContent("this is a comment"); + comment.setCreationDate(new Date(0)); + criticComments.add(comment); + comment.setReplies(new HashSet()); + comment.setScore(0); + + critic = uc.insertCritic(critic, "hash"); pc.insertAlbum(album); post.setPath("path"); } @@ -99,9 +118,13 @@ public void testEditPost(){ public void testDeletePost(){ try{ Post created = ps.createPost(post); + comment.setPost(created); + comment.setCritic(critic); + comment = fs.insertComment(comment); ps.deletePost(created); + post.setComments(new HashSet()); pc.insertPost(post); - } catch(PostException | AlbumException e){ + } catch(PostException | AlbumException | CommentException | VoteException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } diff --git a/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java b/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java index dcda1b0..5308ad1 100644 --- a/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java +++ b/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java @@ -172,4 +172,12 @@ public int getScore(Comment comment) { } return score; } + + @Override + public List getVotes(Comment comment) { + String query = "SELECT v FROM Vote v WHERE v.comment = :comment"; + TypedQuery q = votes.getEntityProvider().getEntityManager().createQuery(query, Vote.class) + .setParameter("comment", comment); + return q.getResultList(); + } } diff --git a/database/src/main/java/com/piccritic/database/feedback/VoteConnector.java b/database/src/main/java/com/piccritic/database/feedback/VoteConnector.java index 7020017..10e629e 100644 --- a/database/src/main/java/com/piccritic/database/feedback/VoteConnector.java +++ b/database/src/main/java/com/piccritic/database/feedback/VoteConnector.java @@ -5,6 +5,8 @@ package com.piccritic.database.feedback; +import java.util.List; + import com.piccritic.database.user.Critic; /** @@ -56,4 +58,11 @@ public interface VoteConnector { * @return */ public int getScore(Comment comment); + + /** + * Returns the votes associated with the comment + * @param comment + * @return + */ + public List getVotes(Comment comment); } diff --git a/website/src/main/java/com/piccritic/website/post/ViewPost.java b/website/src/main/java/com/piccritic/website/post/ViewPost.java index 53d36f8..a44e888 100644 --- a/website/src/main/java/com/piccritic/website/post/ViewPost.java +++ b/website/src/main/java/com/piccritic/website/post/ViewPost.java @@ -9,6 +9,8 @@ import com.piccritic.compute.user.UserService; import com.piccritic.compute.user.UserServiceInterface; import com.piccritic.database.feedback.Comment; +import com.piccritic.database.feedback.CommentException; +import com.piccritic.database.feedback.VoteException; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostException; import com.piccritic.website.feedback.CommentComponent; @@ -81,7 +83,7 @@ public void enter(ViewChangeEvent event) { try { service.deletePost(post); Notification.show("Post deleted successfuly", Type.TRAY_NOTIFICATION); - } catch (PostException e1) { + } catch (PostException | CommentException | VoteException e1) { Notification.show(e1.getLocalizedMessage(), Type.WARNING_MESSAGE); } });