Search in sources :

Example 21 with EntityManager

use of javax.persistence.EntityManager in project che by eclipse.

the class JpaUserDao method doRemove.

@Transactional(rollbackOn = { RuntimeException.class, ServerException.class })
protected Optional<UserImpl> doRemove(String id) throws ServerException {
    final EntityManager manager = managerProvider.get();
    final UserImpl user = manager.find(UserImpl.class, id);
    if (user == null) {
        return Optional.empty();
    }
    eventService.publish(new BeforeUserRemovedEvent(user)).propagateException();
    manager.remove(user);
    manager.flush();
    return Optional.of(user);
}
Also used : EntityManager(javax.persistence.EntityManager) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) BeforeUserRemovedEvent(org.eclipse.che.api.user.server.event.BeforeUserRemovedEvent) Transactional(com.google.inject.persist.Transactional)

Example 22 with EntityManager

use of javax.persistence.EntityManager in project jBPM5-Developer-Guide by Salaboy.

the class SingleSessionPatternsTest method singleSessionPerProcessDefinitionWithRules.

/**
     * This test uses a single session to start all the instances of a process 
     * definition. The singularity of the test is that it never starts a process
     * instance directly. Instead of that the session uses a rule to determine
     * when a process instance must be started.
     * @throws Exception 
     */
@Test
public void singleSessionPerProcessDefinitionWithRules() throws Exception {
    //Creates an entity manager and get the user transaction. We are going
    //to need them later to interact with the business entities persisted
    //by the work item handlers we have configured in our session.
    EntityManager em = getEmf().createEntityManager();
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    //Creates the ksession. In this case the ksession
    //will not only contains the process definition but it is also going to
    //have a rule that is going to start a process instance for each
    //Person object we insert in the session.
    StatefulKnowledgeSession ksession = createProcessWithRulesKnowledgeSession("myProcessDefinitionSession");
    registerWorkItemHandlers(ksession, "myProcessDefinitionSession", em);
    //Instead of manually starting a new process instance we will let the
    //rules to start it when they consider it is necessary.
    //In this case we are going to instantiate a Person object and insert it
    //into the session. After this we will call ksession.fireAllRules() to
    //execute any activated rule.
    Person person = new Person("Salaboy", 29);
    ksession.insert(person);
    ksession.fireAllRules();
    //Dispose the session since we are no longer interested in it.
    ksession.dispose();
    //At this point, a process instance must be started and we should have
    //a business entity persisted in the database. Remember that this entity
    //was persisted by the work item hanlder we have configured for our tasks.
    BusinessEntity businessEntity = getBusinessEntity("myProcessDefinitionSession", em);
    Assert.assertNotNull(businessEntity);
    //Let's restore the session now to insert a new instance of Person and
    //see what happens. In order to restore the session we are using the
    //sessionId present in the business entity we have retrieved from the 
    //database.
    ksession = loadKnowldgeSession(businessEntity.getSessionId(), "myProcessDefinitionSession", em);
    assertNotNull(ksession);
    //Let's create a new Person and insert it in the sesssion. This will
    //cause a new process instance to be launched.
    Person person2 = new Person("Salaboy", 29);
    ksession.insert(person2);
    ksession.fireAllRules();
    //Dispose the session since we are no longer interested in it.
    ksession.dispose();
    //Getting the correct work item to finish:
    //If we don't know which workItem do we want to complete we can create 
    //a query to see which are pending work items for a process or for a 
    //more complex business key.
    //If the thread that wants to notify the engine about the completion of 
    //the external interaction is the one which has created the token inside 
    //the WorkItemHandler it can use that unique value to get the related 
    //workItemId.
    BusinessEntity businessEntityByWorkItemId = getBusinessEntityByWorkItemId(1L, em);
    //Before completing the work item we need to reload the session once again.
    ksession = loadKnowldgeSession(businessEntityByWorkItemId.getSessionId(), "myProcessDefinitionSession", em);
    assertNotNull(ksession);
    try {
        // This needs to happen in the same transaction in order to be consistent
        ut.begin();
        //complete the pending work item handler
        ksession.getWorkItemManager().completeWorkItem(businessEntityByWorkItemId.getWorkItemId(), null);
        //mark the BusinessEntity as completed
        markBusinessEntityAsCompleted(businessEntityByWorkItemId.getId(), em);
        ut.commit();
    } catch (Exception e) {
        System.out.println("Rolling back because of: " + e.getMessage());
        ut.rollback();
    }
    //Dispose the session since we are no longer interested in it.
    ksession.dispose();
    //The only pending workItem related to the processId 2 should be 2
    //We can create queries to find out the pending workItems for a process 
    //instance or to find a process instance related to a business scenario 
    //using this approach.
    List<BusinessEntity> businessEntitiesProcessId = getBusinessEntitiesProcessId(2L, em);
    assertEquals(1, businessEntitiesProcessId.size());
    assertEquals(2, businessEntitiesProcessId.get(0).getWorkItemId());
    assertEquals(2, businessEntitiesProcessId.get(0).getProcessId());
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) BusinessEntity(com.salaboy.sessions.patterns.BusinessEntity) Person(com.salaboy.model.Person) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 23 with EntityManager

use of javax.persistence.EntityManager in project jBPM5-Developer-Guide by Salaboy.

the class SingleSessionPatternsTest method singleSessionPerProcessInstance.

/**
     * This test starts 2 process instances of the same process definition
     * in independent sessions.
     * @throws Exception 
     */
@Test
public void singleSessionPerProcessInstance() throws Exception {
    //Creates an entity manager and get the user transaction. We are going
    //to need them later to interact with the business entities persisted
    //by the work item handlers we have configured in our session.
    EntityManager em = getEmf().createEntityManager();
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    //Initial parameters for process instance #1
    Person person = new Person("Salaboy", 29);
    Map<String, Object> params1 = new HashMap<String, Object>();
    params1.put("person", person);
    //Creates the ksession for process instance #1
    StatefulKnowledgeSession ksession1 = createProcessKnowledgeSession(person.getId());
    registerWorkItemHandlers(ksession1, person.getId(), em);
    int ksession1Id = ksession1.getId();
    //Starts process instance #1
    ksession1.startProcess("com.salaboy.process.AsyncInteractions", params1);
    //We don't want to use the ksession anymore so we will dispose it.
    //At this point MockAsyncExternalServiceWorkItemHandler has persisted
    //a business key that we can use later to retireve the session from
    //the database and continue with the execution of the process.
    ksession1.dispose();
    //Initial parameters for process instance #2
    Person person2 = new Person("Salaboy2", 29);
    Map<String, Object> params2 = new HashMap<String, Object>();
    params2.put("person", person2);
    //Creates a new ksession for process instance #2
    StatefulKnowledgeSession ksession2 = createProcessKnowledgeSession(person2.getId());
    registerWorkItemHandlers(ksession2, person2.getId(), em);
    int ksession2Id = ksession2.getId();
    //Starts process instance #2
    ksession2.startProcess("com.salaboy.process.AsyncInteractions", params2);
    //Dispose ksession2 as we don't want to use it anymore. Just like with
    //process instance #1, the work item handler associated to the task nodes
    //of the process has persisted a business key that we can use to continue
    //with the execution of this session later.
    ksession2.dispose();
    //Let's find the BusinessEntity persisted by process instance #2.
    //The key of the BusinessEntity is the the persnon's id.
    BusinessEntity businessEntity = getBusinessEntity(person2.getId(), em);
    assertNotNull(businessEntity);
    //the BusinessEntity must be of session #2
    assertEquals(businessEntity.getSessionId(), ksession2Id);
    //We shouldn't have more active business entities in the database.
    List<BusinessEntity> activeBusinessEntities = getActiveBusinessEntities(em);
    assertTrue(activeBusinessEntities.size() == 2);
    //Let' restore the session #2 using the information present in the BusinessEntity
    //Since we keep one kbase per ksession we also need to get it using
    //the information present in the BusinessEntity.
    ksession2 = loadKnowldgeSession(businessEntity.getSessionId(), businessEntity.getBusinessKey(), em);
    registerWorkItemHandlers(ksession2, businessEntity.getBusinessKey(), em);
    assertNotNull(ksession2);
    try {
        ut.begin();
        //Now that we have session #2 back we can complete the pending work item 
        //handler with the information present in BusinessEntity we can 
        ksession2.getWorkItemManager().completeWorkItem(businessEntity.getWorkItemId(), null);
        //The BusinessEntity is no longer needed so we can marked as completed
        //in the database.
        markBusinessEntityAsCompleted(businessEntity.getId(), em);
        ut.commit();
    } catch (Exception e) {
        System.out.println("Rolling back because of: " + e.getMessage());
        ut.rollback();
    }
    //We are done with ksession #2
    ksession2.dispose();
    //Now we are going to complete the pending work item handler of 
    //the process instance #1, but first we need to restore the session from
    //the database.
    businessEntity = getBusinessEntity(person.getId(), em);
    assertNotNull(businessEntity);
    //the BusinessEntity must be of session #1
    assertEquals(businessEntity.getSessionId(), ksession1Id);
    //load the ksession using the information present in BusinessEntity
    ksession1 = loadKnowldgeSession(businessEntity.getSessionId(), businessEntity.getBusinessKey(), em);
    assertNotNull(ksession1);
    try {
        // This needs to happen in the same transaction in order to be consistent
        ut.begin();
        //complete the pending work item handler
        ksession1.getWorkItemManager().completeWorkItem(businessEntity.getWorkItemId(), null);
        //mark the BusinessEntity as completed
        markBusinessEntityAsCompleted(businessEntity.getId(), em);
        ut.commit();
    } catch (Exception e) {
        System.out.println("Rolling back because of: " + e.getMessage());
        ut.rollback();
    }
    //dispose ksession #1
    ksession1.dispose();
    //We should have two active business entities in the database. Because the processes have two workitems each.
    activeBusinessEntities = getActiveBusinessEntities(em);
    assertEquals(2, activeBusinessEntities.size());
    //We should have two inactive business entities in the database.
    List<BusinessEntity> inActiveBusinessEntities = getInactiveBusinessEntities(em);
    assertEquals(2, inActiveBusinessEntities.size());
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) BusinessEntity(com.salaboy.sessions.patterns.BusinessEntity) InitialContext(javax.naming.InitialContext) EntityManager(javax.persistence.EntityManager) Person(com.salaboy.model.Person) Test(org.junit.Test)

