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

📄 definitionimpl.java

📁 一个java工作流引擎
💻 JAVA
字号:
package org.jbpm.model.definition.impl;

import java.io.*;
import java.util.*;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.*;
import org.jbpm.model.definition.*;
import org.jbpm.model.execution.impl.*;
import org.jbpm.model.log.impl.*;
import org.jbpm.par.*;


public class DefinitionImpl extends ElementImpl implements Definition {

  private Integer version = null;
  private StartStateImpl startState = null;
  private EndStateImpl endState = null;
  private Map swimlanes = null;
  private List types = null;
  private Map nodes = null;
  private Map variables = null;

	public DefinitionImpl() {}

	public DefinitionImpl(String name) {
		super( name );
	}

  public Integer getVersion() { return version; }
  public void setVersion ( Integer version ) { this.version = version; }

  public StartState getStartState() { return this.startState; }
  public StartStateImpl setStartState(StartStateImpl startState) {
    if ( ( nodes == null )
         || ( ( startState != null  )
              && ( ! nodes.containsKey( startState.getName() ) ) ) ) {
      add( startState ); 
    }
    this.startState = startState;    
    return startState; 
  }

  public EndState getEndState() { return this.endState; }
  public EndStateImpl setEndState(EndStateImpl endState ) { 
    if ( ( nodes == null )
    || ( ( endState != null  )
    && ( ! nodes.containsKey( endState.getName() ) ) ) ) {
      add( endState ); 
    }
    this.endState = endState;    
    return endState; 
  }
  
  public Map getSwimlanes() { return swimlanes; }
  public void setSwimlanes(Map swimlanes) { this.swimlanes = swimlanes; }
  public SwimlaneImpl add(SwimlaneImpl swimlane) {
    if ( swimlane != null ) {
      if ( swimlanes == null ) swimlanes = new HashMap();
      if ( ! swimlanes.containsKey( swimlane.getName() ) ) {
        swimlanes.put( swimlane.getName(), swimlane );
        swimlane.setDefinition( this );
      } else {
        throw new IllegalStateException( "swimlane '" + swimlane.getName() +"' already present in definition '" + name + "'"); 
      }
    }
    return swimlane;
  }
  public void remove( SwimlaneImpl swimlane ) {
    if ( ( swimlane != null )
         && ( swimlanes != null ) ) {
      if ( swimlanes.containsKey( swimlane.getName() ) ) {
        swimlanes.remove( swimlane.getName() );
        swimlane.setDefinition( null );
      }
    }
  }
  public boolean hasSwimlanes() {
    return ( ( swimlanes != null )
             && ( swimlanes.size() > 0 ) );  
  }
  public SwimlaneImpl getSwimlane( String name ) {
    SwimlaneImpl swimlane = null;
    if ( hasSwimlanes() ) {
      swimlane = (SwimlaneImpl) swimlanes.get( name );
    }
    return swimlane;
  }

  public List getTypes() { return types; }
  public void setTypes(List types) { this.types = types; }
	public TypeImpl add(TypeImpl type) {
    if ( type != null ) {
      if ( types == null ) types = new ArrayList();
      if ( ! types.contains( type ) ) {
        types.add( type );
        type.setDefinition( this );
        for ( int i=0; i < types.size(); i++ ) ((TypeImpl)types.get(i)).setIndex(i);
      } else {
        throw new IllegalStateException( "type '" + type.getJavaType() +"' already present in definition '" + name + "'"); 
      }
    }
		return type;
	}
  public void remove( TypeImpl type ) {
    if ( ( type != null )
         && ( types != null ) ) {
      if ( types.contains( type ) ) {
        types.remove( type );
        type.setDefinition( null );
        for ( int i=0; i < types.size(); i++ ) ((TypeImpl)types.get(i)).setIndex(i);
      }
    }
  }
  public boolean hasTypes() {
    return ( ( types != null )
    && ( types.size() > 0 ) );  
  }

