Search in sources :

Example 1 with BusinessCalendar

use of org.jbpm.process.core.timer.BusinessCalendar in project jbpm by kiegroup.

the class RuntimeManagerWithDescriptorTest method testDeployWithFullCustomDeploymentDescriptor.

@Test
public void testDeployWithFullCustomDeploymentDescriptor() throws Exception {
    Map<String, String> resources = new HashMap<String, String>();
    String scriptString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-ScriptTask.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-ScriptTask.bpmn2", scriptString);
    String manualString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-ManualTask.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-ManualTask.bpmn2", manualString);
    String userString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-UserTask.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-UserTask.bpmn2", userString);
    String callString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-CallActivity.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-CallActivity.bpmn2", callString);
    String subProcessString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-CallActivitySubProcess.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-CallActivitySubProcess.bpmn2", subProcessString);
    DeploymentDescriptor customDescriptor = new DeploymentDescriptorImpl("org.jbpm.persistence.jpa");
    customDescriptor.getBuilder().runtimeStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE).addMarshalingStrategy(new ObjectModel("org.jbpm.runtime.manager.impl.deploy" + ".RuntimeManagerWithDescriptorTest$TestMarshallingStrategy")).addConfiguration(new NamedObjectModel("drools.processSignalManagerFactory", "java.lang.String", new Object[] { DefaultSignalManagerFactory.class.getName() })).addEnvironmentEntry(new NamedObjectModel("jbpm.business.calendar", "org.jbpm.runtime.manager.impl.deploy.RuntimeManagerWithDescriptorTest$TestBusinessCalendar")).addEventListener(new ObjectModel("org.jbpm.runtime.manager.impl.deploy" + ".RuntimeManagerWithDescriptorTest$TestProcessEventListener")).addGlobal(new NamedObjectModel("service", "java.util.ArrayList")).addTaskEventListener(new ObjectModel("org.jbpm.runtime.manager.impl.deploy" + ".RuntimeManagerWithDescriptorTest$TestTaskEventListener")).addWorkItemHandler(new NamedObjectModel("Manual Task", "org.jbpm.runtime.manager.impl.deploy" + ".RuntimeManagerWithDescriptorTest$TestWorkItemHandler"));
    resources.put("src/main/resources/" + DeploymentDescriptor.META_INF_LOCATION, customDescriptor.toXml());
    String drl = "package org.jbpm; global java.util.List service; " + "	rule \"Start Hello1\"" + "	  when" + "	  then" + "	    System.out.println(\"Hello\");" + "	end";
    resources.put("src/main/resources/simple.drl", drl);
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.jbpm.test.dd", "-kjar-with-dd", "1.0.0");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, resources);
    installKjar(releaseId, kJar1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder(releaseId).userGroupCallback(userGroupCallback).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    InternalRuntimeManager internalManager = (InternalRuntimeManager) manager;
    RegisterableItemsFactory factory = internalManager.getEnvironment().getRegisterableItemsFactory();
    assertNotNull(factory);
    assertTrue(factory instanceof InternalRegisterableItemsFactory);
    assertNotNull(((InternalRegisterableItemsFactory) factory).getRuntimeManager());
    DeploymentDescriptor descriptor = internalManager.getDeploymentDescriptor();
    assertNotNull(descriptor);
    assertEquals("org.jbpm.persistence.jpa", descriptor.getPersistenceUnit());
    assertEquals("org.jbpm.persistence.jpa", descriptor.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, descriptor.getAuditMode());
    assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, descriptor.getRuntimeStrategy());
    assertEquals(1, descriptor.getMarshallingStrategies().size());
    // assertEquals(1, descriptor.getConfiguration().size());
    assertEquals(1, descriptor.getEnvironmentEntries().size());
    assertEquals(1, descriptor.getEventListeners().size());
    assertEquals(1, descriptor.getGlobals().size());
    assertEquals(1, descriptor.getTaskEventListeners().size());
    assertEquals(1, descriptor.getWorkItemHandlers().size());
    assertEquals(0, descriptor.getRequiredRoles().size());
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    assertNotNull(engine);
    KieSession kieSession = engine.getKieSession();
    // configuration
    assertEquals(((SessionConfiguration) kieSession.getSessionConfiguration()).getSignalManagerFactory(), DefaultSignalManagerFactory.class.getName());
    BusinessCalendar bc = (BusinessCalendar) kieSession.getEnvironment().get("jbpm.business.calendar");
    assertNotNull(bc);
    assertTrue(bc instanceof TestBusinessCalendar);
    // globals
    Object service = kieSession.getGlobal("service");
    assertNotNull(service);
    assertTrue(service instanceof ArrayList);
    // work item handler
    ProcessInstance processInstance = kieSession.startProcess("ManualTask");
    long processInstanceId = processInstance.getId();
    assertNotNull(kieSession.getProcessInstance(processInstanceId));
    kieSession.getWorkItemManager().completeWorkItem(TestWorkItemHandler.getWorkItem().getId(), null);
    assertNull(kieSession.getProcessInstance(processInstanceId));
    manager.disposeRuntimeEngine(engine);
    engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    kieSession = engine.getKieSession();
    // process event listener
    assertArrayEquals(new String[] { "beforeProcessStarted", "afterProcessStarted", "beforeProcessCompleted", "afterProcessCompleted" }, processEvents.toArray());
    processEvents.clear();
    manager.disposeRuntimeEngine(engine);
    engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    kieSession = engine.getKieSession();
    // task event listener
    processInstance = kieSession.startProcess("UserTask");
    processInstanceId = processInstance.getId();
    assertNotNull(kieSession.getProcessInstance(processInstanceId));
    manager.disposeRuntimeEngine(engine);
    engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
    TaskService taskService = engine.getTaskService();
    List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    long taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    assertArrayEquals(new String[] { "beforeTaskAddedEvent", "afterTaskAddedEvent", "beforeTaskStartedEvent", "afterTaskStartedEvent", "beforeTaskCompletedEvent", "afterTaskCompletedEvent" }, taskEvents.toArray());
    manager.disposeRuntimeEngine(engine);
    engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    kieSession = engine.getKieSession();
    // marshalling strategy
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "marshal");
    processInstance = kieSession.startProcess("ParentProcess", params);
    processInstanceId = processInstance.getId();
    ProcessInstance pi = kieSession.getProcessInstance(processInstanceId);
    assertNotNull(pi);
    String varX = (String) ((WorkflowProcessInstance) pi).getVariable("x");
    assertEquals(TestMarshallingStrategy.ALWAYS_RESPOND_WITH, varX);
    manager.disposeRuntimeEngine(engine);
}
Also used : ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) HashMap(java.util.HashMap) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) DefaultSignalManagerFactory(org.jbpm.process.instance.event.DefaultSignalManagerFactory) ArrayList(java.util.ArrayList) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) KieServices(org.kie.api.KieServices) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) KieSession(org.kie.api.runtime.KieSession) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) TaskService(org.kie.api.task.TaskService) ReleaseId(org.kie.api.builder.ReleaseId) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar) TaskSummary(org.kie.api.task.model.TaskSummary) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 2 with BusinessCalendar

