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