  public TypeImpl findTypeByJavaType(String javaType) {
    TypeImpl type = null;

    Iterator iter = types.iterator();
    while ( ( iter.hasNext())
            && ( type == null ) ) {
      TypeImpl candidate = (TypeImpl) iter.next();
      if ( candidate.getJavaType().equals( javaType ) ) {
        type = candidate;
      }
    }

    return type;
  }

  public TypeImpl findTypeByVariableName(String variableName) {
    TypeImpl type = null;

    VariableImpl variable = getVariable( variableName );
    if ( variable != null ) {
      type = variable.getType();
    }
    
    return type;
  }
  
  public TypeImpl findTypeByObject(Object value) {
    TypeImpl type = null;
    
    if ( value != null ) {
      
      if ( hasTypes() ) {
        Collection valueTypeNames = getTypeNames( value.getClass() );
        Iterator iter = types.iterator();
        while ( iter.hasNext()
            && ( type == null ) ) {
          TypeImpl candidate = (TypeImpl) iter.next();
          if ( valueTypeNames.contains( candidate.getJavaType() ) ) {
            type = candidate;
          }
        }
      }
    }
    
    return type;
  }

  public Map getNodes() { return this.nodes; }
  public void setNodes(Map processElements) {  this.nodes = processElements; }
  public NodeImpl add( NodeImpl node ) {
    if ( node != null ) {
      if ( nodes == null ) nodes = new HashMap();
      if ( ! nodes.containsKey( node.getName() ) ) {
        nodes.put( node.getName(), node );
        node.setDefinition( this );
      } else {
        throw new IllegalStateException( "node '" + node.getName() +"' already present in definition '" + name + "'"); 
      }
    }
    return node;
  }
  public void remove( NodeImpl node ) {
    if ( ( node != null )
         && ( nodes != null ) ) {
      if ( nodes.containsKey( node.getName() ) ) {
        nodes.remove( node.getName() );
        node.setDefinition( null );
      }
    }
  }
  public boolean hasNodes() {
    return ( ( nodes != null )
             && ( nodes.size() > 0 ) );  
  }
  public NodeImpl getNode( String nodeName ) {
    return (NodeImpl) nodes.get( nodeName );
  }
  
  public Map getVariables() { return this.variables; }
  public void setVariables(Map variables) { this.variables = variables; }
  public VariableImpl add(VariableImpl variable) {
    if ( variable != null ) {
      if ( variables == null ) variables = new HashMap();
      if ( ! variables.containsKey( variable.getName() ) ) {
        variables.put( variable.getName(), variable );
        variable.setDefinition( this );
      } else {
        throw new IllegalStateException( "variable '" + variable.getName() +"' already present in definition '" + name + "'"); 
      }
    }
    return variable;
  }
  public void remove(VariableImpl variable) {
    if ( ( variable != null )
         && ( variables != null ) ) {
      if ( variables.containsKey( variable.getName() ) ) {
        variables.remove( variable.getName() );
        variable.setDefinition( null );
      }
    }
  }
  public VariableImpl getVariable( String variableName ) {
    VariableImpl variable = null;
    if ( variables != null ) {
      variable = (VariableImpl) variables.get( variableName );
    }
    return variable;
  }

  public String toString() {
    return "process-definition[" + id + "|" + name + "|" + version + "]";
  }

  public EventType getDefaultEventType() {
    return EventType.PROCESS_START;
  }

  public Class loadClass(String className) throws ClassNotFoundException {
    return new DefinitionClassLoader( id ).loadClass( className );
  }
  
  private Collection getTypeNames(Class clazz) {
    Collection typeNames = new ArrayList();
    // add the class name itself
    typeNames.add( clazz.getName() );
    
    // add the interface names
    Class[] interfaces = clazz.getInterfaces();
    for ( int i = 0; i < interfaces.length; i++ ) {
      typeNames.add( interfaces[i].getName() );
    }
    
    while ( clazz.getSuperclass() != null ) {
      clazz = clazz.getSuperclass();
      typeNames.add( clazz.getName() );
    }
    
    return typeNames;
	}

