AnsweredAssumed Answered

Access to Variable ID

Question asked by ingo_ri on Sep 20, 2011
Latest reply on Sep 26, 2011 by ingo_ri
For business reasons I need to extend the database for variables with attributes for a tenant-number and a display-name which are set for the variables at process instance start.

I declared a new table in the database with a foreign key to the ACT_RU_VARIABLE.ID.

I start the process instances which my own subclass of StartProcessInstanceCmd.

In this class I can't access the variable-id, because VariableInstanceEntity is hidden in the VariableScopeImpl and not available in ExecutionEntity, which is returned from super.execute(commandContext).

What about adding a public getter to the Map of variableInstances in the class VariableScopeImpl?

public abstract class VariableScopeImpl implements Serializable, VariableScope {
… 
  public Map<String, VariableInstanceEntity> getVariableInstances() {
    return variableInstances;
  }
}

With this new method I can set the VariableExtension with the foreign key to their Variables:

public class IngoStartProcessInstanceCmd<T> extends StartProcessInstanceCmd<T> {

   public IngoStartProcessInstanceCmd(String processDefinitionKey,
         String processDefinitionId, String businessKey,
         Map<String, Object> variables) {
      super(processDefinitionKey, processDefinitionId, businessKey, variables);
   }

   @Override
   public ProcessInstance execute(CommandContext commandContext) {
      ProcessInstance pi = null;
      try {
         DeploymentCache deploymentCache = Context
            .getProcessEngineConfiguration()
            .getDeploymentCache();
         ProcessDefinitionEntity processDefinition = null;
         if (processDefinitionId != null) {
            processDefinition = deploymentCache.findDeployedProcessDefinitionById(processDefinitionId);
            if (processDefinition == null) {
               throw new ActivitiException("No process definition found for id = '" + processDefinitionId + "'");
            }
         } else if (processDefinitionKey != null) {
            processDefinition = deploymentCache.findDeployedLatestProcessDefinitionByKey(processDefinitionKey);
            if (processDefinition == null) {
               throw new ActivitiException("No process definition found for id = '" + processDefinitionId + "'");
            }
         }
         
         String tenantNumber = (String) variables.get("mandant");

         // Prozessvariablen anlegen
         HashMap<String, Prozessvariable> prozessvariablen =
               (HashMap<String, Prozessvariable>) processDefinition.getProperty(IngoParseListenerConstants.PROZESSVARIABLENMAPPE);
         if (prozessvariablen != null) {
            for (Iterator<String> prozessvarKeys = prozessvariablen.keySet().iterator(); prozessvarKeys.hasNext();) {
               String prozessvarKey = (String) prozessvarKeys.next();
               Prozessvariable prozessvariable = prozessvariablen.get(prozessvarKey);
               log.info("Prozessvariable " + prozessvariable.getKey() + " (" + prozessvariable.getDisplay() + ") " + prozessvariable.getValue());
               if (!variables.containsKey(prozessvariable.getKey())) {
                  variables.put(prozessvariable.getKey(), prozessvariable.getValue());
               }
            }
         }

         pi = super.execute(commandContext);

         ExecutionEntity execution = (ExecutionEntity) pi;

         Map<String, VariableInstanceEntity> variableInstances = execution.getVariableInstances();
         
         for (Iterator<String> variableNames = variableInstances.keySet().iterator(); variableNames.hasNext;) {
            String variableName = (String) variableNames.next();
            Prozessvariable prozessvar = prozessvariablen.get(variableName);
            if (prozessvar != null) {
               String display = prozessvar.getDisplay();
               if (display != null) {
                  String varId = variableInstances.get(variableName).getId();
                  VariableDisplayEntity variableDisplay = new VariableDisplayEntity(varId, display, tenantNumber);
                  commandContext.getDbSqlSession().insert(variableDisplay);
               }
            }
         }
      } catch (Exception e) {
         throw new ActivitiException("Error setting Execution_Extension:", e);
      }

      return pi;
   }

Outcomes