use of org.jbpm.process.core.timer.BusinessCalendar in project jbpm by kiegroup.

the class WorkflowProcessInstanceImpl method configureSLATimer.

public TimerInstance configureSLATimer(String slaDueDateExpression) {
    // setup SLA if provided
    slaDueDateExpression = resolveVariable(slaDueDateExpression);
    if (slaDueDateExpression == null || slaDueDateExpression.trim().isEmpty()) {
        logger.debug("Sla due date expression resolved to no value '{}'", slaDueDateExpression);
        return null;
    }
    logger.debug("SLA due date is set to {}", slaDueDateExpression);
    InternalKnowledgeRuntime kruntime = getKnowledgeRuntime();
    long duration = -1;
    if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null) {
        BusinessCalendar businessCalendar = (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar");
        duration = businessCalendar.calculateBusinessTimeAsDuration(slaDueDateExpression);
    } else {
        duration = DateTimeUtils.parseDuration(slaDueDateExpression);
    }
    TimerInstance timerInstance = new TimerInstance();
    timerInstance.setId(-1);
    timerInstance.setDelay(duration);
    timerInstance.setPeriod(0);
    if (useTimerSLATracking()) {
        ((InternalProcessRuntime) kruntime.getProcessRuntime()).getTimerManager().registerTimer(timerInstance, this);
    }
    return timerInstance;
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar)

Example 3 with BusinessCalendar

use of org.jbpm.process.core.timer.BusinessCalendar in project jbpm by kiegroup.

the class TimerNodeInstance method createTimerInstance.

protected TimerInstance createTimerInstance(InternalKnowledgeRuntime kruntime) {
    Timer timer = getTimerNode().getTimer();
    TimerInstance timerInstance = new TimerInstance();
    if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null) {
        BusinessCalendar businessCalendar = (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar");
        String delay = resolveVariable(timer.getDelay());
        timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(delay));
        if (timer.getPeriod() == null) {
            timerInstance.setPeriod(0);
        } else {
            String period = resolveVariable(timer.getPeriod());
            timerInstance.setPeriod(businessCalendar.calculateBusinessTimeAsDuration(period));
        }
    } else {
        configureTimerInstance(timer, timerInstance);
    }
    timerInstance.setTimerId(timer.getId());
    return timerInstance;
}
Also used : Timer(org.jbpm.process.core.timer.Timer) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar)

Example 4 with BusinessCalendar

use of org.jbpm.process.core.timer.BusinessCalendar in project jbpm by kiegroup.

the class StateBasedNodeInstance method createTimerInstance.

protected TimerInstance createTimerInstance(Timer timer) {
    TimerInstance timerInstance = new TimerInstance();
    KieRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
    if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null) {
        BusinessCalendar businessCalendar = (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar");
        String delay = null;
        switch(timer.getTimeType()) {
            case Timer.TIME_CYCLE:
                if (CronExpression.isValidExpression(timer.getDelay())) {
                    timerInstance.setCronExpression(timer.getDelay());
                } else {
                    String tempDelay = resolveVariable(timer.getDelay());
                    String tempPeriod = resolveVariable(timer.getPeriod());
                    if (DateTimeUtils.isRepeatable(tempDelay)) {
                        String[] values = DateTimeUtils.parseISORepeatable(tempDelay);
                        String tempRepeatLimit = values[0];
                        tempDelay = values[1];
                        tempPeriod = values[2];
                        if (!tempRepeatLimit.isEmpty()) {
                            try {
                                int repeatLimit = Integer.parseInt(tempRepeatLimit);
                                if (repeatLimit > -1) {
                                    timerInstance.setRepeatLimit(repeatLimit + 1);
                                }
                            } catch (NumberFormatException e) {
                            // ignore
                            }
                        }
                    }
                    timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(tempDelay));
                    if (tempPeriod == null) {
                        timerInstance.setPeriod(0);
                    } else {
                        timerInstance.setPeriod(businessCalendar.calculateBusinessTimeAsDuration(tempPeriod));
                    }
                }
                break;
            case Timer.TIME_DURATION:
                delay = resolveVariable(timer.getDelay());
                timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(delay));
                timerInstance.setPeriod(0);
                break;
            case Timer.TIME_DATE:
                // even though calendar is available concrete date was provided so it shall be used
                configureTimerInstance(timer, timerInstance);
            default:
                break;
        }
    } else {
        configureTimerInstance(timer, timerInstance);
    }
    timerInstance.setTimerId(timer.getId());
    return timerInstance;
}
Also used : TimerInstance(org.jbpm.process.instance.timer.TimerInstance) KieRuntime(org.kie.api.runtime.KieRuntime) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar)

Example 5 with BusinessCalendar

use of org.jbpm.process.core.timer.BusinessCalendar in project jbpm by kiegroup.

the class HumanTaskReassignmentTest method configureBusinessCalendar.

private void configureBusinessCalendar(boolean businessHour) {
    Properties configuration = new Properties();
    if (businessHour) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        configuration.setProperty(BusinessCalendarImpl.START_HOUR, "0");
        configuration.setProperty(BusinessCalendarImpl.END_HOUR, "24");
        configuration.setProperty(BusinessCalendarImpl.HOURS_PER_DAY, "24");
        configuration.setProperty(BusinessCalendarImpl.DAYS_PER_WEEK, "7");
        configuration.setProperty(BusinessCalendarImpl.WEEKEND_DAYS, Integer.toString(dayOfWeek));
    } else {
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 1);
        Date tomorrow = c.getTime();
        String dateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        configuration.setProperty(BusinessCalendarImpl.HOLIDAYS, sdf.format(today) + "," + sdf.format(tomorrow));
        configuration.setProperty(BusinessCalendarImpl.HOLIDAY_DATE_FORMAT, dateFormat);
    }
    BusinessCalendar businessCalendar = new BusinessCalendarImpl(configuration);
    ksession.getEnvironment().set("jbpm.business.calendar", businessCalendar);
}
Also used : Calendar(java.util.Calendar) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar) BusinessCalendarImpl(org.jbpm.process.core.timer.BusinessCalendarImpl) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar) Properties(java.util.Properties) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

BusinessCalendar (org.jbpm.process.core.timer.BusinessCalendar)7 Date (java.util.Date)3 TimerInstance (org.jbpm.process.instance.timer.TimerInstance)3 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 Properties (java.util.Properties)2 BusinessCalendarImpl (org.jbpm.process.core.timer.BusinessCalendarImpl)2 HashMap (java.util.HashMap)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)1 Timer (org.jbpm.process.core.timer.Timer)1 DefaultSignalManagerFactory (org.jbpm.process.instance.event.DefaultSignalManagerFactory)1 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)1 Test (org.junit.Test)1 KieServices (org.kie.api.KieServices)1 ReleaseId (org.kie.api.builder.ReleaseId)1 KieRuntime (org.kie.api.runtime.KieRuntime)1 KieSession (org.kie.api.runtime.KieSession)1 RegisterableItemsFactory (org.kie.api.runtime.manager.RegisterableItemsFactory)1