  /**
   * @return the variable instance or null if the type could not be determined.
   */
	public VariableInstanceImpl createVariableInstance(TokenImpl token, String name, Object value) {
    VariableInstanceImpl variableInstance = null;
    
    // if the variable was specified in the processdefinition.xml
    VariableImpl variable = (VariableImpl) variables.get( name );
    if ( variable != null ) {

      // that means that the variable has a type
      TypeImpl type = variable.getType();
      
      // if the type is *not* transient
      if ( ! type.isTransient() ) {
        // create a variable instance for it.
        variableInstance = new VariableInstanceImpl( name, token, variable.getType() );
      } // of course, if this is a transient type, don't do anything

    } else {
      // find the type by matching the value with the declared types.
      TypeImpl type = findTypeByObject( value );
      
      // if the object has a matching type declared in the definition
      if ( type != null ) {
        
        variableInstance = new VariableInstanceImpl( name, token, type );
        
      } else {
        
        DefaultType defaultSerializer = DefaultType.findByValue( value );
        // if the value has a default serializer
        if ( defaultSerializer != null ) {
          
          // create the new variable instance with the default serializer 
          variableInstance = new VariableInstanceImpl( name, token, defaultSerializer );
          
        }
      }
    }

    if ( variableInstance != null ) {
      // log the variable instance creation
      token.add( new VariableCreationLogImpl( variableInstance ) );
    }
    
    return variableInstance;
  }
  
  public String toXml() {
    Document document = DocumentHelper.createDocument();
    document.addDocType( DocType.getRoot(), DocType.getPublicDocTypeId(), DocType.getSystemDocTypeId() );

    org.dom4j.Element root = document.addElement( DocType.getRoot() );
    toXml( root );
    
    StringWriter stringWriter = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter( stringWriter, new OutputFormat( "  ", true ) );

    try {
			xmlWriter.write( document );
		} catch (IOException e) {
      e.printStackTrace();
			e.printStackTrace( new PrintWriter( stringWriter ) );
		}
    
    return stringWriter.toString();
  }

  
  public void toXml(org.dom4j.Element element) {
    /*
    <!ELEMENT process-definition (  description?, 
                                    swimlane*,
                                    type*,
                                    start-state, 
                                    ( state | 
                                      process-state |
                                      decision |
                                      fork |
                                      join
                                    )*,
                                    end-state, 
                                    action* ) >
    <!ATTLIST process-definition name CDATA #REQUIRED >
    */
    
    toXmlNameAndDescription(element);

    if ( swimlanes != null ) {
      element.addText( System.getProperty( "line.separator" ) );
      element.addComment( " SWIMLANES " );
      Iterator iter = swimlanes.values().iterator();
			while (iter.hasNext()) {
				SwimlaneImpl swimlane = (SwimlaneImpl) iter.next();
        swimlane.toXml( element.addElement( "swimlane" ) );
			}
    }

    if ( types != null ) {
      element.addText( System.getProperty( "line.separator" ) );
      element.addComment( " TYPES " );
      Iterator iter = types.iterator();
      while (iter.hasNext()) {
        TypeImpl type = (TypeImpl) iter.next();
        type.toXml( element.addElement( "type" ) );
      }
    }
    
    element.addText( System.getProperty( "line.separator" ) );
    element.addComment( " START-STATE " );
    startState.toXml( element.addElement( "start-state" ) );

    if ( nodes != null ) {
      element.addText( System.getProperty( "line.separator" ) );
      element.addComment( " NODES " );
      Iterator iter = nodes.values().iterator();
      while (iter.hasNext()) {
        NodeImpl node = (NodeImpl) iter.next();
        if ( ( node != startState )
             && ( node != endState ) ) {
          node.toXml( element.addElement( node.getTagName() ) );
        }
      }
    }
    
    element.addText( System.getProperty( "line.separator" ) );
    element.addComment( " END-STATE " );
    endState.toXml( element.addElement( "end-state" ) );

    element.addText( System.getProperty( "line.separator" ) );
    
    toXmlActions( element );
  }
}

⌨️ 快捷键说明

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