Search in sources :

Example 1 with JPAPlaceholderResolverStrategy

use of org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategyTest method before.

@Before
public void before() {
    context = setupWithPoolingDataSource(JBPM_PERSISTENCE_UNIT_NAME);
    // load up the knowledge base
    Environment env = PersistenceUtil.createEnvironment(context);
    env.set(OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new ProcessInstanceResolverStrategy(), new JPAPlaceholderResolverStrategy(env), new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    if (useLocking) {
        env.set(USE_PESSIMISTIC_LOCKING, true);
    }
    KieBase kbase = loadKnowledgeBase();
    // create session
    ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    Assert.assertTrue("Valid KnowledgeSession could not be created.", ksession != null && ksession.getIdentifier() > 0);
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) JPAPlaceholderResolverStrategy(org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy) ProcessInstanceResolverStrategy(org.jbpm.marshalling.impl.ProcessInstanceResolverStrategy) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) Before(org.junit.Before)

Example 2 with JPAPlaceholderResolverStrategy

use of org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy in project jbpm by kiegroup.

the class ParentChildMarshallingJpaTest method testProcess.

@Test
public void testProcess() throws Exception {
    emfDomain = Persistence.createEntityManagerFactory("org.jbpm.persistence.parent-child");
    addEnvironmentEntry(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new JPAPlaceholderResolverStrategy(emfDomain), new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    RuntimeManager manager = createRuntimeManager(Strategy.PROCESS_INSTANCE, "manager", "org/jbpm/test/functional/jpa/parent-child.bpmn");
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    // start a new process instance
    Map<String, Object> params = new HashMap<String, Object>();
    Application application = new Application();
    application.setType("A");
    params.put("application", application);
    ProcessInstance pi = ksession.startProcess("com.sample.bpmn.hello", params);
    System.out.println("A process instance started : pid = " + pi.getId());
    TaskService taskService = runtime.getTaskService();
    assertTrue(taskService instanceof CommandBasedTaskService);
    assertTrue(((CommandBasedTaskService) taskService).getEnvironment().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES) != null);
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    for (TaskSummary taskSummary : list) {
        System.out.println("john starts a task : taskId = " + taskSummary.getId());
        Task task = taskService.getTaskById(taskSummary.getId());
        long documentContentId = task.getTaskData().getDocumentContentId();
        Content content = taskService.getContentById(documentContentId);
        HashMap<String, Object> contents = (HashMap<String, Object>) ContentMarshallerHelper.unmarshall(content.getContent(), ksession.getEnvironment());
        Application outputApplication = (Application) contents.get("input1_application");
        Person person = new Person();
        person.setFullName("John Doe");
        outputApplication.setPerson(person);
        Map<String, Object> results = new LinkedHashMap<String, Object>();
        results.put("output1_application", outputApplication);
        taskService.start(taskSummary.getId(), "john");
        taskService.complete(taskSummary.getId(), "john", results);
    }
    list = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
    for (TaskSummary taskSummary : list) {
        System.out.println("mary starts a task : taskId = " + taskSummary.getId());
        taskService.start(taskSummary.getId(), "mary");
        taskService.complete(taskSummary.getId(), "mary", null);
    }
    manager.disposeRuntimeEngine(runtime);
    // Check!
    EntityManager em = emfDomain.createEntityManager();
    int size = em.createQuery("select i from Person i").getResultList().size();
    assertEquals(1, size);
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) JPAPlaceholderResolverStrategy(org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) Task(org.kie.api.task.model.Task) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TaskService(org.kie.api.task.TaskService) CommandBasedTaskService(org.jbpm.services.task.impl.command.CommandBasedTaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) CommandBasedTaskService(org.jbpm.services.task.impl.command.CommandBasedTaskService) LinkedHashMap(java.util.LinkedHashMap) EntityManager(javax.persistence.EntityManager) Content(org.kie.api.task.model.Content) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Application(org.jbpm.test.entity.Application) Person(org.jbpm.test.entity.Person) Test(org.junit.Test)

Example 3 with JPAPlaceholderResolverStrategy

use of org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy in project jbpm by kiegroup.

the class PatientVariablePersistenceStrategyTest method simplePatientMedicalRecordTest.