Example 24 with EntityManager

use of javax.persistence.EntityManager in project jBPM5-Developer-Guide by Salaboy.

the class MultiSessionsPatternsTest method multiSessionsCollaboration.

/*
     * This test shows how a master session can automatically complete a work 
     * item handler in a slave session when the required information is present. 
     */
@Test
public void multiSessionsCollaboration() throws Exception {
    //Creates an entity manager and get the user transaction. We are going
    //to need them later to interact with the business entities persisted
    //by the work item handlers we have configured in our session.
    EntityManager em = getEmf().createEntityManager();
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    //Creates and persists a new BusinessEntity containing the information
    //required by this test.
    StatefulKnowledgeSession interactionSession = null;
    BusinessEntity interactionSessionEntity = null;
    try {
        // This needs to happen in the same transaction if I want to keep it consistent
        ut.begin();
        //Creates a new session.
        interactionSession = createProcessInteractionKnowledgeSession("InteractionSession", em);
        //persists the required business entity.
        interactionSessionEntity = new BusinessEntity(interactionSession.getId(), 0, 0, "InteractionSession");
        // I need to join the Drools/jBPM transaction 
        em.joinTransaction();
        em.persist(interactionSessionEntity);
        ut.commit();
    } catch (Exception e) {
        System.out.println("Rolling Back because of: " + e.getMessage());
        ut.rollback();
        fail(e.getMessage());
    }
    assertNotNull(interactionSessionEntity);
    assertNotNull(interactionSessionEntity.getId());
    //Dispose the session.
    interactionSession.dispose();
    //Initial parameters for process instance #1
    Person person = new Person("Salaboy", 29);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    //Creates the ksession for process instance #1
    StatefulKnowledgeSession ksession = createProcessOneKnowledgeSession(person.getId());
    registerWorkItemHandlers(ksession, person.getId(), em);
    //Starts process instance #1
    ksession.startProcess("com.salaboy.process.AsyncInteractions", params);
    //Disposes the session.
    ksession.dispose();
    // The interaction Session has the responsability of mapping the WorkItems Ids with their corresponding
    // Business Interactions. This can be done with a Map/Registry or with a Knowledge Session to 
    // describe more complex patterns
    BusinessEntity sessionInteractionKey = getBusinessEntity("InteractionSession", em);
    interactionSession = loadKnowldgeSession(sessionInteractionKey.getSessionId(), "InteractionSession", em);
    interactionSession.setGlobal("em", em);
    interactionSession.setGlobal("ksessionSupport", this);
    // Look for all the pending Business Keys which represent an interaction and insert them into the interaction session
    List<BusinessEntity> pendingBusinessEntities = getActiveBusinessEntities(em);
    for (BusinessEntity be : pendingBusinessEntities) {
        if (!be.getBusinessKey().equals("InteractionSession")) {
            interactionSession.insert(be);
        }
    }
    // As soon as we add Data the completion will be triggered and the process will continue
    interactionSession.insert(new Data());
    interactionSession.fireAllRules();
    ksession.dispose();
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) Data(com.salaboy.model.Data) BusinessEntity(com.salaboy.sessions.patterns.BusinessEntity) InitialContext(javax.naming.InitialContext) EntityManager(javax.persistence.EntityManager) Person(com.salaboy.model.Person) Test(org.junit.Test)

