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