use of org.jbpm.casemgmt.api.CaseNotFoundException in project jbpm by kiegroup.
the class CancelCaseCommand 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");
}
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.fireBeforeCaseCancelled(caseId, caseFile, processInstanceIds);
logger.debug("Case {} consists of following process instances (ids) {}", caseId, processInstanceIds);
processService.abortProcessInstances(processInstanceIds);
caseEventSupport.fireAfterCaseCancelled(caseId, caseFile, processInstanceIds);
if (destroy) {
RuntimeManager runtimeManager = getRuntimeManager(context);
if (runtimeManager instanceof PerCaseRuntimeManager) {
caseEventSupport.fireBeforeCaseDestroyed(caseId, caseFile, processInstanceIds);
logger.debug("Case {} aborted, destroying case data including per case runtime engine (including working memory)", caseId);
((PerCaseRuntimeManager) runtimeManager).destroyCase(CaseContext.get(caseId));
caseEventSupport.fireAfterCaseDestroyed(caseId, caseFile, processInstanceIds);
}
}
return null;
}
use of org.jbpm.casemgmt.api.CaseNotFoundException in project jbpm by kiegroup.
the class CaseServiceImplTest method testStartThenReopenDestroyedCase.
@Test
public void testStartThenReopenDestroyedCase() {
Map<String, Object> data = new HashMap<>();
data.put("name", "my first case");
CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, data);
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, caseFile);
assertNotNull(caseId);
assertEquals(FIRST_CASE_ID, caseId);
try {
CaseInstance cInstance = caseService.getCaseInstance(caseId);
assertNotNull(cInstance);
assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
assertEquals("my first case", cInstance.getCaseDescription());
caseService.destroyCase(caseId);
try {
caseService.reopenCase(caseId, deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID);
fail("Not allowed to reopen destroyed case");
} catch (CaseNotFoundException e) {
// expected
}
caseId = null;
} catch (Exception e) {
logger.error("Unexpected error {}", e.getMessage(), e);
fail("Unexpected exception " + e.getMessage());
} finally {
if (caseId != null) {
caseService.cancelCase(caseId);
}
}
}
use of org.jbpm.casemgmt.api.CaseNotFoundException 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());
}
}
use of org.jbpm.casemgmt.api.CaseNotFoundException in project jbpm by kiegroup.
the class CaseRuntimeDataServiceImpl method getCaseInstanceStages.
@Override
public Collection<CaseStageInstance> getCaseInstanceStages(String caseId, boolean activeOnly, QueryContext queryContext) {
ProcessInstanceDesc pi = runtimeDataService.getProcessInstanceByCorrelationKey(correlationKeyFactory.newCorrelationKey(caseId));
if (pi == null || !pi.getState().equals(ProcessInstance.STATE_ACTIVE)) {
throw new CaseNotFoundException("No case instance found with id " + caseId + " or it's not active anymore");
}
CaseDefinition caseDef = getCase(pi.getDeploymentId(), pi.getProcessId());
List<CaseStageInstance> stages = internalGetCaseStages(caseDef, caseId, activeOnly, queryContext);
return applyPagination(stages, queryContext);
}
use of org.jbpm.casemgmt.api.CaseNotFoundException in project jbpm by kiegroup.
the class CaseRuntimeDataServiceImpl method getCaseInstanceMilestones.
@Override
public Collection<CaseMilestoneInstance> getCaseInstanceMilestones(String caseId, boolean achievedOnly, QueryContext queryContext) {
ProcessInstanceDesc pi = runtimeDataService.getProcessInstanceByCorrelationKey(correlationKeyFactory.newCorrelationKey(caseId));
if (pi == null || !pi.getState().equals(ProcessInstance.STATE_ACTIVE)) {
throw new CaseNotFoundException("No case instance found with id " + caseId + " or it's not active anymore");
}
CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
Collection<org.jbpm.services.api.model.NodeInstanceDesc> nodes = runtimeDataService.getNodeInstancesByCorrelationKeyNodeType(correlationKey, Arrays.asList(ProcessInstance.STATE_ACTIVE), Arrays.asList("MilestoneNode"), queryContext);
Collection<Long> completedNodes = nodes.stream().filter(n -> ((NodeInstanceDesc) n).getType() == 1).map(n -> n.getId()).collect(toList());
Predicate<org.jbpm.services.api.model.NodeInstanceDesc> filterNodes = null;
if (achievedOnly) {
filterNodes = n -> ((NodeInstanceDesc) n).getType() == 1;
} else {
filterNodes = n -> ((NodeInstanceDesc) n).getType() == 0;
}
List<String> foundMilestones = new ArrayList<>();
List<CaseMilestoneInstance> milestones = nodes.stream().filter(filterNodes).map(n -> {
foundMilestones.add(n.getName());
return new CaseMilestoneInstanceImpl(String.valueOf(n.getId()), n.getName(), completedNodes.contains(n.getId()), n.getDataTimeStamp());
}).collect(toList());
if (!achievedOnly) {
// add other milestones that are present in the definition
CaseDefinition caseDef = getCase(pi.getDeploymentId(), pi.getProcessId());
caseDef.getCaseMilestones().stream().filter(cm -> !foundMilestones.contains(cm.getName())).map(cm -> new CaseMilestoneInstanceImpl(cm.getId(), cm.getName(), false, null)).forEach(cmi -> milestones.add(cmi));
}
return applyPagination(milestones, queryContext);
}
Aggregations