OIM API to WithDrawSOATask Using Request ID


  1. package com.oimacademy.SOA;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import oracle.bpel.services.workflow.IWorkflowConstants;
  6. import oracle.bpel.services.workflow.StaleObjectException;
  7. import oracle.bpel.services.workflow.WorkflowException;
  8. import oracle.bpel.services.workflow.client.IWorkflowServiceClient;
  9. import oracle.bpel.services.workflow.client.IWorkflowServiceClientConstants;
  10. import oracle.bpel.services.workflow.client.WorkflowServiceClientFactory;
  11. import oracle.bpel.services.workflow.query.ITaskQueryService;
  12. import oracle.bpel.services.workflow.repos.Column;
  13. import oracle.bpel.services.workflow.repos.Predicate;
  14. import oracle.bpel.services.workflow.repos.TableConstants;
  15. import oracle.bpel.services.workflow.repos.table.WFTaskConstants;
  16. import oracle.bpel.services.workflow.task.ITaskService;
  17. import oracle.bpel.services.workflow.task.model.Task;
  18. import oracle.bpel.services.workflow.verification.IWorkflowContext;
  19. import oracle.iam.platform.workflowservice.exception.IAMWorkflowException;
  20. public class WithdrawSOATask {
  21.   HashMap<IWorkflowServiceClientConstants.CONNECTION_PROPERTY, String > bpelprops = new HashMap<IWorkflowServiceClientConstants.CONNECTION_PROPERTY, String>();
  22.   {     
  23.     bpelprops.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL,"t3://localhost:8001/soa-infra");
  24.     bpelprops.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_CREDENTIALS,"weblogic1");
  25.     bpelprops.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_SECURITY_PRINCIPAL,"weblogic");
  26.     bpelprops.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
  27.   } 
  28.  IWorkflowServiceClient client = WorkflowServiceClientFactory.getWorkflowServiceClient(WorkflowServiceClientFactory.REMOTE_CLIENT,bpelprops,null);
  29.  public static void main(String[] args) throws StaleObjectException {
  30.   try {
  31.    new WithdrawSOATask().withdrawTasks("1005",true);
  32.   } catch (IAMWorkflowException e) {
  33.    // TODO Auto-generated catch block
  34.    e.printStackTrace();
  35.   }
  36.  }
  37.   private IWorkflowContext getAdminWorkflowContext(ITaskQueryService querySvc) throws WorkflowException {       
  38.          IWorkflowContext adminCtx;
  39.          String username = "weblogic";
  40.          String password = "weblogic1";
  41.          adminCtx = querySvc.authenticate(username, password.toCharArray(), "jazn.com");
  42.          return adminCtx;
  43.      }
  44.   public void withdrawTasks(String idenfiticationKey, boolean withdrawAsRequester) throws IAMWorkflowException, StaleObjectException {
  45.        System.out.println( "Getting run as subject");        
  46.          ITaskService taskSvc = client.getTaskService();
  47.          ITaskQueryService querySvc = client.getTaskQueryService();
  48.          IWorkflowContext adminWorkflowCtx = null;
  49.          IWorkflowContext workflowContext = null;         
  50.          try {
  51.              adminWorkflowCtx = getAdminWorkflowContext(querySvc);
  52.              String loggedUser = "JOEDOE";
  53.              if (loggedUser != null && withdrawAsRequester) {
  54.                  workflowContext = querySvc.authenticateOnBehalfOf(adminWorkflowCtx, loggedUser);
  55.              }
  56.              List<Task> tasks = getBPELTasksWithCtx(workflowContext, idenfiticationKey, true, false);
  57.              for (Task task : tasks) {
  58.               // In Parallel composites, the tasks assigned to parallel approvers will be under a root task
  59.               // Withdraw only those root tasks, their taskId will be same as their rootTaskId 
  60.               // this if condition holds good for other composites also.
  61.               // sub tasks have their own task id and their root task id will be their parent's task id 
  62.               System.out.println(" RootTaskId " + task.getSystemAttributes().getRootTaskId());
  63.               System.out.println(" TaskId " + task.getSystemAttributes().getRootTaskId());
  64.               System.out.println(" Assigenees " + task.getSystemAttributes().getAssignees());
  65.               if(task.getSystemAttributes().getTaskId() != null &&
  66.                 task.getSystemAttributes().getRootTaskId().equals(task.getSystemAttributes().getTaskId())) {
  67.                   if (workflowContext != null) {
  68.                         // taskSvc.withdrawTask(workflowContext, task.getSystemAttributes().getTaskId());
  69.                   } else {
  70.                         // taskSvc.withdrawTask(adminWorkflowCtx, task.getSystemAttributes().getTaskId());
  71.                      }
  72.               }
  73.              }
  74.            tasks = getBPELTasksWithCtx(workflowContext, idenfiticationKey, true, true);
  75.            for (Task task : tasks) {              
  76.               System.out.println(" 2nd RootTaskId " + task.getSystemAttributes().getRootTaskId());
  77.               System.out.println(" 2nd TaskId " + task.getSystemAttributes().getRootTaskId());
  78.               System.out.println(" 2nd Assigenees " + task.getSystemAttributes().getAssignees());
  79.               if(task.getSystemAttributes().getTaskId() != null &&
  80.                 task.getSystemAttributes().getRootTaskId().equals(task.getSystemAttributes().getTaskId())) {
  81.                   if (workflowContext != null) {
  82.                          taskSvc.withdrawTask(workflowContext, task.getSystemAttributes().getTaskId());
  83.                   } else {
  84.                          taskSvc.withdrawTask(adminWorkflowCtx, task.getSystemAttributes().getTaskId());
  85.                      }
  86.               }
  87.              }             
  88.          } catch (WorkflowException e) {          
  89.              throw new IAMWorkflowException("IAM-2010038", e);
  90.          } finally {
  91.              try {
  92.                  if (workflowContext != null) {
  93.                      querySvc.destroyWorkflowContext(workflowContext);
  94.                     System.out.println( "Destroyed the Normal Context");
  95.                  }         
  96.                  if (adminWorkflowCtx != null) {
  97.                      querySvc.destroyWorkflowContext(adminWorkflowCtx);
  98.                     System.out.println( "Destroyed the Admin Context");
  99.                  }
  100.              } catch (WorkflowException wfe){
  101.               System.out.println( wfe.getMessage());
  102.                      throw new IAMWorkflowException(wfe);
  103.              } finally {               
  104.              }
  105.          }
  106.          return;
  107.      } 
  108.      private List<Task> getBPELTasksWithCtx(IWorkflowContext ctx, String identificationKey, boolean onlyPendingTasks, boolean onlyAssignedTasks) throws IAMWorkflowException {
  109.          List<String> identificationKeys = new ArrayList<String>();
  110.          identificationKeys.add(identificationKey);
  111.          return fetchBPELTasks(ctx, identificationKeys, onlyPendingTasks, onlyAssignedTasks);
  112.   }     
  113.      private List<Task> fetchBPELTasks(IWorkflowContext ctx, List<String> identificationKeys, boolean onlyPendingTasks, boolean onlyAssignedTasks)
  114.              throws IAMWorkflowException {
  115.              Predicate reqPredicate = null;
  116.              ITaskQueryService querySvc = client.getTaskQueryService();
  117.              try {
  118.                  reqPredicate = new Predicate(
  119.                      TableConstants.WFTASK_IDENTIFICATIONKEY_COLUMN,
  120.                      Predicate.OP_IN, identificationKeys);
  121.                  if(onlyPendingTasks && onlyAssignedTasks) {
  122.                   reqPredicate.addClause(Predicate.AND, Column
  123.                           .getColumn(WFTaskConstants.STATE_COLUMN), Predicate.OP_EQ,
  124.                           IWorkflowConstants.TASK_STATE_ASSIGNED);
  125.                  } else if(onlyPendingTasks && !onlyAssignedTasks){
  126.                   Predicate predicate = new Predicate(
  127.                 Column.getColumn(WFTaskConstants.STATE_COLUMN),
  128.                 Predicate.OP_EQ, IWorkflowConstants.TASK_STATE_ASSIGNED);
  129.                   predicate.addClause(Predicate.OR, Column
  130.                          .getColumn(WFTaskConstants.STATE_COLUMN), Predicate.OP_EQ,
  131.                          IWorkflowConstants.TASK_STATE_INFO_REQUESTED);
  132.                   reqPredicate = new Predicate(reqPredicate, Predicate.AND, predicate);
  133.                  }
  134.              } catch (WorkflowException e) {                 
  135.                  throw new IAMWorkflowException(e);
  136.              }
  137.              List queryColumns = new ArrayList();
  138.              queryColumns.add("TASKID");
  139.              queryColumns.add("TASKNUMBER");
  140.              queryColumns.add("TITLE");
  141.              queryColumns.add("OUTCOME");
  142.              queryColumns.add("STATE");
  143.              queryColumns.add("ASSIGNEDDATE");
  144.              queryColumns.add("ROOTTASKID");
  145.              List<Task> tasks = null;
  146.              try {
  147.                  tasks = querySvc.queryTasks(ctx, queryColumns, null,
  148.                      ITaskQueryService.AssignmentFilter.ALL, null, reqPredicate,
  149.                      null, 0, 0);
  150.              } catch (WorkflowException e) {              
  151.                  throw new IAMWorkflowException(e);
  152.              }
  153.              return tasks;
  154.          }
  155. }

No comments:

Post a Comment