Wednesday 11 January 2017

Teamcenter SOA Service : WorkFlow Submit And Complete with Form Attribute Modifcation

Problem statement :

1) Initiate the workflow 
2) Interact with workflow to provide the input
3) Complete the workflow
At the end of this section you will be able to deal with workflow related activities in Teamcenter

Visual of what we wanted through code ...


String item_uid="E_Sh3sWzY95QJC#ABSEP#EuRh3sWzY95QJC#ABSEP#01fh3sWzY95QJC#ABSEP#0lUh3sWzY95QJC";
String[] arrObjectUID = item_uid.split("#ABSEP#"); int[] arrTypes = new int[arrObjectUID.length]; Arrays.fill(arrTypes, 1); arrTypes[arrObjectUID.length-1]=3;//Reference Attachement //One to One Relation between attachment and attachment types //attachment type=1 //Target attachmetn //attachment type=3 //Reference attachment //Above statement says that last object defined in the item_uid string is attached in the workflow as //reference attachment WorkflowService wfService = WorkflowService.getService(connection); if(wfService==null) { System.out.println("Unable to find WorkflowService.please try again"); } //DataManagementService smService = DataManagementService.getService(connection); for(int i=0;i<arrObjectUID.length;i++) {   System.out.println("arrObjectUID["+i+"]="+arrObjectUID[i]); } for(int i=0;i<arrTypes.length;i++) {   System.out.println("arrTypes["+i+"]="+arrTypes[i]); } ContextData contextData = new ContextData(); contextData.attachmentCount = arrObjectUID.length; contextData.attachments = arrObjectUID; contextData.attachmentTypes = arrTypes; contextData.processTemplate = "Create XYZ Report";//"Simple Release"; System.out.println("Context data initialized"); InstanceInfo instanceResponse = wfService.createInstance(true, null, "TestGENERATION", null,"GENERATION DESC", contextData); if(instanceResponse.serviceData.sizeOfPartialErrors() == 0) System.out.println("Process created Successfully"); else {   System.out.println("Error :"+instanceResponse.serviceData.getPartialError(0).getMessages());   System.out.println("Error :"+instanceResponse.serviceData.getPartialError(0).getErrorValues()); } ModelObject taskobject=instanceResponse.serviceData.getCreatedObject(0); System.out.println("Task Object = "+taskobject.getClass()); DataManagementService dmServiceTI = DataManagementService.getService(connection); Folder userinbox; try { String[] attributes = { "contents","object_name" }; userinbox = (Folder)user.get_userinbox();//Get Inbox for User dmServiceTI.getProperties( new ModelObject[]{ userinbox }, attributes ); //userinbox.get_contents(); System.out.println("userinbox = "+userinbox); System.out.println("userinbox = "+userinbox.get_object_name()); WorkspaceObject[] contents = null; WorkspaceObject UserInboxFolderList[]=null; WorkspaceObject[] tasks_to_perform=null; EPMTask epmtasks[]=null; ModelObject targetattachment[]=null; ModelObject referenceattachment[]=null;  contents = userinbox.get_contents();//Get the Actual Inbox for user which we see in My Worklist dmServiceTI.getProperties( contents, attributes ); for ( WorkspaceObject folder : contents ) { System.out.println("Folder Name = " +folder.get_object_name()); System.out.println("Folder Name = " +folder.getClass()); TaskInbox taskinbox = ( TaskInbox )folder; dmServiceTI.getProperties( new ModelObject[] { taskinbox }, new String[]{ "tasks_to_perform" } ); tasks_to_perform = taskinbox.get_tasks_to_perform(); EPMDoTask dotask=(EPMDoTask) tasks_to_perform[0]; dmServiceTI.getProperties( new ModelObject[]{dotask}, new String[]{ "root_target_attachments" } ); targetattachment = dotask.get_root_target_attachments(); for( ModelObject attachment : targetattachment ) { if ( attachment instanceof Form ) { HashMap hmformupdateattr = new HashMap(); hmformupdateattr.put("rr_report_type", new String[]{ "RR Technical Instruction (PDF)" } ); FormInfo[] inputsTIGenForm = new FormInfo[1]; inputsTIGenForm[0] = new FormInfo(); inputsTIGenForm[0].formObject = attachment; inputsTIGenForm[0].attributesMap = hmformupdateattr; inputsTIGenForm[0].saveDB = true; inputsTIGenForm[0].formType = "RR_CMEReports_Form"; CreateOrUpdateFormsResponse response = dmServiceTI.createOrUpdateForms(inputsTIGenForm); processServiceDataForPartialErrors(response.serviceData); try { wfService.performAction(dotask , "SOA_EPM_complete_action" , ""  , null , "SOA_EPM_completed" , null ); } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } catch (NotLoadedException e1) { e1.printStackTrace(); }
ANOTHER WAY

SessionService service2 = SessionService.getService( connection );