Example 25 with EntityManager

use of javax.persistence.EntityManager in project jBPM5-Developer-Guide by Salaboy.

the class MultiSessionsPatternsTest method multiSessionsWithSessionLocator.

/**
     * This test uses the concept of a SessionLocator to register slaves sessions.
     * Based on rules, the master session decides which (process definition), 
     * when (declaratively expressed with rules) and where (in which slave session)
     * to start a process instance.
     */
@Test
public void multiSessionsWithSessionLocator() throws Exception {
    EntityManager em = getEmf().createEntityManager();
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    StatefulKnowledgeSession interactionSession = null;
    BusinessEntity interactionSessionEntity = null;
    try {
        // This needs to happen in the same transaction if I want to keep it consistent
        ut.begin();
        interactionSession = createProcessInteractionKnowledgeSession("InteractionSession", em);
        interactionSessionEntity = new BusinessEntity(interactionSession.getId(), 0, 0, "InteractionSession");
        // I need to join the Drools/jBPM transaction 
        em.joinTransaction();
        em.persist(interactionSessionEntity);
        ut.commit();
    } catch (Exception e) {
        System.out.println("Rolling Back because of: " + e.getMessage());
        ut.rollback();
    }
    assertNotNull(interactionSessionEntity);
    assertNotNull(interactionSessionEntity.getId());
    interactionSession.dispose();
    // Let's create a session which contains a process and register it in the interaction session:
    StatefulKnowledgeSession processSession = createProcessOneKnowledgeSessionAndRegister("My Business Unit Session", interactionSessionEntity, em);
    processSession.dispose();
    Person person = new Person("Salaboy", 29);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    interactionSession = loadKnowldgeSession(interactionSessionEntity.getSessionId(), "InteractionSession", em);
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(interactionSession);
    interactionSession.setGlobal("em", em);
    interactionSession.setGlobal("ksessionSupport", this);
    // Let's insert a fact in the master session and let the rules choose the appropriate session for us to start a process
    interactionSession.insert(person);
    // The process will be selected and started. Because it contains an async activity a new BusinessEntity will be created
    interactionSession.fireAllRules();
    // Look for all the pending Business Keys which represent an interaction and insert them into the interaction session
    List<BusinessEntity> pendingBusinessEntities = em.createQuery("select be from BusinessEntity be where  " + " be.active = true").getResultList();
    for (BusinessEntity be : pendingBusinessEntities) {
        if (!be.getBusinessKey().equals("InteractionSession")) {
            interactionSession.insert(be);
        }
    }
    // As soon as we add Data the completion will be triggered and the process will continue
    interactionSession.insert(new Data());
    interactionSession.fireAllRules();
    interactionSession.dispose();
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) Data(com.salaboy.model.Data) BusinessEntity(com.salaboy.sessions.patterns.BusinessEntity) InitialContext(javax.naming.InitialContext) EntityManager(javax.persistence.EntityManager) Person(com.salaboy.model.Person) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)1395 Test (org.junit.Test)876 Priority (org.hibernate.envers.test.Priority)249 Query (javax.persistence.Query)200 TestForIssue (org.hibernate.testing.TestForIssue)97 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)87 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)79 List (java.util.List)69 NoResultException (javax.persistence.NoResultException)65 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)63 EntityManagerFactory (javax.persistence.EntityManagerFactory)56 HashMap (java.util.HashMap)50 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)50 ArrayList (java.util.ArrayList)41 PersistenceException (javax.persistence.PersistenceException)40 EntityNotFoundException (javax.persistence.EntityNotFoundException)39 Transactional (com.google.inject.persist.Transactional)38 EntityTransaction (javax.persistence.EntityTransaction)36 Item (org.hibernate.jpa.test.Item)36 NonexistentEntityException (com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException)34