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