001/*
002 * (C) Copyright 2009-2012 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 *     Alexandre Russel
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.platform.routing.core.impl;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.IdRef;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
031import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
032import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
033import org.nuxeo.ecm.platform.routing.core.api.DocumentRoutingEngineService;
034import org.nuxeo.runtime.model.DefaultComponent;
035
036public class DocumentRoutingEngineServiceImpl extends DefaultComponent implements DocumentRoutingEngineService {
037
038    public static final String WORKFLOW_NAME_EVENT_PROPERTY_KEY = "wfName";
039
040    public static final String WORKFLOW_ID_EVENT_PROPERTY_KEY = "wfId";
041
042    @Override
043    public void start(DocumentRoute routeInstance, Map<String, Serializable> map, CoreSession session) {
044        routeInstance.run(session, map);
045    }
046
047    @Override
048    public void resume(DocumentRoute routeInstance, String nodeId, String taskId, Map<String, Object> data,
049            String status, CoreSession session) {
050        routeInstance.resume(session, nodeId, taskId, data, status);
051    }
052
053    @Override
054    public void cancel(DocumentRoute routeInstance, CoreSession session) {
055        final String routeDocId = routeInstance.getDocument().getId();
056        new UnrestrictedSessionRunner(session) {
057            @Override
058            public void run() {
059                DocumentModel routeDoc = session.getDocument(new IdRef(routeDocId));
060                DocumentRoute routeInstance = routeDoc.getAdapter(DocumentRoute.class);
061                if (routeInstance == null) {
062                    throw new NuxeoException("Document " + routeDoc + " can not be adapted to a DocumentRoute");
063                }
064                routeInstance.cancel(session);
065            }
066        }.runUnrestricted();
067        Map<String, Serializable> properties = new HashMap<>();
068        properties.put(WORKFLOW_NAME_EVENT_PROPERTY_KEY, routeInstance.getTitle());
069        properties.put(WORKFLOW_ID_EVENT_PROPERTY_KEY, routeDocId);
070        EventFirer.fireEvent(session, routeInstance.getAttachedDocuments(session), properties,
071                DocumentRoutingConstants.Events.workflowCanceled.name());
072    }
073}