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 *     arussel
018 */
019package org.nuxeo.ecm.platform.routing.core.impl;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentNotFoundException;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
031import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
032import org.nuxeo.ecm.platform.routing.core.audit.RoutingAuditHelper;
033
034/**
035 * @author arussel
036 */
037public class DocumentRouteImpl extends DocumentRouteStepsContainerImpl implements DocumentRoute {
038
039    private static final long serialVersionUID = 1L;
040
041    private static final Log log = LogFactory.getLog(DocumentRouteImpl.class);
042
043    public DocumentRouteImpl(DocumentModel doc, ElementRunner runner) {
044        super(doc, runner);
045    }
046
047    @Override
048    public boolean canUndoStep(CoreSession session) {
049        return false;
050    }
051
052    protected void fireWorkflowCompletionEvent(CoreSession session) {
053        EventFirer.fireEvent(session, this, null, DocumentRoutingConstants.Events.afterRouteFinish.name());
054
055        Map<String, Serializable> eventProperties = new HashMap<String, Serializable>();
056
057        // First compute duration
058        long duration = RoutingAuditHelper.computeDurationSinceWfStarted(getDocument().getId());
059        if (duration >= 0) {
060            eventProperties.put(RoutingAuditHelper.TIME_SINCE_WF_STARTED, duration);
061        }
062
063        eventProperties.put(RoutingAuditHelper.WORKFLOW_INITATIOR, this.getInitiator());
064
065        // Add common info about workflow
066        if (this instanceof GraphRoute) {
067            eventProperties.put(RoutingAuditHelper.WORKFLOW_VARIABLES, (Serializable) ((GraphRoute) this).getVariables());
068        }
069        eventProperties.put("modelId", getModelId());
070        eventProperties.put("modelName", getModelName());
071        EventFirer.fireEvent(session, this, eventProperties, DocumentRoutingConstants.Events.afterWorkflowFinish.name());
072    }
073
074    @Override
075    public String getInitiator() {
076        return (String) document.getPropertyValue(DocumentRoutingConstants.ROUTING_INITIATOR_ID_KEY);
077    }
078
079    /**
080     * @since 7.2
081     */
082    @Override
083    public String getModelId() {
084        return (String) document.getPropertyValue(DocumentRoutingConstants.DOCUMENT_ROUTE_INSTANCE_MODEL_ID);
085    }
086
087    /**
088     * @since 7.2
089     */
090    @Override
091    public String getModelName() {
092        int firstDot = getName().indexOf(".");
093        return firstDot > 0 ? getName().substring(0, firstDot) : getName();
094    }
095
096    @Override
097    public void setDone(CoreSession session) {
098        followTransition(ElementLifeCycleTransistion.toDone, session, false);
099
100        fireWorkflowCompletionEvent(session);
101
102        // Fire events for route audit log
103        for (String attachDocumentID : this.getAttachedDocuments()) {
104            try {
105                DocumentModel doc = session.getDocument(new IdRef(attachDocumentID));
106                AuditEventFirer.fireEvent(session, this, null, "auditLogRoute", doc);
107            } catch (DocumentNotFoundException e) {
108                log.error(String.format("Unable to fetch document with id '%s': %s", attachDocumentID, e.getMessage()));
109                log.debug(e, e);
110            }
111        }
112    }
113
114}