⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultprocessinvocationhandler.java

📁 一个java工作流引擎
💻 JAVA
字号:
package org.jbpm.delegation.processinvocation;

import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
import org.jbpm.*;
import org.jbpm.delegation.*;
import org.jbpm.model.definition.Definition;
import org.jbpm.model.execution.*;
import org.jbpm.model.log.*;

public class DefaultProcessInvocationHandler implements ProcessInvocationHandler, Configurable {
  
  private String actorId = null;
  private String actorName = null;
  private String subProcessName = null;
  private String subProcessStartStateLeavingTransitionName = null;
  private Map startVariableMappings = null;
  private Map endVariableMappings = null;

  public ProcessInstance startSubProcess(ProcessInvocationContext processInvocationContext) throws ExecutionException {
    Definition subProcessDefinition = JbpmServiceLocator.getInstance().getExecutionService().getLatestDefinition( subProcessName );
    
    Map subProcessVariables = new HashMap();
    Iterator iter = startVariableMappings.entrySet().iterator();
		while (iter.hasNext()) {
      Map.Entry entry = (Map.Entry) iter.next();
      String superVariableName = (String) entry.getKey();
      String subVariableName = (String) entry.getValue();

      Object superProcessValue = processInvocationContext.getVariable( superVariableName );
      // apply here any conversions if necessary
      subProcessVariables.put( subVariableName, superProcessValue );
		}
    
    // get the actor
    if ( actorName != null ) {
      actorId = (String) processInvocationContext.getVariable( actorName ); 
    }
    
    if ( actorId == null ) {
      actorId = processInvocationContext.getPreviousActorId();
    }

    // start the sub-process-instance
    InvocationLog invocationLog = processInvocationContext.startSubProcessInstance( actorId, subProcessDefinition.getId(), subProcessVariables, subProcessStartStateLeavingTransitionName );
    return invocationLog.getProcessInstance();
  }

	public String handleSubProcessEnd(ExecutionContext superContext, ExecutionContext subContext) {
    Iterator iter = endVariableMappings.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry entry = (Map.Entry) iter.next();
      String subVariableName = (String) entry.getKey();
      String superVariableName = (String) entry.getValue();
      superContext.setVariable( superVariableName, subContext.getVariable( subVariableName ) );
    }
    // take the default leaving transition 
    // this assumes there is exactly one transition leaving the process-state.
    return null;
	}

	public void configure(String configuration) {
    try {
      
      SAXReader saxReader = new SAXReader();
      Document document = saxReader.read( new ByteArrayInputStream( configuration.getBytes() ) );
      Element root = document.getRootElement();
      
      actorId = getElementText( root, "swimlane-id", null );
      actorName = getElementText( root, "swimlane-name", null );
      
      subProcessName = getElementText( root, "sub-process-name", null );
      if ( subProcessName == null ) {
        throw new ConfigurationException( "the sub-process-name is not configured properly in a DefaultProcessInvocationHandler" );
      }
      subProcessStartStateLeavingTransitionName = getElementText( root, "sub-process-start-state-leaving-transition-name", null );
      
			startVariableMappings = new HashMap();
      Iterator iter = root.elementIterator( "start-mapping" );
			while (iter.hasNext()) {
				Element startMappingElement = (Element) iter.next();
        String superVariable = getElementText( startMappingElement, "super-variable", null );
        String subVariable = getElementText( startMappingElement, "sub-variable", null );
        startVariableMappings.put( superVariable, subVariable );
			}
      
      endVariableMappings = new HashMap();
      iter = root.elementIterator( "end-mapping" );
      while (iter.hasNext()) {
        Element startMappingElement = (Element) iter.next();
        String subVariable = getElementText( startMappingElement, "sub-variable", null );
        String superVariable = getElementText( startMappingElement, "super-variable", null );
        endVariableMappings.put( subVariable, superVariable );
      }

    } catch (Exception e) {
			throw new ConfigurationException( "the default process invocation handler was not configured properly", e );
		}
  }

  private String getElementText( Element xmlElement, String name, String defaultValue ) {
    String text = defaultValue;
    Element subElement = xmlElement.element( name );
    if ( subElement != null ) {
      if ( subElement.isTextOnly() ) {
        text = subElement.getStringValue(); 
      }
    }
    return text;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -