Package org.jbpm.integration.console

Source Code of org.jbpm.integration.console.ProcessManagementImpl

/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This 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; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.integration.console;

import java.io.InputStream;
import java.util.*;

import org.jboss.bpm.console.client.model.ProcessDefinitionRef;
import org.jboss.bpm.console.client.model.ProcessInstanceRef;
import org.jboss.bpm.console.server.integration.ProcessManagement;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.ProcessInstanceQuery;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.history.HistoryProcessInstanceQuery;
import org.jbpm.pvm.internal.env.Environment;
import org.jbpm.pvm.internal.env.EnvironmentFactory;
import org.jbpm.pvm.internal.model.ExecutionImpl;

/**
* @author Heiko.Braun <heiko.braun@jboss.com>
*/
class ProcessManagementImpl extends JBPMIntegration implements ProcessManagement
{

  public List<ProcessDefinitionRef> getProcessDefinitions()
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {
      List<ProcessDefinitionRef> results = new ArrayList<ProcessDefinitionRef>();

      RepositoryService repositoryService = this.processEngine.getRepositoryService();

      // active processes
      List<ProcessDefinition> activePds = repositoryService.createProcessDefinitionQuery()
          .notSuspended()
          .orderAsc(ProcessDefinitionQuery.PROPERTY_NAME)
          .list();
    
      for(ProcessDefinition processDefinition : activePds)
      {
        ProcessDefinitionRef ref = ModelAdaptor.adoptDefinition(processDefinition);
        results.add(ref);
      }

      // suspended  processes
      ProcessDefinitionQuery pdQuery2 = repositoryService.createProcessDefinitionQuery();
      pdQuery2.suspended();
      List<ProcessDefinition> suspendedPds = pdQuery2.list();

      for(ProcessDefinition p : suspendedPds)
      {
        ProcessDefinitionRef ref = ModelAdaptor.adoptDefinition(p);
        ref.setSuspended(true);       
        results.add(ref);
      }

      return results;
    }
    finally
    {
      env.close();
    }

  }

  public ProcessDefinitionRef getProcessDefinition(String procDefId)
  {

    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {
      RepositoryService repositoryService = this.processEngine.getRepositoryService();
      ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
          .processDefinitionId(procDefId)
          .uniqueResult();
      return ModelAdaptor.adoptDefinition(processDefinition);

    }
    finally
    {
      env.close();
    }
  }

  public List<ProcessDefinitionRef> removeProcessDefinition(String procDefId)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {

      RepositoryService repositoryService = this.processEngine.getRepositoryService();
      ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
          .processDefinitionId(procDefId)
          .uniqueResult();
      if (processDefinition!=null) {
        repositoryService.deleteDeploymentCascade(processDefinition.getDeploymentId());
      }
      return getProcessDefinitions();
    }
    finally
    {
      env.close();
    }
  }

  public List<ProcessInstanceRef> getProcessInstances(String procDefId)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {

      ExecutionService execService = this.processEngine.getExecutionService();
      ProcessInstanceQuery query = execService.createProcessInstanceQuery();
      query.processDefinitionId(String.valueOf(procDefId));

      List<ProcessInstance> processInstances = query.list();

      List<ProcessInstanceRef> results = adoptProcessInstances(processInstances);

      // add history info
      // TODO: optimize w. batch query
      HistoryService histService = this.processEngine.getHistoryService();
      for(ProcessInstanceRef inst : results)
      {
        HistoryProcessInstanceQuery hQuery = histService.createHistoryProcessInstanceQuery();
        hQuery.processInstanceId(inst.getId());
        HistoryProcessInstance entry = hQuery.uniqueResult();
        inst.setStartDate(entry.getStartTime());
      }

      return results;
    }
    finally
    {
      env.close();
    }
  }

  private List<ProcessInstanceRef> adoptProcessInstances(List<ProcessInstance> processInstances)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {

      List<ProcessInstanceRef> results = new ArrayList<ProcessInstanceRef>();
      for(Execution processInstance : processInstances)
      {
        if(processInstance.isEnded())
        {
          //JBPM-2055: Execution is already ended. Should not show up in query
          continue;
        }

        if(processInstance.isProcessInstance()) // parent execution
        {
          results.add( ModelAdaptor.adoptExecution((ExecutionImpl)processInstance) );
        }
      }
      return results;
    }
    finally
    {
      env.close();
    }
  }

  public ProcessInstanceRef getProcessInstance(String instanceId)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {
      ExecutionService execService = this.processEngine.getExecutionService();
      ProcessInstanceQuery query = execService.createProcessInstanceQuery();
      query.processInstanceId(instanceId);
      ProcessInstance processInstance = query.uniqueResult();
      return ModelAdaptor.adoptExecution( (ExecutionImpl)processInstance);
    }
    finally
    {
      env.close();
    }
  }


  public Map<String, Object> getInstanceData(String instanceId)
  {
    Map<String, Object> data = new HashMap<String, Object>();

    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {
      ExecutionService execService = this.processEngine.getExecutionService();
      Set<String> keys = execService.getVariableNames(instanceId);
      data = execService.getVariables(instanceId, keys);
    }
    finally
    {
      env.close();
    }

    return data;
  }

  public void setInstanceData(String instanceId, Map<String, Object> data)
  {
    throw new RuntimeException("Not implemented");
  }

  public ProcessInstanceRef newInstance(String definitionId)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {
      ExecutionService execService = this.processEngine.getExecutionService();
      Execution exec = execService.startProcessInstanceById(definitionId);
      return ModelAdaptor.adoptExecution((ExecutionImpl)exec);
    }
    finally{
      env.close();
    }
  }


  public ProcessInstanceRef newInstance(String definitionId, Map<String, Object> processVars)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {
      ExecutionService execService = this.processEngine.getExecutionService();
      Execution exec = execService.startProcessInstanceById(definitionId);
      execService.setVariables(exec.getId(), processVars);
     
      return ModelAdaptor.adoptExecution((ExecutionImpl)exec);
    }
    finally{
      env.close();
    }
  }

  public void endInstance(String instanceId, ProcessInstanceRef.RESULT result)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {

      ExecutionService execService = this.processEngine.getExecutionService();
      Execution exec = execService.findExecutionById(instanceId);
      if(null==exec)
        throw new IllegalArgumentException("No such execution with id "+ instanceId);

      ProcessInstanceRef.RESULT actualResult = result!=null ? result : ProcessInstanceRef.RESULT.COMPLETED;
      execService.endProcessInstance(instanceId, actualResult.toString());
    }
    finally
    {
      env.close();
    }
  }

  public void deleteInstance(String instanceId)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {

      ExecutionService execService = this.processEngine.getExecutionService();
      Execution exec = execService.findExecutionById(instanceId);
      if(null==exec)
        throw new IllegalArgumentException("No such execution with id "+ instanceId);

      execService.deleteProcessInstance(instanceId);
    }
    finally
    {
      env.close();
    }
  }

  public void setProcessState(
      String executionId,
      ProcessInstanceRef.STATE nextState
  )
  {
    throw new RuntimeException("Not implemented");
  }

  public void signalExecution(String executionId, String signal)
  {
    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();

    try
    {
      ExecutionService execService = this.processEngine.getExecutionService();

      if(null==signal)
        execService.signalExecutionById(executionId);
      else
        execService.signalExecutionById(executionId, signal);
    }
    finally
    {
      env.close();
    }

  }

  public void deploy(String fileName, String contentType, InputStream deployment)
  {
    throw new RuntimeException("Not implemented");
  }
}
TOP

Related Classes of org.jbpm.integration.console.ProcessManagementImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.