cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert Javascript Date in Java Date? (In Workflow)

arak
Confirmed Champ
Confirmed Champ
Hi everyone,

I have a workflow with three task which share a field date for each task (task1 have date1, task2 have date1 and date2, and task3 have date1, date2 and date3). This dates is entered in the task and after copied in the metadata of document of the workflow (after of complete the task). But, in the moment which I complete the task, shoot the next error:

org.activiti.engine.ActivitiException: Exception while invoking TaskListener: Exception while invoking TaskListener: Couldn't serialize value 'org.mozilla.javascript.NativeJavaObject@1c5d986a' in variable 'bcwf_fechaEnvio'

I know which one of the errors of the Rhino JavaScript integration in Alfresco, and I know I should convert the value to a standard Java date before setVariableLocal. Actually, I try this:

I create a java class for use in the taskListener (Create) of the task, whith this:


ActivitiScriptNode scriptNode = (ActivitiScriptNode) delegateTask.getVariable("bpm_package");
NativeArray children = (NativeArray) scriptNode.getChildren();
ScriptNode firstChild = (ScriptNode)children.get(0, null);
Map<String, Object> props = firstChild.getProperties();

String jsDate=props.get("{http://www.empresa.cl/wfmodel/content/1.0}fechaEnvio").toString();
System.out.println("Fecha Javascript: "+jsDate);
try {
   Date javaDate = new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(jsDate);
   System.out.println("Funciono :D, la fecha queda asi: "+javaDate);
} catch (ParseException e) {
   // TODO Auto-generated catch block
   System.out.println("No funciono :(");
   e.printStackTrace();
}


But the value of jsDate is "org.mozilla.javascript.NativeJavaObject@3f9cfb5", ¿Can I extract the date value of this object? ¿How to? ¿Some other way?

I create another java class for use in the taskListener (Complete) of the task for copy the value of form task into document, whith this:


ActivitiScriptNode scriptNode = (ActivitiScriptNode) delegateTask.getVariable("bpm_package");
NativeArray children = (NativeArray) scriptNode.getChildren();
ScriptNode firstChild = (ScriptNode)children.get(0, null);
      
firstChild.addAspect("bcwf:infoCalidad");


But, I don't know how to update the properties of the aspect in Java.

¿Any recommend? ¿Some guidance?

Thanks for any help.

Greetings,
Pablo.
4 REPLIES 4

kaynezhang
World-Class Innovator
World-Class Innovator
Since you are implementing Tasklistener using java ,why not operate java NodeRef directly? you don't need to convert it back and forth between java object and js object.

ActivitiScriptNode scriptNode = (ActivitiScriptNode) delegateTask.getVariable("bpm_package");
            if (scriptNode != null)
            {
               NodeRef packageNodeRef = scriptNode.getNodeRef();
      List<ChildAssociationRef> assocs = nodeService.getChildAssocs(packageNodeRef);
               if (assocs.size() == 1)
               {
         NodeRef child = assocs.get(0).getChildRef();
         Map props = nodeService.getProperties(child);
         java.util.Date date =(Date)props.get("{http://www.empresa.cl/wfmodel/content/1.0}fechaEnvio");
      }
            }

Thanks for the reply and your suggestion.

Now, I have another question about this: What is "nodeService"? I searched much about this and the mode of utilization, but I not get successful results :/, my actual code is:

<java>
public class RevisarCasoCreate implements TaskListener {
   
   public void notify(DelegateTask delegateTask) {
     
       ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
            ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
       NodeService nodeService = serviceRegistry.getNodeService();
       ActivitiScriptNode scriptNode = (ActivitiScriptNode) delegateTask.getVariable("bpm_package");

            if (scriptNode != null) {
           
           NodeRef packageNodeRef = scriptNode.getNodeRef();
           List<ChildAssociationRef> assocs = nodeService.getChildAssocs(packageNodeRef);

           if (assocs.size() == 1) {

              NodeRef child = assocs.get(0).getChildRef();
              Map<QName, Serializable> props = nodeService.getProperties(child);
              Date date =(Date) props.get("{http://www.empresa.cl/wfmodel/content/1.0}fechaEnvio");
                        ………..
                        ………..
           }

            }
……………..
</java>


And in my .bpmn file:


<userTask id="revisarCaso" name="Revisar Caso" activiti:assignee="oestades" activiti:formKey="bcwf:revisarCaso">
      <extensionElements>
        <activiti:taskListener event="create" class="cl.empresa.bpm.RevisarCasoCreate"/>
……………


But I get this error in the console:

Cannot create JDBC driver of class '' for connect URL 'null'
java.lang.NullPointerException
   at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:524)
   at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:493)
   at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
   at java.sql.DriverManager.getDriver(DriverManager.java:262)
   at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
   at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
   at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
   at org.alfresco.config.JndiObjectFactoryBean.lookup(JndiObjectFactoryBean.java:44)
   at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
   at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
………..

¿Any idea?

Thanks for any help.

Greetings,
Pablo.

kaynezhang
World-Class Innovator
World-Class Innovator
import org.alfresco.repo.workflow.activiti.ActivitiConstants;
import org.alfresco.repo.workflow.activiti.ActivitiScriptNode;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;

public class RevisarCasoCreate  implements TaskListener {

   @Override
   public void notify(DelegateTask task) {
      ActivitiScriptNode scriptNode = (ActivitiScriptNode) task.getVariable("bpm_package");

      if (scriptNode != null) {
         NodeRef packageNodeRef = scriptNode.getNodeRef();
         NodeService nodeService = this.getServiceRegistry().getNodeService();
         List<ChildAssociationRef> assocs = nodeService.getChildAssocs(packageNodeRef);
         if (assocs.size() == 1) {
            NodeRef child = assocs.get(0).getChildRef();
            Map props = nodeService.getProperties(child);
            java.util.Date date = (Date) props.get("{http://www.empresa.cl/wfmodel/content/1.0}fechaEnvio");
         }

      }
   }
   
   protected ServiceRegistry getServiceRegistry() {

        ProcessEngineConfigurationImpl config = Context.getProcessEngineConfiguration();
        if (config != null) {
            ServiceRegistry registry = (ServiceRegistry) config.getBeans().get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
            if (registry == null) {
                throw new RuntimeException(
                            "Service-registry not present in ProcessEngineConfiguration beans, expected ServiceRegistry with key" +
                            ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
            }
            return registry;
        }

        throw new IllegalStateException("No ProcessEngineCOnfiguration found in active context");

    }   
}
</code>
Your exception has noting to do with your task listener,it must be thrown by  other place,please check your database url in your configuration.

Thanks kaynezhang for the help, is working Smiley Very Happy !

Greetings,
Pablo.