how to load data from the database as local variables?

Have a service task where I am trying to make financial calculations. The data comes from a bill and the bill contains a contract id (This is a persistence id). I was hoping to use that to load the contact and get its data.

I was planning to load the data inside a local variable by adding the code below to data >> local variable >> add >> Edit variable dialog >> edit expression dialog >> script
However, I have no clue how to get the session. The session is required to call BusinessObjectDAOFactory.createDAO()

import org.bonitasoft.engine.bdm.BusinessObjectDAOFactory
import org.bonitasoft.engine.session.APISession;

import com.company.model.Contract
import com.company.model.ContractDAO

BusinessObjectDAOFactory daoFactory = new BusinessObjectDAOFactory();

ContractDAO contractDAO = daoFactory.createDAO(SESSION_IS_REQUIRECT, ContractDAO.class);
Contract contract = contractDAO.findByPersistenceId(bill.contractId);

return contract;

As per version 7.4 I would use the following

import com.company.model.Contract
import com.company.model.ContractDAO

def cont = contractDAO.findByPersistenceId(bill.contractId)

That does the trick. However Bonita discourages using non-business variables to store business data and you may not need declaring local variable in Bonita at all. Just get your bill in calculation script using DAO.

Hi
I need to know how should I have the DAO files to import them to my java project and work with them?

It works. So thanks a lot. However I still don’t understand how contractDAO got initiated! It’s a bit strange that by importing the namespace, suddenly contractDAO cames to existence.

What happens if the BDM was named com.company.model.contract? Notice that the model name starts with a lowercase and not an upper case.

This will result in the DAO being named com.company.model.contractDAO.

In this case will the variable contractDAO exist or not since the class com.company.model.contractDAO has the same name?

Thanks a lot

i have a problem with this

i am trying to do the same on a java class on an actor filter java class.

i create a table on my BDM called Userstest, with 2 fields.

username
userpass

and it makes one query called findByUsername

my class is

/**
*
*/
package org.mycompany.connector;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import org.bonitasoft.engine.connector.ConnectorValidationException;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.filter.UserFilterException;
import org.bonitasoft.engine.api.LoginAPI
import org.bonitasoft.engine.bdm.BusinessObjectDAOFactory
import org.bonitasoft.engine.session.APISession;

import com.company.model.UserstestDAO;
import com.company.model.Userstest;

public class UseridbynameImpl extends AbstractUseridbynameImpl {

@Override
public void validateInputParameters() throws ConnectorValidationException {
	//TODO validate input parameters here 	
	
}

@Override
public List<Long> filter(final String actorName) throws UserFilterException {
    try {
          final long processInstanceId = getExecutionContext().getProcessInstanceId();
          long processInitiator = getAPIAccessor().getProcessAPI().getProcessInstance(processInstanceId).getStartedBy();
          String UserInitiator=getAPIAccessor().getIdentityAPI().getUser(processInitiator).getUserName();
    	    	  Logger logger = Logger.getLogger("org.bonitasoft");
    	    	  logger.severe("Usuario Iniciador: " + UserInitiator);
    	      String reviewname="";
    	    
    	     ** List<Userstest> us = UserstestDAO.findByUsername("juan", 0, 100);
    	      **
    	      logger.severe("Cantidad: " + us.size());
    	      
    		  logger.severe("Usuario Revisor: " + reviewname);
    		  long IdManager = getAPIAccessor().getIdentityAPI().getUserByUserName(reviewname).getId();
    		  
    		  
    	      return Arrays.asList(IdManager);
    } catch (final BonitaException e) {
         throw new UserFilterException(e);
    }
}

@Override
public boolean shouldAutoAssignTaskIfSingleResult() {
	// If this method returns true, the step will be assigned to 
	//the user if there is only one result returned by the filter method
	return super.shouldAutoAssignTaskIfSingleResult();

}

}
when i execute the program, i got the following error:

