001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (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.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 * bstefanescu 016 * stan 017 * 018 * $Id$ 019 */ 020 021package org.nuxeo.ecm.core.rest; 022 023import java.util.Date; 024 025import javax.ejb.TransactionAttribute; 026import javax.ejb.TransactionAttributeType; 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.core.api.NuxeoException; 038import org.nuxeo.ecm.platform.comment.api.CommentManager; 039import org.nuxeo.ecm.platform.comment.workflow.services.CommentsModerationService; 040import org.nuxeo.ecm.webengine.WebException; 041import org.nuxeo.ecm.webengine.forms.FormData; 042import org.nuxeo.ecm.webengine.model.WebAdapter; 043import org.nuxeo.ecm.webengine.model.exceptions.IllegalParameterException; 044import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter; 045import org.nuxeo.runtime.api.Framework; 046 047/** 048 * Comment Service - manages document comments. 049 * <p> 050 * Accepts the following methods: 051 * <ul> 052 * <li>POST - create a new comment 053 * </ul> 054 * 055 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 056 * @author <a href="mailto:stan@nuxeo.com">Sun Seng David TAN</a> 057 * @author rux allow extending the service for the possible customizations. Some atomic actions are provided with 058 * default implementation but allowed for overwriting. 059 */ 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 try { 072 073 DocumentModel comment = session.createDocumentModel("Comment"); 074 comment.setPropertyValue("comment:author", session.getPrincipal().getName()); 075 comment.setPropertyValue("comment:text", cText); 076 comment.setPropertyValue("comment:creationDate", new Date()); 077 comment = createCommentDocument(session, pageDoc, comment); 078 session.save(); 079 publishComment(session, pageDoc, comment); 080 081 return redirect(getTarget().getPath()); 082 } catch (NuxeoException e) { 083 throw WebException.wrap(e); 084 } 085 } 086 087 @GET 088 @Path("reject") 089 @TransactionAttribute(TransactionAttributeType.REQUIRED) 090 public Response reject() { 091 try { 092 DocumentObject dobj = (DocumentObject) getTarget(); 093 CoreSession session = dobj.getCoreSession(); 094 DocumentModel pageDoc = dobj.getDocument(); 095 FormData form = ctx.getForm(); 096 String commentId = form.getString(FormData.PROPERTY); 097 DocumentModel comment = session.getDocument(new IdRef(commentId)); 098 rejectComment(session, pageDoc, comment); 099 return redirect(dobj.getPath()); 100 } catch (NuxeoException e) { 101 throw WebException.wrap("Failed to reject comment", e); 102 } 103 } 104 105 @GET 106 @Path("approve") 107 @TransactionAttribute(TransactionAttributeType.REQUIRED) 108 public Response approve() { 109 try { 110 DocumentObject dobj = (DocumentObject) getTarget(); 111 CoreSession session = dobj.getCoreSession(); 112 DocumentModel pageDoc = dobj.getDocument(); 113 FormData form = ctx.getForm(); 114 String commentId = form.getString(FormData.PROPERTY); 115 DocumentModel comment = session.getDocument(new IdRef(commentId)); 116 approveComent(session, pageDoc, comment); 117 return redirect(dobj.getPath()); 118 } catch (NuxeoException e) { 119 throw WebException.wrap("Failed to approve comment", e); 120 } 121 } 122 123 @GET 124 @Path("delete") 125 public Response remove() { 126 try { 127 return deleteComment(); 128 } catch (NuxeoException e) { 129 throw WebException.wrap("Failed to delete comment", e); 130 } 131 } 132 133 @DELETE 134 public Response deleteComment() { 135 DocumentObject dobj = (DocumentObject) getTarget(); 136 CoreSession session = dobj.getCoreSession(); 137 FormData form = ctx.getForm(); 138 String docId = form.getString(FormData.PROPERTY); 139 DocumentModel comment = session.getDocument(new IdRef(docId)); 140 deleteComment(dobj.getDocument(), comment); 141 return redirect(dobj.getPath()); 142 } 143 144 public static CommentManager getCommentManager() { 145 return Framework.getService(CommentManager.class); 146 } 147 148 public static CommentsModerationService getCommentsModerationService() { 149 return Framework.getService(CommentsModerationService.class); 150 } 151 152 /** 153 * Can be overwritten to allow creation of localized comment. Defaults to create comment in comments root. 154 * 155 * @param session the core session 156 * @param target commented document 157 * @param comment comment itself 158 * @return the comment created 159 */ 160 protected DocumentModel createCommentDocument(CoreSession session, DocumentModel target, DocumentModel comment) 161 { 162 return getCommentManager().createComment(target, comment); 163 } 164 165 /** 166 * Can be overwritten to allow workflow. Defaults to publish right away. 167 * 168 * @param session the core session 169 * @param target commented document 170 * @param comment comment itself 171 */ 172 protected void publishComment(CoreSession session, DocumentModel target, DocumentModel comment) 173 { 174 getCommentsModerationService().publishComment(session, comment); 175 } 176 177 /** 178 * Can be overwritten to allow workflow. Defaults to delete right away. 179 * 180 * @param target commented document 181 * @param comment comment itself 182 */ 183 protected void deleteComment(DocumentModel target, DocumentModel comment) { 184 getCommentManager().deleteComment(target, comment); 185 } 186 187 /** 188 * Can be overwritten to allow workflow. Defaults to reject and delete right away. 189 * 190 * @param target commented document 191 * @param comment comment itself 192 */ 193 protected void rejectComment(CoreSession session, DocumentModel target, DocumentModel comment) 194 { 195 getCommentsModerationService().rejectComment(session, target, comment.getId()); 196 getCommentManager().deleteComment(target, comment); 197 } 198 199 /** 200 * Can be overwritten to allow workflow. Defaults to approve right away. 201 * 202 * @param target commented document 203 * @param comment comment itself 204 */ 205 protected void approveComent(CoreSession session, DocumentModel target, DocumentModel comment) 206 { 207 getCommentsModerationService().approveComent(session, target, comment.getId()); 208 } 209 210}