cancel
Showing results for 
Search instead for 
Did you mean: 

Start adhoc workflow using JAVA API

fracat71
Champ on-the-rise
Champ on-the-rise
Hi all,
i want to start the  jbpm$wf:adhoc workflow with a scheduled action that run as admin and assignee to USER_TEST.
The code works fine but the Task is on admin dashlet (My work to do) not inside USER_TEST, but if i click Save the Task goes to USER_TEST dashlet.
I think that is a problem due to the initiator.

Can anybody help me?

scheduled-action-services-context.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
   
    <!–
    Define the model factory used to generate object models suitable for use with freemarker templates.
    –>
    <bean id="templateActionModelFactory" class="org.alfresco.repo.action.scheduled.FreeMarkerWithLuceneExtensionsModelFactory">
        <property name="serviceRegistry">
            <ref bean="ServiceRegistry"/>
        </property>
    </bean>
   
    <!–
    An example action template that defines an action to add the generalclassifiable aspect to all nodes that do not have
    and add a category defined by path.   
    –>
    <bean id="addAmministratiAction" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">
        <property name="actionName">
            <value>AmministratiActionExecuter</value>
        </property>
        <property name="parameterTemplates">
            <map>
                <entry>
                    <key>
                        <value>QUERY</value>
                    </key>
                    <value>PROVA</value>
                </entry>        
            </map>
        </property>
        <property name="templateActionModelFactory">
            <ref bean="templateActionModelFactory"/>
        </property>
        <property name="dictionaryService">
            <ref bean="DictionaryService"/>
        </property>
        <property name="actionService">
            <ref bean="ActionService"/>
        </property>
        <property name="templateService">
            <ref bean="TemplateService"/>
        </property>
    </bean>
   

    <bean id="addClassifiableAspectEveryTenMinutes" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">
        <property name="transactionMode">
            <value>UNTIL_FIRST_FAILURE</value>
        </property>
        <property name="compensatingActionMode">
            <value>IGNORE</value>
        </property>
        <property name="searchService">
            <ref bean="SearchService"/>
        </property>
        <property name="templateService">
            <ref bean="TemplateService"/>
        </property>
        <property name="queryLanguage">
            <value>lucene</value>
        </property>
        <property name="stores">
            <list>
                <value>workspace://SpacesStore</value>
            </list>
        </property>
        <!– Find all nodes that do not have the aspect –>
      <property name="queryTemplate">
            <value>PATH:"/app:company_home"</value>
      </property>
        <property name="cronExpression">
            <value>0 0/5 * * * ?</value>
        </property>
        <property name="jobName">
            <value>jobA</value>
        </property>
        <property name="jobGroup">
            <value>jobGroup</value>
        </property>
        <property name="triggerName">
            <value>triggerA</value>
        </property>
        <property name="triggerGroup">
            <value>triggerGroup</value>
        </property>
        <property name="scheduler">
            <ref bean="schedulerFactory"/>
        </property>
        <property name="actionService">
            <ref bean="ActionService"/>
        </property>
        <property name="templateActionModelFactory">
            <ref bean="templateActionModelFactory"/>
        </property>
        <property name="templateActionDefinition">
            <ref bean="addAmministratiAction"/>
        </property>
        <property name="transactionService">
            <ref bean="TransactionService"/>
        </property>
        <property name="runAsUser">
            <value>admin</value>
        </property>
    </bean>
</beans>


addAmministratiAction :

   public void doExample(ServiceRegistry serviceRegistry) throws Exception {
      //
      // create a workflow service
      //
      WorkflowService workflowService = serviceRegistry.getWorkflowService();
      PersonService personService = serviceRegistry.getPersonService();
      List a = workflowService.getAllDefinitions();

      Iterator i = a.iterator();
      String wid = "";
      while (i.hasNext()) {
         WorkflowDefinition wd = (WorkflowDefinition) i.next();
         System.out.println("workflow name—–>" + wd.name);
         if (wd.name.equals("jbpm$wf:parallelgroupreview") || wd.name.equals("jbpm$wf:adhoc") ) {
            System.out.println(wd.id);
            wid = wd.id;
         }
      }

      // Create a workflow node reference
      NodeRef workflowNodeRef = workflowService.createPackage(null);
      // Finally kick the workflow off.
      Map<QName, Serializable> parameters = new HashMap<QName, Serializable>();
      Date dueDate = new Date(System.currentTimeMillis());
      parameters.put(WorkflowModel.ASSOC_PACKAGE, workflowNodeRef);      
      NodeRef personNodeId =  personService.getPerson("USER_TEST");
      parameters.put(WorkflowModel.ASSOC_ASSIGNEE,personNodeId );
      parameters.put(WorkflowModel.PROP_DUE_DATE, dueDate);
      parameters.put(WorkflowModel.PROP_DESCRIPTION, "PROP_DESCRIPTION");
      parameters.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, dueDate);
      parameters.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "PROP_WORKFLOW_DESCRIPTION");
      
      
      WorkflowPath path = workflowService.startWorkflow(wid, parameters);
      
      
      System.out.println("initiated successfully");

      WorkflowTaskQuery query = new WorkflowTaskQuery();

      query.setTaskState(WorkflowTaskState.COMPLETED);
      query.setTaskState(WorkflowTaskState.IN_PROGRESS);
      List<WorkflowTask> tasks = workflowService.queryTasks(query);
      System.out.println("before");
      // create a list of transient nodes to represents
      for (WorkflowTask task : tasks) {
         System.out.println("Task name—>" + task.name);
         WorkflowTaskDefinition wdef = task.definition;
         System.out.println("map size———–>" + wdef.metadata.getDefaultValues().size());

         Map m = task.properties;
         Set s = m.keySet();
         Iterator it = s.iterator();
         while (it.hasNext()) {
            QName key = (QName) it.next();
            // System.out.println("key——–>"+ m.get(key));
            // Serializable pd=(Serializable)m.get(key);
            System.out.println("Key" + key.getLocalName());
            System.out.println("value——->" + m.get(key));
         }
      }
   }

