cancel
Showing results for 
Search instead for 
Did you mean: 

Re:Workflow control through button click

lakshmi
Champ in-the-making
Champ in-the-making
Hi,

I have a requirement, that whenever a content is created i should have a additional button like 'submit to worklfow'. When a user clicks on the button, the content should follow my workflow.

Help me how to implement this.

I intially had a 'execue script as a rule', this will send my doc to wf as soon as it is created in a space. But now i need to control it with a button in UI.

pls help out

Thanks,
Lakshmi.
12 REPLIES 12

lakshmi
Champ in-the-making
Champ in-the-making
Hi,

Let me put it in this way ,


I have a workflow.js:

var workflow = actions.create("start-workflow");
workflow.parameters.workflowName = "jbpm$wf:myWorkflow";
workflow.parameters["bpm:workflowDescription"] = document.name;
workflow.execute(document);

I set a rule 'Execute this script' in my space. Now my question is : Is it possible to execute the script on click of button in UI???

Thanks,
Lakshmi

dhalupa
Champ on-the-rise
Champ on-the-rise
Take a look at the following interface: org.alfresco.service.cmr.repository.ScriptService. The source itself is very well documented and you will get all the informations you need to execute the script.

The reference to the ScriptService could be injected in Managed Bean which will implement Action listener method of your custom button

Kind regards,

Denis

lakshmi
Champ in-the-making
Champ in-the-making
Hi can you pls be more clear? Just tell me if am right?

I will have to write a class that will implement the method in interface ScriptService, then in my jsp call this method thru actionlistener.

If so there are two arguments in executeScript() method. One is the scriptclasspath . Other is given as ''Object model to process script against"
What shud i pass for this argument.

Sorry am very new to alfresco and its sourde code.

Thanks,
Lakshmi

dhalupa
Champ on-the-rise
Champ on-the-rise
ScriptService is a Spring bean (declaration can be found in WEB-INF/classes/alfresco/public-services-context.xml, search for ScriptService). Reference to a Spring bean could be obtained through ServiceRegistry (getScriptService() method) or could be injected into the Managed Bean like this

<managed-bean>
      <description>Bean that provides information for the About page</description>
      <managed-bean-name>TestBean</managed-bean-name>
      <managed-bean-class>test.Bean</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      <managed-property>
         <property-name>scriptService</property-name>
         <value>#{ScriptService}</value>
      </managed-property>
   </managed-bean>
In object model variable you have to pass references to all object you would like to have avaliable in javascript, have a look at
org.alfresco.repo.jscrip.RhinoScriptTest.java,  testScriptActions() method for an example of usage

Kind regards,

Denis

lakshmi
Champ in-the-making
Champ in-the-making
Hi Denis,

I wrote my class and here are my entries:

faces-config-bean.xml:

<managed-bean>
      <managed-bean-name>testService</managed-bean-name>
      <managed-bean-class>org.alfresco.repo.mytest.RhinoScriptTest</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      <managed-property>
          <property-name>scriptService</property-name>
          <value>#{ScriptService}</value>
         </managed-property>
</managed-bean>

In my jsp i gave:

<h:commandButton id="workflow-button" styleClass="wizardButton"
value="Start Workflow"
action="#{testService.invokeScript}"/>


invokeScript () is my method in RhinoScriptTest class, that i wrote following the example testScriptActions() method you gave.

But i get his error when i click on the commandbutton:

17:55:45,234 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/alfresco].[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
javax.faces.FacesException: Error calling action method of component with id add-content-upload-end:workflow-button
   at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:74)
   at javax.faces.component.UICommand.broadcast(UICommand.java:106)
   at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
   at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
   at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:105)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.alfresco.web.app.servlet.AuthenticationFilter.doFilter(AuthenticationFilter.java:81)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
   at java.lang.Thread.run(Thread.java:595)
Caused by: javax.faces.el.EvaluationException: Exception while invoking expression #{testService.invokeScript}
   at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:165)
   at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
   … 23 more
