Search in sources :

Example 1 with MarshallerReaderContext

use of io.automatiko.engine.workflow.marshalling.impl.MarshallerReaderContext in project automatiko-engine by automatiko-io.

the class ProcessInstanceMarshaller method importWorkflowProcessInstance.

public WorkflowProcessInstance importWorkflowProcessInstance(String header, String data, List<Map<String, String>> timers, Process<?> process) {
    Map<String, io.automatiko.engine.api.definition.process.Process> processes = new HashMap<String, io.automatiko.engine.api.definition.process.Process>();
    io.automatiko.engine.api.definition.process.Process p = ((AbstractProcess<?>) process).process();
    // this can include version number in the id
    processes.put(process.id(), p);
    // this is raw process id as defined in bpmn or so
    processes.put(p.getId(), p);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0])) {
        Map<String, Object> localEnv = new HashMap<String, Object>(env);
        localEnv.put("_import_", true);
        localEnv.put("_services_", ((AbstractProcess<?>) process).services());
        AutomatikoMessages.ProcessInstance.Builder builder = AutomatikoMessages.ProcessInstance.newBuilder();
        JsonFormat.parser().merge(data, builder);
        AutomatikoMessages.Header.Builder headerBuilder = AutomatikoMessages.Header.newBuilder();
        JsonFormat.parser().merge(header, headerBuilder);
        MarshallerReaderContext context = new MarshallerReaderContext(bais, null, processes, localEnv) {

            @Override
            protected void readStreamHeader() throws IOException, StreamCorruptedException {
            }
        };
        context.parameterObject = builder.build();
        PersisterHelper.loadStrategiesIndex(context, headerBuilder.build());
        io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(builder.getProcessType());
        WorkflowProcessInstance pi = (WorkflowProcessInstance) marshaller.readProcessInstance(context);
        context.close();
        return pi;
    } catch (Exception e) {
        throw new RuntimeException("Error while unmarshalling process instance", e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Process(io.automatiko.engine.api.workflow.Process) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) MarshallerReaderContext(io.automatiko.engine.workflow.marshalling.impl.MarshallerReaderContext) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) StringExportedProcessInstance(io.automatiko.engine.workflow.StringExportedProcessInstance) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 2 with MarshallerReaderContext

use of io.automatiko.engine.workflow.marshalling.impl.MarshallerReaderContext in project automatiko-engine by automatiko-io.

the class ProcessInstanceMarshaller method unmarshallWorkflowProcessInstance.

public WorkflowProcessInstance unmarshallWorkflowProcessInstance(byte[] data, Process<?> process) {
    Map<String, io.automatiko.engine.api.definition.process.Process> processes = new HashMap<String, io.automatiko.engine.api.definition.process.Process>();
    io.automatiko.engine.api.definition.process.Process p = ((AbstractProcess<?>) process).process();
    // this can include version number in the id
    processes.put(process.id(), p);
    // this is raw process id as defined in bpmn or so
    processes.put(p.getId(), p);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
        MarshallerReaderContext context = new MarshallerReaderContext(bais, null, processes, this.env);
        ObjectInputStream stream = context.stream;
        String processInstanceType = stream.readUTF();
        io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processInstanceType);
        WorkflowProcessInstance pi = (WorkflowProcessInstance) marshaller.readProcessInstance(context);
        context.close();
        return pi;
    } catch (Exception e) {
        throw new RuntimeException("Error while unmarshalling process instance", e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) Process(io.automatiko.engine.api.workflow.Process) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) MarshallerReaderContext(io.automatiko.engine.workflow.marshalling.impl.MarshallerReaderContext) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)2 Process (io.automatiko.engine.api.workflow.Process)2 AbstractProcess (io.automatiko.engine.workflow.AbstractProcess)2 MarshallerReaderContext (io.automatiko.engine.workflow.marshalling.impl.MarshallerReaderContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 StreamCorruptedException (java.io.StreamCorruptedException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ExportedProcessInstance (io.automatiko.engine.api.workflow.ExportedProcessInstance)1 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)1 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)1 StringExportedProcessInstance (io.automatiko.engine.workflow.StringExportedProcessInstance)1 ObjectInputStream (java.io.ObjectInputStream)1