cancel
Showing results for 
Search instead for 
Did you mean: 

How to save process variables to custom database?

sherlock
Champ in-the-making
Champ in-the-making
Hi,


I want to store process variables into database after completion of the process.
I'm new to CDI so  not able to store the variable values in database.
Can anybody please help me….


my code is as following


public interface UserDao {
  
    void saveIncident(String incidentID,long number,String title,String locDesc,String cause,String actionTaken,String injuries,String fatalities,String assignor,Date incidentDate,String actFlag);

}




@SessionScoped
public class ManagedBeanUserDao implements UserDao,Serializable {

   private static final long serialVersionUID = 1L;

   @Inject
    private EntityManager entityManager;

    @Inject
    private UserTransaction utx;
@Override
   public void saveIncident(String incidentID, long number, String title,
         String locDesc, String cause, String actionTaken, String injuries,
         String fatalities, String assignor, Date incidentDate,String actFlag) {
      // TODO Auto-generated method stub
      
      try {
            try {
             [b]  [color=#4000FF] utx.begin();[/color][/b]
                try {
                   
                   IncdtMstrEO mstrEO=new IncdtMstrEO();
                  mstrEO.setIncdtId(incidentID);
                  mstrEO.setIncdtNum(number);
                  mstrEO.setIncdtTitle(title);
                  mstrEO.setLocDesc(locDesc);
                  mstrEO.setCause(cause);
                  mstrEO.setActTaken(actionTaken);
                  mstrEO.setInjur(injuries);
                  mstrEO.setFatal(fatalities);
                  mstrEO.setOwner(assignor);
                  mstrEO.setCrtDate(incidentDate);
                  mstrEO.setActFlag("Y");
                  entityManager.persist(mstrEO);
                   
                } catch (NoResultException e) {
                    System.out.println(e);
                }
            } finally {
                utx.commit();
            }
        } catch (Exception e) {
            try {
              [b] [color=#4040FF] utx.rollback();[/color][/b]
            } catch (SystemException se) {
                throw new RuntimeException(se);
            }
            throw new RuntimeException(e);
        }
      
   }
}



@Named
@SessionScoped
public class GreetController implements Serializable{

@Inject
    private UserDao userDao;

public void saveIncident(String incidentID,long number,String title,String locDesc,String cause,String actionTaken,String injuries,String fatalities,String assignor,Date incidentDate,String actFlag)
   {
      System.out.println("UserDao Object————–"+userDao);
      userDao.saveIncident(incidentID, number, title, locDesc, cause, actionTaken, injuries, fatalities, assignor,incidentDate, actFlag);
   }
}




@SessionScoped
public class IncidentSaveService implements JavaDelegate,Serializable {

   
   private static final long serialVersionUID = 1L;
   
      
   @Inject
   GreetController controller;
   
   @Override
   public void execute(DelegateExecution execute) throws Exception {
      
      System.out.println("Incident Id——–"+execute.getProcessInstanceId());
      System.out.println("incident date————"+(Date)execute.getVariable("incidentDate"));
      System.out.println("Incident Number——-"+execute.getVariable("incidentNumber"));
      System.out.println("incident title———"+execute.getVariable("incidentTitle"));
      System.out.println("incident location description—-"+execute.getVariable("locDesc"));
      System.out.println("cause—————-"+execute.getVariable("cause"));
      System.out.println("action taken———–"+execute.getVariable("actionTaken"));
      System.out.println("injuries———"+execute.getVariable("injuries"));
      System.out.println("fatalities———"+execute.getVariable("fatalities"));
      System.out.println("assignor————-"+execute.getVariable("assignor").toString());
      System.out.println("Approve————-"+execute.getVariable("approve").toString());
      
      String incidentID=(String)execute.getProcessInstanceId();
      long incidentNumber=(new Long(execute.getVariable("incidentNumber").toString()).longValue());
      String title=execute.getVariable("incidentTitle").toString();
      String locDesc=execute.getVariable("locDesc").toString();
      String cause=execute.getVariable("cause").toString();
      String actTaken=execute.getVariable("actionTaken").toString();
      String injuries=execute.getVariable("injuries").toString();
      String fatal=execute.getVariable("fatalities").toString();
      String owner=execute.getVariable("assignor").toString();
      String actFlag=(execute.getVariable("approve").toString()=="true"?"Y":"N");
      System.out.println(owner);
      System.out.println("UserDao——————"+userDao);
       [b][color=#FF0000]controller.saveIncident(incidentID, incidentNumber, title, locDesc, cause, actTaken, injuries, fatal, owner, new Date(), actFlag);[/color][/b]
      

   }

}





Here the controller object in the IncidentSaveService i'm getting as null.Event if i create it as new object i'm getting error at the code specified in the blue. please help me.
5 REPLIES 5

frederikherema1
Star Contributor
Star Contributor
How does your BPMN20.xml look like? Maybe you wired the service-task JavaDelegate in a wring way (e.g.? using activiti:class). If the JavaDelegate is not retrieved from the CDI-context, it makes sense that the @Injects are not working…

sherlock
Champ in-the-making
Champ in-the-making
How does your BPMN20.xml look like? Maybe you wired the service-task JavaDelegate in a wring way (e.g.? using activiti:class). If the JavaDelegate is not retrieved from the CDI-context, it makes sense that the @Injects are not working…


Hi frederik,

Thanks for the reply. I am not using any spring configuration in the project. I am trying to get the process variables in the service task and i achieved it, but i am not able to store the variables in the database.

How to achieve this can you pls explain me….

I had tried to achieve this by injecting controller object, but i got the controller object as null. I tried to inject EntityManager by persistence cotext that too gave me null.

I have mentioned the bpmn file please check and help me how to save my process variables in persistence.




<process id="incident" name="Incident Demo" isExecutable="true">
    <startEvent id="startevent1" name="Start" activiti:formKey="create.xhtml"></startEvent>
    <userTask id="usertask1" name="Update Incident" activiti:assignee="${manager}" activiti:formKey="update.xhtml"></userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <serviceTask id="servicetask1" name="Save Incident" activiti:class="com.rolta.onv.im.server.IncidentSaveService"></serviceTask>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="servicetask1"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
  </process>



Thanks
Sherlock

frederikherema1
Star Contributor
Star Contributor
I see you're using activiti:class="com.rolta.onv.im.server.IncidentSaveService" (As I suspected in my previous post). When you use this, activiti will just instantiate an instance of the given class, without any "special" treatment at all. So the constructor will just be called. All the @Injects won't work because the instance is not retrieved from the CDI-context.

You'll have to use activiti:delegateExpression="${incidentSaveService}" instead, combined with a CdiJtaProcessEngineConfiguration (which has a CdiResolver inside, capable of resolving CDI-beans in expressions used in activiti). This will enable you to use the CDI-bean named "incidentSaveService" in activiti.

sherlock
Champ in-the-making
Champ in-the-making
I see you're using activiti:class="com.rolta.onv.im.server.IncidentSaveService" (As I suspected in my previous post). When you use this, activiti will just instantiate an instance of the given class, without any "special" treatment at all. So the constructor will just be called. All the @Injects won't work because the instance is not retrieved from the CDI-context.

You'll have to use activiti:delegateExpression="${incidentSaveService}" instead, combined with a CdiJtaProcessEngineConfiguration (which has a CdiResolver inside, capable of resolving CDI-beans in expressions used in activiti). This will enable you to use the CDI-bean named "incidentSaveService" in activiti.

Thanks Frederik. your solution worked out for me.

aicha
Champ in-the-making
Champ in-the-making
Hey , I think you were working in a project "Manage incidents" . I have a project in  class and I need soe help, could you please help me ?
Thank you