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