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.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    public Response reject() {
090        try {
091            DocumentObject dobj = (DocumentObject) getTarget();
092            CoreSession session = dobj.getCoreSession();
093            DocumentModel pageDoc = dobj.getDocument();
094            FormData form = ctx.getForm();
095            String commentId = form.getString(FormData.PROPERTY);
096            DocumentModel comment = session.getDocument(new IdRef(commentId));
097            rejectComment(session, pageDoc, comment);
098            return redirect(dobj.getPath());
099        } catch (NuxeoException e) {
100            throw WebException.wrap("Failed to reject comment", e);
101        }
102    }
103
104    @GET
105    @Path("approve")
106    public Response approve() {
107        try {
108            DocumentObject dobj = (DocumentObject) getTarget();
109            CoreSession session = dobj.getCoreSession();
110            DocumentModel pageDoc = dobj.getDocument();
111            FormData form = ctx.getForm();
112            String commentId = form.getString(FormData.PROPERTY);
113            DocumentModel comment = session.getDocument(new IdRef(commentId));
114            approveComent(session, pageDoc, comment);
115            return redirect(dobj.getPath());
116        } catch (NuxeoException e) {
117            throw WebException.wrap("Failed to approve comment", e);
118        }
119    }
120
121    @GET
122    @Path("delete")
123    public Response remove() {
124        try {
125            return deleteComment();
126        } catch (NuxeoException e) {
127            throw WebException.wrap("Failed to delete comment", e);
128        }
129    }
130
131    @DELETE
132    public Response deleteComment() {
133        DocumentObject dobj = (DocumentObject) getTarget();
134        CoreSession session = dobj.getCoreSession();
135        FormData form = ctx.getForm();
136        String docId = form.getString(FormData.PROPERTY);
137        DocumentModel comment = session.getDocument(new IdRef(docId));
138        deleteComment(dobj.getDocument(), comment);
139        return redirect(dobj.getPath());
140    }
141
142    public static CommentManager getCommentManager() {
143        return Framework.getService(CommentManager.class);
144    }
145
146    public static CommentsModerationService getCommentsModerationService() {
147        return Framework.getService(CommentsModerationService.class);
148    }
149
150    /**
151     * Can be overwritten to allow creation of localized comment. Defaults to create comment in comments root.
152     *
153     * @param session the core session
154     * @param target commented document
155     * @param comment comment itself
156     * @return the comment created
157     */
158    protected DocumentModel createCommentDocument(CoreSession session, DocumentModel target, DocumentModel comment)
159            {
160        return getCommentManager().createComment(target, comment);
161    }
162
163    /**
164     * Can be overwritten to allow workflow. Defaults to publish right away.
165     *
166     * @param session the core session
167     * @param target commented document
168     * @param comment comment itself
169     */
170    protected void publishComment(CoreSession session, DocumentModel target, DocumentModel comment)
171            {
172        getCommentsModerationService().publishComment(session, comment);
173    }
174
175    /**
176     * Can be overwritten to allow workflow. Defaults to delete right away.
177     *
178     * @param target commented document
179     * @param comment comment itself
180     */
181    protected void deleteComment(DocumentModel target, DocumentModel comment) {
182        getCommentManager().deleteComment(target, comment);
183    }
184
185    /**
186     * Can be overwritten to allow workflow. Defaults to reject and delete right away.
187     *
188     * @param target commented document
189     * @param comment comment itself
190     */
191    protected void rejectComment(CoreSession session, DocumentModel target, DocumentModel comment)
192            {
193        getCommentsModerationService().rejectComment(session, target, comment.getId());
194        getCommentManager().deleteComment(target, comment);
195    }
196
197    /**
198     * Can be overwritten to allow workflow. Defaults to approve right away.
199     *
200     * @param target commented document
201     * @param comment comment itself
202     */
203    protected void approveComent(CoreSession session, DocumentModel target, DocumentModel comment)
204            {
205        getCommentsModerationService().approveComent(session, target, comment.getId());
206    }
207
208}