001/* 002 * (C) Copyright 2014 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 * Mariana Cedica 016 */ 017package org.nuxeo.ecm.platform.routing.core.api.scripting; 018 019import java.util.Calendar; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.automation.OperationContext; 024import org.nuxeo.ecm.core.api.NuxeoException; 025import org.nuxeo.ecm.platform.routing.core.impl.GraphNode; 026 027/** 028 * @since 5.9.3 029 */ 030public class RoutingScriptingFunctions { 031 032 private Log log = LogFactory.getLog(RoutingScriptingFunctions.class); 033 034 public static final String BINDING_KEY = "WorkflowFn"; 035 036 protected GraphNode.EscalationRule rule; 037 038 protected OperationContext ctx; 039 040 public RoutingScriptingFunctions(OperationContext ctx) { 041 this.ctx = ctx; 042 } 043 044 public RoutingScriptingFunctions(OperationContext ctx, GraphNode.EscalationRule rule) { 045 this.ctx = ctx; 046 this.rule = rule; 047 } 048 049 /** 050 * Returns the time difference in milliseconds between the current time and the time the current workflow was 051 * started 052 */ 053 public long timeSinceWorkflowWasStarted() { 054 return Calendar.getInstance().getTimeInMillis() - ((Calendar) ctx.get("workflowStartTime")).getTimeInMillis(); 055 } 056 057 /** 058 * Returns the time difference in milliseconds between the current time and the time the current node was started 059 */ 060 public long timeSinceTaskWasStarted() { 061 return Calendar.getInstance().getTimeInMillis() - ((Calendar) ctx.get("nodeStartTime")).getTimeInMillis(); 062 } 063 064 /** 065 * Returns the time difference in milliseconds between the current time and the task due date 066 */ 067 public long timeSinceDueDateIsOver() { 068 return Calendar.getInstance().getTimeInMillis() - ((Calendar) ctx.get("taskDueTime")).getTimeInMillis(); 069 } 070 071 /** 072 * Returns -1 if the current rule hasn't been executed or the execution date was not set on this rule; Returns the 073 * time difference in milliseconds between the current time and the last time the rule was executed ( equivalent to 074 * the rule being evaluated to 'true'). 075 */ 076 public long timeSinceRuleHasBeenFalse() { 077 if (rule == null) { 078 throw new NuxeoException("No escalation rule available in this context"); 079 } 080 Calendar lastExecutionTime = rule.getLastExecutionTime(); 081 if (lastExecutionTime == null) { 082 log.warn("Trying to evaluate timeSinceRuleHasBeenFalse() for the rule " + rule.getId() 083 + " that hasn't been executed yet"); 084 return -1L; 085 } 086 if (!rule.isExecuted()) { 087 log.warn("Rule " + rule.getId() + " was never executed. Use with " + BINDING_KEY 088 + " ruleAlreadyExecuted()."); 089 return -1L; 090 } 091 return Calendar.getInstance().getTimeInMillis() - rule.getLastExecutionTime().getTimeInMillis(); 092 } 093 094 /** 095 * Returns 'true' if the current rule has been executed 096 */ 097 public boolean ruleAlreadyExecuted() { 098 if (rule == null) { 099 throw new NuxeoException("No escalation rule available in this context"); 100 } 101 return rule.isExecuted(); 102 } 103}