001/*
002 * (C) Copyright 2013 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 *     Mariana Cedica
016 */
017package org.nuxeo.ecm.platform.routing.core.listener;
018
019import java.util.List;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.IdRef;
023import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
024import org.nuxeo.ecm.core.api.repository.RepositoryManager;
025import org.nuxeo.ecm.core.event.Event;
026import org.nuxeo.ecm.core.event.EventListener;
027import org.nuxeo.ecm.platform.routing.core.api.DocumentRoutingEscalationService;
028import org.nuxeo.ecm.platform.routing.core.impl.GraphNode;
029import org.nuxeo.ecm.platform.routing.core.impl.GraphNode.EscalationRule;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Triggers the execution of all escalation rules on all running workflows. This listener is notified by the
034 * 'executeEscalationRules' event.
035 *
036 * @since 5.7.2
037 */
038public class DocumentRoutingEscalationListener implements EventListener {
039
040    public static final String EXECUTE_ESCALATION_RULE_EVENT = "executeEscalationRules";
041
042    @Override
043    public void handleEvent(Event event) {
044        if (!EXECUTE_ESCALATION_RULE_EVENT.equals(event.getName())) {
045            return;
046        }
047        RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class);
048        for (String repositoryName : repositoryManager.getRepositoryNames()) {
049            triggerEsclationRulesExecution(repositoryName);
050        }
051    }
052
053    protected void triggerEsclationRulesExecution(String repositoryName) {
054        new UnrestrictedSessionRunner(repositoryName) {
055
056            @Override
057            public void run() {
058                DocumentRoutingEscalationService escalationService = Framework.getLocalService(DocumentRoutingEscalationService.class);
059                List<String> nodeIds = escalationService.queryForSuspendedNodesWithEscalation(session);
060                for (String id : nodeIds) {
061                    DocumentModel nodeDoc = session.getDocument(new IdRef(id));
062                    GraphNode node = nodeDoc.getAdapter(GraphNode.class);
063                    List<EscalationRule> rules = escalationService.computeEscalationRulesToExecute(node);
064                    for (EscalationRule rule : rules) {
065                        escalationService.scheduleExecution(rule, session);
066                    }
067                }
068            }
069        }.runUnrestricted();
070    }
071}