Search in sources :

Example 6 with CaseEventSupport

use of org.jbpm.casemgmt.impl.event.CaseEventSupport in project jbpm by kiegroup.

the class ReopenCaseCommand method execute.

@Override
public Void execute(Context context) {
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    CaseFileInstance caseFile = getCaseFile(ksession, caseId);
    caseEventSupport.fireBeforeCaseReopened(caseId, caseFile, deploymentId, caseDefinitionId, data);
    logger.debug("Updating case file in working memory");
    FactHandle factHandle = ksession.getFactHandle(caseFile);
    ((CaseFileInstanceImpl) caseFile).setCaseReopenDate(new Date());
    if (data != null && !data.isEmpty()) {
        caseFile.addAll(data);
    }
    ksession.update(factHandle, caseFile);
    logger.debug("Starting process instance for case {} and case definition {}", caseId, caseDefinitionId);
    CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
    Map<String, Object> params = new HashMap<>();
    // set case id to allow it to use CaseContext when creating runtime engine
    params.put(EnvironmentName.CASE_ID, caseId);
    long processInstanceId = processService.startProcess(deploymentId, caseDefinitionId, correlationKey, params);
    logger.debug("Case {} successfully reopened (process instance id {})", caseId, processInstanceId);
    caseEventSupport.fireAfterCaseReopened(caseId, caseFile, deploymentId, caseDefinitionId, data, processInstanceId);
    return null;
}
Also used : CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) FactHandle(org.kie.api.runtime.rule.FactHandle) HashMap(java.util.HashMap) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) RegistryContext(org.drools.core.command.impl.RegistryContext) Date(java.util.Date) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CorrelationKey(org.kie.internal.process.CorrelationKey) KieSession(org.kie.api.runtime.KieSession)

Example 7 with CaseEventSupport

use of org.jbpm.casemgmt.impl.event.CaseEventSupport in project jbpm by kiegroup.

the class AddDynamicTaskCommand method execute.

@Override
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    if (processInstance == null) {
        throw new ProcessInstanceNotFoundException("No process instance found with id " + processInstanceId);
    }
    CaseFileInstance caseFile = getCaseFile(ksession, caseId);
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    caseEventSupport.fireBeforeDynamicTaskAdded(caseId, caseFile, processInstanceId, nodeType, parameters);
    DynamicUtils.addDynamicWorkItem(processInstance, ksession, nodeType, parameters);
    caseEventSupport.fireAfterDynamicTaskAdded(caseId, caseFile, processInstanceId, nodeType, parameters);
    return null;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException)

Example 8 with CaseEventSupport

use of org.jbpm.casemgmt.impl.event.CaseEventSupport in project jbpm by kiegroup.

the class CloseCaseCommand method execute.

@Override
public Void execute(Context context) {
    CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
    Collection<ProcessInstanceDesc> caseProcesses = runtimeDataService.getProcessInstancesByCorrelationKey(correlationKey, new QueryContext(0, 1000));
    if (caseProcesses.isEmpty()) {
        throw new CaseNotFoundException("Case with id " + caseId + " was not found");
    }
    final List<Long> processInstanceIds = caseProcesses.stream().filter(pi -> pi.getState().equals(ProcessInstance.STATE_ACTIVE)).sorted((ProcessInstanceDesc o1, ProcessInstanceDesc o2) -> {
        return Long.valueOf(o2.getParentId()).compareTo(Long.valueOf(o1.getParentId()));
    }).map(pi -> pi.getId()).collect(toList());
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    CaseFileInstance caseFile = getCaseFile(ksession, caseId);
    caseEventSupport.fireBeforeCaseClosed(caseId, caseFile, comment);
    logger.debug("Process instances {} that will be completed as part of the close of the case {}", processInstanceIds, caseId);
    processService.execute(deploymentId, CaseContext.get(caseId), new ExecutableCommand<Void>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Void execute(Context context) {
            KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
            for (Long processInstanceId : processInstanceIds) {
                WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
                processInstance.setState(ProcessInstance.STATE_COMPLETED, comment);
                logger.debug("Process instance {} set to state completed", processInstanceId);
            }
            return null;
        }
    });
    caseEventSupport.fireAfterCaseClosed(caseId, caseFile, comment);
    return null;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) RegistryContext(org.drools.core.command.impl.RegistryContext) Logger(org.slf4j.Logger) ProcessService(org.jbpm.services.api.ProcessService) CorrelationKey(org.kie.internal.process.CorrelationKey) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) Collection(java.util.Collection) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) LoggerFactory(org.slf4j.LoggerFactory) QueryContext(org.kie.api.runtime.query.QueryContext) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) RuntimeDataService(org.jbpm.services.api.RuntimeDataService) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) KieInternalServices(org.kie.internal.KieInternalServices) KieSession(org.kie.api.runtime.KieSession) CorrelationKeyFactory(org.kie.internal.process.CorrelationKeyFactory) CaseContext(org.kie.internal.runtime.manager.context.CaseContext) RegistryContext(org.drools.core.command.impl.RegistryContext) QueryContext(org.kie.api.runtime.query.QueryContext) Context(org.kie.api.runtime.Context) CaseContext(org.kie.internal.runtime.manager.context.CaseContext) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) RegistryContext(org.drools.core.command.impl.RegistryContext) QueryContext(org.kie.api.runtime.query.QueryContext) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CorrelationKey(org.kie.internal.process.CorrelationKey) KieSession(org.kie.api.runtime.KieSession) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance)

