From 057a034780bf8b0436bacbe66caac41468022a8e Mon Sep 17 00:00:00 2001 From: STG-LV-Havana Date: Wed, 19 May 2021 10:16:23 +0300 Subject: [PATCH] delete comments --- .../fr/lirmm/aren/service/CommentService.java | 52 +++++++++++++++++-- .../lirmm/aren/ws/rest/CommentRESTFacade.java | 16 ++++-- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/main/java/fr/lirmm/aren/service/CommentService.java b/src/main/java/fr/lirmm/aren/service/CommentService.java index 84b14b3..ed8f508 100644 --- a/src/main/java/fr/lirmm/aren/service/CommentService.java +++ b/src/main/java/fr/lirmm/aren/service/CommentService.java @@ -1,14 +1,12 @@ package fr.lirmm.aren.service; -import java.util.List; +import java.util.*; import javax.ws.rs.NotFoundException; import fr.lirmm.aren.model.Comment; import fr.lirmm.aren.model.TagSet; -import java.util.Set; -import java.util.HashSet; -import java.util.Iterator; + import java.util.function.BiConsumer; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; @@ -90,6 +88,51 @@ public class CommentService extends AbstractService { .getResultList()); } + /** + * + * @param commentId + * @return + */ + public void clear(Long commentId) { + Comment comment=this.find(commentId) ; + super.remove(comment); + this.afterRemove(comment) ; + } + + public void removeTreeComment(Long id){ + System.out.println("id : "+id); + List comments = getEntityManager().createNativeQuery("WITH RECURSIVE _comments AS ( " + + "SELECT id, created,argumentation,end_container,end_offset,hypostases,moderated,opinion,proposed_tags,reformulation,selection,signaled,start_container,start_offset,tags,owner_id,debate_id,parent_id " + +"FROM comments " + + "WHERE id=?1 " + +"UNION SELECT c.id, c.created,c.argumentation,c.end_container,c.end_offset,c.hypostases,c.moderated,c.opinion,c.proposed_tags,c.reformulation,c.selection,c.signaled,c.start_container,c.start_offset,c.tags,c.owner_id,c.debate_id,c.parent_id " + +"FROM comments c " + +"INNER JOIN _comments _coms ON _coms.id=c.parent_id" + +") SELECT * FROM _comments " + +"ORDER BY id DESC", Comment.class) + .setParameter(1,id) + .getResultList(); + + + if (!comments.isEmpty()) { + //Collections.sort(comments, (c1, c2) -> (c1.getParent()!=null && c2.getParent()!=null)?c1.getParent().getId().compareTo(c1.getParent().getId()):-1); + + for(Comment comment : comments){ + this.deleteNotification(comment.getId()) ; + this.remove(comment); + } + } + } + + private void deleteNotification(Long idComment){ + this.transactionBegin(); + getEntityManager().createQuery("DELETE FROM Notification notif " + + "WHERE notif.comment.id = :idComment") + .setParameter("idComment", idComment) + .executeUpdate(); + this.commit(); + } + /** * * @param comment @@ -181,4 +224,5 @@ public class CommentService extends AbstractService { public void updateAllTags() { this.updateAllTags(null); } + } diff --git a/src/main/java/fr/lirmm/aren/ws/rest/CommentRESTFacade.java b/src/main/java/fr/lirmm/aren/ws/rest/CommentRESTFacade.java index bffdcdc..2b4dc12 100644 --- a/src/main/java/fr/lirmm/aren/ws/rest/CommentRESTFacade.java +++ b/src/main/java/fr/lirmm/aren/ws/rest/CommentRESTFacade.java @@ -1,23 +1,22 @@ package fr.lirmm.aren.ws.rest; +import javax.annotation.security.PermitAll; import javax.annotation.security.RolesAllowed; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; -import javax.ws.rs.POST; -import javax.ws.rs.Path; +import javax.ws.rs.*; import fr.lirmm.aren.service.CommentService; import fr.lirmm.aren.model.Comment; import fr.lirmm.aren.model.TagSet; import fr.lirmm.aren.service.BroadcasterService; import fr.lirmm.aren.service.HttpRequestService; -import javax.ws.rs.PUT; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; + import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.sse.Sse; import javax.ws.rs.sse.SseEventSink; +import java.util.Set; /** * JAX-RS resource class for Comments managment @@ -127,4 +126,11 @@ public class CommentRESTFacade extends AbstractRESTFacade { broadcasterService.broadcastComment(comment); return comment.getTags(); } + + @DELETE + @Path("/delete/{id}") + @PermitAll + public void findTreeById(@PathParam("id") Long id){ + commentService.removeTreeComment(id) ; + } }