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 *       Kevin Leturc <kleturc@nuxeo.com>
018 *       Nuno Cunha <ncunha@nuxeo.com>
019 */
020package org.nuxeo.ecm.restapi.server.jaxrs.comment;
021
022import javax.ws.rs.DELETE;
023import javax.ws.rs.DefaultValue;
024import javax.ws.rs.GET;
025import javax.ws.rs.POST;
026import javax.ws.rs.PUT;
027import javax.ws.rs.Path;
028import javax.ws.rs.PathParam;
029import javax.ws.rs.Produces;
030import javax.ws.rs.QueryParam;
031import javax.ws.rs.core.MediaType;
032import javax.ws.rs.core.Response;
033
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.PartialList;
036import org.nuxeo.ecm.platform.comment.api.Comment;
037import org.nuxeo.ecm.platform.comment.api.CommentManager;
038import org.nuxeo.ecm.webengine.model.WebAdapter;
039import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * @since 10.3
044 */
045@WebAdapter(name = CommentAdapter.NAME, type = "commentAdapter")
046@Produces(MediaType.APPLICATION_JSON)
047public class CommentAdapter extends DefaultAdapter {
048
049    public static final String NAME = "comment";
050
051    @POST
052    public Response createComment(Comment comment) {
053        CommentManager commentManager = Framework.getService(CommentManager.class);
054        Comment result = commentManager.createComment(getContext().getCoreSession(), comment);
055        return Response.status(Response.Status.CREATED).entity(result).build();
056    }
057
058    @GET
059    public PartialList<Comment> getComments(@QueryParam("pageSize") @DefaultValue("0") Long pageSize,
060            @QueryParam("currentPageIndex") @DefaultValue("0") Long currentPageIndex) {
061        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
062        CommentManager commentManager = Framework.getService(CommentManager.class);
063        return commentManager.getComments(getContext().getCoreSession(), doc.getId(), pageSize, currentPageIndex,
064                false);
065    }
066
067    @GET
068    @Path("{commentId}")
069    public Comment getComment(@PathParam("commentId") String commentId) {
070        CommentManager commentManager = Framework.getService(CommentManager.class);
071        return commentManager.getComment(getContext().getCoreSession(), commentId);
072    }
073
074    @GET
075    @Path("external/{entityId}")
076    public Comment getExternalComment(@PathParam("entityId") String entityId) {
077        CommentManager commentManager = Framework.getService(CommentManager.class);
078        return commentManager.getExternalComment(getContext().getCoreSession(), entityId);
079    }
080
081    @PUT
082    @Path("{commentId}")
083    public Response updateComment(@PathParam("commentId") String commentId, Comment comment) {
084        CommentManager commentManager = Framework.getService(CommentManager.class);
085        Comment updatedComment = commentManager.updateComment(getContext().getCoreSession(), commentId, comment);
086        return Response.ok(updatedComment).build();
087    }
088
089    @PUT
090    @Path("external/{entityId}")
091    public Comment updateExternalComment(@PathParam("entityId") String entityId, Comment comment) {
092        CommentManager commentManager = Framework.getService(CommentManager.class);
093        commentManager.updateExternalComment(getContext().getCoreSession(), entityId, comment);
094        return comment;
095    }
096
097    @DELETE
098    @Path("{commentId}")
099    public Response deleteComment(@PathParam("commentId") String commentId) {
100        CommentManager commentManager = Framework.getService(CommentManager.class);
101        commentManager.deleteComment(getContext().getCoreSession(), commentId);
102        return Response.noContent().build();
103    }
104
105    @DELETE
106    @Path("external/{entityId}")
107    public Response deleteExternalComment(@PathParam("entityId") String entityId) {
108        CommentManager commentManager = Framework.getService(CommentManager.class);
109        commentManager.deleteExternalComment(getContext().getCoreSession(), entityId);
110        return Response.noContent().build();
111    }
112
113}