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