Search in sources :

Example 1 with CaseService

use of org.jbpm.casemgmt.api.CaseService in project jbpm by kiegroup.

the class StartCaseWorkItemHandler method abortWorkItem.

@Override
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
    String caseId = (String) workItem.getParameter(CASE_ID);
    boolean destroy = true;
    if (workItem.getParameter(DESTROY_ON_ABORT) != null) {
        destroy = Boolean.parseBoolean((String) workItem.getParameter(DESTROY_ON_ABORT));
    }
    CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
    try {
        if (destroy) {
            logger.debug("Case {} is going to be destroyed", caseId);
            caseService.destroyCase(caseId);
        } else {
            logger.debug("Case {} is going to be canceled", caseId);
            caseService.cancelCase(caseId);
        }
    } catch (CaseNotFoundException e) {
        logger.warn("Case instance {} was not found", caseId);
    } catch (Exception e) {
        logger.error("Unexpected error during canceling case instance {}", caseId, e);
    }
}
Also used : CaseService(org.jbpm.casemgmt.api.CaseService) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException)

Example 2 with CaseService

use of org.jbpm.casemgmt.api.CaseService in project jbpm by kiegroup.

the class StartProcessSLAViolationListener method afterSLAViolated.

@Override
public void afterSLAViolated(SLAViolatedEvent event) {
    CaseFileInstance caseFile = getCaseFile((KieSession) event.getKieRuntime());
    if (caseFile != null) {
        String caseId = ((WorkflowProcessInstanceImpl) event.getProcessInstance()).getCorrelationKey();
        if (caseFile.getCaseId().equals(caseId)) {
            logger.debug("Case instance {} has SLA violation, escalating starting new process instance for {}", caseId, processId);
            CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
            Long slaViolationProcessInstanceId = caseService.addDynamicSubprocess(caseId, processId, null);
            logger.debug("Process instance with id {} was created to handle SLA violation for case {}", slaViolationProcessInstanceId, caseId);
        }
    }
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) CaseService(org.jbpm.casemgmt.api.CaseService)

Example 3 with CaseService

use of org.jbpm.casemgmt.api.CaseService in project jbpm by kiegroup.

the class AsyncCloseCaseCommand method execute.

@Override
public ExecutionResults execute(CommandContext ctx) throws Exception {
    String caseId = (String) ((WorkItem) ctx.getData("workItem")).getParameter("CaseId");
    CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
    caseService.closeCase(caseId, "Closing case from async command");
    return new ExecutionResults();
}
Also used : ExecutionResults(org.kie.api.executor.ExecutionResults) CaseService(org.jbpm.casemgmt.api.CaseService)

Example 4 with CaseService

use of org.jbpm.casemgmt.api.CaseService in project jbpm by kiegroup.

the class NotifyOwnerSLAViolationListener method afterSLAViolated.

@Override
public void afterSLAViolated(SLAViolatedEvent event) {
    CaseFileInstance caseFile = getCaseFile((KieSession) event.getKieRuntime());
    if (caseFile != null) {
        String caseId = ((WorkflowProcessInstanceImpl) event.getProcessInstance()).getCorrelationKey();
        if (caseFile.getCaseId().equals(caseId)) {
            try {
                Collection<OrganizationalEntity> adminAssignments = ((CaseAssignment) caseFile).getAssignments("owner");
                String recipients = adminAssignments.stream().map(oe -> userInfo.getEmailForEntity(oe)).collect(Collectors.joining(";"));
                logger.debug("Case instance {} has SLA violation, notifying owner", caseId);
                CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
                Map<String, Object> parameters = buildEmailParameters(recipients, caseId, event);
                TaskSpecification taskSpec = caseService.newTaskSpec("Email", "SLA Violation for case " + caseId, parameters);
                caseService.addDynamicTask(caseId, taskSpec);
            } catch (IllegalArgumentException e) {
                logger.debug("There is no owner role defined in case instance {}, unable to notify SLA violation", caseId);
            }
        }
    }
}
Also used : Cacheable(org.kie.internal.runtime.Cacheable) TaskSpecification(org.jbpm.casemgmt.api.dynamic.TaskSpecification) Logger(org.slf4j.Logger) UserInfo(org.kie.internal.task.api.UserInfo) UserDataServiceProvider(org.jbpm.runtime.manager.impl.identity.UserDataServiceProvider) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) Collection(java.util.Collection) CaseService(org.jbpm.casemgmt.api.CaseService) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ClassObjectFilter(org.drools.core.ClassObjectFilter) CaseAssignment(org.kie.api.runtime.process.CaseAssignment) Collectors(java.util.stream.Collectors) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Map(java.util.Map) ServiceRegistry(org.jbpm.services.api.service.ServiceRegistry) SLAViolatedEvent(org.kie.api.event.process.SLAViolatedEvent) KieSession(org.kie.api.runtime.KieSession) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) TaskSpecification(org.jbpm.casemgmt.api.dynamic.TaskSpecification) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) CaseService(org.jbpm.casemgmt.api.CaseService) CaseAssignment(org.kie.api.runtime.process.CaseAssignment)

