org.camunda.bpm.engine.delegate.VariableScope Java Examples

The following examples show how to use org.camunda.bpm.engine.delegate.VariableScope. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaskDecorator.java    From camunda-bpm-platform with Apache License 2.0 7 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void initializeTaskCandidateGroups(TaskEntity task, VariableScope variableScope) {
  Set<Expression> candidateGroupIdExpressions = taskDefinition.getCandidateGroupIdExpressions();

  for (Expression groupIdExpr : candidateGroupIdExpressions) {
    Object value = groupIdExpr.getValue(variableScope);

    if (value instanceof String) {
      List<String> candiates = extractCandidates((String) value);
      task.addCandidateGroups(candiates);

    } else if (value instanceof Collection) {
      task.addCandidateGroups((Collection) value);

    } else {
      throw new ProcessEngineException("Expression did not resolve to a string or collection of strings");
    }
  }
}
 
Example #2
Source File: TaskDecorator.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected void initializeTaskFollowUpDate(TaskEntity task, VariableScope variableScope) {
  Expression followUpDateExpression = taskDefinition.getFollowUpDateExpression();
  if(followUpDateExpression != null) {
    Object followUpDate = followUpDateExpression.getValue(variableScope);
    if(followUpDate != null) {
      if (followUpDate instanceof Date) {
        task.setFollowUpDate((Date) followUpDate);

      } else if (followUpDate instanceof String) {
        BusinessCalendar businessCalendar = getBusinessCalender();
        task.setFollowUpDate(businessCalendar.resolveDuedate((String) followUpDate));

      } else {
        throw new ProcessEngineException("Follow up date expression does not resolve to a Date or Date string: " +
            followUpDateExpression.getExpressionText());
      }
    }
  }
}
 
Example #3
Source File: SourceExecutableScript.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public Object evaluate(ScriptEngine engine, VariableScope variableScope, Bindings bindings) {
  if (shouldBeCompiled) {
    compileScript(engine);
  }

  if (getCompiledScript() != null) {
    return super.evaluate(engine, variableScope, bindings);
  }
  else {
    try {
      return evaluateScript(engine, bindings);
    } catch (ScriptException e) {
      if (e.getCause() instanceof BpmnError) {
        throw (BpmnError) e.getCause();
      }
      String activityIdMessage = getActivityIdExceptionMessage(variableScope);
      throw new ScriptEvaluationException("Unable to evaluate script" + activityIdMessage + ":" + e.getMessage(), e);
    }
  }
}
 
Example #4
Source File: JuelExpression.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public Object getValue(VariableScope variableScope, BaseDelegateExecution contextExecution) {
  ELContext elContext = expressionManager.getElContext(variableScope);
  try {
    ExpressionGetInvocation invocation = new ExpressionGetInvocation(valueExpression, elContext, contextExecution);
    Context.getProcessEngineConfiguration()
      .getDelegateInterceptor()
      .handleInvocation(invocation);
    return invocation.getInvocationResult();
  } catch (PropertyNotFoundException pnfe) {
    throw new ProcessEngineException("Unknown property used in expression: " + expressionText+". Cause: "+pnfe.getMessage(), pnfe);
  } catch (MethodNotFoundException mnfe) {
    throw new ProcessEngineException("Unknown method used in expression: " + expressionText+". Cause: "+mnfe.getMessage(), mnfe);
  } catch(ELException ele) {
    throw new ProcessEngineException("Error while evaluating expression: " + expressionText+". Cause: "+ele.getMessage(), ele);
  } catch (Exception e) {
    throw new ProcessEngineException("Error while evaluating expression: " + expressionText+". Cause: "+e.getMessage(), e);
  }
}
 
Example #5
Source File: TaskDecorator.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected void initializeTaskDueDate(TaskEntity task, VariableScope variableScope) {
  Expression dueDateExpression = taskDefinition.getDueDateExpression();
  if(dueDateExpression != null) {
    Object dueDate = dueDateExpression.getValue(variableScope);
    if(dueDate != null) {
      if (dueDate instanceof Date) {
        task.setDueDate((Date) dueDate);

      } else if (dueDate instanceof String) {
        BusinessCalendar businessCalendar = getBusinessCalender();
        task.setDueDate(businessCalendar.resolveDuedate((String) dueDate));

      } else {
        throw new ProcessEngineException("Due date expression does not resolve to a Date or Date string: " +
            dueDateExpression.getExpressionText());
      }
    }
  }
}
 
Example #6
Source File: TaskDecorator.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public void decorate(TaskEntity task, VariableScope variableScope) {
  // set the taskDefinition
  task.setTaskDefinition(taskDefinition);

  // name
  initializeTaskName(task, variableScope);
  // description
  initializeTaskDescription(task, variableScope);
  // dueDate
  initializeTaskDueDate(task, variableScope);
  // followUpDate
  initializeTaskFollowUpDate(task, variableScope);
  // priority
  initializeTaskPriority(task, variableScope);
  // assignments
  initializeTaskAssignments(task, variableScope);
}
 
Example #7
Source File: DelegateExpressionTaskListener.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public void notify(DelegateTask delegateTask) {
  // Note: we can't cache the result of the expression, because the
  // execution can change: eg. delegateExpression='${mySpringBeanFactory.randomSpringBean()}'

  VariableScope variableScope = delegateTask.getExecution();
  if (variableScope == null) {
    variableScope = delegateTask.getCaseExecution();
  }

  Object delegate = expression.getValue(variableScope);
  applyFieldDeclaration(fieldDeclarations, delegate);

  if (delegate instanceof TaskListener) {
    try {
      Context.getProcessEngineConfiguration()
        .getDelegateInterceptor()
        .handleInvocation(new TaskListenerInvocation((TaskListener)delegate, delegateTask));
    }catch (Exception e) {
      throw new ProcessEngineException("Exception while invoking TaskListener: "+e.getMessage(), e);
    }
  } else {
    throw new ProcessEngineException("Delegate expression " + expression
            + " did not resolve to an implementation of " + TaskListener.class );
  }
}
 
Example #8
Source File: CallableElementUtil.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public static DecisionDefinition getDecisionDefinitionToCall(VariableScope execution, BaseCallableElement callableElement) {
  String decisionDefinitionKey = callableElement.getDefinitionKey(execution);
  String tenantId = callableElement.getDefinitionTenantId(execution);

  DeploymentCache deploymentCache = getDeploymentCache();

  DecisionDefinition decisionDefinition = null;
  if (callableElement.isLatestBinding()) {
    decisionDefinition = deploymentCache.findDeployedLatestDecisionDefinitionByKeyAndTenantId(decisionDefinitionKey, tenantId);

  } else if (callableElement.isDeploymentBinding()) {
    String deploymentId = callableElement.getDeploymentId();
    decisionDefinition = deploymentCache.findDeployedDecisionDefinitionByDeploymentAndKey(deploymentId, decisionDefinitionKey);

  } else if (callableElement.isVersionBinding()) {
    Integer version = callableElement.getVersion(execution);
    decisionDefinition = deploymentCache.findDeployedDecisionDefinitionByKeyVersionAndTenantId(decisionDefinitionKey, version, tenantId);

  } else if (callableElement.isVersionTagBinding()) {
    String versionTag = callableElement.getVersionTag(execution);
    decisionDefinition = deploymentCache.findDeployedDecisionDefinitionByKeyVersionTagAndTenantId(decisionDefinitionKey, versionTag, tenantId);
  }

  return decisionDefinition;
}
 
Example #9
Source File: CallableElement.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public String getBusinessKey(VariableScope variableScope) {
  if (businessKeyValueProvider == null) {
    return null;
  }

  Object result = businessKeyValueProvider.getValue(variableScope);

  if (result != null && !(result instanceof String)) {
    throw new ClassCastException("Cannot cast '"+result+"' to String");
  }

  return (String) result;
}
 
