All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.definition.activity.ConditionDefinition Maven / Gradle / Ivy

/**
 * Copyright (C) 2007  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 * 
 * Modified by Matthieu Chaffotte - BonitaSoft S.A.
 **/
package org.ow2.bonita.definition.activity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.bonita.facade.exception.BonitaWrapperException;
import org.ow2.bonita.facade.exception.ExpressionEvaluationException;
import org.ow2.bonita.facade.runtime.ActivityInstance;
import org.ow2.bonita.facade.runtime.ProcessInstance;
import org.ow2.bonita.facade.uuid.ProcessInstanceUUID;
import org.ow2.bonita.pvm.listener.EventListenerExecution;
import org.ow2.bonita.pvm.model.Condition;
import org.ow2.bonita.runtime.InternalExecution;
import org.ow2.bonita.services.Querier;
import org.ow2.bonita.util.EnvTool;
import org.ow2.bonita.util.GroovyException;
import org.ow2.bonita.util.GroovyUtil;

/**
 * @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes,
 *         Pierre Vigneras
 */
public class ConditionDefinition implements Condition {

  private static final long serialVersionUID = 1L;
  private static final Logger LOG = Logger.getLogger(ConditionDefinition.class.getName());
  /**
   * contains the condition set onto the transition; for instance:
   * "!str1.equals("toto")" where str1 is the name of a runtime dataField
   */
  protected String value;

  protected ConditionDefinition() {
  }

  public ConditionDefinition(final String value) {
    this.value = value;
  }

  public boolean evaluate(final EventListenerExecution internalExecution) {
    try {
      return this.evaluateGroovy(internalExecution);
    } catch (Exception e) {
      throw new BonitaWrapperException(new ExpressionEvaluationException(e.getMessage()));
    }
  }

  private boolean evaluateGroovy(final EventListenerExecution internalExecution) throws GroovyException {
    String groovyExpression = GroovyUtil.GROOVY_START_DELIMITER + value + GroovyUtil.GROOVY_END_DELIMITER;
    
    InternalExecution exec = (InternalExecution)internalExecution;
    ProcessInstanceUUID instanceUUID = exec.getInstance().getUUID();
    
    Querier journal = EnvTool.getJournalQueriers();
    ActivityInstance activityInstance = journal.getActivityInstance(instanceUUID, exec.getNodeName(), exec.getIterationId(), exec.getActivityInstanceId());
    ProcessInstance instance = journal.getProcessInstance(instanceUUID);
    
    Map activityVars = null;
    if (activityInstance != null) {
    	activityVars = activityInstance.getLastKnownVariableValues();
    }
    final Map instanceVars = instance.getLastKnownVariableValues();
    
    final Map allVars = new HashMap();
    if (instanceVars != null) {
    	allVars.putAll(instanceVars);
    }
    if (activityVars != null) {
    	allVars.putAll(activityVars);
    }
    
    Set setVar = allVars.keySet();
    final Iterator it = setVar.iterator();
    if (LOG.isLoggable(Level.FINEST)) {
      LOG.finest("Adding variables to expressionEvaluator : " + setVar);
    }
    while (it.hasNext()) {
      final String key = it.next();
      final Object var = allVars.get(key);
      if (var != null)  {
        if (LOG.isLoggable(Level.FINEST)) {
          LOG.finest("Adding variable : " + key + " of type : " + var.getClass() + " in expression evaluator");
        }
      }
      if (var == null) {
        if (LOG.isLoggable(Level.FINEST)) {
          LOG.finest("Variable " + var + " added to expression evaluator is null");
        }
      }
    }
    Object result = GroovyUtil.evaluate(groovyExpression, allVars, instanceUUID.getProcessDefinitionUUID());
    return ((Boolean)result).booleanValue();
  }

  public String getValue() {
    return this.value;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy