Search in sources :

Example 1 with MyEntityMethods

use of org.jbpm.persistence.session.objects.MyEntityMethods in project jbpm by kiegroup.

the class VariablePersistenceStrategyTest method testPersistenceVariablesSubProcess.

@Test
public void testPersistenceVariablesSubProcess() throws NamingException, NotSupportedException, SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    MyEntity myEntity = new MyEntity("This is a test Entity with annotation in fields");
    MyEntityMethods myEntityMethods = new MyEntityMethods("This is a test Entity with annotations in methods");
    MyEntityOnlyFields myEntityOnlyFields = new MyEntityOnlyFields("This is a test Entity with annotations in fields and without accesors methods");
    MyVariableSerializable myVariableSerializable = new MyVariableSerializable("This is a test SerializableObject");
    EntityManager em = emf.createEntityManager();
    UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    utx.begin();
    em.joinTransaction();
    em.persist(myEntity);
    em.persist(myEntityMethods);
    em.persist(myEntityOnlyFields);
    utx.commit();
    em.close();
    Environment env = createEnvironment();
    KieBase kbase = createKnowledgeBase("VariablePersistenceStrategySubProcess.rf");
    KieSession ksession = createSession(kbase, env);
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("x", "SomeString");
    parameters.put("y", myEntity);
    parameters.put("m", myEntityMethods);
    parameters.put("f", myEntityOnlyFields);
    parameters.put("z", myVariableSerializable);
    long processInstanceId = ksession.startProcess("com.sample.ruleflow", parameters).getId();
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = ksession.getProcessInstance(processInstanceId);
    assertNull(processInstance);
}
Also used : MyEntity(org.jbpm.persistence.session.objects.MyEntity) UserTransaction(javax.transaction.UserTransaction) TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) HashMap(java.util.HashMap) WorkItem(org.kie.api.runtime.process.WorkItem) InitialContext(javax.naming.InitialContext) MyEntityOnlyFields(org.jbpm.persistence.session.objects.MyEntityOnlyFields) EntityManager(javax.persistence.EntityManager) MyEntityMethods(org.jbpm.persistence.session.objects.MyEntityMethods) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) MyVariableSerializable(org.jbpm.persistence.session.objects.MyVariableSerializable) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 2 with MyEntityMethods

use of org.jbpm.persistence.session.objects.MyEntityMethods in project jbpm by kiegroup.

the class VariablePersistenceStrategyTest method testPersistenceVariables.