Example #10
Source File: DefaultFormHandler.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void submitFormVariables(VariableMap properties, VariableScope variableScope) {
  boolean userOperationLogEnabled = Context.getCommandContext().isUserOperationLogEnabled();
  Context.getCommandContext().enableUserOperationLog();

  VariableMap propertiesCopy = new VariableMapImpl(properties);

  // support legacy form properties
  for (FormPropertyHandler formPropertyHandler: formPropertyHandlers) {
    // submitFormProperty will remove all the keys which it takes care of
    formPropertyHandler.submitFormProperty(variableScope, propertiesCopy);
  }

  // support form data:
  for (FormFieldHandler formFieldHandler : formFieldHandlers) {
    if (!formFieldHandler.isBusinessKey()) {
      formFieldHandler.handleSubmit(variableScope, propertiesCopy, properties);
    }
  }

  // any variables passed in which are not handled by form-fields or form
  // properties are added to the process as variables
  for (String propertyId: propertiesCopy.keySet()) {
    variableScope.setVariable(propertyId, propertiesCopy.getValueTyped(propertyId));
  }

  fireFormPropertyHistoryEvents(properties, variableScope);

  Context.getCommandContext().setLogUserOperationEnabled(userOperationLogEnabled);
}
 
Example #11
Source File: FormFieldValidationConstraintHandler.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void validate(Object submittedValue, VariableMap submittedValues, FormFieldHandler formFieldHandler, VariableScope variableScope) {
  try {

    FormFieldValidatorContext context = new DefaultFormFieldValidatorContext(variableScope, config, submittedValues, formFieldHandler);
    if(!validator.validate(submittedValue, context)) {
      throw new FormFieldValidatorException(formFieldHandler.getId(), name, config, submittedValue, "Invalid value submitted for form field '"+formFieldHandler.getId()+"': validation of "+this+" failed.");
    }
  } catch(FormFieldValidationException e) {
    throw new FormFieldValidatorException(formFieldHandler.getId(), name, config, submittedValue, "Invalid value submitted for form field '"+formFieldHandler.getId()+"': validation of "+this+" failed.", e);
  }
}
 
Example #12
Source File: BaseCallableElement.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public String getVersionTag(VariableScope variableScope) {
  Object result = versionTagValueProvider.getValue(variableScope);

  if (result != null) {
    if (result instanceof String) {
      return (String) result;
    } else {
      throw new ProcessEngineException("It is not possible to transform '"+result+"' into a string.");
    }
  }

  return null;
}
 
Example #13
Source File: DelegateFormHandler.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void submitFormVariables(final VariableMap properties, final VariableScope variableScope) {
  performContextSwitch(new Callable<Void> () {
    public Void call() throws Exception {
      Context.getProcessEngineConfiguration()
          .getDelegateInterceptor()
          .handleInvocation(new SubmitFormVariablesInvocation(formHandler, properties, variableScope));

      return null;
    }
  });
}
 
Example #14
Source File: MyStartFormHandler.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void submitFormVariables(VariableMap properties, VariableScope variableScope) {
  ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
  IdentityService identityService = processEngineConfiguration.getIdentityService();
  RuntimeService runtimeService = processEngineConfiguration.getRuntimeService();

  logAuthentication(identityService);
  logInstancesCount(runtimeService);
}
 
Example #15
Source File: FormFieldHandler.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void handleSubmit(VariableScope variableScope, VariableMap values, VariableMap allValues) {
  TypedValue submittedValue = (TypedValue) values.getValueTyped(id);
  values.remove(id);

  // perform validation
  for (FormFieldValidationConstraintHandler validationHandler : validationHandlers) {
    Object value = null;
    if(submittedValue != null) {
      value = submittedValue.getValue();
    }
    validationHandler.validate(value, allValues, this, variableScope);
  }

  // update variable(s)
  TypedValue modelValue = null;
  if (submittedValue != null) {
    if (type != null) {
      modelValue = type.convertToModelValue(submittedValue);
    }
    else {
      modelValue = submittedValue;
    }
  }
  else if (defaultValueExpression != null) {
    final TypedValue expressionValue = Variables.untypedValue(defaultValueExpression.getValue(variableScope));
    if (type != null) {
      // first, need to convert to model value since the default value may be a String Constant specified in the model xml.
      modelValue = type.convertToModelValue(Variables.untypedValue(expressionValue));
    }
    else if (expressionValue != null) {
      modelValue = Variables.stringValue(expressionValue.getValue().toString());
    }
  }

  if (modelValue != null) {
    if (id != null) {
      variableScope.setVariable(id, modelValue);
    }
  }
}
 
