use of org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl 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;
}
use of org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl in project jbpm by kiegroup.
the class CaseFileInstanceMarshallingStrategy method marshal.
@Override
public byte[] marshal(Context context, ObjectOutputStream os, Object object) throws IOException {
logger.debug("About to marshal {}", object);
CaseFileInstanceImpl caseFile = (CaseFileInstanceImpl) object;
Map<String, Object> caseFileContent = new HashMap<>();
caseFileContent.put(CASE_ID_KEY, caseFile.getCaseId());
caseFileContent.put(CASE_DEF_ID_KEY, caseFile.getDefinitionId());
caseFileContent.put(CASE_START_KEY, caseFile.getCaseStartDate());
caseFileContent.put(CASE_END_KEY, caseFile.getCaseEndDate());
caseFileContent.put(CASE_REOPEN_KEY, caseFile.getCaseReopenDate());
caseFileContent.put(CASE_ROLE_ASSIGNMENTS_KEY, new HashMap<>(caseFile.getRolesAssignments()));
caseFileContent.put(CASE_COMMENTS_KEY, new ArrayList<>(caseFile.getComments()));
logger.debug("CaseFileContent before case file data is {}", caseFileContent);
List<SerializedContent> caseDataContent = new ArrayList<>();
caseFileContent.put(CASE_DATA_KEY, caseDataContent);
// transform with various strategies data that belong to a case
for (Entry<String, Object> dataEntry : caseFile.getData().entrySet()) {
byte[] content = null;
String marshallerName = null;
logger.debug("About to find marshaller for {}", dataEntry.getValue());
for (ObjectMarshallingStrategy marshaller : marshallersByName.values()) {
if (marshaller.accept(dataEntry.getValue())) {
content = marshaller.marshal(context, os, dataEntry.getValue());
marshallerName = marshaller.getClass().getName();
logger.debug("Object {} marshalled by {}", dataEntry.getValue(), marshallerName);
break;
}
}
SerializedContent serializedContent = new SerializedContent(marshallerName, dataEntry.getKey(), content);
caseDataContent.add(serializedContent);
logger.debug("Serialized content for object {} is {}", dataEntry.getValue(), serializedContent);
}
caseFileContent.put(CASE_DATA_RESTRICTIONS_KEY, new HashMap<>(caseFile.getAccessRestrictions()));
caseFileContent.put(CASE_PARENT_INSTANCE_ID_KEY, caseFile.getParentInstanceId());
caseFileContent.put(CASE_PARENT_WORK_ITEM_ID_KEY, caseFile.getParentWorkItemId());
byte[] caseFileBytes = caseFileMarshaller.marshal(context, os, caseFileContent);
logger.debug("Content of the case file instance after marshaller is of length {}", (caseFileBytes == null ? 0 : caseFileBytes.length));
return caseFileBytes;
}
use of org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl 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.impl.model.instance.CaseFileInstanceImpl 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.impl.model.instance.CaseFileInstanceImpl in project jbpm by kiegroup.
the class CaseServiceImpl method newCaseFileInstanceWithRestrictions.
@Override
public CaseFileInstance newCaseFileInstanceWithRestrictions(String deploymentId, String caseDefinition, Map<String, Object> data, Map<String, List<String>> accessRestrictions) {
CaseDefinition def = caseRuntimeDataService.getCase(deploymentId, caseDefinition);
if (def == null) {
throw new CaseDefinitionNotFoundException("Case definition " + caseDefinition + " does not exist in deployment " + deploymentId);
}
CaseFileInstanceImpl caseFile = new CaseFileInstanceImpl(caseDefinition, data);
caseFile.setupRoles(def.getCaseRoles());
Map<String, List<String>> combinedAccessRestrictions = def.getDataAccessRestrictions();
if (accessRestrictions != null) {
combinedAccessRestrictions.putAll(accessRestrictions);
}
caseFile.setAccessRestrictions(combinedAccessRestrictions);
return caseFile;
}
Aggregations