Package com.mythics.demo.bpm

Source Code of com.mythics.demo.bpm.CreateInstance

package com.mythics.demo.bpm;

import java.io.IOException;
import java.io.StringWriter;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import java.util.Map;

import oracle.bpel.services.bpm.common.IBPMContext;
import oracle.bpel.services.workflow.StaleObjectException;
import oracle.bpel.services.workflow.WorkflowException;

import oracle.bpel.services.workflow.client.IWorkflowServiceClient;
import oracle.bpel.services.workflow.client.config.ClientConfigurationUtil;
import oracle.bpel.services.workflow.client.config.WorkflowServicesClientConfigurationType;
import oracle.bpel.services.workflow.task.ITaskService;
import oracle.bpel.services.workflow.task.model.Task;

import oracle.bpel.services.workflow.worklist.api.util.WorklistUtil;
import oracle.bpel.services.workflow.worklist.servlet.Constants;

import oracle.bpm.client.BPMServiceClientFactory;
import oracle.bpm.services.client.IBPMServiceClient;
import oracle.bpm.services.common.exception.BPMException;
import oracle.bpm.services.processmetadata.IProcessMetadataService;
import oracle.bpm.services.processmetadata.ProcessMetadataServiceException;
import oracle.bpm.services.processmetadata.ProcessMetadataSummary;

import org.w3c.dom.Element;
import oracle.xml.parser.v2.XMLElement;

public class CreateInstance {
    private static String CLIENT_CONFIG = "wf_client_config.xml";
    private static BPMServiceClientFactory bpmFactory_;
    private static IBPMServiceClient bpmClient_;
    private static IWorkflowServiceClient wfClient_;
    private static IBPMContext bpmCtx_;
    private static ITaskService taskService_;
   
    public static void main (String[] args) {
        try {
            // get a BPM factory
            bpmFactory_ = getServiceClientFactory(CLIENT_CONFIG);
           
            // get a BPM client
            bpmClient_ = bpmFactory_.getBPMServiceClient();
       
            if (bpmClient_ != null) {
              System.out.println("Got a BPM client.");
            }
           
            // TODO: Change credentials for your environment. You must also change your wf_client_config to point to your domain
            bpmCtx_ = getContext("requestor1", "welcome1", null);
       
            if (bpmCtx_ != null) {
              System.out.println("Got a BPM context.");
            }
         
            // Get a handle on a BPM WorkflowServiceClient so we can get more information about the task we create later.         
            wfClient_ = bpmFactory_.getWorkflowServiceClient();
         
            // Get a handle on a BPM Task Service so we can use it to update the task.
            taskService_ = wfClient_.getTaskService();
         

            List<ProcessMetadataSummary> procMetaSummary = getInitiatableProcessList(bpmCtx_);
            System.out.println("Got a list of initiable processes.");
           
            for (ProcessMetadataSummary pms : procMetaSummary) {
                String compositeDN, processName;
                compositeDN = pms.getCompositeDN();
                processName = pms.getProcessName();
                String processDN = pms.getCompositeDN() + "/" +  pms.getProcessName();               
               
                if (processName.equalsIgnoreCase("process")) {
                    //TODO: Change this value to the number of requests you wish to create
                    int numOfInstancesToCreate = 4;
                    for (int i=0; i < numOfInstancesToCreate; i++){
                        Task initdTask = initiateProcess(bpmCtx_, processDN);//null;
                       
                        // get the task number from the new task so we can use it to the get URL of the human workflow to display later.
                        int taskNum = initdTask.getSystemAttributes().getTaskNumber();
                       
                        initdTask = wfClient_.getTaskQueryService().getTaskDetailsByNumber(bpmCtx_, taskNum);
                       
                        Element rootElement = initdTask.getPayloadAsElement();
                        displayPayloadAsString((XMLElement)rootElement);
                       
                        // Manipulate payload values as necessary
                        String ticketNum = "";
                        Date dt = new Date();
                        SimpleDateFormat dateFormatMonthYear = new SimpleDateFormat("MMyy");
                        String ticketNumberMMyy = dateFormatMonthYear.format(dt);
                        ticketNum = ticketNumberMMyy + "-" + initdTask.getProcessInfo().getInstanceId();
                        rootElement.getElementsByTagName("ticketNumber").item(0).setTextContent(ticketNum);
                        initdTask.setPayloadAsElement(rootElement);
                       
                        displayPayloadAsString((XMLElement)initdTask.getPayloadAsElement());
                       
                        try {
                            initdTask = taskService_.updateTask(bpmCtx_, initdTask);
                        } catch (StaleObjectException e) {
                            e.printStackTrace();
                        }
                       
                        // Create a map to pass params to the WorklistUtil object. This is used to get the URL of the Human Workflow Task to display
                        Map params = new HashMap();
                        params.put(Constants.BPM_WORKLIST_TASK_ID, initdTask.getSystemAttributes().getTaskId());
                        params.put(Constants.BPM_WORKLIST_CONTEXT, bpmCtx_.getToken());
                        String url = "";
                        try {
                          url = WorklistUtil.getTaskDisplayURL(wfClient_, bpmCtx_, initdTask, null, "worklist", params);
                        } catch (Exception e) {
                          e.printStackTrace();
                        }
                        System.out.println("Task URL: " + url);
                    }
                }
            }
        } catch (WorkflowException e) {
            e.printStackTrace();
        } catch (ProcessMetadataServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
              if (bpmCtx_ != null) {
                try {
                  bpmFactory_.getBPMUserAuthenticationService().destroyBPMContext(bpmCtx_);
                  System.out.println("Garbage collected the bpm context.");
                } catch (BPMException e) {
                  System.out.println("Unable to dispose of BPM Context for user: " + bpmCtx_.getUser());
                }
            }
        }
    }
   