Example #16
Source File: ListValueProvider.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public Object getValue(VariableScope variableScope) {
  List<Object> valueList = new ArrayList<Object>();
  for (ParameterValueProvider provider : providerList) {
    valueList.add(provider.getValue(variableScope));
  }
  return valueList;
}
 
Example #17
Source File: EventSubscriptionDeclaration.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public EventSubscriptionEntity createSubscriptionForStartEvent(ProcessDefinitionEntity processDefinition) {
  EventSubscriptionEntity eventSubscriptionEntity = new EventSubscriptionEntity(eventType);

  VariableScope scopeForExpression = StartProcessVariableScope.getSharedInstance();
  String eventName = resolveExpressionOfEventName(scopeForExpression);
  eventSubscriptionEntity.setEventName(eventName);
  eventSubscriptionEntity.setActivityId(activityId);
  eventSubscriptionEntity.setConfiguration(processDefinition.getId());
  eventSubscriptionEntity.setTenantId(processDefinition.getTenantId());

  return eventSubscriptionEntity;
}
 
Example #18
Source File: ScriptingEnvironment.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public Object execute(ExecutableScript script, VariableScope scope, Bindings bindings, ScriptEngine scriptEngine) {

    final String scriptLanguage = script.getLanguage();

    // first, evaluate the env scripts (if any)
    List<ExecutableScript> envScripts = getEnvScripts(scriptLanguage);
    for (ExecutableScript envScript : envScripts) {
      envScript.execute(scriptEngine, scope, bindings);
    }

    // next evaluate the actual script
    return script.execute(scriptEngine, scope, bindings);
  }
 
Example #19
Source File: ResourceExecutableScript.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public Object evaluate(ScriptEngine engine, VariableScope variableScope, Bindings bindings) {
  if (scriptSource == null) {
    loadScriptSource();
  }
  return super.evaluate(engine, variableScope, bindings);
}
 
Example #20
Source File: AbstractGetFormVariablesCmd.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected TypedValue createVariable(FormField formField, VariableScope variableScope) {
  TypedValue value = formField.getValue();

  if(value != null) {
    return value;
  }
  else {
    return null;
  }

}
 
Example #21
Source File: ExecutableScript.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected String getActivityIdExceptionMessage(VariableScope variableScope) {
  String activityId = null;
  String definitionIdMessage = "";

  if (variableScope instanceof DelegateExecution) {
    activityId = ((DelegateExecution) variableScope).getCurrentActivityId();
    definitionIdMessage = " in the process definition with id '" + ((DelegateExecution) variableScope).getProcessDefinitionId() + "'";
  } else if (variableScope instanceof TaskEntity) {
    TaskEntity task = (TaskEntity) variableScope;
    if (task.getExecution() != null) {
      activityId = task.getExecution().getActivityId();
      definitionIdMessage = " in the process definition with id '" + task.getProcessDefinitionId() + "'";
    }
    if (task.getCaseExecution() != null) {
      activityId = task.getCaseExecution().getActivityId();
      definitionIdMessage = " in the case definition with id '" + task.getCaseDefinitionId() + "'";
    }
  } else if (variableScope instanceof DelegateCaseExecution) {
    activityId = ((DelegateCaseExecution) variableScope).getActivityId();
    definitionIdMessage = " in the case definition with id '" + ((DelegateCaseExecution) variableScope).getCaseDefinitionId() + "'";
  }

  if (activityId == null) {
    return "";
  } else {
    return " while executing activity '" + activityId + "'" + definitionIdMessage;
  }
}
 
Example #22
Source File: VariableScopeElResolver.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void setValue(ELContext context, Object base, Object property, Object value) {
  if (base == null) {
    String variable = (String) property;
    Object object = context.getContext(VariableScope.class);
    if (object != null) {
      VariableScope variableScope = (VariableScope) object;
      if (variableScope.hasVariable(variable)) {
        variableScope.setVariable(variable, value);
        context.setPropertyResolved(true);
      }
    }
  }
}
 