@Test
public void testPersistenceVariables() throws NamingException, NotSupportedException, SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    EntityManager em = emf.createEntityManager();
    UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    if (utx.getStatus() == Status.STATUS_NO_TRANSACTION) {
        utx.begin();
        em.joinTransaction();
    }
    int origNumMyEntities = em.createQuery("select i from MyEntity i").getResultList().size();
    int origNumMyEntityMethods = em.createQuery("select i from MyEntityMethods i").getResultList().size();
    int origNumMyEntityOnlyFields = em.createQuery("select i from MyEntityOnlyFields i").getResultList().size();
    if (utx.getStatus() == Status.STATUS_ACTIVE) {
        utx.commit();
    }
    // Setup entities
    MyEntity myEntity = new MyEntity("This is a test Entity with annotation in fields");
    MyEntityMethods myEntityMethods = new MyEntityMethods("This is a test Entity with annotations in methods");
    MyEntityOnlyFields myEntityOnlyFields = new MyEntityOnlyFields("This is a test Entity with annotations in fields and without accesors methods");
    MyVariableSerializable myVariableSerializable = new MyVariableSerializable("This is a test SerializableObject");
    // persist entities
    utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    utx.begin();
    em.joinTransaction();
    em.persist(myEntity);
    em.persist(myEntityMethods);
    em.persist(myEntityOnlyFields);
    utx.commit();
    em.close();
    // More setup
    Environment env = createEnvironment();
    KieBase kbase = createKnowledgeBase("VariablePersistenceStrategyProcess.rf");
    KieSession ksession = createSession(kbase, env);
    logger.debug("### Starting process ###");
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("x", "SomeString");
    parameters.put("y", myEntity);
    parameters.put("m", myEntityMethods);
    parameters.put("f", myEntityOnlyFields);
    parameters.put("z", myVariableSerializable);
    // Start process
    long processInstanceId = ksession.startProcess("com.sample.ruleflow", parameters).getId();
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    // Test results
    List<?> result = emf.createEntityManager().createQuery("select i from MyEntity i").getResultList();
    assertEquals(origNumMyEntities + 1, result.size());
    result = emf.createEntityManager().createQuery("select i from MyEntityMethods i").getResultList();
    assertEquals(origNumMyEntityMethods + 1, result.size());
    result = emf.createEntityManager().createQuery("select i from MyEntityOnlyFields i").getResultList();
    assertEquals(origNumMyEntityOnlyFields + 1, result.size());
    logger.debug("### Retrieving process instance ###");
    ksession = reloadSession(ksession, kbase, env);
    WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    assertEquals("SomeString", processInstance.getVariable("x"));
    assertEquals("This is a test Entity with annotation in fields", ((MyEntity) processInstance.getVariable("y")).getTest());
    assertEquals("This is a test Entity with annotations in methods", ((MyEntityMethods) processInstance.getVariable("m")).getTest());
    assertEquals("This is a test Entity with annotations in fields and without accesors methods", ((MyEntityOnlyFields) processInstance.getVariable("f")).test);
    assertEquals("This is a test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("z")).getText());
    assertNull(processInstance.getVariable("a"));
    assertNull(processInstance.getVariable("b"));
    assertNull(processInstance.getVariable("c"));
    logger.debug("### Completing first work item ###");
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    logger.debug("### Retrieving process instance ###");
    ksession = reloadSession(ksession, kbase, env);
    processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    assertEquals("SomeString", processInstance.getVariable("x"));
    assertEquals("This is a test Entity with annotation in fields", ((MyEntity) processInstance.getVariable("y")).getTest());
    assertEquals("This is a test Entity with annotations in methods", ((MyEntityMethods) processInstance.getVariable("m")).getTest());
    assertEquals("This is a test Entity with annotations in fields and without accesors methods", ((MyEntityOnlyFields) processInstance.getVariable("f")).test);
    assertEquals("This is a test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("z")).getText());
    assertEquals("Some new String", processInstance.getVariable("a"));
    assertEquals("This is a new test Entity", ((MyEntity) processInstance.getVariable("b")).getTest());
    assertEquals("This is a new test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("c")).getText());
    logger.debug("### Completing second work item ###");
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    logger.debug("### Retrieving process instance ###");
    ksession = reloadSession(ksession, kbase, env);
    processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    assertEquals("SomeString", processInstance.getVariable("x"));
    assertEquals("This is a test Entity with annotation in fields", ((MyEntity) processInstance.getVariable("y")).getTest());
    assertEquals("This is a test Entity with annotations in methods", ((MyEntityMethods) processInstance.getVariable("m")).getTest());
    assertEquals("This is a test Entity with annotations in fields and without accesors methods", ((MyEntityOnlyFields) processInstance.getVariable("f")).test);
    assertEquals("This is a test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("z")).getText());
    assertEquals("Some changed String", processInstance.getVariable("a"));
    assertEquals("This is a changed test Entity", ((MyEntity) processInstance.getVariable("b")).getTest());
    assertEquals("This is a changed test SerializableObject", ((MyVariableSerializable) processInstance.getVariable("c")).getText());
    logger.debug("### Completing third work item ###");
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
    assertNull(processInstance);
}
Also used : UserTransaction(javax.transaction.UserTransaction) MyEntity(org.jbpm.persistence.session.objects.MyEntity) TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) HashMap(java.util.HashMap) WorkItem(org.kie.api.runtime.process.WorkItem) InitialContext(javax.naming.InitialContext) MyEntityOnlyFields(org.jbpm.persistence.session.objects.MyEntityOnlyFields) EntityManager(javax.persistence.EntityManager) MyEntityMethods(org.jbpm.persistence.session.objects.MyEntityMethods) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) MyVariableSerializable(org.jbpm.persistence.session.objects.MyVariableSerializable) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 3 with MyEntityMethods

use of org.jbpm.persistence.session.objects.MyEntityMethods in project jbpm by kiegroup.

the class VariablePersistenceStrategyTest method testPersistenceVariablesWithTypeChange.

@Test
public void testPersistenceVariablesWithTypeChange() throws NamingException, NotSupportedException, SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    MyEntity myEntity = new MyEntity("This is a test Entity with annotation in fields");
    MyEntityMethods myEntityMethods = new MyEntityMethods("This is a test Entity with annotations in methods");
    MyEntityOnlyFields myEntityOnlyFields = new MyEntityOnlyFields("This is a test Entity with annotations in fields and without accesors methods");
    MyVariableSerializable myVariableSerializable = new MyVariableSerializable("This is a test SerializableObject");
    EntityManager em = emf.createEntityManager();
    UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    int s = utx.getStatus();
    if (utx.getStatus() == Status.STATUS_NO_TRANSACTION) {
        utx.begin();
    }
    em.joinTransaction();
    em.persist(myEntity);
    em.persist(myEntityMethods);
    em.persist(myEntityOnlyFields);
    if (utx.getStatus() == Status.STATUS_ACTIVE) {
        utx.commit();
    }
    em.close();
    Environment env = createEnvironment();
    KieBase kbase = createKnowledgeBase("VariablePersistenceStrategyProcessTypeChange.rf");
    KieSession ksession = createSession(kbase, env);
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("x", "SomeString");
    parameters.put("y", myEntity);
    parameters.put("m", myEntityMethods);
    parameters.put("f", myEntityOnlyFields);
    parameters.put("z", myVariableSerializable);
    long processInstanceId = ksession.startProcess("com.sample.ruleflow", parameters).getId();
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = ksession.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNull(workItem);
    ksession = reloadSession(ksession, kbase, env);
    processInstance = ksession.getProcessInstance(processInstanceId);
    assertNull(processInstance);
}
Also used : MyEntity(org.jbpm.persistence.session.objects.MyEntity) UserTransaction(javax.transaction.UserTransaction) TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) HashMap(java.util.HashMap) WorkItem(org.kie.api.runtime.process.WorkItem) InitialContext(javax.naming.InitialContext) MyEntityOnlyFields(org.jbpm.persistence.session.objects.MyEntityOnlyFields) EntityManager(javax.persistence.EntityManager) MyEntityMethods(org.jbpm.persistence.session.objects.MyEntityMethods) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) MyVariableSerializable(org.jbpm.persistence.session.objects.MyVariableSerializable) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)3 InitialContext (javax.naming.InitialContext)3 EntityManager (javax.persistence.EntityManager)3 UserTransaction (javax.transaction.UserTransaction)3 MyEntity (org.jbpm.persistence.session.objects.MyEntity)3 MyEntityMethods (org.jbpm.persistence.session.objects.MyEntityMethods)3 MyEntityOnlyFields (org.jbpm.persistence.session.objects.MyEntityOnlyFields)3 MyVariableSerializable (org.jbpm.persistence.session.objects.MyVariableSerializable)3 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)3 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)3 Test (org.junit.Test)3 KieBase (org.kie.api.KieBase)3 Environment (org.kie.api.runtime.Environment)3 KieSession (org.kie.api.runtime.KieSession)3 WorkItem (org.kie.api.runtime.process.WorkItem)3 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)3 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2