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