use of org.drools.core.marshalling.impl.ProcessMarshallerWriteContext in project jbpm by kiegroup.
the class ProcessInstanceInfo method transform.
public void transform() {
// if (processInstance == null) {
// return;
// }
ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean variablesChanged = false;
try {
ProcessMarshallerWriteContext context = new ProcessMarshallerWriteContext(baos, null, null, null, null, this.env);
context.setProcessInstanceId(processInstance.getId());
context.setState(processInstance.getState() == ProcessInstance.STATE_ACTIVE ? ProcessMarshallerWriteContext.STATE_ACTIVE : ProcessMarshallerWriteContext.STATE_COMPLETED);
String processType = ((ProcessInstanceImpl) processInstance).getProcess().getType();
saveProcessInstanceType(context, processInstance, processType);
ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType);
Object result = marshaller.writeProcessInstance(context, processInstance);
if (marshaller instanceof ProtobufRuleFlowProcessInstanceMarshaller && result != null) {
JBPMMessages.ProcessInstance _instance = (JBPMMessages.ProcessInstance) result;
PersisterHelper.writeToStreamWithHeader(context, _instance);
}
context.close();
} catch (IOException e) {
throw new IllegalArgumentException("IOException while storing process instance " + processInstance.getId() + ": " + e.getMessage(), e);
}
byte[] newByteArray = baos.toByteArray();
if (variablesChanged || !Arrays.equals(newByteArray, processInstanceByteArray)) {
this.state = processInstance.getState();
this.lastModificationDate = new Date();
this.processInstanceByteArray = newByteArray;
this.eventTypes.clear();
for (String type : processInstance.getEventTypes()) {
eventTypes.add(type);
}
}
if (!processInstance.getProcessId().equals(this.processId)) {
this.processId = processInstance.getProcessId();
}
((WorkflowProcessInstanceImpl) processInstance).setPersisted(true);
}
use of org.drools.core.marshalling.impl.ProcessMarshallerWriteContext in project drools by kiegroup.
the class JPAPlaceholderResolverStrategy method addMapping.
protected void addMapping(Object entityId, String entityType, Object entity, ObjectOutputStream context, EntityManager em) {
if (entityId instanceof Number && entity instanceof VariableEntity && context instanceof ProcessMarshallerWriteContext) {
ProcessMarshallerWriteContext processContext = (ProcessMarshallerWriteContext) context;
VariableEntity variableEntity = (VariableEntity) entity;
MappedVariable mappedVariable = new MappedVariable(((Number) entityId).longValue(), entityType, processContext.getProcessInstanceId(), processContext.getTaskId(), processContext.getWorkItemId());
if (processContext.getState() == ProcessMarshallerWriteContext.STATE_ACTIVE) {
variableEntity.addMappedVariables(mappedVariable);
} else {
MappedVariable toBeRemoved = variableEntity.findMappedVariables(mappedVariable);
if (toBeRemoved != null) {
toBeRemoved = em.find(MappedVariable.class, toBeRemoved.getMappedVarId());
em.remove(toBeRemoved);
variableEntity.removeMappedVariables(toBeRemoved);
}
}
}
}
use of org.drools.core.marshalling.impl.ProcessMarshallerWriteContext in project jbpm by kiegroup.
the class MarshalVariablesProcessEventListener method afterProcessCompleted.
public void afterProcessCompleted(ProcessCompletedEvent event) {
ObjectMarshallingStrategy[] strategies = (ObjectMarshallingStrategy[]) event.getKieRuntime().getEnvironment().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES);
VariableScopeInstance variableScope = (VariableScopeInstance) ((WorkflowProcessInstance) event.getProcessInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
Map<String, Object> variables = variableScope.getVariables();
for (Map.Entry<String, Object> variable : variables.entrySet()) {
logger.debug("Searching for applicable strategy to handle variable name '{}' value '{}'", variable.getKey(), variable.getValue());
for (ObjectMarshallingStrategy strategy : strategies) {
// are removed together with process instance
if (strategy instanceof SerializablePlaceholderResolverStrategy) {
continue;
}
if (strategy.accept(variable.getValue())) {
logger.debug("Strategy of type {} found to handle variable '{}'", strategy, variable.getKey());
try {
ProcessMarshallerWriteContext context = new ProcessMarshallerWriteContext(new ByteArrayOutputStream(), null, null, null, null, event.getKieRuntime().getEnvironment());
context.setProcessInstanceId(event.getProcessInstance().getId());
context.setState(ProcessMarshallerWriteContext.STATE_COMPLETED);
strategy.marshal(null, context, variable.getValue());
logger.debug("Variable '{}' successfully persisted by strategy {}", variable.getKey(), strategy);
break;
} catch (Exception e) {
logger.warn("Errer while storing process variable {} due to {}", variable.getKey(), e.getMessage());
logger.debug("Variable marshal error:", e);
}
}
}
}
}
use of org.drools.core.marshalling.impl.ProcessMarshallerWriteContext in project jbpm by kiegroup.
the class ContentMarshallerHelper method marshallContent.
@SuppressWarnings("unchecked")
public static byte[] marshallContent(Task task, Object o, Environment env) {
ProcessMarshallerWriteContext context;
try {
MarshallingConfigurationImpl marshallingConfigurationImpl = null;
if (env != null) {
marshallingConfigurationImpl = new MarshallingConfigurationImpl((ObjectMarshallingStrategy[]) env.get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES), false, false);
} else {
marshallingConfigurationImpl = new MarshallingConfigurationImpl(new ObjectMarshallingStrategy[] { new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) }, false, false);
}
ObjectMarshallingStrategyStore objectMarshallingStrategyStore = marshallingConfigurationImpl.getObjectMarshallingStrategyStore();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
context = new ProcessMarshallerWriteContext(stream, null, null, null, objectMarshallingStrategyStore, env);
if (task != null) {
context.setTaskId(task.getId());
context.setProcessInstanceId(task.getTaskData().getProcessInstanceId());
context.setWorkItemId(task.getTaskData().getWorkItemId());
// determine state of the task
int taskState = ProcessMarshallerWriteContext.STATE_ACTIVE;
if (task.getTaskData().getStatus() == Status.Completed || task.getTaskData().getStatus() == Status.Error || task.getTaskData().getStatus() == Status.Exited || task.getTaskData().getStatus() == Status.Failed || task.getTaskData().getStatus() == Status.Obsolete) {
taskState = ProcessMarshallerWriteContext.STATE_COMPLETED;
}
context.setState(taskState);
}
Map<String, Object> input = null;
if (o instanceof Map) {
input = (Map<String, Object>) o;
} else {
// in case there is only single variable to be stored place it into a map under special key
input = new HashMap<String, Object>();
input.put(SINGLE_VAR_KEY, o);
}
Message marshallVariable = ProtobufProcessMarshaller.marshallVariablesContainer(context, input);
PersisterHelper.writeToStreamWithHeader(context, marshallVariable);
context.close();
return stream.toByteArray();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
Aggregations