Search in sources :

Example 11 with CaseNotFoundException

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

the class CaseServiceImpl method internalGetCaseFileInstance.

/*
     * internal methods
     */
@SuppressWarnings("unchecked")
protected CaseFileInstance internalGetCaseFileInstance(String caseId, String deploymentId) {
    logger.debug("Retrieving case file from working memory for case " + caseId);
    Collection<CaseFileInstance> caseFiles = (Collection<CaseFileInstance>) processService.execute(deploymentId, CaseContext.get(caseId), commandsFactory.newGetObjects(new ClassObjectFilter(CaseFileInstance.class)));
    if (caseFiles.size() == 0) {
        throw new CaseNotFoundException("Case with id " + caseId + " was not found");
    } else if (caseFiles.size() == 1) {
        CaseFileInstance caseFile = caseFiles.iterator().next();
        logger.debug("Single case file {} found in working memory", caseFile);
        // apply authorization
        Map<String, Object> filteredData = authorizationManager.filterByDataAuthorization(caseId, caseFile, caseFile.getData());
        ((CaseFileInstanceImpl) caseFile).setData(filteredData);
        for (Object variable : caseFile.getData().values()) {
            if (variable instanceof LazyLoaded<?>) {
                ((LazyLoaded<?>) variable).load();
            }
        }
        return caseFile;
    }
    logger.warn("Multiple case files found in working memory (most likely not using PER_CASE strategy), trying to filter out...");
    CaseFileInstance caseFile = caseFiles.stream().filter(cf -> cf.getCaseId().equals(caseId)).findFirst().orElse(null);
    logger.warn("Case file {} after filtering {}", caseFile, (caseFile == null ? "not found" : "found"));
    if (caseFile != null) {
        // apply authorization
        Map<String, Object> filteredData = authorizationManager.filterByDataAuthorization(caseId, caseFile, caseFile.getData());
        ((CaseFileInstanceImpl) caseFile).setData(filteredData);
        for (Object variable : caseFile.getData().values()) {
            if (variable instanceof LazyLoaded<?>) {
                ((LazyLoaded<?>) variable).load();
            }
        }
    }
    return caseFile;
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ClassObjectFilter(org.drools.core.ClassObjectFilter) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) Collection(java.util.Collection) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) Map(java.util.Map) HashMap(java.util.HashMap) LazyLoaded(org.kie.internal.utils.LazyLoaded)

Example 12 with CaseNotFoundException

use of org.jbpm.casemgmt.api.CaseNotFoundException 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 13 with CaseNotFoundException

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

the class CaseServiceImpl method startCase.

@Override
public String startCase(String deploymentId, String caseDefinitionId, CaseFileInstance caseFile) {
    CaseDefinition caseDef = caseRuntimeDataService.getCase(deploymentId, caseDefinitionId);
    if (caseDef == null) {
        throw new CaseNotFoundException("Case definition " + caseDefinitionId + " not found");
    }
    String caseId = caseIdGenerator.generate(caseDef.getIdentifierPrefix(), (caseFile == null ? new HashMap<>() : caseFile.getData()));
    logger.debug("Generated case id {} for case definition id {}", caseId, caseDefinitionId);
    if (caseFile == null) {
        caseFile = new CaseFileInstanceImpl(caseId, caseDefinitionId);
        ((CaseFileInstanceImpl) caseFile).setupRoles(caseDef.getCaseRoles());
        logger.debug("CaseFile was not given, creating new empty one.");
    } else {
        ((CaseFileInstanceImpl) caseFile).setCaseId(caseId);
        logger.debug("CaseFile {} was given, associating it with case {}", caseFile, caseId);
    }
    // If owner is provided in the case file use that, otherwise default to current logged in user.
    boolean hasOwner = ((CaseFileInstanceImpl) caseFile).getAssignments().stream().anyMatch(role -> role.getRoleName().equals(AuthorizationManager.OWNER_ROLE));
    if (hasOwner == false) {
        ((CaseFileInstanceImpl) caseFile).assignOwner(newUser(identityProvider.getName()));
    }
    processService.execute(deploymentId, CaseContext.get(caseId), new StartCaseCommand(identityProvider, caseId, deploymentId, caseDefinitionId, caseFile, processService));
    return caseId;
}
Also used : StartCaseCommand(org.jbpm.casemgmt.impl.command.StartCaseCommand) CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException)

Example 14 with CaseNotFoundException

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

the class AbstractCaseServicesBaseTest method assertCaseInstanceActive.

public void assertCaseInstanceActive(String caseId) {
    try {
        CaseInstance caseInstance = caseService.getCaseInstance(caseId);
        assertThat(caseInstance).isNotNull();
        assertThat(caseInstance.getStatus()).isEqualTo(CaseStatus.OPEN.getId());
    } catch (CaseNotFoundException ex) {
        fail("Case instance is not active");
    }
}
Also used : CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException)

Aggregations

CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)14 HashMap (java.util.HashMap)6 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)6 ProcessInstanceDesc (org.jbpm.services.api.model.ProcessInstanceDesc)6 List (java.util.List)5 CaseInstance (org.jbpm.casemgmt.api.model.instance.CaseInstance)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 CaseDefinition (org.jbpm.casemgmt.api.model.CaseDefinition)4 QueryContext (org.kie.api.runtime.query.QueryContext)4 Collectors.toList (java.util.stream.Collectors.toList)3 CaseActiveException (org.jbpm.casemgmt.api.CaseActiveException)3 CaseStageInstance (org.jbpm.casemgmt.api.model.instance.CaseStageInstance)3 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)3 RuntimeDataService (org.jbpm.services.api.RuntimeDataService)3 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)3 KieInternalServices (org.kie.internal.KieInternalServices)3 IdentityProvider (org.kie.internal.identity.IdentityProvider)3 CorrelationKey (org.kie.internal.process.CorrelationKey)3 CorrelationKeyFactory (org.kie.internal.process.CorrelationKeyFactory)3