Example #23
Source File: DynamicExecutableScript.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public Object evaluate(ScriptEngine scriptEngine, VariableScope variableScope, Bindings bindings) {
  String source = getScriptSource(variableScope);
  try {
    return scriptEngine.eval(source, bindings);
  }
  catch (ScriptException e) {
    String activityIdMessage = getActivityIdExceptionMessage(variableScope);
    throw new ScriptEvaluationException("Unable to evaluate script" + activityIdMessage + ": " + e.getMessage(), e);
  }
}
 
Example #24
Source File: EventSubscriptionDeclaration.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves the event name within the given scope.
 */
public String resolveExpressionOfEventName(VariableScope scope) {
  if (isExpressionAvailable()) {
    return (String) eventName.getValue(scope);
  } else {
    return null;
  }
}
 
Example #25
Source File: ScriptCondition.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public boolean tryEvaluate(VariableScope scope, DelegateExecution execution) {
  boolean result = false;

  try {
    result = evaluate(scope, execution);
  } catch (ProcessEngineException pee) {
    if (! (pee.getMessage().contains("No such property") ||
           pee.getCause() instanceof ScriptEvaluationException) ) {
      throw pee;
    }
  }

  return result;
}
 
Example #26
Source File: VariableScopeElResolver.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public boolean isReadOnly(ELContext context, Object base, Object property) {
  if (base == null) {
    String variable = (String) property;
    Object object = context.getContext(VariableScope.class);
    return object != null && !((VariableScope)object).hasVariable(variable);
  }
  return true;
}
 
Example #27
Source File: VariableScopeElResolver.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public Object getValue(ELContext context, Object base, Object property)  {

    Object object = context.getContext(VariableScope.class);
    if(object != null) {
      VariableScope variableScope = (VariableScope) object;
      if (base == null) {
        String variable = (String) property; // according to javadoc, can only be a String

        if( (EXECUTION_KEY.equals(property) && variableScope instanceof ExecutionEntity)
                || (TASK_KEY.equals(property) && variableScope instanceof TaskEntity)
                || (variableScope instanceof CaseExecutionEntity
                && (CASE_EXECUTION_KEY.equals(property) || EXECUTION_KEY.equals(property))) ) {
          context.setPropertyResolved(true);
          return variableScope;
        } else if (EXECUTION_KEY.equals(property) && variableScope instanceof TaskEntity) {
          context.setPropertyResolved(true);
          return ((TaskEntity) variableScope).getExecution();
        } else if(LOGGED_IN_USER_KEY.equals(property)){
          context.setPropertyResolved(true);
          return Context.getCommandContext().getAuthenticatedUserId();
        } else {
          if (variableScope.hasVariable(variable)) {
            context.setPropertyResolved(true); // if not set, the next elResolver in the CompositeElResolver will be called
            return variableScope.getVariable(variable);
          }
        }
      }
    }

    // property resolution (eg. bean.value) will be done by the BeanElResolver (part of the CompositeElResolver)
    // It will use the bean resolved in this resolver as base.

    return null;
  }
 
Example #28
Source File: TaskDecorator.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected void initializeTaskDescription(TaskEntity task, VariableScope variableScope) {
  Expression descriptionExpression = taskDefinition.getDescriptionExpression();
  if (descriptionExpression != null) {
    String description = (String) descriptionExpression.getValue(variableScope);
    task.setDescription(description);
  }
}
 
Example #29
Source File: FluentMock.java    From camunda-bpm-mockito with Apache License 2.0 5 votes vote down vote up
protected void setVariables(final VariableScope variableScope, final Map<String, Object> variables) {
  if (variables == null || variables.isEmpty()) {
    return;
  }
  for (final Entry<String, Object> variable : variables.entrySet()) {
    variableScope.setVariable(variable.getKey(), variable.getValue());
  }
}
 
Example #30
Source File: TaskDecorator.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected void initializeTaskName(TaskEntity task, VariableScope variableScope) {
  Expression nameExpression = taskDefinition.getNameExpression();
  if (nameExpression != null) {
    String name = (String) nameExpression.getValue(variableScope);
    task.setName(name);
  }
}