Search in sources :

Example 1 with ProcessMarshallerWriteContext

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);
}
Also used : ProtobufRuleFlowProcessInstanceMarshaller(org.jbpm.marshalling.impl.ProtobufRuleFlowProcessInstanceMarshaller) ProcessInstanceMarshaller(org.jbpm.marshalling.impl.ProcessInstanceMarshaller) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) JBPMMessages(org.jbpm.marshalling.impl.JBPMMessages) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Date(java.util.Date) ProtobufRuleFlowProcessInstanceMarshaller(org.jbpm.marshalling.impl.ProtobufRuleFlowProcessInstanceMarshaller) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) PersistentProcessInstance(org.jbpm.persistence.api.PersistentProcessInstance) ProcessMarshallerWriteContext(org.drools.core.marshalling.impl.ProcessMarshallerWriteContext)

Example 2 with ProcessMarshallerWriteContext

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);
            }
        }
    }
}
Also used : ProcessMarshallerWriteContext(org.drools.core.marshalling.impl.ProcessMarshallerWriteContext)

Example 3 with ProcessMarshallerWriteContext

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);
                }
            }
        }
    }
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) ProcessMarshallerWriteContext(org.drools.core.marshalling.impl.ProcessMarshallerWriteContext)

Example 4 with ProcessMarshallerWriteContext

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;
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) ObjectMarshallingStrategyStore(org.kie.api.marshalling.ObjectMarshallingStrategyStore) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) Message(com.google.protobuf.Message) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) ProcessMarshallerWriteContext(org.drools.core.marshalling.impl.ProcessMarshallerWriteContext)

Aggregations

ProcessMarshallerWriteContext (org.drools.core.marshalling.impl.ProcessMarshallerWriteContext)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)2 Map (java.util.Map)2 SerializablePlaceholderResolverStrategy (org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy)2 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)2 Message (com.google.protobuf.Message)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)1 JBPMMessages (org.jbpm.marshalling.impl.JBPMMessages)1 ProcessInstanceMarshaller (org.jbpm.marshalling.impl.ProcessInstanceMarshaller)1 ProtobufRuleFlowProcessInstanceMarshaller (org.jbpm.marshalling.impl.ProtobufRuleFlowProcessInstanceMarshaller)1 PersistentProcessInstance (org.jbpm.persistence.api.PersistentProcessInstance)1 VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)1 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)1 ObjectMarshallingStrategyStore (org.kie.api.marshalling.ObjectMarshallingStrategyStore)1 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)1