001/* 002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Funsho David 018 */ 019 020package org.nuxeo.ecm.restapi.server.jaxrs.comment; 021 022import java.util.ArrayList; 023import java.util.List; 024 025import javax.ws.rs.DELETE; 026import javax.ws.rs.GET; 027import javax.ws.rs.POST; 028import javax.ws.rs.PUT; 029import javax.ws.rs.Path; 030import javax.ws.rs.PathParam; 031import javax.ws.rs.Produces; 032import javax.ws.rs.QueryParam; 033import javax.ws.rs.core.MediaType; 034import javax.ws.rs.core.Response; 035 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.core.api.DocumentModel; 038import org.nuxeo.ecm.platform.comment.api.Annotation; 039import org.nuxeo.ecm.platform.comment.api.AnnotationService; 040import org.nuxeo.ecm.platform.comment.api.Comment; 041import org.nuxeo.ecm.platform.comment.api.CommentManager; 042import org.nuxeo.ecm.webengine.model.WebAdapter; 043import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter; 044import org.nuxeo.runtime.api.Framework; 045 046/** 047 * @since 10.1 048 */ 049@WebAdapter(name = AnnotationAdapter.NAME, type = "annotationAdapter") 050@Produces(MediaType.APPLICATION_JSON) 051public class AnnotationAdapter extends DefaultAdapter { 052 053 public static final String NAME = "annotation"; 054 055 @POST 056 public Response createAnnotation(Annotation annotation) { 057 AnnotationService annotationService = Framework.getService(AnnotationService.class); 058 Annotation result = annotationService.createAnnotation(getContext().getCoreSession(), annotation); 059 return Response.status(Response.Status.CREATED).entity(result).build(); 060 } 061 062 @GET 063 public List<Annotation> getAnnotations(@QueryParam("xpath") String xpath) { 064 DocumentModel doc = getTarget().getAdapter(DocumentModel.class); 065 AnnotationService annotationService = Framework.getService(AnnotationService.class); 066 return annotationService.getAnnotations(getContext().getCoreSession(), doc.getId(), xpath); 067 } 068 069 @GET 070 @Path("comments") 071 public List<Comment> getComments(@QueryParam("annotationIds") List<String> annotationIds) { 072 CommentManager commentManager = Framework.getService(CommentManager.class); 073 List<Comment> comments = new ArrayList<>(); 074 for (String annotationId : annotationIds) { 075 comments.addAll(getAllComments(annotationId, commentManager, getContext().getCoreSession())); 076 } 077 return comments; 078 } 079 080 protected List<Comment> getAllComments(String annotationId, CommentManager commentManager, CoreSession session) { 081 List<Comment> allComments = new ArrayList<>(); 082 List<Comment> comments = commentManager.getComments(session, annotationId); 083 for (Comment comment : comments) { 084 allComments.addAll(getAllComments(comment.getId(), commentManager, session)); 085 allComments.add(comment); 086 } 087 return allComments; 088 } 089 090 @GET 091 @Path("{annotationId}") 092 public Annotation getAnnotation(@PathParam("annotationId") String annotationId) { 093 AnnotationService annotationService = Framework.getService(AnnotationService.class); 094 return annotationService.getAnnotation(getContext().getCoreSession(), annotationId); 095 } 096 097 @GET 098 @Path("external/{entityId}") 099 public Annotation getExternalAnnotation(@PathParam("entityId") String entityId) { 100 AnnotationService annotationService = Framework.getService(AnnotationService.class); 101 return annotationService.getExternalAnnotation(getContext().getCoreSession(), entityId); 102 } 103 104 @PUT 105 @Path("{annotationId}") 106 public Annotation updateAnnotation(@PathParam("annotationId") String annotationId, Annotation annotation) { 107 AnnotationService annotationService = Framework.getService(AnnotationService.class); 108 annotationService.updateAnnotation(getContext().getCoreSession(), annotationId, annotation); 109 return annotation; 110 } 111 112 @PUT 113 @Path("external/{entityId}") 114 public Annotation updateExternalAnnotation(@PathParam("entityId") String entityId, Annotation annotation) { 115 AnnotationService annotationService = Framework.getService(AnnotationService.class); 116 annotationService.updateExternalAnnotation(getContext().getCoreSession(), entityId, annotation); 117 return annotation; 118 } 119 120 @DELETE 121 @Path("{annotationId}") 122 public Response deleteAnnotation(@PathParam("annotationId") String annotationId) { 123 AnnotationService annotationService = Framework.getService(AnnotationService.class); 124 annotationService.deleteAnnotation(getContext().getCoreSession(), annotationId); 125 return Response.noContent().build(); 126 } 127 128 @DELETE 129 @Path("external/{entityId}") 130 public Response deleteExternalAnnotation(@PathParam("entityId") String entityId) { 131 AnnotationService annotationService = Framework.getService(AnnotationService.class); 132 annotationService.deleteExternalAnnotation(getContext().getCoreSession(), entityId); 133 return Response.noContent().build(); 134 } 135 136}