    /**
       * @param fileName
       * @return
       */
    public static BPMServiceClientFactory getServiceClientFactory(String fileName) {
      WorkflowServicesClientConfigurationType cfg = ClientConfigurationUtil.getClientConfiguration(fileName, null);
      if(cfg == null) {
        throw new RuntimeException("workflow client configuration file not found:"+fileName);
      }
      cfg.setClientType("REMOTE");
   
      return BPMServiceClientFactory.getInstance(cfg, null);
    }
   
    /**
       * IBPMContext is a subclass of workflow context so this can be used everywhere
       *
       * @param factory
       * @param userName
       * @param password
       * @return
       * @throws WorkflowException
       */
    public static IBPMContext getContext(String userName, String password, String authenticationContext) throws WorkflowException {
      IWorkflowServiceClient client = bpmFactory_.getWorkflowServiceClient();
      char[] passwordCharArray = null;
      if(password != null) {
        passwordCharArray = password.toCharArray();
      }
      IBPMContext ctx = null;
      try {
        ctx =
            (IBPMContext) bpmFactory_.getBPMUserAuthenticationService().authenticate(userName, passwordCharArray, authenticationContext);
      } catch (BPMException e) {
        e.printStackTrace();
      }
      return ctx;
    }
   
    public static List<ProcessMetadataSummary> getInitiatableProcessList(IBPMContext context) throws Exception {
      IProcessMetadataService service = bpmClient_.getProcessMetadataService()
      return service.getInitiatableProcesses(context);
    }
   
    public static Task initiateProcess(IBPMContext context, String processDn) throws Exception {
      return bpmClient_.getInstanceManagementService().createProcessInstanceTask(context, processDn );                                  
    }
   
    /**
     * Displays the instance payload
     * @param payload
     */
    public static void displayPayloadAsString(XMLElement payload) {
      StringWriter writer = new StringWriter();
      try {
        payload.print(writer);
      } catch (IOException e) {
        System.out.println("Unable to output XML payload. " + e.getMessage());
      }
      String payloadAsString = writer.toString();
      System.out.println("Payload: " + payloadAsString);
    }
}
TOP

Related Classes of com.mythics.demo.bpm.CreateInstance

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.