6 REPLIES 6

fracat71
Champ on-the-rise
Champ on-the-rise
Hi all,
but nobody knows what is "Workflow Engine Id" inside "Modify Content Properties" of a process definition "Data Dictionary > Workflow Definitions"

I try with 1,2,3,4,5,6,7,8,9,10…. but nothing.

Regards

fracat71
Champ on-the-rise
Champ on-the-rise
jbpm

fracat71
Champ on-the-rise
Champ on-the-rise
Hi all,
i have found the solution:
   public static void doExample(ServiceRegistry serviceRegistry) throws Exception {
      //
      // create a workflow service
      //
      WorkflowService workflowService = serviceRegistry.getWorkflowService();
      
      PersonService personService = serviceRegistry.getPersonService();
      List a = workflowService.getAllDefinitions();

      Iterator i = a.iterator();
      String wid = "";
      while (i.hasNext()) {
         Object obj = (Object) i.next();
         WorkflowDefinition wd = (WorkflowDefinition)obj;
         
         //String engineId = (String) serviceRegistry.getNodeService().getProperty((NodeRef)obj,WorkflowModel.PROP_WORKFLOW_DEF_ENGINE_ID);
         System.out.println("workflow name—–>" + wd.name);
         if (wd.name.equals("jbpm$wf:parallelgroupreview") || wd.name.equals("jbpm$wf:adhoc") ) {
            System.out.println(wd.id);
            wid = wd.id;
         }
      }

      // Create a workflow node reference
      NodeRef workflowNodeRef = workflowService.createPackage(null);
      // Finally kick the workflow off.
      Map<QName, Serializable> parameters = new HashMap<QName, Serializable>();
      Date dueDate = new Date(System.currentTimeMillis());
      parameters.put(WorkflowModel.ASSOC_PACKAGE, workflowNodeRef);      
      NodeRef personNodeId =  personService.getPerson("ABRAMO.SALZILLO");
      parameters.put(WorkflowModel.ASSOC_ASSIGNEE,personNodeId );
      parameters.put(WorkflowModel.PROP_DUE_DATE, dueDate);
      parameters.put(WorkflowModel.PROP_DESCRIPTION, "PROP_DESCRIPTION");
      parameters.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, dueDate);
      parameters.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "PROP_WORKFLOW_DESCRIPTION");
      
      
      System.out.println("initiated successfully");
      WorkflowPath wfPath = workflowService.startWorkflow(wid, parameters);
      
                 // New code
      String workflowId = wfPath.instance.id;
      String wfPathId = wfPath.id;
      List<WorkflowTask> wfTasks = workflowService.getTasksForWorkflowPath(wfPathId);
      
      for (Iterator iterator = wfTasks.iterator(); iterator.hasNext();) {
         WorkflowTask workflowTask = (WorkflowTask) iterator.next();
         System.out.println("Task name—>" + workflowTask.name);
      }
      
      WorkflowTask wfFirstStartTask = wfTasks.get(0);
      WorkflowTask wfFirstStartTaskEnd = workflowService.endTask(wfFirstStartTask.id, null);
      
      System.out.println("task started successfully");
      

      WorkflowTaskQuery query = new WorkflowTaskQuery();

      query.setTaskState(WorkflowTaskState.COMPLETED);
      query.setTaskState(WorkflowTaskState.IN_PROGRESS);
      List<WorkflowTask> tasks = workflowService.queryTasks(query);
      System.out.println("before");
      // create a list of transient nodes to represents
      for (WorkflowTask task : tasks) {
         System.out.println("Task name—>" + task.name);
         WorkflowTaskDefinition wdef = task.definition;
         System.out.println("map size———–>" + wdef.metadata.getDefaultValues().size());

         Map m = task.properties;
         Set s = m.keySet();
         Iterator it = s.iterator();
         while (it.hasNext()) {
            QName key = (QName) it.next();
            // System.out.println("key——–>"+ m.get(key));
            // Serializable pd=(Serializable)m.get(key);
            System.out.println("Key" + key.getLocalName());
            System.out.println("value——->" + m.get(key));
         }
      }
   }   

saurabh
Champ in-the-making
Champ in-the-making
@fracat71 – Can you please tell me What is Service Registry. I mean while calling the method 'doExample' , what will you pass as an argument.

I am a new bie to alfresco .. and wanted to learn it…

Thanks in advance .

vince20151
Champ in-the-making
Champ in-the-making
Fracat71 - Thanks so much for the post. The sample code was a great help. I was able to follow and duplicate the functionality to instantiate a workflow case.
I was trying to turn on email notification for the workflow case and added this line to the code
parameters.put(WorkflowModel.PROP_SEND_EMAIL_NOTIFICATIONS, "Y");

However, no email was sent when the workflow case was created. Any idea why? Is "Y" to correct value to set for it?

Thanks a bunch!

vince20151
Champ in-the-making
Champ in-the-making
Well! I figured it out. the value must be set to "true" not "Y". Email is being sent now.
parameters.put(WorkflowModel.PROP_SEND_EMAIL_NOTIFICATIONS, "true");