//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package psdi.workflow;

import java.rmi.RemoteException;
import psdi.mbo.Mbo;
import psdi.mbo.MboRemote;
import psdi.mbo.MboServerInterface;
import psdi.mbo.MboSet;
import psdi.mbo.SqlFormat;
import psdi.server.MXServer;
import psdi.util.MXException;

public class WFTransactionSet extends MboSet implements WFTransactionSetRemote {
    public WFTransactionSet(MboServerInterface ms) throws RemoteException {
        super(ms);
    }

    protected Mbo getMboInstance(MboSet ms) throws MXException, RemoteException {
        return new WFTransaction(ms);
    }

    public void writeTransaction(MboRemote writer, String transType, String memo) throws RemoteException, MXException {
        MboRemote xact = this.add(2L);
        if (writer instanceof WFAction) {
            WFAction wfAction = (WFAction)writer;
            WFNode wfNode = wfAction.getNode();
            WFInstance wfInstance = wfNode.getWFInstance();
            xact.setValue("ACTIONPERFORMED", wfAction.getString("ACTION"), 2L);
            xact.setValue("ACTIONID", wfAction.getString("ACTIONID"), 2L);
            xact.setValue("NODETYPE", wfNode.getString("NODETYPE"), 2L);
            xact.setValue("NODEID", wfAction.getString("OWNERNODEID"), 2L);
            xact.setValue("PROCESSNAME", wfAction.getString("PROCESSNAME"), 2L);
            xact.setValue("PROCESSREV", wfAction.getString("PROCESSREV"), 2L);
            xact.setValue("OWNERTABLE", wfInstance.getString("OWNERTABLE"), 2L);
            xact.setValue("OWNERID", wfInstance.getLong("OWNERID"), 2L);
            xact.setValue("WFID", wfInstance.getString("WFID"), 2L);
            String nodeType = this.getTranslator().toInternalString("WFNODETYPE", wfNode.getString("NODETYPE"));
            if (nodeType.equals("WFSTART")) {
                xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", "WFSTART", xact), 2L);
            } else if (nodeType.equals("WFTASK")) {
                if (wfAction.getBoolean("ISPOSITIVE")) {
                    xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", "ACCEPT", xact), 2L);
                } else {
                    xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", "REJECT", xact), 2L);
                }
            } else {
                if (nodeType.equals("WFCONDITION") || nodeType.equals("WFSUBPROCESS")) {
                    if (wfAction.getBoolean("ISPOSITIVE")) {
                        xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", "TRUE", xact), 2L);
                    } else {
                        xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", "FALSE", xact), 2L);
                    }

                    xact.setValue("PERSONID", this.getUserInfo().getUserName(), 2L);
                    xact.setValue("TRANSDATE", MXServer.getMXServer().getDate(), 2L);
                    return;
                }

                xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", "WFACTION", xact), 2L);
            }
        } else if (writer instanceof WFAssignment) {
            WFAssignment wfAssignment = (WFAssignment)writer;
            xact.setValue("ASSIGNID", wfAssignment.getString("ASSIGNID"), 2L);
            xact.setValue("PROCESSNAME", wfAssignment.getString("PROCESSNAME"), 2L);
            xact.setValue("NODEID", wfAssignment.getString("NODEID"), 2L);
            xact.setValue("OWNERTABLE", wfAssignment.getString("OWNERTABLE"), 2L);
            xact.setValue("OWNERID", wfAssignment.getLong("OWNERID"), 2L);
            xact.setValue("WFID", wfAssignment.getString("WFID"), 2L);
            xact.setValue("PROCESSREV", wfAssignment.getString("PROCESSREV"), 2L);
            xact.setValue("NODETYPE", this.getTranslator().toExternalDefaultValue("WFNODETYPE", "WFTASK", xact), 2L);
            xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", transType, xact), 2L);
            xact.setValue("WFTASKTYPE", wfAssignment.getTask().getString("WFTASKTYPE"), 2L);
        } else if (writer instanceof WFNode) {
            WFNode wfNode = (WFNode)writer;
            WFInstance wfInstance = wfNode.getWFInstance();
            WFCallStack wfCallStack = wfNode.getWFCallStack();
            xact.setValue("NODEID", wfNode.getString("NODEID"), 2L);
            xact.setValue("NODETYPE", wfNode.getString("NODETYPE"), 2L);
            xact.setValue("PROCESSREV", wfNode.getString("PROCESSREV"), 2L);
            xact.setValue("PROCESSNAME", wfCallStack.getString("PROCESSNAME"), 2L);
            xact.setValue("OWNERTABLE", wfInstance.getString("OWNERTABLE"), 2L);
            xact.setValue("OWNERID", wfInstance.getLong("OWNERID"), 2L);
            xact.setValue("WFID", wfInstance.getString("WFID"), 2L);
            xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", transType, xact), 2L);
        } else if (writer instanceof WFInstance) {
            WFInstance wfInstance = (WFInstance)writer;
            xact.setValue("NODEID", 0, 2L);
            xact.setValue("NODETYPE", this.getTranslator().toExternalDefaultValue("WFNODETYPE", "WFSTOP", xact), 2L);
            MboRemote revision = wfInstance.getMboSet("$GETREVISION", "WFREVISION", "mainprocess = :processname and revision = :revision and processname = mainprocess").getMbo(0);
            if (revision != null) {
                xact.setValue("PROCESSREV", revision.getString("PROCESSREV"), 2L);
                xact.setValue("PROCESSNAME", revision.getString("PROCESSNAME"), 2L);
            } else {
                xact.setValue("PROCESSREV", -1, 2L);
                xact.setValue("PROCESSNAME", wfInstance.getString("PROCESSNAME"), 2L);
            }

            xact.setValue("OWNERTABLE", wfInstance.getString("OWNERTABLE"), 2L);
            xact.setValue("OWNERID", wfInstance.getLong("OWNERID"), 2L);
            xact.setValue("WFID", wfInstance.getString("WFID"), 2L);
            xact.setValue("TRANSTYPE", this.getTranslator().toExternalDefaultValue("WFTRANSTYPE", transType, xact), 2L);
        }

        xact.setValue("PERSONID", this.getUserInfo().getUserName(), 2L);
        xact.setValue("MEMO", memo, 2L);
        xact.setValue("TRANSDATE", MXServer.getMXServer().getDate(), 2L);
    }

    public void writeTransaction(String transType, String memo) throws MXException, RemoteException {
        this.writeTransaction(this.getOwner(), transType, memo);
    }

    public void getTransactions() throws MXException, RemoteException {
        MboRemote owner = this.getOwner();
        if (owner != null) {
            SqlFormat sqf = new SqlFormat(owner, "ownertable = :1 and ownerid = :2");
            sqf.setObject(1, "MAXOBJECT", "OBJECTNAME", owner.getName());
            sqf.setLong(2, owner.getUniqueIDValue());
            this.reset();
        }

    }
}
