001/*
002 * (C) Copyright 2012 Nuxeo SA (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 *     bjalon
016 */
017package org.nuxeo.ecm.mobile.webengine.adapter;
018
019import java.util.Calendar;
020import java.util.List;
021
022import javax.ws.rs.FormParam;
023import javax.ws.rs.GET;
024import javax.ws.rs.POST;
025import javax.ws.rs.Path;
026import javax.ws.rs.PathParam;
027import javax.ws.rs.core.Response;
028
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.core.api.PropertyException;
032import org.nuxeo.ecm.platform.comment.api.CommentableDocument;
033import org.nuxeo.ecm.webengine.model.WebAdapter;
034
035/**
036 * Provide view and action on document comments
037 *
038 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
039 * @since 5.5
040 */
041@WebAdapter(name = "comment", type = "Comment", targetType = "MobileDocument")
042public class CommentAdapter extends DefaultMobileAdapter {
043
044    private CommentableDocument commentableDocument;
045
046    @GET
047    public Object doGet() {
048        return getView("index");
049    }
050
051    @POST
052    public Object doPost(@FormParam("newComment") String newTextComment) throws PropertyException {
053        DocumentModel comment = initializeEmptyComment();
054        comment.setPropertyValue("comment:text", newTextComment);
055        getCommentableDocument().addComment(comment);
056        ctx.getCoreSession().saveDocument(getDocumentModel());
057        return getView("index");
058    }
059
060    @POST
061    @Path("{commentIdParent}")
062    public Object doPostReplyForm(@FormParam("newComment") String newTextComment,
063            @PathParam("commentIdParent") String commentIdParent) throws PropertyException {
064        DocumentModel commentParent = null;
065        if (commentIdParent != null && !"null".equals(commentIdParent)) {
066            commentParent = ctx.getCoreSession().getDocument(new IdRef(commentIdParent));
067        }
068        DocumentModel comment = initializeEmptyComment();
069        comment.setPropertyValue("comment:text", newTextComment);
070        if (commentParent != null) {
071            getCommentableDocument().addComment(commentParent, comment);
072        } else {
073            getCommentableDocument().addComment(comment);
074        }
075        ctx.getCoreSession().saveDocument(getDocumentModel());
076        return redirect(this.getPath());
077    }
078
079    @GET
080    @Path("{commentIdParent}")
081    public Object doGetReplyForm(@PathParam("commentIdParent") String commentIdParent) throws PropertyException {
082        return getView("new").arg("parentId", commentIdParent);
083    }
084
085    @GET
086    @Path("{commentId}/@delete")
087    public Object doDeleteComment(@PathParam("commentId") String commentId) throws PropertyException {
088        ctx.getCoreSession().removeDocument(new IdRef(commentId));
089        return Response.ok().build();
090    }
091
092    public List<DocumentModel> getComments() {
093        List<DocumentModel> comments = getCommentableDocument().getComments();
094        return comments;
095
096    }
097
098    public List<DocumentModel> getComments(DocumentModel commentParent) {
099        List<DocumentModel> comments = getCommentableDocument().getComments(commentParent);
100        return comments;
101
102    }
103
104    public boolean hasWriteRightOnComment(DocumentModel comment) {
105        return ctx.getCoreSession().hasPermission(comment.getRef(), "Write");
106    }
107
108    private CommentableDocument getCommentableDocument() {
109        if (commentableDocument == null) {
110            commentableDocument = getDocumentModel().getAdapter(CommentableDocument.class);
111        }
112        return commentableDocument;
113    }
114
115    public boolean hasAddingCommentRight() {
116        return ctx.getCoreSession().hasPermission(getDocumentModel().getRef(), "Write");
117    }
118
119    protected DocumentModel initializeEmptyComment() {
120        DocumentModel comment = ctx.getCoreSession().createDocumentModel("Comment");
121
122        String[] contributors = new String[1];
123        contributors[0] = ctx.getPrincipal().getName();
124        comment.setProperty("dublincore", "contributors", contributors);
125        comment.setProperty("dublincore", "created", Calendar.getInstance());
126        comment.setProperty("comment", "author", ctx.getPrincipal().getName());
127        comment.setProperty("comment", "creationDate", Calendar.getInstance());
128        return comment;
129    }
130
131}