001/* 002 * (C) Copyright 2014 Nuxeo SA (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 * vpasquier <vpasquier@nuxeo.com> 018 */ 019package org.nuxeo.box.api.comment; 020 021import org.nuxeo.box.api.adapter.BoxAdapter; 022import org.nuxeo.box.api.comment.adapter.BoxCommentAdapter; 023import org.nuxeo.box.api.file.adapter.BoxFileAdapter; 024import org.nuxeo.box.api.marshalling.dao.BoxComment; 025import org.nuxeo.box.api.marshalling.exceptions.BoxJSONException; 026import org.nuxeo.box.api.marshalling.exceptions.BoxRestException; 027import org.nuxeo.box.api.service.BoxService; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.DocumentNotFoundException; 031import org.nuxeo.ecm.core.api.IdRef; 032import org.nuxeo.ecm.core.api.NuxeoException; 033import org.nuxeo.ecm.platform.comment.api.CommentableDocument; 034import org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants; 035import org.nuxeo.ecm.webengine.WebException; 036import org.nuxeo.ecm.webengine.model.WebObject; 037import org.nuxeo.ecm.webengine.model.impl.AbstractResource; 038import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl; 039import org.nuxeo.runtime.api.Framework; 040 041import javax.ws.rs.DELETE; 042import javax.ws.rs.GET; 043import javax.ws.rs.POST; 044import javax.ws.rs.PUT; 045import javax.ws.rs.Path; 046import javax.ws.rs.PathParam; 047import javax.ws.rs.Produces; 048import javax.ws.rs.core.MediaType; 049import javax.ws.rs.core.Response; 050import java.lang.reflect.InvocationTargetException; 051import java.text.ParseException; 052import java.util.Date; 053 054/** 055 * WebObject for a Box Comment 056 * 057 * @since 5.9.3 058 */ 059@WebObject(type = "comment") 060@Produces({ MediaType.APPLICATION_JSON }) 061public class BoxCommentObject extends AbstractResource<ResourceTypeImpl> { 062 063 BoxService boxService; 064 065 BoxFileAdapter boxFile; 066 067 @Override 068 public void initialize(Object... args) { 069 boxService = Framework.getLocalService(BoxService.class); 070 if (args != null && args.length == 1) { 071 try { 072 String fileId = (String) args[0]; 073 CoreSession session = ctx.getCoreSession(); 074 DocumentModel file = session.getDocument(new IdRef(fileId)); 075 boxFile = (BoxFileAdapter) file.getAdapter(BoxAdapter.class); 076 } catch (NuxeoException e) { 077 throw WebException.wrap(e); 078 } 079 setRoot(true); 080 } 081 } 082 083 @GET 084 @Path("{commentId}") 085 public String doGetComment(@PathParam("commentId") final String commentId) throws DocumentNotFoundException, 086 BoxJSONException { 087 final CoreSession session = ctx.getCoreSession(); 088 final DocumentModel comment = session.getDocument(new IdRef(commentId)); 089 // Adapt nx document to box comment adapter 090 final BoxCommentAdapter commentAdapter = comment.getAdapter(BoxCommentAdapter.class); 091 return boxService.toJSONString(commentAdapter.getBoxComment()); 092 } 093 094 @POST 095 public String doPostComment(String jsonBoxComment) throws DocumentNotFoundException, 096 BoxJSONException { 097 final CoreSession session = ctx.getCoreSession(); 098 BoxComment boxComment = boxService.getBoxComment(jsonBoxComment); 099 // Fetch the target 100 DocumentModel target = session.getDocument(new IdRef(boxComment.getItem().getId())); 101 // Create the nx document from box comment information 102 DocumentModel comment = session.createDocumentModel(CommentsConstants.COMMENT_DOC_TYPE); 103 comment.setProperty("comment", "text", boxComment.getMessage()); 104 comment.setProperty("comment", "author", session.getPrincipal().getName()); 105 comment.setProperty("comment", "creationDate", new Date()); 106 // create the comment 107 CommentableDocument commentableDocument = target.getAdapter(CommentableDocument.class); 108 if (commentableDocument == null) { 109 throw new BoxRestException("This document cannot be commented", 110 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); 111 } 112 DocumentModel newComment = commentableDocument.addComment(comment); 113 newComment.attach(session.getSessionId()); 114 final BoxCommentAdapter commentAdapter = newComment.getAdapter(BoxCommentAdapter.class); 115 return boxService.toJSONString(commentAdapter.getBoxComment()); 116 } 117 118 @PUT 119 @Path("{commentId}") 120 public String doPutComment(@PathParam("commentId") String commentId, String jsonBoxComment) throws 121 BoxJSONException, ParseException, IllegalAccessException, InvocationTargetException, 122 DocumentNotFoundException { 123 final CoreSession session = ctx.getCoreSession(); 124 // Fetch the nx document comment 125 final DocumentModel nxComment = session.getDocument(new IdRef(commentId)); 126 // Create box comment from json payload 127 BoxComment boxCommentUpdated = boxService.getBoxComment(jsonBoxComment); 128 // Adapt nx document to box comment adapter 129 final BoxCommentAdapter nxDocumentAdapter = nxComment.getAdapter(BoxCommentAdapter.class); 130 // Update both nx document and box comment adapter 131 nxDocumentAdapter.setBoxComment(boxCommentUpdated); 132 nxDocumentAdapter.save(session); 133 // Return the new box comment json 134 return boxService.toJSONString(nxDocumentAdapter.getBoxComment()); 135 } 136 137 @DELETE 138 @Path("{commentId}") 139 public void doDeleteComment(@PathParam("commentId") String commentId) { 140 final CoreSession session = ctx.getCoreSession(); 141 session.removeDocument(new IdRef(commentId)); 142 session.save(); 143 } 144 145 @GET 146 public String doGetComments() throws DocumentNotFoundException, BoxJSONException { 147 return boxService.toJSONString(boxFile.getComments()); 148 } 149}