2018-04-03 18:01:22.174 -0300 GRAVE: org.bonitasoft Usuario Iniciador: walter.bates
2018-04-03 18:01:22.176 -0300 GRAVE: org.bonitasoft.engine.execution.work.InSessionBonitaWork THREAD_ID=495 | HOSTNAME=MacBook-Pro-de-Alejandro.local | TENANT_ID=1 | The work [ExecuteFlowNodeWork: flowNodeInstanceId: 120068] failed. The failure will be handled.
2018-04-03 18:01:22.179 -0300 GRAVE: org.bonitasoft.engine.execution.work.InSessionBonitaWork THREAD_ID=495 | HOSTNAME=MacBook-Pro-de-Alejandro.local | TENANT_ID=1 | org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException : “PROCESS_DEFINITION_ID=8569080591741980647 | PROCESS_NAME=Pool | PROCESS_VERSION=1.0 | PROCESS_INSTANCE_ID=10034 | ROOT_PROCESS_INSTANCE_ID=10034 | FLOW_NODE_DEFINITION_ID=6407571758219735293 | FLOW_NODE_INSTANCE_ID=120068 | FLOW_NODE_NAME=review | org.bonitasoft.engine.core.filter.exception.SUserFilterExecutionException: groovy.lang.MissingMethodException: No signature of method: static com.company.model.UserstestDAO.findByUsername() is applicable for argument types: (java.lang.String, java.lang.Integer, java.lang.Integer) values: [alejandro, 0, 100]”
org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException: PROCESS_DEFINITION_ID=8569080591741980647 | PROCESS_NAME=Pool | PROCESS_VERSION=1.0 | PROCESS_INSTANCE_ID=10034 | ROOT_PROCESS_INSTANCE_ID=10034 | FLOW_NODE_DEFINITION_ID=6407571758219735293 | FLOW_NODE_INSTANCE_ID=120068 | FLOW_NODE_NAME=review | org.bonitasoft.engine.core.filter.exception.SUserFilterExecutionException: groovy.lang.MissingMethodException: No signature of method: static com.company.model.UserstestDAO.findByUsername() is applicable for argument types: (java.lang.String, java.lang.Integer, java.lang.Integer) values: [alejandro, 0, 100]
at org.bonitasoft.engine.execution.StateBehaviors.mapActors(StateBehaviors.java:295)
at org.bonitasoft.engine.execution.state.InitializingActivityWithBoundaryEventsStateImpl.beforeConnectors(InitializingActivityWithBoundaryEventsStateImpl.java:90)
at org.bonitasoft.engine.execution.state.OnEnterOrOnFinishConnectorState.execute(OnEnterOrOnFinishConnectorState.java:59)
at org.bonitasoft.engine.execution.state.OnEnterOrOnFinishConnectorState.execute(OnEnterOrOnFinishConnectorState.java:1)
at org.bonitasoft.engine.execution.FlowNodeExecutorImpl.executeState(FlowNodeExecutorImpl.java:129)
at org.bonitasoft.engine.execution.FlowNodeExecutorImpl.updateState(FlowNodeExecutorImpl.java:180)
at org.bonitasoft.engine.execution.FlowNodeExecutorImpl.stepForward(FlowNodeExecutorImpl.java:164)
at org.bonitasoft.engine.execution.FlowNodeExecutorImpl.executeFlowNode(FlowNodeExecutorImpl.java:338)
at org.bonitasoft.engine.execution.work.ExecuteFlowNodeWork.work(ExecuteFlowNodeWork.java:85)
at org.bonitasoft.engine.execution.work.TxBonitaWork$1.call(TxBonitaWork.java:48)
at org.bonitasoft.engine.execution.work.TxBonitaWork$1.call(TxBonitaWork.java:1)
at org.bonitasoft.engine.transaction.JTATransactionServiceImpl.executeInTransaction(JTATransactionServiceImpl.java:274)
at org.bonitasoft.engine.execution.work.TxBonitaWork.work(TxBonitaWork.java:53)
at org.bonitasoft.engine.execution.work.LockProcessInstanceWork.work(LockProcessInstanceWork.java:74)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.failurewrapping.TxInHandleFailureWrappingWork.work(TxInHandleFailureWrappingWork.java:42)
at org.bonitasoft.engine.execution.work.InSessionBonitaWork.work(InSessionBonitaWork.java:58)
at org.bonitasoft.engine.work.BonitaThreadPoolExecutor.lambda$submit$0(BonitaThreadPoolExecutor.java:90)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.bonitasoft.engine.core.filter.exception.SUserFilterExecutionException: groovy.lang.MissingMethodException: No signature of method: static com.company.model.UserstestDAO.findByUsername() is applicable for argument types: (java.lang.String, java.lang.Integer, java.lang.Integer) values: [alejandro, 0, 100]
at org.bonitasoft.engine.core.filter.impl.UserFilterServiceImpl.executeFilter(UserFilterServiceImpl.java:124)
at org.bonitasoft.engine.userfilter.UserFilterServiceDecorator.executeFilter(UserFilterServiceDecorator.java:68)
at org.bonitasoft.engine.execution.StateBehaviors.mapUsingUserFilters(StateBehaviors.java:315)
at org.bonitasoft.engine.execution.StateBehaviors.mapActors(StateBehaviors.java:287)
… 21 more
Caused by: java.util.concurrent.ExecutionException: groovy.lang.MissingMethodException: No signature of method: static com.company.model.UserstestDAO.findByUsername() is applicable for argument types: (java.lang.String, java.lang.Integer, java.lang.Integer) values: [alejandro, 0, 100]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl.getValue(ConnectorExecutorImpl.java:149)
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl.execute(ConnectorExecutorImpl.java:122)
at org.bonitasoft.engine.core.filter.impl.UserFilterServiceImpl.executeFilterInClassloader(UserFilterServiceImpl.java:211)
at org.bonitasoft.engine.core.filter.impl.UserFilterServiceImpl.executeFilter(UserFilterServiceImpl.java:116)
… 24 more
Caused by: groovy.lang.MissingMethodException: No signature of method: static com.company.model.UserstestDAO.findByUsername() is applicable for argument types: (java.lang.String, java.lang.Integer, java.lang.Integer) values: [alejandro, 0, 100]
at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1498)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1484)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)
at org.mycompany.connector.UseridbynameImpl.filter(UseridbynameImpl.groovy:39)
at org.bonitasoft.engine.core.filter.impl.SConnectorUserFilterAdapter.execute(SConnectorUserFilterAdapter.java:66)
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl$ExecuteConnectorCallable.call(ConnectorExecutorImpl.java:206)
at org.bonitasoft.engine.connector.impl.ConnectorExecutorImpl$ExecuteConnectorCallable.call(ConnectorExecutorImpl.java:176)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
… 3 more

Please i need help, i have been looking all day trying to make this work but the message is the same that the program doesn’t find that method.

any idea somebody why this doesn’t work???