Caused by: javax.faces.el.PropertyNotFoundException: Bean: org.alfresco.repo.mytest.RhinoScriptTest, property: scriptService
   at org.apache.myfaces.el.PropertyResolverImpl.getPropertyDescriptor(PropertyResolverImpl.java:445)
   at org.apache.myfaces.el.PropertyResolverImpl.getPropertyDescriptor(PropertyResolverImpl.java:416)
   at org.apache.myfaces.el.PropertyResolverImpl.getType(PropertyResolverImpl.java:284)
   at org.apache.myfaces.config.ManagedBeanBuilder.initializeProperties(ManagedBeanBuilder.java:164)
   at org.apache.myfaces.config.ManagedBeanBuilder.buildManagedBean(ManagedBeanBuilder.java:55)
   at org.apache.myfaces.el.VariableResolverImpl.resolveVariable(VariableResolverImpl.java:311)
   at org.springframework.web.jsf.DelegatingVariableResolver.resolveVariable(DelegatingVariableResolver.java:110)
   at org.alfresco.web.app.AlfrescoVariableResolver.resolveVariable(AlfrescoVariableResolver.java:91)
   at org.apache.myfaces.el.ValueBindingImpl$ELVariableResolver.resolveVariable(ValueBindingImpl.java:569)
   at org.apache.commons.el.NamedValue.evaluate(NamedValue.java:124)
   at org.apache.myfaces.el.ValueBindingImpl.resolveToBaseAndProperty(ValueBindingImpl.java:450)
   at org.apache.myfaces.el.MethodBindingImpl.resolveToBaseAndProperty(MethodBindingImpl.java:180)
   at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:114)
   … 24 more


Pls help out.

Thanks,
lakshmi.

dhalupa
Champ on-the-rise
Champ on-the-rise
To be able to inject script service into managed bean, your managed bean has to have setter method which will be called by the JSF framework.
Therefore you have to add this to your managed bean implementation

private ScriptService scriptService;
public void setScripService(ScriptService scriptService){
    this.scriptService=scriptService
}

Hope this helps,

Kind regards,

Denis

kevinr
Star Contributor
Star Contributor
Another way is to wire the script path directly into the client action config, see the 'script' parameter here: http://wiki.alfresco.com/wiki/Externalised_Client_Actions#Action_Definition_Config_Elements

Thanks,

Kevin

dhalupa
Champ on-the-rise
Champ on-the-rise
Besides another it is also much more elegant way Smiley Happy

Kind regards,

Denis

lakshmi
Champ in-the-making
Champ in-the-making
Hi Denis,

Giving a setter for scriptService worked. but now when i execute the script i get the following error:

18:05:57,359 INFO  [repo.mytest.RhinoScriptTest] invokeScript
18:05:58,109 INFO  [repo.mytest.RhinoScriptTest] after createNode
18:05:58,125 INFO  [repo.mytest.RhinoScriptTest] Before executescript
org.alfresco.service.cmr.repository.ScriptException: Failed to execute script 'o
rg/alfresco/repo/mytest/workflow.js': Can't find method org.alfresco.repo.jscrip
t.ScriptAction.execute(org.mozilla.javascript.Undefined). (AlfrescoScript#3)
        at org.alfresco.repo.jscript.RhinoScriptService.executeScript(RhinoScrip
tService.java:118)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
       ……………….

My class for your ref:
 
    public void invokeScript()
    {
       logger.info("invokeScript");
                    StoreRef store = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "rhino_" + System.currentTimeMillis());
                    NodeRef root = nodeService.getRootNode(store);
                   
                    try
                    {
                        ChildAssociationRef childRef = nodeService.createNode(
                                root,
                                BaseNodeServiceTest.ASSOC_TYPE_QNAME_TEST_CHILDREN,
                                QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "script_content"),
                                ContentModel.TYPE_CONTENT,
                                null);
                        logger.info("after createNode");

TemplateNode concept
                        Map<String, Object> model = new HashMap<String, Object>();
                        model.put("doc", new Node(childRef.getChildRef(), serviceRegistry, null));
                        model.put("root", new Node(root, serviceRegistry, null));
                       
                        logger.info("Before executescript");
                        Object result = scriptService.executeScript(TESTSCRIPT_CLASSPATH2, model);
                        System.out.println("Result from TESTSCRIPT_CLASSPATH2: " + result.toString());
                                      }
                    catch (Throwable err)
                    {
                        err.printStackTrace();
                     
                    }
                
    }

  private static final String TESTSCRIPT_CLASSPATH2 = "org/alfresco/repo/mytest/workflow.js";
  



I even packaged the class org.alfresco.repo.jscrip
t.ScriptAction along with my class. But it says 'cannot find method ', though the method is there.

I also dont know how to pass the parameters to my script. As my script is to execute workflow, i need to pass the current document reference.

I altered the container.jsp to add a button and call my action:

<tr>
<td align="center">
<h:commandButton id="workflow-button" styleClass="wizardButton"
value="Start Workflow"
action="#{testService.invokeScript}"
disabled="#{WizardManager.finishButtonDisabled}" />
</td>
</tr>
Pls help out.