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