Example 5 with CaseService

use of org.jbpm.casemgmt.api.CaseService in project jbpm by kiegroup.

the class StartCaseWorkItemHandler method executeWorkItem.

@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    String deploymentId = (String) workItem.getParameter(DEPLOYMENT_ID);
    if (deploymentId == null) {
        deploymentId = ((org.drools.core.process.instance.WorkItem) workItem).getDeploymentId();
    }
    RuntimeManager targetRuntimeManager = RuntimeManagerRegistry.get().getManager(deploymentId);
    if (targetRuntimeManager == null || !(targetRuntimeManager instanceof PerCaseRuntimeManager)) {
        throw new IllegalArgumentException("Requested target deployment does not exist or is not per case strategy");
    }
    String caseDefinitionId = (String) workItem.getParameter(CASE_DEFINITION_ID);
    if (caseDefinitionId == null || caseDefinitionId.trim().isEmpty()) {
        throw new IllegalArgumentException(CASE_DEFINITION_ID + " is a required parameter for StartCaseWorkItemHandler");
    }
    Map<String, Object> caseFileData = new HashMap<>();
    Map<String, List<String>> accessRestrictions = new HashMap<>();
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    parseParameters(workItem, caseFileData, roleAssignments, accessRestrictions);
    long processInstanceId = ((WorkItemImpl) workItem).getProcessInstanceId();
    long workItemId = workItem.getId();
    logger.debug("Parent process instance id {} and work item instance id {} for new case instance", processInstanceId, workItemId);
    CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
    CaseFileInstance subCaseFile = caseService.newCaseFileInstanceWithRestrictions(deploymentId, caseDefinitionId, caseFileData, roleAssignments, accessRestrictions);
    ((CaseFileInstanceImpl) subCaseFile).setParentInstanceId(processInstanceId);
    ((CaseFileInstanceImpl) subCaseFile).setParentWorkItemId(workItemId);
    String caseId = caseService.startCase(deploymentId, caseDefinitionId, subCaseFile);
    logger.debug("Case with id {} has been successfully started");
    boolean independent = Boolean.parseBoolean((String) workItem.getParameter(INDEPENDENT));
    if (independent) {
        Map<String, Object> results = new HashMap<>();
        results.put(CASE_ID, caseId);
        try {
            CaseFileInstance snapshot = caseService.getCaseFileInstance(caseId);
            results.putAll(snapshot.getData());
        } catch (CaseNotFoundException e) {
            // case is already completed
            logger.debug("Case is already completed, not possible to fetch case file data any more");
        }
        logger.debug("Completing directly (without waiting for case instance {} completion) work item with id {}", caseId, workItem.getId());
        ((CaseFileInstanceImpl) subCaseFile).setParentInstanceId(null);
        ((CaseFileInstanceImpl) subCaseFile).setParentWorkItemId(null);
        manager.completeWorkItem(workItem.getId(), results);
    } else {
        // save case id so the abort work item can abort/destroy the case instance
        ((WorkItemImpl) workItem).setParameter(CASE_ID, caseId);
        logger.debug("Waiting for case instance {} completion before completing work item with id {}", caseId, workItem.getId());
    }
}
Also used : PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ArrayList(java.util.ArrayList) List(java.util.List) CaseService(org.jbpm.casemgmt.api.CaseService)

Aggregations

CaseService (org.jbpm.casemgmt.api.CaseService)6 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)4 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)3 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Collectors (java.util.stream.Collectors)2 ClassObjectFilter (org.drools.core.ClassObjectFilter)2 CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)2 TaskSpecification (org.jbpm.casemgmt.api.dynamic.TaskSpecification)2 ServiceRegistry (org.jbpm.services.api.service.ServiceRegistry)2 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)2 SLAViolatedEvent (org.kie.api.event.process.SLAViolatedEvent)2 KieSession (org.kie.api.runtime.KieSession)2 CaseAssignment (org.kie.api.runtime.process.CaseAssignment)2 Cacheable (org.kie.internal.runtime.Cacheable)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1