Sunday, March 31, 2013

Membuat Initiate Task pada Oracle BPM dengan BPM API

Pada Oracle BPM, setiap proses memiliki initiate task, yaitu task yang digunakan untuk melakukan inisialisasi pada proses tertentu.

Umumnya initiate task dilakukan melalui bpm workspace atau melalui web service atau melalui BPM API.

Berikut langkah-langkah untuk melakukan inisialisasi initiate task menggunakan BPM API
1. Buat object JAXB dari file XSD yang ada pada rancangan proses BPM.

2. Gunakan oracle.bpm.client.BPMServiceClientFactory untuk mendapatkan service instance BPM
public static BPMServiceClientFactory getServiceClientFactory(String username,
                                                                  String password,
                                                                  String url) {
        Map properties =
            new HashMap();

        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.CLIENT_TYPE,
                       WorkflowServiceClientFactory.REMOTE_CLIENT);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL,
                       url);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_INITIAL_CONTEXT_FACTORY,
                       "weblogic.jndi.WLInitialContextFactory");
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_PRINCIPAL,
                       username);
        properties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_CREDENTIALS,
                       password);
        return BPMServiceClientFactory.getInstance(properties, null, null);
    }

3. Marshalling object JAXB ke dokumen XML (org.w3c.dom.Element)
public static Element marshallJaxbElementInitiateTask(Object obj) throws JAXBException,
                                                                 ParserConfigurationException,
                                                                 Exception {
        
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Document document = createDocument();
        
        Element payload = createPayloadElement(document);

        marshaller.marshal(obj, document);
        Element elt = document.getDocumentElement();
        
        payload.appendChild(elt);

        return payload;
    }

4. Lalu inisialisasi task dengan mengirim dokumen XML disertai ProjectName, ProcessName dan Version
public String initiateTask(String projectName, String processName, String version, Element payload) throws WorkflowException,
                                                           Exception {
        // Get the authentication context
        IBPMContext ctx = processUtils_.getBPMContext();
        
        // Check that current user is authorized to init the process
        String processDN =
            processUtils_.getInitableProcessDN(ctx, projectName, version,
                                               processName);
        if (processDN == null) {
            System.out.println("Process is not initable. Check deployment and privileges [user:" +
                               ctx.getUser() + "][process:" + projectName +
                               "/" + processName + "/" + version + "]");
            return null;
        }
        // InstanceManagmentService supports process instance services including initiating a process
        IInstanceManagementService instanceSvc =
            processUtils_.getBPMServiceClient().getInstanceManagementService();
        
        // Creates process and returns the task associated with the initiator human task in the process
        Task task = instanceSvc.createProcessInstanceTask(ctx, processDN);
        String taskId = task.getSystemAttributes().getTaskId();
        
        int taskNumber = task.getSystemAttributes().getTaskNumber();
        System.out.println("Initiator task [taskNumber:" + taskNumber + "][taskId:" + taskId + "]");
        
        // Returned task is "sparse", re-fetch to be able to update it with payload and outcome
        task = processUtils_.getHwfServiceClient().getTaskQueryService().getTaskDetailsById(ctx,
                                                                             taskId);
        // Set the payload on the task
        task.setPayloadAsElement(payload);
        
        // HWF TaskService is used to update task
        ITaskService taskSvc =
            processUtils_.getHwfServiceClient().getTaskService();
        
        // Update the task
        task = taskSvc.updateTask(ctx, task);
        
        // Update the outcome.  For initiator task the "SUBMIT" outcome is the default and completes the activity
        taskSvc.updateTaskOutcome(ctx, task,
                                  ProcessUtils.INIT_TASK_SUBMIT_OUTCOME);
        
        System.out.println("Process Instance Id : " +
                           task.getProcessInfo().getInstanceId());
        
        return task.getProcessInfo().getInstanceId();
    }

5. Berikut potongan code pada fungsi mainnya
public static void main(String[] args) {
        BPMServiceClientFactory cf =
            ProcessUtils.getServiceClientFactory("weblogic","welcome1","t3://localhost:7001");
        ProcessUtils processUtils = new ProcessUtils(cf);
        
        String projectName = "Leave";
        String processName = "LeaveProcess";
        String version = "1.0";

        TaskClient client = new TaskClient(processUtils);
        try {
            
            //create payload object
            CutiRequest inputType = new CutiRequest();
            inputType.setApprovalId(100);
            inputType.setDocs("fileLocatiom");
            inputType.setEmpID(100);
            inputType.setEmpNumber("empNumber");
            inputType.setFullName("fullName");
            inputType.setLocation("location");
            inputType.setRemark("renark");
            inputType.setTelNo("telNo");
            
            SubmitLeaveTaskPayloadType obj = new SubmitLeaveTaskPayloadType();
            obj.setCutiRequest(inputType);
            
            //create payload as jaxb element
            Element payload = ProcessUtils.marshallJaxbElementInitiateTask(obj);
            
            String processInstanceId = client.initiateTask(projectName, processName, version, payload);
            System.out.println(processInstanceId);
        } catch (WorkflowException e) {
            e.printStackTrace();
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Code ini terinspirasi dari java.net.

Semoga bermanfaat.  Cheers!!!

No comments:

Post a Comment