001/*
002 * (C) Copyright 2009 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 *     mcedica
018 */
019package org.nuxeo.ecm.platform.comment.workflow.services;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.automation.task.CreateTask;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.IdRef;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.core.api.NuxeoPrincipal;
035import org.nuxeo.ecm.core.api.event.CoreEventConstants;
036import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
037import org.nuxeo.ecm.core.event.Event;
038import org.nuxeo.ecm.core.event.EventProducer;
039import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
040import org.nuxeo.ecm.platform.comment.api.CommentConstants;
041import org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants;
042import org.nuxeo.ecm.platform.task.Task;
043import org.nuxeo.ecm.platform.task.TaskService;
044import org.nuxeo.ecm.platform.task.core.service.DocumentTaskProvider;
045import org.nuxeo.runtime.api.Framework;
046
047public class CommentsModerationServiceImpl implements CommentsModerationService {
048
049    private static final Log log = LogFactory.getLog(CommentsModerationService.class);
050
051    @Override
052    public void startModeration(CoreSession session, DocumentModel doc, String commentID, ArrayList<String> moderators)
053            {
054        TaskService taskService = Framework.getService(TaskService.class);
055        if (moderators == null || moderators.isEmpty()) {
056            throw new NuxeoException("No moderators defined");
057        }
058        Map<String, String> vars = new HashMap<String, String>();
059        vars.put(CommentsConstants.COMMENT_ID, commentID);
060        vars.put(Task.TaskVariableName.needi18n.name(), "true");
061        vars.put(Task.TaskVariableName.taskType.name(), CommentConstants.COMMENT_TASK_TYPE);
062
063        vars.put(CreateTask.OperationTaskVariableName.createdFromCreateTaskOperation.name(), "false");
064        vars.put(CreateTask.OperationTaskVariableName.acceptOperationChain.name(), CommentsConstants.ACCEPT_CHAIN_NAME);
065        vars.put(CreateTask.OperationTaskVariableName.rejectOperationChain.name(), CommentsConstants.REJECT_CHAIN_NAME);
066
067        taskService.createTask(session, (NuxeoPrincipal) session.getPrincipal(), doc,
068                CommentsConstants.MODERATION_DIRECTIVE_NAME, moderators, false, null, null, null, vars, null);
069    }
070
071    public Task getModerationTask(TaskService taskService, CoreSession session, DocumentModel doc, String commentId)
072            {
073        List<Task> tasks = DocumentTaskProvider.getTasks("GET_COMMENT_MODERATION_TASKS", session, false, null,
074                doc.getId(), session.getPrincipal().getName(), commentId);
075        if (tasks != null && !tasks.isEmpty()) {
076            if (tasks.size() > 1) {
077                log.error("There are several moderation workflows running, " + "taking only first found");
078            }
079            Task task = tasks.get(0);
080            return task;
081        }
082        return null;
083    }
084
085    @Override
086    public void approveComent(CoreSession session, DocumentModel doc, String commentId) {
087        TaskService taskService = Framework.getService(TaskService.class);
088        Task moderationTask = getModerationTask(taskService, session, doc, commentId);
089        if (moderationTask == null) {
090            session.followTransition(new IdRef(commentId), CommentsConstants.TRANSITION_TO_PUBLISHED_STATE);
091        } else {
092            taskService.acceptTask(session, (NuxeoPrincipal) session.getPrincipal(), moderationTask, null);
093        }
094
095        Map<String, Serializable> eventInfo = new HashMap<String, Serializable>();
096        eventInfo.put("emailDetails", "test");
097        notifyEvent(session, CommentsConstants.COMMENT_PUBLISHED, null, null, null, doc);
098    }
099
100    @Override
101    public void rejectComment(CoreSession session, DocumentModel doc, String commentId) {
102        TaskService taskService = Framework.getService(TaskService.class);
103        Task moderationTask = getModerationTask(taskService, session, doc, commentId);
104        if (moderationTask == null) {
105            session.followTransition(new IdRef(commentId), CommentsConstants.REJECT_STATE);
106        } else {
107            taskService.rejectTask(session, (NuxeoPrincipal) session.getPrincipal(), moderationTask, null);
108        }
109    }
110
111    @Override
112    public void publishComment(CoreSession session, DocumentModel comment) {
113        session.followTransition(comment.getRef(), CommentsConstants.TRANSITION_TO_PUBLISHED_STATE);
114
115        notifyEvent(session, CommentsConstants.COMMENT_PUBLISHED, null, null, null, comment);
116    }
117
118    protected void notifyEvent(CoreSession session, String eventId, Map<String, Serializable> properties,
119            String comment, String category, DocumentModel dm) {
120
121        // Default category
122        if (category == null) {
123            category = DocumentEventCategories.EVENT_DOCUMENT_CATEGORY;
124        }
125
126        if (properties == null) {
127            properties = new HashMap<String, Serializable>();
128        }
129
130        properties.put(CoreEventConstants.REPOSITORY_NAME, session.getRepositoryName());
131        properties.put(CoreEventConstants.SESSION_ID, session.getSessionId());
132        properties.put(CoreEventConstants.DOC_LIFE_CYCLE, dm.getCurrentLifeCycleState());
133
134        DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), dm);
135
136        ctx.setProperties(properties);
137        ctx.setComment(comment);
138        ctx.setCategory(category);
139
140        EventProducer evtProducer = Framework.getService(EventProducer.class);
141        Event event = ctx.newEvent(eventId);
142        evtProducer.fireEvent(event);
143    }
144
145}