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    @Override
041    public void start(DocumentRoute routeInstance, Map<String, Serializable> map, CoreSession session) {
042        routeInstance.run(session, map);
043    }
044
045    @Override
046    public void resume(DocumentRoute routeInstance, String nodeId, String taskId, Map<String, Object> data,
047            String status, CoreSession session) {
048        routeInstance.resume(session, nodeId, taskId, data, status);
049    }
050
051    @Override
052    public void cancel(DocumentRoute routeInstance, CoreSession session) {
053        final String routeDocId = routeInstance.getDocument().getId();
054        new UnrestrictedSessionRunner(session) {
055            @Override
056            public void run() {
057                DocumentModel routeDoc = session.getDocument(new IdRef(routeDocId));
058                DocumentRoute routeInstance = routeDoc.getAdapter(DocumentRoute.class);
059                if (routeInstance == null) {
060                    throw new NuxeoException("Document " + routeDoc + " can not be adapted to a DocumentRoute");
061                }
062                routeInstance.cancel(session);
063            }
064        }.runUnrestricted();
065        Map<String, Serializable> properties = new HashMap<String, Serializable>();
066        properties.put(WORKFLOW_NAME_EVENT_PROPERTY_KEY, routeInstance.getTitle());
067        EventFirer.fireEvent(session, routeInstance.getAttachedDocuments(session), properties,
068                DocumentRoutingConstants.Events.workflowCanceled.name());
069    }
070}