/*
 *
 */
package com.fp.bpmlib.transaction.flow;

import com.fp.bpmlib.Flujo;
import com.fp.bpmlib.RegisterFlow;
import com.fp.dto.rules.TransactionRule;
import com.fp.dto.save.SaveRequest;

/**
 * Class StartFlow encargada de.
 *
 * @author jvaca
 * @version 2.1
 */
public class StartFlow extends TransactionRule {

    /** serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** El valor de completed. */
    protected boolean completed = false;

    /** El valor de p. */
    protected Flujo flujo;

    /**
     * Normal process.
     *
     * @param pRequest the request
     * @return save request
     * @throws Exception la exception
     */
    @Override
    public SaveRequest normalProcess(SaveRequest pRequest) throws Exception {
        try {
            flujo = new Flujo();
            flujo.iniciar(this.flow, pRequest);
            if (this.flujo.getProcessInstance().getState() != org.kie.api.runtime.process.ProcessInstance.STATE_COMPLETED) {
                RegisterFlow rf = new RegisterFlow(this.flujo, pRequest);
                rf.registerFlow();
            } else {
                this.completed = true;
            }
        } finally {
            //la session se cierra automaticamente al trabajar con JTA.
        }
        return pRequest;
    }

    /**
     * Reverse process.
     *
     * @param pRequest the request
     * @return save request
     * @throws Exception la exception
     */
    @Override
    public SaveRequest reverseProcess(SaveRequest pRequest) throws Exception {
        return pRequest;
    }

}