001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     vpasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.box.api.comment;
018
019import org.nuxeo.box.api.adapter.BoxAdapter;
020import org.nuxeo.box.api.comment.adapter.BoxCommentAdapter;
021import org.nuxeo.box.api.file.adapter.BoxFileAdapter;
022import org.nuxeo.box.api.marshalling.dao.BoxComment;
023import org.nuxeo.box.api.marshalling.exceptions.BoxJSONException;
024import org.nuxeo.box.api.marshalling.exceptions.BoxRestException;
025import org.nuxeo.box.api.service.BoxService;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentNotFoundException;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.platform.comment.api.CommentableDocument;
032import org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants;
033import org.nuxeo.ecm.webengine.WebException;
034import org.nuxeo.ecm.webengine.model.WebObject;
035import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
036import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
037import org.nuxeo.runtime.api.Framework;
038
039import javax.ws.rs.DELETE;
040import javax.ws.rs.GET;
041import javax.ws.rs.POST;
042import javax.ws.rs.PUT;
043import javax.ws.rs.Path;
044import javax.ws.rs.PathParam;
045import javax.ws.rs.Produces;
046import javax.ws.rs.core.MediaType;
047import javax.ws.rs.core.Response;
048import java.lang.reflect.InvocationTargetException;
049import java.text.ParseException;
050import java.util.Date;
051
052/**
053 * WebObject for a Box Comment
054 *
055 * @since 5.9.3
056 */
057@WebObject(type = "comment")
058@Produces({ MediaType.APPLICATION_JSON })
059public class BoxCommentObject extends AbstractResource<ResourceTypeImpl> {
060
061    BoxService boxService;
062
063    BoxFileAdapter boxFile;
064
065    @Override
066    public void initialize(Object... args) {
067        boxService = Framework.getLocalService(BoxService.class);
068        if (args != null && args.length == 1) {
069            try {
070                String fileId = (String) args[0];
071                CoreSession session = ctx.getCoreSession();
072                DocumentModel file = session.getDocument(new IdRef(fileId));
073                boxFile = (BoxFileAdapter) file.getAdapter(BoxAdapter.class);
074            } catch (NuxeoException e) {
075                throw WebException.wrap(e);
076            }
077            setRoot(true);
078        }
079    }
080
081    @GET
082    @Path("{commentId}")
083    public String doGetComment(@PathParam("commentId") final String commentId) throws DocumentNotFoundException,
084            BoxJSONException {
085        final CoreSession session = ctx.getCoreSession();
086        final DocumentModel comment = session.getDocument(new IdRef(commentId));
087        // Adapt nx document to box comment adapter
088        final BoxCommentAdapter commentAdapter = comment.getAdapter(BoxCommentAdapter.class);
089        return boxService.toJSONString(commentAdapter.getBoxComment());
090    }
091
092    @POST
093    public String doPostComment(String jsonBoxComment) throws DocumentNotFoundException,
094            BoxJSONException {
095        final CoreSession session = ctx.getCoreSession();
096        BoxComment boxComment = boxService.getBoxComment(jsonBoxComment);
097        // Fetch the target
098        DocumentModel target = session.getDocument(new IdRef(boxComment.getItem().getId()));
099        // Create the nx document from box comment information
100        DocumentModel comment = session.createDocumentModel(CommentsConstants.COMMENT_DOC_TYPE);
101        comment.setProperty("comment", "text", boxComment.getMessage());
102        comment.setProperty("comment", "author", session.getPrincipal().getName());
103        comment.setProperty("comment", "creationDate", new Date());
104        // create the comment
105        CommentableDocument commentableDocument = target.getAdapter(CommentableDocument.class);
106        if (commentableDocument == null) {
107            throw new BoxRestException("This document cannot be commented",
108                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
109        }
110        DocumentModel newComment = commentableDocument.addComment(comment);
111        newComment.attach(session.getSessionId());
112        final BoxCommentAdapter commentAdapter = newComment.getAdapter(BoxCommentAdapter.class);
113        return boxService.toJSONString(commentAdapter.getBoxComment());
114    }
115
116    @PUT
117    @Path("{commentId}")
118    public String doPutComment(@PathParam("commentId") String commentId, String jsonBoxComment) throws
119            BoxJSONException, ParseException, IllegalAccessException, InvocationTargetException,
120            DocumentNotFoundException {
121        final CoreSession session = ctx.getCoreSession();
122        // Fetch the nx document comment
123        final DocumentModel nxComment = session.getDocument(new IdRef(commentId));
124        // Create box comment from json payload
125        BoxComment boxCommentUpdated = boxService.getBoxComment(jsonBoxComment);
126        // Adapt nx document to box comment adapter
127        final BoxCommentAdapter nxDocumentAdapter = nxComment.getAdapter(BoxCommentAdapter.class);
128        // Update both nx document and box comment adapter
129        nxDocumentAdapter.setBoxComment(boxCommentUpdated);
130        nxDocumentAdapter.save(session);
131        // Return the new box comment json
132        return boxService.toJSONString(nxDocumentAdapter.getBoxComment());
133    }
134
135    @DELETE
136    @Path("{commentId}")
137    public void doDeleteComment(@PathParam("commentId") String commentId) {
138        final CoreSession session = ctx.getCoreSession();
139        session.removeDocument(new IdRef(commentId));
140        session.save();
141    }
142
143    @GET
144    public String doGetComments() throws DocumentNotFoundException, BoxJSONException {
145        return boxService.toJSONString(boxFile.getComments());
146    }
147}