AnsweredAssumed Answered

Start adhoc workflow using JAVA API

Question asked by fracat71 on Jun 10, 2010
Latest reply on Jul 24, 2013 by saurabh
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));
         }
      }
   }

Outcomes