Search in sources :

Example 6 with CaseFileInstanceImpl

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;
}
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 CaseFileInstanceImpl

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) ArrayList(java.util.ArrayList)

Example 8 with CaseFileInstanceImpl

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());
    }
}
Also used : PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ArrayList(java.util.ArrayList) List(java.util.List) CaseService(org.jbpm.casemgmt.api.CaseService)

Example 9 with CaseFileInstanceImpl

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;
}
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 10 with CaseFileInstanceImpl

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;
}
Also used : CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) List(java.util.List) ArrayList(java.util.ArrayList) CaseDefinitionNotFoundException(org.jbpm.casemgmt.api.CaseDefinitionNotFoundException)

Aggregations

CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)16 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 List (java.util.List)6 CaseDefinition (org.jbpm.casemgmt.api.model.CaseDefinition)5 CaseDefinitionNotFoundException (org.jbpm.casemgmt.api.CaseDefinitionNotFoundException)4 Map (java.util.Map)3 ClassObjectFilter (org.drools.core.ClassObjectFilter)3 RegistryContext (org.drools.core.command.impl.RegistryContext)3 CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)3 CaseEventSupport (org.jbpm.casemgmt.impl.event.CaseEventSupport)3 KieSession (org.kie.api.runtime.KieSession)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 Collection (java.util.Collection)2 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 CaseRoleInstance (org.jbpm.casemgmt.api.model.instance.CaseRoleInstance)2 CommentInstance (org.jbpm.casemgmt.api.model.instance.CommentInstance)2 CommentInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl)2