Skip to content

Commit

Permalink
JBPM-4646 - Case management - support for reopening the case, refacto…
Browse files Browse the repository at this point in the history
…r after move of CaseData and CaseAssignment, support for new methods of ProcessContext (#629)
  • Loading branch information
mswiderski committed Oct 7, 2016
1 parent f745036 commit 1066b47
Show file tree
Hide file tree
Showing 21 changed files with 650 additions and 17 deletions.
@@ -0,0 +1,35 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jbpm.casemgmt.api;

/**
* Thrown then reopening a case that is already started/reopened
*
*/
public class CaseActiveException extends RuntimeException {

private static final long serialVersionUID = -6105558767536810447L;

public CaseActiveException(String message, Throwable cause) {
super(message, cause);
}

public CaseActiveException(String message) {
super(message);
}

}
Expand Up @@ -115,6 +115,29 @@ public interface CaseService {
*/
void destroyCase(String caseId) throws CaseNotFoundException;

/**
* Reopens case given by case id by starting another instance of case definition. It will inherit all data
* from case file that was available in before case was closed/canceled.
* @param caseId unique case id in the format PREFIX-GENERATED_ID as described on startCase method
* @param deploymentId deployment id of project that case definition belongs to
* @param caseDefinitionId id of case definition
* @throws CaseNotFoundException thrown in case case was not found with given id
* @throws CaseActiveException thrown when case is still active
*/
void reopenCase(String caseId, String deploymentId, String caseDefinitionId) throws CaseNotFoundException;

/**
* Reopens case given by case id by starting another instance of case definition. It will inherit all data
* from case file that was available in before case was closed/canceled.
* @param caseId unique case id in the format PREFIX-GENERATED_ID as described on startCase method
* @param deploymentId deployment id of project that case definition belongs to
* @param caseDefinitionId id of case definition
* @param data additional data to be set on case file
* @throws CaseNotFoundException thrown in case case was not found with given id
* @throws CaseActiveException thrown when case is still active
*/
void reopenCase(String caseId, String deploymentId, String caseDefinitionId, Map<String, Object> data) throws CaseNotFoundException;

/*
* dynamic case operations section
*/
Expand Down
Expand Up @@ -36,6 +36,10 @@ public interface CaseEventListener extends EventListener {
void beforeCaseDestroyed(CaseDestroyEvent event);

void afterCaseDestroyed(CaseDestroyEvent event);

void beforeCaseReopen(CaseReopenEvent event);

void afterCaseReopen(CaseReopenEvent event);

void beforeCaseCommentAdded(CaseCommentEvent event);

Expand Down
@@ -0,0 +1,80 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jbpm.casemgmt.api.event;

import java.util.Map;

/**
* Represents occurrence of start case operation
*/
public class CaseReopenEvent extends CaseEvent {

private String deploymentId;
private String caseDefinitionId;
private Map<String, Object> data;
private Long processInstanceId;

public CaseReopenEvent(String caseId, String deploymentId, String caseDefinitionId, Map<String, Object> data) {
super(caseId);
this.deploymentId = deploymentId;
this.caseDefinitionId = caseDefinitionId;
this.data = data;
}

public CaseReopenEvent(String caseId, String deploymentId, String caseDefinitionId, Map<String, Object> data, Long processInstanceId) {
super(caseId);
this.deploymentId = deploymentId;
this.caseDefinitionId = caseDefinitionId;
this.data = data;
this.processInstanceId = processInstanceId;
}

/**
* Returns deployment id that case belongs to
*/
public String getDeploymentId() {
return deploymentId;
}

/**
* Returns case definition id
*/
public String getCaseDefinitionId() {
return caseDefinitionId;
}

/**
* Returns case file associated with the case
*/
public Map<String, Object> getData() {
return data;
}

/**
* Returns process instance id of the case process instance
*/
public Long getProcessInstanceId() {
return processInstanceId;
}

@Override
public String toString() {
return "StartCaseEvent [deploymentId=" + deploymentId + ", caseDefinitionId=" + caseDefinitionId + ", caseId=" + getCaseId() + "]";
}


}
Expand Up @@ -19,7 +19,7 @@
import java.util.Date;
import java.util.Map;

import org.kie.internal.process.CaseData;
import org.kie.api.runtime.process.CaseData;

/**
* Represent contextual data of a given Case
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.stream.Collectors;

import org.drools.core.ClassObjectFilter;
import org.jbpm.casemgmt.api.CaseActiveException;
import org.jbpm.casemgmt.api.CaseNotFoundException;
import org.jbpm.casemgmt.api.CaseRuntimeDataService;
import org.jbpm.casemgmt.api.CaseService;
Expand All @@ -48,6 +49,7 @@
import org.jbpm.casemgmt.impl.command.CaseCommentCommand;
import org.jbpm.casemgmt.impl.command.ModifyRoleAssignmentCommand;
import org.jbpm.casemgmt.impl.command.RemoveDataCaseFileInstanceCommand;
import org.jbpm.casemgmt.impl.command.ReopenCaseCommand;
import org.jbpm.casemgmt.impl.command.StartCaseCommand;
import org.jbpm.casemgmt.impl.dynamic.HumanTaskSpecification;
import org.jbpm.casemgmt.impl.dynamic.WorkItemTaskSpecification;
Expand Down Expand Up @@ -196,6 +198,28 @@ public void destroyCase(String caseId) throws CaseNotFoundException {
processService.execute(pi.getDeploymentId(), ProcessInstanceIdContext.get(pi.getId()), new CancelCaseCommand(caseId, processService, runtimeDataService, true));
}


@Override
public void reopenCase(String caseId, String deploymentId, String caseDefinitionId) throws CaseNotFoundException {
reopenCase(caseId, deploymentId, caseDefinitionId, new HashMap<>());

}

@Override
public void reopenCase(String caseId, String deploymentId, String caseDefinitionId, Map<String, Object> data) throws CaseNotFoundException {
ProcessInstanceDesc pi = runtimeDataService.getProcessInstanceByCorrelationKey(correlationKeyFactory.newCorrelationKey(caseId));
if (pi != null) {
throw new CaseActiveException("Case with id " + caseId + " is still active and cannot be reopened");
}
logger.debug("About to reopen case {} by starting process instance {} from deployment {} with additional data {}",
caseId, caseDefinitionId, deploymentId, data);

processService.execute(deploymentId, CaseContext.get(caseId), new ReopenCaseCommand(caseId, deploymentId, caseDefinitionId, data, processService));

}



/*
* Dynamic operations on a case
*/
Expand Down Expand Up @@ -485,5 +509,4 @@ protected CaseEventSupport getCaseEventSupport(String deploymentId) {
return emptyCaseEventSupport;
}


}
Expand Up @@ -23,10 +23,10 @@
import org.jbpm.casemgmt.api.model.instance.CaseFileInstance;
import org.jbpm.casemgmt.impl.event.CaseEventSupport;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.process.CaseAssignment;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.api.task.model.OrganizationalEntity;
import org.kie.internal.command.Context;
import org.kie.internal.process.CaseAssignment;

/**
* Modifies case role assignments
Expand Down
@@ -0,0 +1,97 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jbpm.casemgmt.impl.command;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.drools.core.ClassObjectFilter;
import org.drools.core.command.impl.KnowledgeCommandContext;
import org.jbpm.casemgmt.api.CaseNotFoundException;
import org.jbpm.casemgmt.api.model.instance.CaseFileInstance;
import org.jbpm.casemgmt.impl.event.CaseEventSupport;
import org.jbpm.services.api.ProcessService;
import org.kie.api.runtime.EnvironmentName;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.KieInternalServices;
import org.kie.internal.command.Context;
import org.kie.internal.process.CorrelationKey;
import org.kie.internal.process.CorrelationKeyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReopenCaseCommand extends CaseCommand<Void> {

private static final long serialVersionUID = 6811181095390934146L;

private static final Logger logger = LoggerFactory.getLogger(ReopenCaseCommand.class);

private static CorrelationKeyFactory correlationKeyFactory = KieInternalServices.Factory.get().newCorrelationKeyFactory();

private String caseId;
private String deploymentId;
private String caseDefinitionId;
private Map<String, Object> data;

private transient ProcessService processService;

public ReopenCaseCommand(String caseId, String deploymentId, String caseDefinitionId, Map<String, Object> data, ProcessService processService) {
this.caseId = caseId;
this.deploymentId = deploymentId;
this.caseDefinitionId = caseDefinitionId;
this.data = data;
this.processService = processService;
}

@Override
public Void execute(Context context) {

CaseEventSupport caseEventSupport = getCaseEventSupport(context);
caseEventSupport.fireBeforeCaseReopened(caseId, deploymentId, caseDefinitionId, data);
KieSession ksession = ((KnowledgeCommandContext) context).getKieSession();

if (data != null && !data.isEmpty()) {
logger.debug("Updating case file in working memory");
Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
if (caseFiles.size() == 0) {
throw new CaseNotFoundException("Case with id " + caseId + " was not found");
}
CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
FactHandle factHandle = ksession.getFactHandle(caseFile);

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, deploymentId, caseDefinitionId, data, processInstanceId);
return null;
}

public void setProcessService(ProcessService processService) {
this.processService = processService;
}

}
Expand Up @@ -28,6 +28,7 @@
import org.jbpm.casemgmt.api.event.CaseDynamicSubprocessEvent;
import org.jbpm.casemgmt.api.event.CaseDynamicTaskEvent;
import org.jbpm.casemgmt.api.event.CaseEventListener;
import org.jbpm.casemgmt.api.event.CaseReopenEvent;
import org.jbpm.casemgmt.api.event.CaseRoleAssignmentEvent;
import org.jbpm.casemgmt.api.event.CaseStartEvent;
import org.jbpm.casemgmt.api.model.instance.CaseFileInstance;
Expand Down Expand Up @@ -126,7 +127,35 @@ public void fireAfterCaseDestroyed(String caseId, List<Long> processInstanceIds)
iter.next().afterCaseDestroyed(event);
} while (iter.hasNext());
}
}
}

/*
* fire*CaseReopened
*/

public void fireBeforeCaseReopened(String caseId, String deploymentId, String caseDefinitionId, Map<String, Object> data) {
final Iterator<CaseEventListener> iter = getEventListenersIterator();

if (iter.hasNext()) {
final CaseReopenEvent event = new CaseReopenEvent(caseId, deploymentId, caseDefinitionId, data);

do{
iter.next().beforeCaseReopen(event);
} while (iter.hasNext());
}
}

public void fireAfterCaseReopened(String caseId, String deploymentId, String caseDefinitionId, Map<String, Object> data, long processInstanceId) {
final Iterator<CaseEventListener> iter = getEventListenersIterator();

if (iter.hasNext()) {
final CaseReopenEvent event = new CaseReopenEvent(caseId, deploymentId, caseDefinitionId, data, processInstanceId);

do {
iter.next().afterCaseReopen(event);
} while (iter.hasNext());
}
}

/*
* fire*CaseCommentAdded
Expand Down
Expand Up @@ -32,8 +32,8 @@
import org.jbpm.casemgmt.api.model.instance.CaseFileInstance;
import org.jbpm.casemgmt.api.model.instance.CaseRoleInstance;
import org.jbpm.casemgmt.api.model.instance.CommentInstance;
import org.kie.api.runtime.process.CaseAssignment;
import org.kie.api.task.model.OrganizationalEntity;
import org.kie.internal.process.CaseAssignment;

/*
* Implementation note: since the CaseFileInstanceImpl will be marshalled/unmarshalled by
Expand Down
Expand Up @@ -154,7 +154,7 @@ public void testGetCaseDefinitions() {
milestone = mappedMilestones.get("Milestone2");
assertEquals("_5", milestone.getId());
assertEquals("Milestone2", milestone.getName());
assertEquals("org.kie.internal.process.CaseData(data.get(\"dataComplete\") == true)", milestone.getAchievementCondition());
assertEquals("org.kie.api.runtime.process.CaseData(data.get(\"dataComplete\") == true)", milestone.getAchievementCondition());
assertEquals(false, milestone.isMandatory());

assertNotNull(caseDef.getCaseStages());
Expand Down

0 comments on commit 1066b47

Please sign in to comment.