OIM API to WithDrawSOATask Using Request ID


package com.SOA;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import oracle.bpel.services.workflow.IWorkflowConstants;
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.IWorkflowServiceClientConstants;
import oracle.bpel.services.workflow.client.WorkflowServiceClientFactory;
import oracle.bpel.services.workflow.query.ITaskQueryService;
import oracle.bpel.services.workflow.repos.Column;
import oracle.bpel.services.workflow.repos.Predicate;
import oracle.bpel.services.workflow.repos.TableConstants;
import oracle.bpel.services.workflow.repos.table.WFTaskConstants;
import oracle.bpel.services.workflow.task.ITaskService;
import oracle.bpel.services.workflow.task.model.Task;
import oracle.bpel.services.workflow.verification.IWorkflowContext;
import oracle.iam.platform.workflowservice.exception.IAMWorkflowException;
public class WithdrawSOATask {

  HashMap<IWorkflowServiceClientConstants.CONNECTION_PROPERTY, String > bpelprops = new HashMap<IWorkflowServiceClientConstants.CONNECTION_PROPERTY, String>();
  {     
    bpelprops.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL,"t3://localhost:8001/soa-infra");
    bpelprops.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_CREDENTIALS,"weblogic1");
    bpelprops.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_PRINCIPAL,"weblogic");
    bpelprops.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
  }
 
 IWorkflowServiceClient client = WorkflowServiceClientFactory.getWorkflowServiceClient(WorkflowServiceClientFactory.REMOTE_CLIENT,bpelprops,null);
 public static void main(String[] args) throws StaleObjectException {
  try {
   new WithdrawSOATask().withdrawTasks("1005",true);
  } catch (IAMWorkflowException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
  private IWorkflowContext getAdminWorkflowContext(ITaskQueryService querySvc) throws WorkflowException {       
         IWorkflowContext adminCtx;
         String username = "weblogic";
         String password = "weblogic1";
         adminCtx = querySvc.authenticate(username, password.toCharArray(), "jazn.com");
         return adminCtx;
     }
  public void withdrawTasks(String idenfiticationKey, boolean withdrawAsRequester) throws IAMWorkflowException, StaleObjectException {
   
  
        System.out.println( "Getting run as subject");       
               
         ITaskService taskSvc = client.getTaskService();
         ITaskQueryService querySvc = client.getTaskQueryService();
         IWorkflowContext adminWorkflowCtx = null;
         IWorkflowContext workflowContext = null;
         
         try {
             adminWorkflowCtx = getAdminWorkflowContext(querySvc);

             String loggedUser = "JOEDOE";
             if (loggedUser != null && withdrawAsRequester) {
                 workflowContext = querySvc.authenticateOnBehalfOf(adminWorkflowCtx, loggedUser);
             }
             List<Task> tasks = getBPELTasksWithCtx(workflowContext, idenfiticationKey, true, false);
             for (Task task : tasks) {
              // In Parallel composites, the tasks assigned to parallel approvers will be under a root task
              // Withdraw only those root tasks, their taskId will be same as their rootTaskId 
              // this if condition holds good for other composites also.
              // sub tasks have their own task id and their root task id will be their parent's task id 
              System.out.println(" RootTaskId " + task.getSystemAttributes().getRootTaskId());
              System.out.println(" TaskId " + task.getSystemAttributes().getRootTaskId());
              System.out.println(" Assigenees " + task.getSystemAttributes().getAssignees());
              if(task.getSystemAttributes().getTaskId() != null &&
                task.getSystemAttributes().getRootTaskId().equals(task.getSystemAttributes().getTaskId())) {
                  if (workflowContext != null) {
                        // taskSvc.withdrawTask(workflowContext, task.getSystemAttributes().getTaskId());
                  } else {
                        // taskSvc.withdrawTask(adminWorkflowCtx, task.getSystemAttributes().getTaskId());
                     }
              }
             }
           tasks = getBPELTasksWithCtx(workflowContext, idenfiticationKey, true, true);
           for (Task task : tasks) {              
              System.out.println(" 2nd RootTaskId " + task.getSystemAttributes().getRootTaskId());
              System.out.println(" 2nd TaskId " + task.getSystemAttributes().getRootTaskId());
              System.out.println(" 2nd Assigenees " + task.getSystemAttributes().getAssignees());
              if(task.getSystemAttributes().getTaskId() != null &&
                task.getSystemAttributes().getRootTaskId().equals(task.getSystemAttributes().getTaskId())) {
                  if (workflowContext != null) {
                         taskSvc.withdrawTask(workflowContext, task.getSystemAttributes().getTaskId());
                  } else {
                         taskSvc.withdrawTask(adminWorkflowCtx, task.getSystemAttributes().getTaskId());
                     }
              }
             }
             
         } catch (WorkflowException e) {
          
             throw new IAMWorkflowException("IAM-2010038", e);
         } finally {
             try {
                 if (workflowContext != null) {
                     querySvc.destroyWorkflowContext(workflowContext);
                    System.out.println( "Destroyed the Normal Context");
                 }         
                 if (adminWorkflowCtx != null) {
                     querySvc.destroyWorkflowContext(adminWorkflowCtx);
                    System.out.println( "Destroyed the Admin Context");
                 }
             } catch (WorkflowException wfe){
              System.out.println( wfe.getMessage());
                     throw new IAMWorkflowException(wfe);
             } finally {
               
             }
         }
         return;
     }
  
  
     private List<Task> getBPELTasksWithCtx(IWorkflowContext ctx, String identificationKey, boolean onlyPendingTasks, boolean onlyAssignedTasks) throws IAMWorkflowException {
         List<String> identificationKeys = new ArrayList<String>();
         identificationKeys.add(identificationKey);
         return fetchBPELTasks(ctx, identificationKeys, onlyPendingTasks, onlyAssignedTasks);
  }
     
     
     private List<Task> fetchBPELTasks(IWorkflowContext ctx, List<String> identificationKeys, boolean onlyPendingTasks, boolean onlyAssignedTasks)
             throws IAMWorkflowException {
             Predicate reqPredicate = null;
             ITaskQueryService querySvc = client.getTaskQueryService();
             try {
                 reqPredicate = new Predicate(
                     TableConstants.WFTASK_IDENTIFICATIONKEY_COLUMN,
                     Predicate.OP_IN, identificationKeys);
                 if(onlyPendingTasks && onlyAssignedTasks) {
                  reqPredicate.addClause(Predicate.AND, Column
                          .getColumn(WFTaskConstants.STATE_COLUMN), Predicate.OP_EQ,
                          IWorkflowConstants.TASK_STATE_ASSIGNED);
                 } else if(onlyPendingTasks && !onlyAssignedTasks){
                  Predicate predicate = new Predicate(
                Column.getColumn(WFTaskConstants.STATE_COLUMN),
                Predicate.OP_EQ, IWorkflowConstants.TASK_STATE_ASSIGNED);
                  predicate.addClause(Predicate.OR, Column
                         .getColumn(WFTaskConstants.STATE_COLUMN), Predicate.OP_EQ,
                         IWorkflowConstants.TASK_STATE_INFO_REQUESTED);
                  reqPredicate = new Predicate(reqPredicate, Predicate.AND, predicate);
                 }
             } catch (WorkflowException e) {                 
                 throw new IAMWorkflowException(e);
             }

             List queryColumns = new ArrayList();
             queryColumns.add("TASKID");
             queryColumns.add("TASKNUMBER");
             queryColumns.add("TITLE");
             queryColumns.add("OUTCOME");
             queryColumns.add("STATE");
             queryColumns.add("ASSIGNEDDATE");
             queryColumns.add("ROOTTASKID");

             List<Task> tasks = null;
             try {
                 tasks = querySvc.queryTasks(ctx, queryColumns, null,
                     ITaskQueryService.AssignmentFilter.ALL, null, reqPredicate,
                     null, 0, 0);
             } catch (WorkflowException e) {              
                 throw new IAMWorkflowException(e);
             }

             return tasks;
         }
}

No comments:

Post a Comment

About OIM

Oracle Identity Management enables organizations to effectively manage the end - to - end life - cycle of user ide...

Popular Posts