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