001/*
002 * (C) Copyright 2011 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 *     ldoguin
018 */
019package org.nuxeo.ecm.platform.comment.workflow;
020
021import org.nuxeo.ecm.automation.OperationContext;
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
028import org.nuxeo.ecm.automation.task.CreateTask.OperationTaskVariableName;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentRef;
032import org.nuxeo.ecm.core.api.IdRef;
033import org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants;
034import org.nuxeo.ecm.platform.comment.workflow.utils.FollowTransitionUnrestricted;
035import org.nuxeo.ecm.platform.task.Task;
036
037/**
038 * @since 5.5
039 */
040@Operation(id = ModerateCommentOperation.ID, category = Constants.CAT_DOCUMENT, label = "Follow publish or reject transition", description = "Follow publish if accept is true, reject otherwise.", addToStudio = false)
041public class ModerateCommentOperation {
042
043    public static final String ID = "Comment.Moderate";
044
045    @Context
046    protected OperationContext ctx;
047
048    @Context
049    protected CoreSession session;
050
051    @Param(name = "accept")
052    protected Boolean accept;
053
054    @OperationMethod(collector = DocumentModelCollector.class)
055    public DocumentModel run(DocumentModel doc) {
056        moderate(doc.getRef());
057        return doc;
058    }
059
060    @OperationMethod(collector = DocumentModelCollector.class)
061    public DocumentModel run(DocumentRef docRef) {
062        moderate(docRef);
063        return session.getDocument(docRef);
064    }
065
066    protected void moderate(DocumentRef docRef) {
067        DocumentModel taskDoc = (DocumentModel) ctx.get(OperationTaskVariableName.taskDocument.name());
068        Task task = taskDoc.getAdapter(Task.class);
069        DocumentRef targetDocRef = new IdRef(task.getVariable(CommentsConstants.COMMENT_ID));
070        FollowTransitionUnrestricted runner;
071        if (accept) {
072            runner = new FollowTransitionUnrestricted(session, targetDocRef,
073                    CommentsConstants.TRANSITION_TO_PUBLISHED_STATE);
074        } else {
075            runner = new FollowTransitionUnrestricted(session, targetDocRef, CommentsConstants.REJECT_STATE);
076        }
077        runner.runUnrestricted();
078    }
079
080}