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.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.PartialList;
037import org.nuxeo.ecm.platform.comment.api.Comment;
038import org.nuxeo.ecm.platform.comment.api.CommentManager;
039import org.nuxeo.ecm.webengine.model.WebAdapter;
040import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
041import org.nuxeo.runtime.api.Framework;
042
043/**
044 * @since 10.3
045 */
046@WebAdapter(name = CommentAdapter.NAME, type = "commentAdapter")
047@Produces(MediaType.APPLICATION_JSON)
048public class CommentAdapter extends DefaultAdapter {
049
050    public static final String NAME = "comment";
051
052    @POST
053    public Response createComment(Comment comment) {
054        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
055        comment.setParentId(doc.getId());
056        CommentManager commentManager = Framework.getService(CommentManager.class);
057        // Set logged user as author
058        comment.setAuthor(getContext().getCoreSession().getPrincipal().getName());
059        Comment result = commentManager.createComment(getContext().getCoreSession(), comment);
060        return Response.status(Response.Status.CREATED).entity(result).build();
061    }
062
063    @GET
064    public PartialList<Comment> getComments(@QueryParam("pageSize") @DefaultValue("0") Long pageSize,
065            @QueryParam("currentPageIndex") @DefaultValue("0") Long currentPageIndex) {
066        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
067        CommentManager commentManager = Framework.getService(CommentManager.class);
068        return commentManager.getComments(getContext().getCoreSession(), doc.getId(), pageSize, currentPageIndex,
069                false);
070    }
071
072    @GET
073    @Path("{commentId}")
074    public Comment getComment(@PathParam("commentId") String commentId) {
075        CommentManager commentManager = Framework.getService(CommentManager.class);
076        return commentManager.getComment(getContext().getCoreSession(), commentId);
077    }
078
079    @GET
080    @Path("external/{entityId}")
081    public Comment getExternalComment(@PathParam("entityId") String entityId) {
082        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
083        CommentManager commentManager = Framework.getService(CommentManager.class);
084        return commentManager.getExternalComment(getContext().getCoreSession(), doc.getId(), entityId);
085    }
086
087    @PUT
088    @Path("{commentId}")
089    public Response updateComment(@PathParam("commentId") String commentId, Comment comment) {
090        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
091        comment.setParentId(doc.getId());
092        CommentManager commentManager = Framework.getService(CommentManager.class);
093        // Fetch original comment author
094        CoreSession session = getContext().getCoreSession();
095        String author = commentManager.getComment(session, commentId).getAuthor();
096        comment.setAuthor(author);
097        Comment updatedComment = commentManager.updateComment(session, commentId, comment);
098        return Response.ok(updatedComment).build();
099    }
100
101    @PUT
102    @Path("external/{entityId}")
103    public Comment updateExternalComment(@PathParam("entityId") String entityId, Comment comment) {
104        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
105        CommentManager commentManager = Framework.getService(CommentManager.class);
106        // Fetch original comment author
107        CoreSession session = getContext().getCoreSession();
108        String author = commentManager.getExternalComment(session, doc.getId(), entityId).getAuthor();
109        comment.setAuthor(author);
110        commentManager.updateExternalComment(session, doc.getId(), entityId, comment);
111        return comment;
112    }
113
114    @DELETE
115    @Path("{commentId}")
116    public Response deleteComment(@PathParam("commentId") String commentId) {
117        CommentManager commentManager = Framework.getService(CommentManager.class);
118        commentManager.deleteComment(getContext().getCoreSession(), commentId);
119        return Response.noContent().build();
120    }
121
122    @DELETE
123    @Path("external/{entityId}")
124    public Response deleteExternalComment(@PathParam("entityId") String entityId) {
125        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
126        CommentManager commentManager = Framework.getService(CommentManager.class);
127        commentManager.deleteExternalComment(getContext().getCoreSession(), doc.getId(), entityId);
128        return Response.noContent().build();
129    }
130
131}