<% /*
    $Revision$
    $Id$

    File:           model_mld_workflow.inc

    Description:    Vanuit CodeCharge gegenereerd model voor mld_workflow

    Context:

    Notes:
*/
%>
<!-- #include file="../api2/model_mld_workflowrule.inc" -->
<%
// model_mld_workflow is het pseudomodel van een compleet workflow*schema*
function model_mld_workflow()
{
    var this_model = new model_mld_workflowstep();
    delete this_model.fields.start;
    delete this_model.fields.jointype;
    delete this_model.fields.level;
    this_model.fields.stdmelding.label = L("mld_workflowstep_start");

    this_model.record_title = L("mld_workflow");
    this_model.records_title = L("mld_workflow_m");

    // Een include van model_mld_workflowstep met joinfield 'start_key'
    // met alle meldingen in dit schema zou hier niet misstaan.
    // Problemen: de start_key is bij de startmelding niet ingevuld
    //            model_mld_workflowstep heeft ook nog weer een include
    //            rules en scaffolding kan drie-die (toch?) niet aan

    return this_model;
}

// model_mld_workflowstep is van een individuele step (melding) in een schema
function model_mld_workflowstep(mystart_key)
{
    this.table = "mld_workflowstep";
    this.primary = "mld_workflowstep_key";
    this.records_name = "mld_workflowsteps";
    this.record_name = "mld_workflowstep";
    this.autfunction = "WEB_MLDMGT";
    this.record_title = L("mld_workflowstep");
    this.records_title = L("mld_workflowstep_m");

    this.fields = {
        "id": {
            "dbs": "mld_workflowstep_key",
            "label": "Key",
            "typ": "key",
            "required": true,
            "filter": "exact",
            "seq": "mld_s_mld_workflowstep_key",
            "readonly": true
        },
        "name": {
            "dbs": "mld_workflowstep_omschrijving",
            "label": L("mld_workflowstep_omschr"),
            "typ": "varchar",
            "required": true,
            "translate": true
        },
        "start": {
            "dbs": "mld_workflowstep_start_key",
            "label": L("mld_workflowstep_start"),
            "typ": "key",
            "foreign": {
               "tbl": "mld_workflowstep",
               "key": "mld_workflowstep_key",
               "desc": "mld_workflowstep_omschrijving",
               "where": "mld_workflowstep_start_key IS NULL"
            }
        },
        "stdmelding": {
            "dbs": "mld_stdmelding_key",
            "label": L("mld_stdmelding_key"),
            "typ": "key",
            "foreign": {
                "tbl": "(select m.mld_stdmelding_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_stdmelding m"
                     + "      , mld_v_aanwezigdiscipline d"
                     + "      , ins_srtdiscipline srt"
                     + "  where m.mld_ins_discipline_key = d.ins_discipline_key"
                     + "    and srt.ins_srtdiscipline_key = d.ins_srtdiscipline_key"
                     + "    and (mld_stdmelding_vervaldatum IS NULL OR mld_stdmelding_vervaldatum > SYSDATE)"
                     + "    and m.mld_stdmelding_verwijder IS NULL"
                     + "  order by d.ins_discipline_omschrijving asc"
                     + "         , m.mld_stdmelding_omschrijving asc)",
                "key": "mld_stdmelding_key",
                "desc": "stdmelding_omschr"
            },
            "required": true,
            "filter": "exact",
            "LOVinit": ""
        },
        "jointype": {
            "dbs": "mld_workflowstep_jointype",
            "label": L("mld_workflowstep_jointype"),
            "typ": "number",
            "LOV": "0;AND;1;(X)OR;2;SYNC",
            "required": true,
            "defaultvalue": "0"
        },
        "level": {
            "dbs": "mylevel",
            "label": L("mld_workflowstep_level"),
            "sql":  " NVL ( TO_CHAR(1 + (SELECT ll"
                  + "        FROM (    SELECT MAX (LEVEL) ll, mld_workflowstep_next_key"
                  + "                    FROM mld_workflowrule r"
                  + "              CONNECT BY PRIOR r.mld_workflowstep_next_key ="
                  + "                            r.mld_workflowstep_key"
                  + "                GROUP BY mld_workflowstep_next_key)"
                  + "       WHERE mld_workflowstep_next_key = mld_workflowstep.mld_workflowstep_key)), "
                  + "          CASE WHEN mld_workflowstep_start_key IS NULL THEN '1 (START)' ELSE 'UNREACHABLE' END)",
            "typ": "varchar"
        }
    };

    this.includes = {
        "rules": {
            "model": new model_mld_workflowrule(mystart_key),
            "joinfield": "prevstep",
            "enable_update": true
        }
    };

    var gparams = { GET: { } };

    if (mystart_key > 0)
    {
        gparams.GET.wheres = [ "(mld_workflowstep_start_key = {0} OR mld_workflowstep.mld_workflowstep_key = {0})".format(mystart_key)];
        delete this.fields.name;
        this.fields.start.defaultvalue = mystart_key;
        this.fields.start.hidden_fld = true;
        this.REST_POST = function (params, jsondata) // in mld_workflowstep.asp is een pseudoveld 'prevstep' toegevoegd
        {
            var prevstep = jsondata.prevstep;
            delete this.fields.prevstep; // die pseudokolom doen we zelf
            var result = generic_REST_POST(this)(params, jsondata); // eerst gewoon opslaan
            // Nu nog een mld_workflowrule record aanmaken
            this.includes.rules.model.REST_POST(params, { prevstep: prevstep, nextstep: result.key});
            return result;
        }
    }
    else
    {
        gparams.GET.wheres = [ "mld_workflowstep_start_key IS NULL" ];
        this.REST_POST   = generic_REST_POST(this);
    }

    this.REST_GET    = generic_REST_GET(this, gparams);
    this.REST_PUT    = generic_REST_PUT(this);
    this.REST_DELETE = generic_REST_DELETE(this);
}
%>