ObjectPropertyPolicy objPropPolicy = new ObjectPropertyPolicy();
PolicyType policyType = new PolicyType( "EPMTask", new String[]{ "real_state", "object_name", "parent_task" } );
objPropPolicy.addType( policyType );
service2.setObjectPropertyPolicy( objPropPolicy );

DataManagementService dmServiceTI = DataManagementService.getService(connection);
ModelObject targetattachment[]=null;

System.out.println("Inside submit_to_workflow");
String item_uid="E_Sh3sWzY95QJC#ABSEP#EuRh3sWzY95QJC#ABSEP#01fh3sWzY95QJC#ABSEP#0lUh3sWzY95QJC";

String[] arrObjectUID = item_uid.split("#ABSEP#");
int[] arrTypes = new int[arrObjectUID.length];
Arrays.fill(arrTypes, 1);
arrTypes[arrObjectUID.length-1] = 3;//Reference Attachement
WorkflowService wfService = WorkflowService.getService(connection);

if(wfService==null)
{
  System.out.println("Unable to find WorkflowService.please try again");
}
//DataManagementService smService = DataManagementService.getService(connection);
for(int i=0;i<arrObjectUID.length;i++)
{
   System.out.println("arrObjectUID["+i+"]="+arrObjectUID[i]);
}
for(int i=0;i<arrTypes.length;i++)
{
   System.out.println("arrTypes["+i+"]="+arrTypes[i]);
}
ContextData contextData = new ContextData();
contextData.attachmentCount = arrObjectUID.length;
contextData.attachments = arrObjectUID;
contextData.attachmentTypes = arrTypes;
contextData.processTemplate = "Create XYZ Report";//"Simple Release";
System.out.println("Context data initialized");
InstanceInfo instanceResponse = wfService.createInstance(true, null, "TestGENERATION", null,"GENERATION DESC", contextData);

EPMTask taskToForward = null;
if(instanceResponse.serviceData.sizeOfPartialErrors() == 0)
{
  System.out.println("Process created Successfully");
  int sizeOfUpdatedObjects = instanceResponse.serviceData.sizeOfUpdatedObjects();
  for (int i = 0; i < sizeOfUpdatedObjects; i++)
  {
     ModelObject updatedObject = instanceResponse.serviceData.getUpdatedObject( i );
     try
     {
      if ( updatedObject instanceof EPMTask )
      {

       String sName = updatedObject.getPropertyObject( "object_name" ).getStringValue();
       String sParent = updatedObject.getPropertyObject( "parent_task" ).getDisplayableValue();

       if ( sName.equalsIgnoreCase( "Select Report Type" )

         && sParent.equalsIgnoreCase( "Select CAPP Report Type for Op" ) )
       {
        taskToForward = (EPMTask) updatedObject;
        System.out.println( "taskToForward.get_real_state() - " + taskToForward.get_real_state() );
        break;
      }
     }
   }
   catch (NotLoadedException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

  }
 }
}
else
{
       System.out.println("Error :"+instanceResponse.serviceData.getPartialError(0).getMessages());
       System.out.println("Error :"+instanceResponse.serviceData.getPartialError(0).getErrorValues());

}
try
{
    Job wrkjob=(Job) instanceResponse.serviceData.getCreatedObject(0);
    EPMTask root_task=null;
    dmServiceTI.getProperties( new ModelObject[]{wrkjob}, new String[]{ "root_task" } );
    root_task = wrkjob.get_root_task();
    dmServiceTI.getProperties( new ModelObject[]{root_task}, new String[]{ "target_attachments" } );
    targetattachment=root_task.get_target_attachments();

     for( ModelObject attachment : targetattachment )
     {//Here We are going to update the form
      if ( attachment instanceof Form )
      {
       HashMap hmformupdateattr = new HashMap();
       //LOV type attribute
       hmformupdateattr.put("rr_report_type", new String[]{ "RR Technical Instruction (PDF)" } );
       
       FormInfo[] inputsTIGenForm = new FormInfo[1];
       inputsTIGenForm[0] = new FormInfo();
       inputsTIGenForm[0].formObject = attachment;
       inputsTIGenForm[0].attributesMap = hmformupdateattr;
       inputsTIGenForm[0].saveDB = true;
       inputsTIGenForm[0].formType = "RR_CMEReports_Form";

       CreateOrUpdateFormsResponse response = dmServiceTI.createOrUpdateForms(inputsTIGenForm);
       processServiceDataForPartialErrors(response.serviceData);

       //dmServiceTI.refreshObjects( new ModelObject[]{ taskToForward } );
       System.out.println( "taskToForward.get_real_state() - " + taskToForward.get_real_state() );
       //Here we are going to complete the task using api
       ServiceData performAction = wfService.performAction( taskToForward, "SOA_EPM_complete_action" , ""  , null , "SOA_EPM_completed" , null );
       System.out.println( performAction );

      }
     }
 }
 catch (NotLoadedException e2)
 {
   // TODO Auto-generated catch block
   e2.printStackTrace();
  }
  catch (ServiceException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }