All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.vertxup.workflow.service.FlowService Maven / Gradle / Ivy

The newest version!
package cn.vertxup.workflow.service;

import cn.vertxup.workflow.domain.tables.daos.WFlowDao;
import io.horizon.spi.ui.Form;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.tp.workflow.refine.Wf;
import io.vertx.tp.workflow.uca.camunda.Io;
import io.vertx.up.eon.KName;
import io.vertx.up.unity.Ux;
import io.vertx.up.util.Ut;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.model.bpmn.instance.StartEvent;

/**
 * @author Lang
 */
public class FlowService implements FlowStub {
    @Override
    public Future fetchFlow(final String definitionKey, final String sigma) {
        // 1. Fetch workflow definition from Camunda
        final Io io = Io.ioEventStart();
        final JsonObject workflowJ = new JsonObject();
        final ProcessDefinition definition = io.inProcess(definitionKey);
        workflowJ.mergeIn(Wf.outBpmn(definition), true);
        return io.children(definition.getId())
            .compose(starts -> io.out(workflowJ, starts))
            // Fetch X_FLOW
            .compose(definitionJ -> this.flowInternal(definitionKey, sigma).compose(workflow -> {
                // Combine result
                final JsonObject response = new JsonObject();
                response.mergeIn(workflow);
                response.mergeIn(definitionJ);
                // configuration should be JsonObject type
                Ut.valueToJObject(
                    response,
                    KName.Flow.CONFIG_START,
                    KName.Flow.CONFIG_AUTHORIZED,
                    KName.Flow.CONFIG_END,
                    KName.Flow.CONFIG_RUN,
                    KName.Flow.CONFIG_GENERATE,
                    KName.Flow.UI_CONFIG,
                    KName.Flow.UI_ASSIST,
                    KName.Flow.UI_LINKAGE
                );
                // Simply the linkage configuration for sharing global part
                final JsonObject linkageJ = Ut.valueJObject(response, KName.Flow.UI_LINKAGE);
                response.put(KName.Flow.UI_LINKAGE, Wf.outLinkage(linkageJ));
                return Ux.future(response);
            }));
    }

    @Override
    public Future fetchForm(final ProcessDefinition definition,
                                        final String sigma) {
        // Io Building
        final Io ioForm = Io.ioForm();
        final Io ioFlow = Io.ioFlow();

        final JsonObject response = new JsonObject();
        return Ux.future(definition)


            // Form Fetch -> Out
            .compose(ioForm::start)
            .compose(formInput -> this.formInternal(formInput, sigma))
            .compose(formOutput -> ioForm.out(response, formOutput))


            // Workflow Fetch -> Out
            .compose(nil -> ioFlow.start(definition))
            .compose(workflow -> ioFlow.out(response, workflow));
    }

    @Override
    public Future fetchForm(final ProcessInstance instance, final Task task,
                                        final String sigma) {
        final JsonObject response = new JsonObject();
        final Io ioForm = Io.ioForm();
        final Io ioFlow = Io.ioFlow();
        return Ux.future(task)
            // Fetch form data
            .compose(ioForm::run)
            .compose(formInput -> this.formInternal(formInput, sigma))
            .compose(formOutput -> ioForm.out(response, formOutput))

            // Fetch workflow
            .compose(nil -> ioFlow.run(task))
            .compose(workflow -> ioFlow.out(response, workflow));
    }

    @Override
    public Future fetchForm(final HistoricProcessInstance instance,
                                        final String sigma) {
        final JsonObject response = new JsonObject();

        final Io ioForm = Io.ioForm();
        final Io ioFlow = Io.ioFlow();
        return Ux.future(instance)


            // Fetch form data
            .compose(ioForm::end)
            .compose(formInput -> this.formInternal(formInput, sigma))
            .compose(formOutput -> ioForm.out(response, formOutput))


            // Fetch workflow
            .compose(nil -> ioFlow.end(instance))
            .compose(workflow -> ioFlow.out(response, workflow));
    }

    private Future flowInternal(final String definitionKey, final String sigma) {
        final JsonObject condition = Ux.whereAnd();
        condition.put(KName.CODE, definitionKey);
        condition.put(KName.SIGMA, sigma);
        return Ux.Jooq.on(WFlowDao.class).fetchJOneAsync(condition);
    }

    private Future formInternal(final JsonObject formInput, final String sigma) {
        final JsonObject parameters = formInput.copy();
        parameters.put(KName.SIGMA, sigma);
        return Ux.channel(Form.class, JsonObject::new, stub -> stub.fetchUi(parameters));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy