Skip to content

Instantly share code, notes, and snippets.

@hloydl
Last active July 16, 2017 14:42
Show Gist options
  • Save hloydl/f7679c78913d770a9eadcb74d28827a5 to your computer and use it in GitHub Desktop.
Save hloydl/f7679c78913d770a9eadcb74d28827a5 to your computer and use it in GitHub Desktop.
Camunda context switch
package org.camunda.bpm.extension.graphql.resolvers;
import com.coxautodev.graphql.tools.GraphQLResolver;
import org.camunda.bpm.BpmPlatform;
import org.camunda.bpm.application.ProcessApplicationContext;
import org.camunda.bpm.application.ProcessApplicationReference;
import org.camunda.bpm.engine.*;
import org.camunda.bpm.engine.history.HistoricVariableInstance;
import org.camunda.bpm.engine.identity.User;
import org.camunda.bpm.engine.impl.application.ProcessApplicationManager;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.TaskEntity;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.rest.util.ApplicationContextPathUtil;
import org.camunda.bpm.engine.variable.value.TypedValue;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class TaskEntityResolver implements GraphQLResolver<TaskEntity> {
private ProcessEngine pe;
private ProcessEngineConfigurationImpl pec;
private RepositoryService repositoryService;
private RuntimeService runtimeService;
private IdentityService identityService;
public TaskEntityResolver() {
//super(TaskEntity.class);
pe = BpmPlatform.getDefaultProcessEngine();
if (pe == null) {
pe = ProcessEngines.getDefaultProcessEngine(false);
}
pec = (ProcessEngineConfigurationImpl) pe.getProcessEngineConfiguration();
repositoryService = pe.getRepositoryService();
runtimeService = pe.getRuntimeService();
identityService = pe.getIdentityService();
}
public ProcessDefinition processDefinition(TaskEntity taskEntity) {
...
}
public ProcessInstance processInstance (TaskEntity taskEntity) {
...
}
public ExecutionEntity executionEntity(TaskEntity taskEntity) {
...
}
public User assignee(TaskEntity taskEntity) {
...
}
public String contextPath(TaskEntity taskEntity) {
...
}
public List<HistoricVariableInstance> variables(TaskEntity taskEntity) {
List<HistoricVariableInstance> variables = pe.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(taskEntity.getProcessInstanceId()).list();
String pdid = taskEntity.getProcessDefinitionId();
String executionId = taskEntity.getExecutionId();
ProcessDefinition pd = repositoryService.getProcessDefinition(pdid);
String deploymentId = pd.getDeploymentId();
ProcessApplicationManager processApplicationManager = pec.getProcessApplicationManager();
ProcessApplicationReference targetProcessApplication = processApplicationManager.getProcessApplicationForDeployment(deploymentId);
String processApplicationName = targetProcessApplication.getName(); // e.g. "creditDecisionApplication" - the name of the Java Class of the Process Application
// from https://github.com/camunda/camunda-bpm-platform/blob/master/engine/src/main/java/org/camunda/bpm/application/ProcessApplicationContext.java
// Example using a variable that is serialized with a Camunda Spin data format:
// *
// * <pre>
// * try {
// * ProcessApplicationContext.setCurrentProcessApplication("someProcessApplication");
// * runtimeService.setVariable(
// * "processInstanceId",
// * "variableName",
// * Variables.objectValue(anObject).serializationDataFormat(SerializationDataFormats.JSON).create());
// * } finally {
// * ProcessApplicationContext.clear();
// * }
// * </pre>
try {
ProcessApplicationContext.setCurrentProcessApplication(processApplicationName);
for (HistoricVariableInstance variableInstance : variables) {
String typedName = variableInstance.getTypeName();
String name = variableInstance.getName();
TypedValue typedValue = variableInstance.getTypedValue();
Object variable = runtimeService.getVariable(executionId,name);
System.out.println(variable);
}
} finally {
ProcessApplicationContext.clear();
}
return variables;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment