001/* 002 * (C) Copyright 2006-2008 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 * bstefanescu 018 * stan 019 * 020 * $Id$ 021 */ 022 023package org.nuxeo.ecm.core.rest; 024 025import java.util.Date; 026 027import javax.ws.rs.DELETE; 028import javax.ws.rs.FormParam; 029import javax.ws.rs.GET; 030import javax.ws.rs.POST; 031import javax.ws.rs.Path; 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.IdRef; 037import org.nuxeo.ecm.platform.comment.api.CommentManager; 038import org.nuxeo.ecm.platform.comment.workflow.services.CommentsModerationService; 039import org.nuxeo.ecm.webengine.forms.FormData; 040import org.nuxeo.ecm.webengine.model.WebAdapter; 041import org.nuxeo.ecm.webengine.model.exceptions.IllegalParameterException; 042import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter; 043import org.nuxeo.runtime.api.Framework; 044 045/** 046 * Comment Service - manages document comments. 047 * <p> 048 * Accepts the following methods: 049 * <ul> 050 * <li>POST - create a new comment 051 * </ul> 052 * 053 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 054 * @author <a href="mailto:[email protected]">Sun Seng David TAN</a> 055 * @author rux allow extending the service for the possible customizations. Some atomic actions are provided with 056 * default implementation but allowed for overwriting. 057 * @deprecated since 10.3, use {@link org.nuxeo.ecm.restapi.server.jaxrs.comment.CommentAdapter} instead. 058 */ 059@Deprecated 060@WebAdapter(name = "comments", type = "CommentService", targetType = "Document", targetFacets = { "Commentable" }) 061public class CommentService extends DefaultAdapter { 062 063 @POST 064 public Response doPost(@FormParam("text") String cText) { 065 if (cText == null) { 066 throw new IllegalParameterException("Expecting a 'text' parameter"); 067 } 068 DocumentObject dobj = (DocumentObject) getTarget(); 069 CoreSession session = dobj.getCoreSession(); 070 DocumentModel pageDoc = dobj.getDocument(); 071 DocumentModel comment = session.createDocumentModel("Comment"); 072 comment.setPropertyValue("comment:author", session.getPrincipal().getName()); 073 comment.setPropertyValue("comment:text", cText); 074 comment.setPropertyValue("comment:creationDate", new Date()); 075 comment = createCommentDocument(session, pageDoc, comment); 076 session.save(); 077 publishComment(session, pageDoc, comment); 078 079 return redirect(getTarget().getPath()); 080 } 081 082 @GET 083 @Path("reject") 084 public Response reject() { 085 DocumentObject dobj = (DocumentObject) getTarget(); 086 CoreSession session = dobj.getCoreSession(); 087 DocumentModel pageDoc = dobj.getDocument(); 088 FormData form = ctx.getForm(); 089 String commentId = form.getString(FormData.PROPERTY); 090 DocumentModel comment = session.getDocument(new IdRef(commentId)); 091 rejectComment(session, pageDoc, comment); 092 return redirect(dobj.getPath()); 093 } 094 095 @GET 096 @Path("approve") 097 public Response approve() { 098 DocumentObject dobj = (DocumentObject) getTarget(); 099 CoreSession session = dobj.getCoreSession(); 100 DocumentModel pageDoc = dobj.getDocument(); 101 FormData form = ctx.getForm(); 102 String commentId = form.getString(FormData.PROPERTY); 103 DocumentModel comment = session.getDocument(new IdRef(commentId)); 104 approveComent(session, pageDoc, comment); 105 return redirect(dobj.getPath()); 106 } 107 108 @GET 109 @Path("delete") 110 public Response remove() { 111 return deleteComment(); 112 } 113 114 @DELETE 115 public Response deleteComment() { 116 DocumentObject dobj = (DocumentObject) getTarget(); 117 CoreSession session = dobj.getCoreSession(); 118 FormData form = ctx.getForm(); 119 String docId = form.getString(FormData.PROPERTY); 120 DocumentModel comment = session.getDocument(new IdRef(docId)); 121 deleteComment(dobj.getDocument(), comment); 122 return redirect(dobj.getPath()); 123 } 124 125 public static CommentManager getCommentManager() { 126 return Framework.getService(CommentManager.class); 127 } 128 129 public static CommentsModerationService getCommentsModerationService() { 130 return Framework.getService(CommentsModerationService.class); 131 } 132 133 /** 134 * Can be overwritten to allow creation of localized comment. Defaults to create comment in comments root. 135 * 136 * @param session the core session 137 * @param target commented document 138 * @param comment comment itself 139 * @return the comment created 140 */ 141 protected DocumentModel createCommentDocument(CoreSession session, DocumentModel target, DocumentModel comment) { 142 return getCommentManager().createComment(target, comment); 143 } 144 145 /** 146 * Can be overwritten to allow workflow. Defaults to publish right away. 147 * 148 * @param session the core session 149 * @param target commented document 150 * @param comment comment itself 151 */ 152 protected void publishComment(CoreSession session, DocumentModel target, DocumentModel comment) { 153 getCommentsModerationService().publishComment(session, comment); 154 } 155 156 /** 157 * Can be overwritten to allow workflow. Defaults to delete right away. 158 * 159 * @param target commented document 160 * @param comment comment itself 161 */ 162 protected void deleteComment(DocumentModel target, DocumentModel comment) { 163 getCommentManager().deleteComment(target.getCoreSession(), comment.getId()); 164 } 165 166 /** 167 * Can be overwritten to allow workflow. Defaults to reject and delete right away. 168 * 169 * @param target commented document 170 * @param comment comment itself 171 */ 172 protected void rejectComment(CoreSession session, DocumentModel target, DocumentModel comment) { 173 getCommentsModerationService().rejectComment(session, target, comment.getId()); 174 getCommentManager().deleteComment(target.getCoreSession(), comment.getId()); 175 } 176 177 /** 178 * Can be overwritten to allow workflow. Defaults to approve right away. 179 * 180 * @param target commented document 181 * @param comment comment itself 182 */ 183 protected void approveComent(CoreSession session, DocumentModel target, DocumentModel comment) { 184 getCommentsModerationService().approveComent(session, target, comment.getId()); 185 } 186 187}