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;
}
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;
}
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;
}
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");
}
}
Aggregations