public void writeProcessInstance()

in jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceWriter.java [85:165]


    public void writeProcessInstance(WorkflowProcessInstanceImpl workFlow, OutputStream os) throws IOException {
        context.set(MarshallerContextName.MARSHALLER_PROCESS_INSTANCE, (RuleFlowProcessInstance) workFlow);
        LOGGER.debug("writing process instance {}", workFlow.getId());
        AbstractProcess<?> process = ((AbstractProcess<?>) context.get(MarshallerContextName.MARSHALLER_PROCESS));
        KogitoProcessRuntime runtime = process.getProcessRuntime();
        Arrays.stream(listeners).forEach(e -> e.beforeMarshallProcess(runtime, workFlow));

        KogitoProcessInstanceProtobuf.ProcessInstance.Builder instance = KogitoProcessInstanceProtobuf.ProcessInstance.newBuilder()
                .setId(workFlow.getStringId())
                .setProcessId(workFlow.getProcessId())
                .setState(workFlow.getState())
                .setProcessType(workFlow.getProcess().getType())
                .setSignalCompletion(workFlow.isSignalCompletion());

        if (workFlow.getProcessVersion() != null) {
            instance.setProcessVersion(workFlow.getProcessVersion());
        }
        if (workFlow.getStartDate() != null) {
            instance.setStartDate(workFlow.getStartDate().getTime());
        }
        if (workFlow.getDescription() != null) {
            instance.setDescription(workFlow.getDescription());
        }
        if (workFlow.getDeploymentId() != null) {
            instance.setDeploymentId(workFlow.getDeploymentId());
        }
        instance.addAllCompletedNodeIds(workFlow.getCompletedNodeIds());
        if (workFlow.getCorrelationKey() != null) {
            instance.setBusinessKey(workFlow.getCorrelationKey());
        }

        instance.setSla(buildSLAContext(workFlow.getSlaCompliance(), workFlow.getSlaDueDate(), workFlow.getSlaTimerId()));

        if (workFlow.getCancelTimerId() != null) {
            instance.setCancelTimerId(workFlow.getCancelTimerId());
        }

        if (workFlow.getParentProcessInstanceId() != null) {
            instance.setParentProcessInstanceId(workFlow.getParentProcessInstanceId());
        }
        if (workFlow.getRootProcessInstanceId() != null) {
            instance.setRootProcessInstanceId(workFlow.getRootProcessInstanceId());
        }
        if (workFlow.getRootProcessId() != null) {
            instance.setRootProcessId(workFlow.getRootProcessId());
        }
        if (workFlow.getNodeIdInError() != null) {
            instance.setErrorNodeId(workFlow.getNodeIdInError());
        }
        if (workFlow.getNodeInstanceIdInError() != null) {
            instance.setErrorNodeInstanceId(workFlow.getNodeInstanceIdInError());
        }
        if (workFlow.getErrorMessage() != null) {
            instance.setErrorMessage(workFlow.getErrorMessage());
        }
        if (workFlow.getReferenceId() != null) {
            instance.setReferenceId(workFlow.getReferenceId());
        }

        HeadersPersistentConfig headersConfig = context.get(MARSHALLER_HEADERS_CONFIG);
        if (workFlow.getHeaders() != null && headersConfig != null && headersConfig.enabled()) {
            Stream<Entry<String, List<String>>> stream = workFlow.getHeaders().entrySet().stream();
            if (headersConfig.excluded() != null && !headersConfig.excluded().isEmpty()) {
                stream = stream.filter(e -> !headersConfig.excluded().contains(e.getKey()));
            }
            instance.addAllHeaders(stream.map(e -> HeaderEntry.newBuilder().setKey(e.getKey()).addAllValue(e.getValue()).build()).collect(Collectors.toList()));
        }

        instance.addAllSwimlaneContext(buildSwimlaneContexts((SwimlaneContextInstance) workFlow.getContextInstance(SwimlaneContext.SWIMLANE_SCOPE)));

        instance.setContext(buildWorkflowContext(workFlow));

        KogitoProcessInstanceProtobuf.ProcessInstance piProtobuf = instance.build();

        String format = this.context.get(MARSHALLER_FORMAT);
        if (format != null && MARSHALLER_FORMAT_JSON.equals(format)) {
            os.write(JsonFormat.printer().usingTypeRegistry(protobufTypeRegistryFactoryInstance().create()).print(piProtobuf).getBytes());
        } else {
            piProtobuf.writeTo(os);
        }
    }