<% /*
    $Revision$
    $Id$

    File:           model_mld_workflow_rule.inc

    Description:    Vanuit CodeCharge gegenereerd model voor mld_workflow_rule

    Context:

    Notes:
*/
%>
<%

function model_mld_workflowrule(mystart_key)
{
    this.table = "mld_workflowrule";
    this.primary = "mld_workflowrule_key";
    this.records_name = "mld_workflowrules";
    this.record_name = "mld_workflowrule";
    this.autfunction = "WEB_MLDMGT";
    this.record_title = L("mld_workflowrule");
    this.records_title = L("mld_workflowrule_m");


    this.fields = {
        "id": {
            "dbs": "mld_workflowrule_key",
            "label": "Key",
            "typ": "key",
            "required": true,
            "filter": "exact",
            "seq": "mld_s_mld_workflowrule_key",
            "readonly": true
        },
        "prevstep": {
            "dbs": "mld_workflowstep_key",
            "label": L("mld_workflowrule_step_prev"),
            "typ": "key",
            "foreign": {
                "tbl": "(SELECT s.mld_workflowstep_key"
                     + "      , mld_workflowstep_start_key"
                     + "      , case when srt.ins_srtdiscipline_prefix is not null then srt.ins_srtdiscipline_prefix || '-' end || d.ins_discipline_omschrijving || '/ ' || m.mld_stdmelding_omschrijving  stdmelding_omschr"
                     + "   FROM mld_workflowstep s"
                     + "      , mld_stdmelding m"
                     + "      , mld_v_aanwezigdiscipline d"
                     + "      , ins_srtdiscipline srt"
                     + "  WHERE m.mld_stdmelding_key = s.mld_stdmelding_key"
                     + "    AND m.mld_ins_discipline_key = d.ins_discipline_key"
                     + "    AND srt.ins_srtdiscipline_key = d.ins_srtdiscipline_key"
                     + "  ORDER by d.ins_discipline_omschrijving asc"
                     + "         , m.mld_stdmelding_omschrijving asc)",
                "key": "mld_workflowstep_key",
                "desc": "stdmelding_omschr",
                "where" : "(mld_workflowstep_start_key = " + mystart_key + " OR mld_workflowstep_key = " + mystart_key + ")"
            },
            "required": true,
            "uniquewith": "nextstep"
        },
// Alleen maar verwarrend
//        "name": {
//            "dbs": "mld_workflowrule_omschrijving",
//            "label": L("mld_workflowrule_omschr"),
//            "typ": "varchar"
//        },
        "nextstep": {
            "dbs": "mld_workflowstep_next_key",
            "label": L("mld_workflowrule_step_next"),
            "typ": "key",
            "foreign": {
                "tbl": "(SELECT s.mld_workflowstep_key"
                     + "      , mld_workflowstep_start_key"
                     + "      , case when srt.ins_srtdiscipline_prefix is not null then srt.ins_srtdiscipline_prefix || '-' end || d.ins_discipline_omschrijving || '/ ' || m.mld_stdmelding_omschrijving  stdmelding_omschr"
                     + "   FROM mld_workflowstep s"
                     + "      , mld_stdmelding m"
                     + "      , mld_v_aanwezigdiscipline d"
                     + "      , ins_srtdiscipline srt"
                     + "  WHERE m.mld_stdmelding_key = s.mld_stdmelding_key"
                     + "    AND m.mld_ins_discipline_key = d.ins_discipline_key"
                     + "    AND srt.ins_srtdiscipline_key = d.ins_srtdiscipline_key"
                     + "  ORDER by d.ins_discipline_omschrijving asc"
                     + "         , m.mld_stdmelding_omschrijving asc)",
                "key": "mld_workflowstep_key",
                "desc": "stdmelding_omschr",
                "where" : "(mld_workflowstep_start_key = " + mystart_key + " OR mld_workflowstep_key = " + mystart_key + ")"
            },
            "required": true,
            "uniquewith": "prevstep"
        },
        "_hint" : {
            "dbs": "",
            "label": "",
            "typ": "label",
            "labelvalue": L("mld_workflow_prev_in_overview")
        },
        "condition": {
            "dbs": "mld_workflowrule_condition",
            "label": L("mld_workflowrule_condition"),
            "typ": "memo"
        },
        "otherpath": {
            "dbs": "mld_workflowrule_result",
            "label": "Otherpath",
            "typ": "number",
            "required": true,
            "hidden_fld": true,
            "defaultvalue": 1
        }
    };
    var prev_key = getQParamInt("prevstep", -1)
    if (prev_key > 0)
    {
        var start_key = Oracle.Get("mld_workflowstep_start_key", "mld_workflowstep", prev_key) || prev_key;
        //this.fields.prevstep.foreign.where = "mld_workflowstep_start_key = " + start_key;
        this.fields.nextstep.foreign.where = "mld_workflowstep_start_key = " + start_key
                                           + " AND mld_workflowstep_key <> " + prev_key
                                           + " AND mld_workflowstep_key NOT IN"         // en geen prev stap van onszelf. Voorkom cyclus
                                           + "   (    SELECT mld_workflowstep_key"
                                           + "          FROM mld_workflowrule r"
                                           + "    START WITH mld_workflowstep_next_key = " + prev_key
                                           + "    CONNECT BY PRIOR r.mld_workflowstep_key ="
                                           + "                  r.mld_workflowstep_next_key)";
    }

    // Dit lijkt nog even nodig ook voor de parameter overdracht ('step is undefined')
    // Het moet (nog) wel weg?
    this.edit = {
        "modal": true
    };
    this.list = {
        "columns": ["id", "nextstep"]
    };

    this.REST_GET    = generic_REST_GET(this);
    this.REST_POST   = generic_REST_POST(this);
    this.REST_PUT    = generic_REST_PUT(this);
    this.REST_DELETE = generic_REST_DELETE(this);
}
%>