@Test
public void simplePatientMedicalRecordTest() throws Exception {
    Patient salaboy = new Patient("salaboy");
    MedicalRecord medicalRecord = new MedicalRecord("Last Three Years Medical Hisotry", salaboy);
    emfDomain = Persistence.createEntityManagerFactory("org.jbpm.persistence.patient.example");
    addEnvironmentEntry(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new JPAPlaceholderResolverStrategy(emfDomain), new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    EntityManager em = emfDomain.createEntityManager();
    createRuntimeManager("org/jbpm/test/functional/jpa/patient-appointment.bpmn");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    logger.info("### Starting process ###");
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("medicalRecord", medicalRecord);
    ProcessInstance process = ksession.startProcess("org.jbpm.PatientAppointment", parameters);
    long processInstanceId = process.getId();
    // The process is in the first Human Task waiting for its completion
    Assert.assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());
    List<? extends VariableInstanceLog> varLogs = runtimeEngine.getAuditService().findVariableInstances(processInstanceId, "medicalRecord");
    assertNotNull(varLogs);
    assertEquals(1, varLogs.size());
    // gets frontDesk's tasks
    List<TaskSummary> frontDeskTasks = taskService.getTasksAssignedAsPotentialOwner("frontDesk", "en-UK");
    Assert.assertEquals(1, frontDeskTasks.size());
    // doctor doesn't have any task
    List<TaskSummary> doctorTasks = taskService.getTasksAssignedAsPotentialOwner("doctor", "en-UK");
    Assert.assertTrue(doctorTasks.isEmpty());
    // manager doesn't have any task
    List<TaskSummary> managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
    Assert.assertTrue(managerTasks.isEmpty());
    taskService.start(frontDeskTasks.get(0).getId(), "frontDesk");
    // frontDesk completes its task
    MedicalRecord taskMedicalRecord = getTaskContent(runtimeEngine, frontDeskTasks.get(0));
    assertNotNull(taskMedicalRecord.getId());
    taskMedicalRecord.setDescription("Initial Description of the Medical Record");
    Map<String, Object> output = new HashMap<String, Object>();
    output.put("output1", taskMedicalRecord);
    taskService.complete(frontDeskTasks.get(0).getId(), "frontDesk", output);
    varLogs = runtimeEngine.getAuditService().findVariableInstances(processInstanceId, "medicalRecord");
    assertNotNull(varLogs);
    assertEquals(2, varLogs.size());
    assertTrue(varLogs.get(0).getValue().contains("Last Three Years Medical Hisotry"));
    assertTrue(varLogs.get(1).getValue().contains("Initial Description of the Medical Record"));
    // Now doctor has 1 task
    doctorTasks = taskService.getTasksAssignedAsPotentialOwner("doctor", "en-UK");
    Assert.assertEquals(1, doctorTasks.size());
    // No tasks for manager yet
    managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
    Assert.assertTrue(managerTasks.isEmpty());
    // modify the entity from outside
    taskMedicalRecord.setDescription("Initial Description of the Medical Record - Updated");
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    try {
        ut.begin();
        em.merge(taskMedicalRecord);
        ut.commit();
    } catch (Exception ex) {
        ut.rollback();
        throw ex;
    }
    taskMedicalRecord = getTaskContent(runtimeEngine, doctorTasks.get(0));
    assertNotNull(taskMedicalRecord.getId());
    taskMedicalRecord.setDescription("Initial Description of the Medical Record - Updated");
    taskService.start(doctorTasks.get(0).getId(), "doctor");
    // Check that we have the Modified Document
    taskMedicalRecord = em.find(MedicalRecord.class, taskMedicalRecord.getId());
    Assert.assertEquals("Initial Description of the Medical Record - Updated", taskMedicalRecord.getDescription());
    taskMedicalRecord.setDescription("Medical Record Validated by Doctor");
    List<RecordRow> rows = new ArrayList<RecordRow>();
    RecordRow recordRow = new RecordRow("CODE-999", "Just a regular Cold");
    recordRow.setMedicalRecord(medicalRecord);
    rows.add(recordRow);
    taskMedicalRecord.setRows(rows);
    taskMedicalRecord.setPriority(1);
    output = new HashMap<String, Object>();
    output.put("output2", taskMedicalRecord);
    taskService.complete(doctorTasks.get(0).getId(), "doctor", output);
    // tasks for manager
    managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
    Assert.assertEquals(1, managerTasks.size());
    taskService.start(managerTasks.get(0).getId(), "manager");
    Patient patient = taskMedicalRecord.getPatient();
    patient.setNextAppointment(new Date());
    output = new HashMap<String, Object>();
    output.put("output3", taskMedicalRecord);
    // ksession.getWorkItemManager().registerWorkItemHandler("Human Task", htHandler);
    taskService.complete(managerTasks.get(0).getId(), "manager", output);
    // since persisted process instance is completed it should be null
    process = ksession.getProcessInstance(process.getId());
    Assert.assertNull(process);
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) UserTransaction(javax.transaction.UserTransaction) JPAPlaceholderResolverStrategy(org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RecordRow(org.jbpm.test.entity.RecordRow) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) ArrayList(java.util.ArrayList) Patient(org.jbpm.test.entity.Patient) MedicalRecord(org.jbpm.test.entity.MedicalRecord) IOException(java.io.IOException) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test)

Example 4 with JPAPlaceholderResolverStrategy

use of org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy in project jbpm by kiegroup.

the class CaseMarshallerFactory method withJpa.

/**
 * Add JPA marshalling strategy to be used by CaseFileMarshaller
 * @param puName persistence unit name to be used
 * @return this factory instance
 */
public CaseMarshallerFactory withJpa(String puName) {
    marshallers.add(new JPAPlaceholderResolverStrategy(puName, classLoader));
    this.toString.append(".withJpa(\"" + puName + "\")");
    return this;
}
Also used : JPAPlaceholderResolverStrategy(org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy)

Example 5 with JPAPlaceholderResolverStrategy

use of org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy in project jbpm by kiegroup.

the class VariablePersistenceStrategyTest method createEnvironment.

private Environment createEnvironment() {
    Environment env = PersistenceUtil.createEnvironment(context);
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new JPAPlaceholderResolverStrategy(env), new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    return env;
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) JPAPlaceholderResolverStrategy(org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy) Environment(org.kie.api.runtime.Environment)

Aggregations

JPAPlaceholderResolverStrategy (org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy)6 SerializablePlaceholderResolverStrategy (org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy)5 HashMap (java.util.HashMap)3 EntityManager (javax.persistence.EntityManager)3 Test (org.junit.Test)3 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)3 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)3 Environment (org.kie.api.runtime.Environment)2 KieSession (org.kie.api.runtime.KieSession)2 TaskService (org.kie.api.task.TaskService)2 TaskSummary (org.kie.api.task.model.TaskSummary)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 UserTransaction (javax.transaction.UserTransaction)1 ProcessInstanceResolverStrategy (org.jbpm.marshalling.impl.ProcessInstanceResolverStrategy)1 CommandBasedTaskService (org.jbpm.services.task.impl.command.CommandBasedTaskService)1 Application (org.jbpm.test.entity.Application)1 DocumentVariable (org.jbpm.test.entity.DocumentVariable)1