Example 9 with CaseEventSupport

use of org.jbpm.casemgmt.impl.event.CaseEventSupport in project jbpm by kiegroup.

the class ModifyRoleAssignmentCommand method execute.

@Override
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
    if (caseFiles.size() != 1) {
        throw new IllegalStateException("Not able to find distinct case file - found case files " + caseFiles.size());
    }
    CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
    FactHandle factHandle = ksession.getFactHandle(caseFile);
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    if (add) {
        caseEventSupport.fireBeforeCaseRoleAssignmentAdded(caseFile.getCaseId(), caseFile, roleName, entity);
        ((CaseAssignment) caseFile).assign(roleName, entity);
        caseEventSupport.fireAfterCaseRoleAssignmentAdded(caseFile.getCaseId(), caseFile, roleName, entity);
    } else {
        caseEventSupport.fireBeforeCaseRoleAssignmentRemoved(caseFile.getCaseId(), caseFile, roleName, entity);
        ((CaseAssignment) caseFile).remove(roleName, entity);
        caseEventSupport.fireAfterCaseRoleAssignmentRemoved(caseFile.getCaseId(), caseFile, roleName, entity);
    }
    ksession.update(factHandle, caseFile);
    triggerRules(ksession);
    return null;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) ClassObjectFilter(org.drools.core.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseAssignment(org.kie.api.runtime.process.CaseAssignment)

Example 10 with CaseEventSupport

use of org.jbpm.casemgmt.impl.event.CaseEventSupport in project jbpm by kiegroup.

the class RemoveDataCaseFileInstanceCommand method execute.

@Override
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
    if (caseFiles.size() != 1) {
        throw new IllegalStateException("Not able to find distinct case file - found case files " + caseFiles.size());
    }
    CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
    // apply authorization
    authorizationManager.checkDataAuthorization(caseFile.getCaseId(), caseFile, variableNames);
    FactHandle factHandle = ksession.getFactHandle(caseFile);
    Map<String, Object> remove = new HashMap<>();
    variableNames.forEach(p -> remove.put(p, caseFile.getData(p)));
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    caseEventSupport.fireBeforeCaseDataRemoved(caseFile.getCaseId(), caseFile, caseFile.getDefinitionId(), remove);
    variableNames.forEach(p -> caseFile.remove(p));
    ksession.update(factHandle, caseFile);
    triggerRules(ksession);
    caseEventSupport.fireAfterCaseDataRemoved(caseFile.getCaseId(), caseFile, caseFile.getDefinitionId(), remove);
    return null;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) ClassObjectFilter(org.drools.core.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Aggregations

CaseEventSupport (org.jbpm.casemgmt.impl.event.CaseEventSupport)13 RegistryContext (org.drools.core.command.impl.RegistryContext)12 KieSession (org.kie.api.runtime.KieSession)12 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)11 Context (org.kie.api.runtime.Context)6 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)6 FactHandle (org.kie.api.runtime.rule.FactHandle)6 IdentityProvider (org.kie.internal.identity.IdentityProvider)5 ClassObjectFilter (org.drools.core.ClassObjectFilter)4 CorrelationKey (org.kie.internal.process.CorrelationKey)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)3 ProcessInstanceNotFoundException (org.jbpm.services.api.ProcessInstanceNotFoundException)3 CaseContext (org.kie.internal.runtime.manager.context.CaseContext)3 Map (java.util.Map)2 Collectors.toList (java.util.stream.Collectors.toList)2 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)2 CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)2