AnsweredAssumed Answered

Form Property 'Long' Is Interpreted by Java Class as 'Intege

Question asked by ct1 on Sep 27, 2011
Latest reply on Sep 28, 2011 by ct1
I specified a "long" type for an activiti:formProperty in the BPMN20.xml (actually in Designer) for variables "loanAmount" and "income".  Then in a Java class I tried to do execution.getVariable("varName") casting the return value to both a Long and a String.  Each time I had a "ClassCastException: java.lang.Integer cannot be cast to java.lang.String (or java.lang.Long)".  Is "long" data type supported or should I expect to always use Integer in the Java programs?

Thank you.

The bpmn20.xml file:


    <startEvent id="theStart" name="Start">
      <extensionElements>
        <activiti:formProperty id="name" name="Name" type="string" required="true" readable="true" writable="true"></activiti:formProperty>
        <activiti:formProperty id="emailAddress" name="Email address" type="string" required="true" readable="true" writable="true"></activiti:formProperty>
        <activiti:formProperty id="loanAmount" name="Loan Amount" type="long" required="true" readable="true" writable="true"></activiti:formProperty>
        <activiti:formProperty id="income" name="Income" type="long" required="true" readable="true" writable="true"></activiti:formProperty>
      </extensionElements>
    </startEvent>
    <scriptTask id="checkCredit" name="Check Credit" scriptFormat="groovy">
      <script><![CDATA[out:println " In script of Check Credit, Checking credit for " + name;creditCheckOk = false;if((income / 2) > loanAmount){creditCheckOk = true};out:println "Credit checked for " + name + " creditCheckOk is " + creditCheckOk;]]></script>
    </scriptTask>
    <sequenceFlow id="flow1" name="" sourceRef="theStart" targetRef="checkCredit"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" name="" sourceRef="checkCredit" targetRef="createApplication"></sequenceFlow>
    <serviceTask id="createApplication" name="Service Task" activiti:class="com.argo.loanreq2.CreateApplicationTask"></serviceTask>
    <userTask id="evaluateLoanRequest" name="Evaluate Loan Request" activiti:assignee="fozzie">
      <documentation>Evaluate the application</documentation>
      <extensionElements>
        <activiti:formProperty id="income" name="Income of Customer" variable="${loanApplication.income}" required="false" readable="true" writable="false"></activiti:formProperty>
        <activiti:formProperty id="requestedAmount" name="Requested loan amount" variable="${loanApplication.requestedAmount}" required="false" readable="true" writable="false"></activiti:formProperty>
        <activiti:formProperty id="customerName" name="Customer Name" variable="${loanApplication.customerName}" required="false" readable="true" writable="false"></activiti:formProperty>
        <activiti:formProperty id="creditCheckOk" name="Outcome of credit check" variable="${loanApplication.creditCheckOk" required="false" readable="true" writable="false"></activiti:formProperty>
        <activiti:formProperty id="motivation" name="Motivation" required="false" readable="true" writable="true"></activiti:formProperty>
        <activiti:formProperty id="requestApproved" name="Do you approve the request?" type="enum" required="true" readable="true" writable="true"></activiti:formProperty>
      </extensionElements>
    </userTask>


The Java class:


package com.argo.loanreq2;

import java.util.HashMap;

public class CreateApplicationTask implements JavaDelegate
{
   public void execute(DelegateExecution execution)
   {
      LoanApplication la = new LoanApplication();
      
      la.setCreditCheckOk( (Boolean) execution   
                              .getVariable("creditCheckOk"));
      
      la.setCustomerName( (String) execution.getVariable("name"));
      
      Map<String, Object> theVariables = new HashMap<String, Object>();
      
      theVariables = execution.getVariables();
      
      Set <String> varNames = execution.getVariableNames();
      Iterator varNamesIterator = varNames.iterator();
      
      while(varNamesIterator.hasNext())
      {
         String oneName = new String();
         oneName = (String) varNamesIterator.next();
         
         Object varObj = (Object) theVariables.get(oneName);
         
         String className = new String();
         
         className = varObj.getClass().getName();
         
         System.out.println("In execute() have oneName = "
                        + oneName
                        + " of class = "
                        + className);
                        
      }
               
      Long aLongIncome = Long.valueOf( (String) execution.getVariable("income"));
//      la.setIncome( (Long) execution.getVariable("income"));
      la.setIncome(aLongIncome);
      
      Long aLongReqAmt =
         Long.valueOf( (String) execution.getVariable("loanAmount"));
//      la.setRequestedAmount( (Long) execution.getVariable("loanAmount"));
      la.setRequestedAmount(aLongReqAmt);
      
      la.setEmailAddress( (String) execution.getVariable("emailAddress"));
      
      System.out.println("In execute() set la.creditCheckOk = "
                     + la.getCreditCheckOk());
      System.out.println("In execute() set la.customeName = "
            + la.getCustomerName());
      System.out.println("In execute() set la.income = "
            + la.getIncome());
      System.out.println("In execute() set la.requestedAmount = "
            + la.getRequestedAmount());
      System.out.println("In execute() set la.emailAddress = "
            + la.getEmailAddress());
      
      execution.setVariable("loanApplication", la);
   }
}


The Exception:

INFO: XPath currently not supported as expressionLanguage
In script of Check Credit, Checking credit for Miss Piggy
Credit checked for Miss Piggy creditCheckOk is true
In execute() have oneName = loanAmount of class = java.lang.Integer
In execute() have oneName = income of class = java.lang.Integer
In execute() have oneName = name of class = java.lang.String
In execute() have oneName = creditCheckOk of class = java.lang.Boolean
In execute() have oneName = emailAddress of class = java.lang.String
Sep 27, 2011 9:52:45 AM org.activiti.engine.impl.interceptor.CommandContext close
SEVERE: Error while closing command context
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
   at com.argo.loanreq2.CreateApplicationTask.execute(CreateApplicationTask.java:47)
   at org.activiti.engine.impl.bpmn.behavior.ServiceTaskJavaDelegateActivityBehavior.execute(ServiceTaskJavaDelegateActivityBehavior.java:47)
   at org.activiti.engine.impl.bpmn.behavior.ServiceTaskJavaDelegateActivityBehavior.execute(ServiceTaskJavaDelegateActivityBehavior.java:38)
   at org.activiti.engine.impl.bpmn.helper.ClassDelegate.execute(ClassDelegate.java:104)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationActivityExecute.execute(AtomicOperationActivityExecute.java:40)

Outcomes