<%@ language = "JavaScript" %>
<%
/*
    $Revision$
    $Id$

    File:               get_kenmerk_info_ajax.asp
    Description:        Ophalen van allerlei info om client-side data te hebben
    Parameters:         req_info:   srtkenmerk, srtdiscipline, discipline, stdmelding, typeopdracht, kenmerk leveren sql's voor dropboxen.
                                    kenmerkwaarde maakt de sql-expressie waarmee aan een udr-rapport een kenmerkkolom kan worden toegevoegd.

    Context:
    Note:
*/
%>

<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc"-->
<%
user.checkAutorisation("WEB_UDRMAN");

var module    = getQParam("module", "");
var req_info  = getQParam("req_info");
var srtk_key  = getQParamInt("srtkenm_key", -1);
var kenm_key  = getQParamInt("kenm_key", -1);
var srtd_key  = getQParamInt("srtd_key", -1);
var disc_key  = getQParamInt("disc_key", -1);
var groep_key = getQParamInt("groep_key", -1);
var deel_key  = getQParamInt("deel_key", -1);
var cols_key  = getQParamInt("cols_key", -1);

var result = { req_info: req_info
             , module: module
             , srtkenm: srtk_key
             , srtdisc: srtd_key
             , discipl: disc_key
             , groep: groep_key
             , deel: deel_key
             , lov: []
             };
var sql = "";

var module_db = module;
switch (module)
{
    case "OPD": module_db = "MLD"; break;
    case "CTR": module_db = "INS"; break;
}

switch (req_info)
{
    case "srtdiscipline":
    {
        switch (module)
        {
            case "ALG": sql = "SELECT 1  key, " + safe.quoted_sql(L("lcl_district")) + "  oms FROM DUAL UNION "  // D
                            + "SELECT 2  key, " + safe.quoted_sql(L("lcl_location")) + "  oms FROM DUAL UNION "  // L
                            + "SELECT 3  key, " + safe.quoted_sql(L("lcl_terra"))    + "  oms FROM DUAL UNION "  // T
                            + "SELECT 4  key, " + safe.quoted_sql(L("lcl_building")) + "  oms FROM DUAL UNION "  // G
                            + "SELECT 5  key, " + safe.quoted_sql(L("lcl_floor"))    + "  oms FROM DUAL UNION "  // V
                            + "SELECT 6  key, " + safe.quoted_sql(L("lcl_room"))     + "  oms FROM DUAL";        // R
                        break;
            case "BES": sql = "SELECT 1  key, " + safe.quoted_sql(L("bes_kenmerkbestel_m")) + "  oms FROM DUAL UNION "
                            + "SELECT 2  key, " + safe.quoted_sql(L("bes_kenmerk_m"))       + "  oms FROM DUAL";
                        break;
            case "CTR": sql = "SELECT ins_discipline_key  key"
                            + "     , ins_discipline_omschrijving  oms"
                            + "  FROM ins_tab_discipline"
                            + " WHERE ins_discipline_verwijder IS NULL"
                            + "   AND ins_discipline_module = 'CTR'"
                            + " ORDER BY 2";
                        break;
            case "MLD": sql = "SELECT ins_srtdiscipline_key  key"
                            + "     , ins_srtdiscipline_omschrijving  oms"
                            + "  FROM ins_srtdiscipline"
                            + " WHERE ins_srtdiscipline_module = 'MLD'"
                            + "   AND ins_srtdiscipline_verwijder IS NULL"
                            + " ORDER BY 2";
                        break;
            case "RES": sql = "SELECT 1  key, " + safe.quoted_sql(L("res_activiteit_key")) + "  oms FROM DUAL UNION "  // A
                            + "SELECT 2  key, " + safe.quoted_sql(L("res_discipline_key")) + "  oms FROM DUAL UNION "  // D
                            + "SELECT 3  key, " + safe.quoted_sql(L("res_artikel"))        + "  oms FROM DUAL";        // C
                        break;
        }
        break;
    }
    case "discipline":
    {
        sql = "SELECT ins_discipline_key  key"
            + "     , ins_discipline_omschrijving  oms"
            + "  FROM ins_tab_discipline"
            + " WHERE ins_discipline_module = " + safe.quoted_sql(module_db)
            + "   AND ins_discipline_verwijder IS NULL";
        if (srtd_key > 0)
        {
            if (module == "CTR")
            {
                sql += " AND ins_discipline_key IN"
                        + "(SELECT ins_discipline_key"
                        + "   FROM ins_srtcontrole c"
                        + "      , ins_v_allsrtinstallatie i"
                        + "  WHERE c.ins_srtinstallatie_key = i.ins_srtinstallatie_key"
                        + "    AND c.ins_srtcontrole_niveau = i.niveau"
                        + "    AND c.ctr_discipline_key = " + srtd_key
                        + ")";
            }
            else
            {
                sql += " AND ins_srtdiscipline_key = " + srtd_key;
            }
        }
        sql += " ORDER BY 2";
        break;
    }
    case "groep":
    {
        switch (module)
        {
            case "BES": sql = "SELECT bes_srtgroep_key  key"
                            + "     , bes_srtgroep_omschrijving  oms"
                            + "  FROM bes_srtgroep"
                            + " WHERE bes_srtgroep_verwijder IS NULL"
                            + (disc_key > 0 ? " AND ins_discipline_key = " + disc_key : "")
                            + " ORDER BY 2";
                        break;
            case "INS":
            case "CTR": sql = "SELECT ins_srtgroep_key  key"
                            + "     , ins_srtgroep_omschrijving  oms"
                            + "  FROM ins_srtgroep"
                            + " WHERE ins_srtgroep_verwijder IS NULL"
                            + (disc_key > 0 ? " AND ins_discipline_key = " + disc_key : "")
                            + " ORDER BY 2";
                        break;
            case "MLD": sql = "SELECT mld_stdmelding_key  key"
                            + "     , mld_stdmelding_omschrijving  oms"
                            + "  FROM mld_stdmelding"
                            + " WHERE mld_stdmelding_verwijder IS NULL"
                            + (disc_key > 0 ? " AND mld_ins_discipline_key = " + disc_key : "")
                            + " ORDER BY 2";
                        break;
            case "OPD": sql = "SELECT mld_typeopdr_key  key"
                            + "     , mld_typeopdr_omschrijving  oms"
                            + "  FROM mld_typeopdr"
                            + " ORDER BY 2";
                        break;
        }
        break;
    }
    case "deel":
    {
        switch (module)
        {
            case "INS":
            case "CTR": sql = "SELECT a.ins_srtinstallatie_key  key"
                            + "     , DECODE(niveau, 'D', a.discipline_oms, 'G', a.srtgroep_oms, 'S', a.srtdeel_oms)  oms"
                            + "  FROM ins_v_allsrtinstallatie a"
                            + "     , ins_srtdeel s"
                            + " WHERE a.niveau = 'S'"
                            + "   AND a.ins_srtdeel_key = s.ins_srtdeel_key"
                            + "   AND s.ins_srtdeel_verwijder IS NULL"
                            + "   AND a.ins_srtgroep_key = " + groep_key
                            + " ORDER BY 2";
                        break;
            case "RES": sql = "SELECT res_activiteit_key  key"
                            + "     , res_activiteit_omschrijving  oms"
                            + "  FROM res_activiteit"
                            + " WHERE res_activiteit_verwijder IS NULL"
                            + " ORDER BY 2";
                        break;
        }
        break;
    }

    case "srtkenmerk":
    {
        sql = "SELECT " + module_db + "_srtkenmerk_key  key"
            + "     , " + module_db + "_srtkenmerk_omschrijving  oms"
            + "  FROM " + module_db + "_srtkenmerk"
            + " WHERE " + module_db + "_srtkenmerk_verwijder IS NULL"
            + "   AND " + module_db + "_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
            + " ORDER BY 2";
        break;
    }
    case "kenmerk":
    {
        switch (module)
        {
            case "ALG": var decode_niveau = ["","D","L","T","G","V","R"];
                        sql = "SELECT k.alg_kenmerk_key  key"
                            + "     , k.alg_kenmerk_omschrijving  oms"
                            + "  FROM alg_kenmerk k"
                            + " WHERE k.alg_kenmerk_verwijder IS NULL"
                            + "   AND k.alg_kenmerk_kenmerktype NOT IN ('F','M','E','X')"
                            + "   AND k.alg_kenmerk_niveau = " + safe.quoted_sql(decode_niveau[srtd_key])
                            + " ORDER BY 2";
                        break;
            case "BES": if (srtd_key == 1)  // kenmerken van bestelling
                        {
                            sql = "SELECT k.bes_kenmerk_key  key"
                                + "     , COALESCE(" + lcl.xsql("k.bes_kenmerk_omschrijving", "k.bes_kenmerk_key")
                                        + "       ," + lcl.xsql("s.bes_srtkenmerk_omschrijving", "s.bes_srtkenmerk_key")
                                        + "       )"
                                        + ( disc_key == -1 
                                          ? "||' ('||" + lcl.xsql("d.ins_discipline_omschrijving", "d.ins_discipline_key") + "||')'" 
                                          : ""
                                          ) + " oms"
                                + "  FROM bes_kenmerk k"
                                + "     , bes_srtkenmerk s"
                                + "     , bes_v_aanwezigdiscipline d"
                                + " WHERE k.bes_srtkenmerk_key = s.bes_srtkenmerk_key"
                                + "   AND k.bes_srtinstallatie_key = d.ins_discipline_key"
                                + "   AND k.bes_kenmerk_verwijder IS NULL"
                                + "   AND k.bes_kenmerk_type = 'B'"
                                + "   AND s.bes_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
                                + (disc_key > 0 ? " AND k.bes_srtinstallatie_key = " + disc_key : "")
                                + (srtk_key > 0 ? " AND k.bes_srtkenmerk_key = " + srtk_key : "")
                                + " ORDER BY 2";
                        }
                        else  // kenmerken van bestelitems
                        {
                            sql = "SELECT k.bes_kenmerk_key  key"
                                + "     , COALESCE(" + lcl.xsql("k.bes_kenmerk_omschrijving", "k.bes_kenmerk_key")
                                        + "       ," + lcl.xsql("s.bes_srtkenmerk_omschrijving", "s.bes_srtkenmerk_key")
                                        + "       )"
                                        + ( disc_key == -1 
                                          ? "||' ('||" + lcl.xsql("i.discipline_oms", "i.bes_discipline_key") + "||')'" 
                                          : ""
                                          ) + " oms"
                                + "  FROM bes_kenmerk k"
                                + "     , bes_srtkenmerk s"
                                + "     , bes_v_allsrtinstallatie i"
                                + " WHERE k.bes_srtkenmerk_key = s.bes_srtkenmerk_key"
                                + "   AND k.bes_srtinstallatie_key = i.bes_srtinstallatie_key"
                                + "   AND k.bes_kenmerk_niveau = i.niveau"
                                + "   AND k.bes_kenmerk_verwijder IS NULL"
                                + "   AND k.bes_kenmerk_type = 'I'"
                                + "   AND s.bes_srtkenmerk_verwijder IS NULL"
                                + "   AND s.bes_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
                                + (disc_key > 0 ? " AND i.bes_discipline_key = " + disc_key : "")
                                + (srtk_key > 0 ? " AND s.bes_srtkenmerk_key = " + srtk_key : "")
                                + " ORDER BY 2";
                        }
                        break;
            case "CNT": sql = "SELECT k.cnt_kenmerk_key  key"
                            + "     , COALESCE(" + lcl.xsql("k.cnt_kenmerk_omschrijving", "k.cnt_kenmerk_key")
                                    + "       ," + lcl.xsql("s.cnt_srtkenmerk_omschrijving", "s.cnt_srtkenmerk_key")
                                    + "       )"
                                    + ( disc_key == -1
                                      ? " ||' ('|| d.ins_discipline_omschrijving ||')'"
                                      : ""
                                      ) + " oms"        
                            + "  FROM cnt_kenmerk k"
                            + "     , cnt_srtkenmerk s"
                            + "     , cnt_v_aanwezigdiscipline d"
                            + " WHERE k.cnt_srtkenmerk_key = s.cnt_srtkenmerk_key"
                            + "   AND k.cnt_srtcontract_key = d.ins_discipline_key(+)"
                            + "   AND k.cnt_kenmerk_verwijder IS NULL"
                            + "   AND s.cnt_srtkenmerk_verwijder IS NULL"
                            + "   AND s.cnt_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
                            + (disc_key > 0 ? " AND k.cnt_srtcontract_key = " + disc_key : "")
                            + (srtk_key > 0 ? " AND s.cnt_srtkenmerk_key = " + srtk_key : "")
                            + " ORDER BY 2";
                        break;
            case "INS": sql = "SELECT k.ins_kenmerk_key  key"
                            + "     , COALESCE(" + lcl.xsql("k.ins_kenmerk_omschrijving", "k.ins_kenmerk_key")
                                    + "       ," + lcl.xsql("s.ins_srtkenmerk_omschrijving", "s.ins_srtkenmerk_key")
                                    + "       )"
                                    + ( deel_key == -1
                                      ? "||' ('|| DECODE(i.niveau, 'D', i.discipline_oms, 'G', i.srtgroep_oms, 'S', i.srtdeel_oms) ||')'"
                                      : ""
                                      ) + " oms"
                            + "  FROM ins_kenmerk k"
                            + "     , ins_v_allsrtinstallatie i"
                            + "     , ins_srtkenmerk s"
                            + " WHERE k.ins_srtinstallatie_key = i.ins_srtinstallatie_key"
                            + "   AND k.ins_srtkenmerk_key = s.ins_srtkenmerk_key"
                            + "   AND k.ins_kenmerk_niveau = i.niveau"
                            + "   AND k.ins_kenmerk_niveau <> 'C'"
                            + "   AND k.ins_kenmerk_verwijder IS NULL"
                            + "   AND s.ins_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
                            + (disc_key  > 0 ? " AND i.ins_discipline_key = " + disc_key : "")
                            + (groep_key > 0 ? " AND i.ins_srtgroep_key = " + groep_key : "")
                            + (deel_key  > 0 ? " AND i.ins_srtdeel_key = " + deel_key : "")
                            + (srtk_key  > 0 ? " AND k.ins_srtkenmerk_key = " + srtk_key : "")
                            + " ORDER BY 2";
                        break;
            case "CTR": sql = "SELECT k.ins_kenmerk_key  key"
                            + "     , COALESCE(" + lcl.xsql("k.ins_kenmerk_omschrijving", "k.ins_kenmerk_key")
                                    + "       ," + lcl.xsql("s.ins_srtkenmerk_omschrijving", "s.ins_srtkenmerk_key")
                                    + "       )"
                                    + ( deel_key == -1
                                      ? "||' ('|| DECODE(i.niveau, 'D', c.ins_srtcontrole_omschrijving, 'G', i.srtgroep_oms, 'S', i.srtdeel_oms) ||')'"
                                      : ""
                                      ) + " oms"
                            + "  FROM ins_kenmerk k"
                            + "     , ins_srtkenmerk s"
                            + "     , ins_srtcontrole c"
                            + "     , ins_v_allsrtinstallatie i"
                            + " WHERE k.ins_srtinstallatie_key = c.ins_srtcontrole_key"
                            + "   AND k.ins_srtkenmerk_key = s.ins_srtkenmerk_key"
                            + "   AND k.ins_kenmerk_verwijder IS NULL"
                            + "   AND s.ins_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
                            + "   AND k.ins_kenmerk_niveau = 'C'"
                            + "   AND c.ins_srtinstallatie_key = i.ins_srtinstallatie_key"
                            + "   AND c.ins_srtcontrole_niveau = i.niveau"
                            + (srtd_key  > 0 ? " AND c.ctr_discipline_key = " + srtd_key : "")
                            + (disc_key  > 0 ? " AND i.ins_discipline_key = " + disc_key : "")
                            + (groep_key > 0 ? " AND i.ins_srtgroep_key = " + groep_key : "")
                            + (deel_key  > 0 ? " AND i.ins_srtdeel_key = " + deel_key : "")
                            + (srtk_key  > 0 ? " AND k.ins_srtkenmerk_key = " + srtk_key : "")
                            + " ORDER BY 2";
                       break;
            case "MLD": var niveau = (groep_key > 0 ? "S" : (disc_key > 0 ? "D" : (groep_key > 0 ? "T" : "A")));
                        var sql_w = "";
                        if (groep_key > 0)
                        {
                            sql_w = " AND k.mld_stdmelding_key = " + groep_key + " AND k.mld_kenmerk_niveau = 'S'";
                        }
                        else if (disc_key > 0)
                        {
                            sql_w = " AND k.mld_stdmelding_key = " + disc_key + " AND k.mld_kenmerk_niveau = 'D'";
                        }
                        else if (srtd_key > 0)
                        {
                            sql_w = " AND k.mld_stdmelding_key = " + srtd_key + " AND k.mld_kenmerk_niveau = 'T'";
                        }

                        if (srtk_key > 0)
                        {
                            sql_w += " AND k.mld_srtkenmerk_key = " + srtk_key;
                        }

                        sql = "SELECT k.mld_kenmerk_key  key"
                            + "     , COALESCE (" + lcl.xsql("k.mld_kenmerk_omschrijving", "k.mld_kenmerk_key")
                                    + "        ," + lcl.xsql("s.mld_srtkenmerk_omschrijving", "s.mld_srtkenmerk_key") 
                                    + "        ) oms"
                            + "  FROM mld_kenmerk k"
                            + "     , mld_srtkenmerk s"
                            + "     , ( SELECT 'S'                    mld_kenmerk_niveau"
                                    + "      , m.mld_stdmelding_key   ref_mld_stdmelding_key"
                                    + "      , m.mld_stdmelding_key"
                                    + "      , d.ins_discipline_key"
                                    + "      , d.ins_srtdiscipline_key"
                                    + "   FROM mld_stdmelding m"
                                    + "      , mld_discipline d"
                                    + "  WHERE m.mld_ins_discipline_key = d.ins_discipline_key"
                                    + "    AND m.mld_stdmelding_verwijder IS NULL"
                                    + "    AND d.ins_discipline_verwijder IS NULL"
                                    + " UNION ALL"
                                    + " SELECT 'D'                    mld_kenmerk_niveau"
                                    + "      , d.ins_discipline_key   ref_mld_stdmelding_key"
                                    + "      , NULL"
                                    + "      , d.ins_discipline_key"
                                    + "      , d.ins_srtdiscipline_key"
                                    + "   FROM mld_discipline d"
                                    + "  WHERE d.ins_discipline_verwijder IS NULL"
                                    + " UNION ALL"
                                    + " SELECT 'T'                    mld_kenmerk_niveau"
                                    + "      , ins_srtdiscipline_key  ref_mld_stdmelding_key"
                                    + "      , NULL"
                                    + "      , NULL"
                                    + "      , d.ins_srtdiscipline_key"
                                    + "   FROM ins_srtdiscipline d"
                                    + " UNION"
                                    + " SELECT 'A'                    mld_kenmerk_niveau"
                                    + "      , NULL                   ref_mld_stdmelding_key"
                                    + "      , NULL"
                                    + "      , NULL"
                                    + "      , NULL"
                                    + "   FROM DUAL"
                            + "       ) n"
                            + " WHERE k.mld_srtkenmerk_key = s.mld_srtkenmerk_key"
                            + "   AND k.mld_kenmerk_niveau = n.mld_kenmerk_niveau"
                            + "   AND k.mld_kenmerk_verwijder IS NULL"
                            + "   AND (  k.mld_stdmelding_key = n.ref_mld_stdmelding_key"
                            + "       OR k.mld_stdmelding_key IS NULL"
                            + "       )"
                            + "   AND s.mld_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
                            + (groep_key > 0 ? " AND n.mld_stdmelding_key = "    + groep_key : "")
                            + (disc_key  > 0 ? " AND n.ins_discipline_key = "    + disc_key  : "")
                            + (srtd_key  > 0 ? " AND n.ins_srtdiscipline_key = " + srtd_key  : "")
                            //+ (ref_stdm_key ? " AND k.mld_stdmelding_key = " + ref_stdm_key : "")
                            + (srtk_key > 0 ? " AND k.mld_srtkenmerk_key = " + srtk_key : "")
                            + " ORDER BY 2";
                        break;
            case "OPD": var sql_w = "";
                        if (groep_key > 0)
                        {
                            sql_w = " AND k.mld_typeopdr_key = " + groep_key
                                  + " AND k.mld_kenmerk_niveau = 'O'";
                        }
                        sql = "SELECT DISTINCT k.mld_kenmerk_key  key"
                            + "     , COALESCE (" + lcl.xsql("k.mld_kenmerk_omschrijving", "k.mld_kenmerk_key")
                                    + "        ," + lcl.xsql("v.mld_srtkenmerk_omschrijving", "v.mld_srtkenmerk_key") 
                                    + "        ) oms"
                            + "  FROM mld_kenmerk k"
                            + "     , mld_srtkenmerk v"
                            + "     , mld_typeopdr s"
                            + " WHERE (1=1)"
                            + "   AND k.mld_srtkenmerk_key = v.mld_srtkenmerk_key"
                            + "   AND k.mld_kenmerk_verwijder IS NULL"
                            + "   AND v.mld_srtkenmerk_verwijder IS NULL"
                            + "   AND v.mld_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
                            + "   AND k.mld_kenmerk_niveau IN ('P','O')"
                            + sql_w
                            + " ORDER BY 2";
                        break;
            case "RES": var decode_niveau = ["","A","D","C"];
                        sql = "SELECT k.res_kenmerk_key  key"
                            + "     , COALESCE(" + lcl.xsql("k.res_kenmerk_omschrijving", "k.res_kenmerk_key")
                                    + "       ," + lcl.xsql("v.res_srtkenmerk_omschrijving", "v.res_srtkenmerk_key")
                                    + "       )"
                                    + ( deel_key == -1
                                      ? "||' ('||" + lcl.xsql("a.res_activiteit_omschrijving", "a.res_activiteit_key") + "||')'"
                                      : ""
                                      ) + " oms"
                            + "  FROM res_kenmerk k"
                            + "     , res_srtkenmerk v"
                            + "     , res_activiteit a"
                            + " WHERE k.res_srtkenmerk_key = v.res_srtkenmerk_key"
                            + "   AND k.res_activiteit_key = a.res_activiteit_key"
                            + "   AND k.res_kenmerk_verwijder IS NULL"
                            + "   AND v.res_srtkenmerk_verwijder IS NULL"
                            + "   AND v.res_srtkenmerk_kenmerktype NOT IN ('F','M','E','X')"
                            + (srtd_key > 0 ? " AND k.res_kenmerk_niveau = " + safe.quoted_sql(["","A","D","C"][srtd_key]) : "")
                            + (disc_key > 0 ? " AND k.res_discipline_key = " + disc_key : "")
                            + (deel_key > 0 ? " AND k.res_activiteit_key = " + deel_key : "")
                            + " ORDER BY 2";
                        break;
        }
        break;
    }
    case "kenmerkwaarde":
    {
        var src = "";
        // De te maken kenmerk-sql moet via de waarde van deze kolom gekoppeld worden.
        var sql = "SELECT fac_usrrap_cols_column_name"
                + "  FROM fac_usrrap_cols"
                + " WHERE fac_usrrap_cols_key = " + cols_key;
        var oRs = Oracle.Execute(sql);
        var safe_key_col_name = oRs("fac_usrrap_cols_column_name").Value;
        oRs.Close();

        // Aan de hand van de module de te gebruiken tabellen bepalen.
        switch (module)
        {
            case "ALG": tbl = { srtk: ""
                              , kenm: "alg_kenmerk"
                              , vlue: "alg_onrgoedkenmerk"
                              , refk: "alg_onrgoed_key"
                              , colval: "alg_onrgoedkenmerk_waarde"
                              }
                              break;
            case "BES": tbl = { srtk: "bes_srtkenmerk"
                              , kenm: "bes_kenmerk"
                              , vlue: (srtd_key == 1 ? "bes_kenmerkbestell" : "bes_kenmerkbesteli")
                              , refk: (srtd_key == 1 ? "bes_bestelling_key" : "bes_bestelling_item_key")
                              , colval: (srtd_key == 1 ? "bes_kenmerkbestell_waarde" : "bes_kenmerkbesteli_waarde")
                              }
                              break;
            case "CNT": tbl = { srtk: "cnt_srtkenmerk"
                              , kenm: "cnt_kenmerk"
                              , vlue: "cnt_kenmerkcontract"
                              , refk: "cnt_contract_key"
                              , colval: "cnt_kenmerkcontract_waarde"
                              }
                              break;
            case "INS": tbl = { srtk: "ins_srtkenmerk"
                              , kenm: "ins_kenmerk"
                              , vlue: "ins_kenmerkdeel"
                              , refk: "ins_deel_key"
                              , colval: "ins_kenmerkdeel_waarde"
                              }
                              break;
            case "CTR": tbl = { srtk: "ins_srtkenmerk"
                              , kenm: "ins_kenmerk"
                              , vlue: "ins_kmdeelsrtcontr"
                              , refk: "ins_deelsrtcontrole_key"
                              , colval: "ins_kmdeelsrtcontr_waarde"
                              }
                              break;
            case "MLD": tbl = { srtk: "mld_srtkenmerk"
                              , kenm: "mld_kenmerk"
                              , vlue: "mld_kenmerkmelding"
                              , refk: "mld_melding_key"
                              , colval: "mld_kenmerkmelding_waarde"
                              };
                        break;
            case "OPD": tbl = { srtk: "mld_srtkenmerk"
                              , kenm: "mld_kenmerk"
                              , vlue: "mld_kenmerkopdr"
                              , refk: "mld_opdr_key"
                              , colval: "mld_kenmerkopdr_waarde"
                              }
                        break;
            case "RES": tbl = { srtk: "res_srtkenmerk"
                              , kenm: "res_kenmerk"
                              , vlue: "res_kenmerkwaarde"
                              , refk: "res_rsv_ruimte_key"
                              , colval: "res_kenmerkreservering_waarde"
                              }
        }

        if (tbl.srtk)
        {
            var sql_k = "SELECT xk."+ tbl.kenm + "_key           kenmerk_key"
                      + "     , xs."+ tbl.srtk + "_omschrijving  kenmerk_omschrijving"
                      + "     , xs."+ tbl.srtk + "_kenmerktype   kenmerk_kenmerktype"
                      + "     , xs."+ tbl.srtk + "_lengte        kenmerk_lengte"
                      + "     , xs."+ tbl.srtk + "_dec           kenmerk_dec"
                      + "     , xs.fac_kenmerkdomein_key         domein_key"
                      + "  FROM "+ tbl.kenm + " xk"
                      + "     , "+ tbl.srtk + " xs"
                      + " WHERE xk."+ tbl.srtk + "_key = xs."+ tbl.srtk + "_key"
        }
        else
        {
            var sql_k = "SELECT xk."+ tbl.kenm + "_key           kenmerk_key"
                      + "     , xk."+ tbl.kenm + "_omschrijving  kenmerk_omschrijving"
                      + "     , xk."+ tbl.kenm + "_kenmerktype   kenmerk_kenmerktype"
                      + "     , xk."+ tbl.kenm + "_lengte        kenmerk_lengte"
                      + "     , xk."+ tbl.kenm + "_dec           kenmerk_dec"
                      + "     , xk.fac_kenmerkdomein_key         domein_key"
                      + "  FROM "+ tbl.kenm + " xk"
        }
        //
        var sql = "SELECT k.kenmerk_key"
                + "     , k.kenmerk_omschrijving"
                + "     , k.kenmerk_kenmerktype"
                + "     , k.kenmerk_lengte"
                + "     , k.kenmerk_dec"
                + "     , d.fac_kenmerkdomein_objectnaam"
                + "     , d.fac_kenmerkdomein_kolomnaam"
                + "     , d.fac_kenmerkdomein_kolomtxt"
                + "     , d.fac_usrtab_key"
                + "     , k.domein_key"
                + "  FROM ("+ sql_k + ") k"
                + "     , fac_kenmerkdomein d"
                + " WHERE k.domein_key = d.fac_kenmerkdomein_key(+)"
                + "   AND k.kenmerk_key = " + kenm_key;
        var oRs = Oracle.Execute(sql);
        var kenm_key = oRs("kenmerk_key").Value;
        var kenm_naam = oRs("kenmerk_omschrijving").Value;
        var data_type = oRs("kenmerk_kenmerktype").Value;
        var data_len = oRs("kenmerk_lengte").Value;
        var data_dec = oRs("kenmerk_dec").Value;
        var ref_tab = oRs("fac_kenmerkdomein_objectnaam").Value;
        var ref_col = oRs("fac_kenmerkdomein_kolomnaam").Value;
        var ref_txt = oRs("fac_kenmerkdomein_kolomtxt").Value;
        var ref_key = oRs("fac_usrtab_key").Value;
        oRs.Close();

        if (data_type == "R" || data_type == "r" || data_type == "S")
        {   // Waarde ophalen via kenmerkdomein
            src = "SELECT rt." + ref_txt
                + "  FROM " + tbl.vlue + " kenm"
                + "     , " + ref_tab + " rt"
                + " WHERE kenm." + tbl.vlue + "_verwijder IS NULL"
                + "   AND rt." + ref_col + " = fac.safe_to_number(kenm." + tbl.colval + ")"
                + (ref_key ? " AND rt.fac_usrtab_key = " + ref_key : "")
                + "   AND kenm." + tbl.kenm + "_key = " + kenm_key
                + "   AND kenm." + tbl.refk + " = {0}." + safe_key_col_name;
        }
        else if (data_type == "M" || data_type == "F" || data_type == "X" || data_type == "E")
        {   // kenmerkwaarde verwijst naar een bestand
            src = "";
        }
        else
        {   // De overige kenmerkwaarden staan direct in de tabel.
            // Er dan misschien nog dataconverie nodig.
            src = "SELECT kenm." + tbl.colval
                + "  FROM " + tbl.vlue + " kenm"
                + " WHERE kenm." + tbl.vlue + "_verwijder IS NULL"
                + "   AND kenm." + tbl.kenm + "_key = " + kenm_key
                + "   AND kenm." + tbl.refk + " = {0}." + safe_key_col_name;

            switch (data_type)
            {
                case "D": src = "SELECT CAST((" + src + ") AS DATE, 'dd-mm-yyyy hh24:mi:ss') FROM DUAL";
                          break;
                case "T": src = "SELECT CAST((" + src + ") AS DATE, 'hh24:mi:ss') FROM DUAL";
                          break;
                case "N": src = "SELECT CAST((" + src + ") AS " +((data_dec && data_dec > 0) ? "FLOAT" : "INTEGER") + ") FROM DUAL";
                          break;
            }
        }

        // Conversie datatype kenmerk naar datatype rapport:
        var kenm_type;
        switch (data_type)
        {
            case "D": kenm_type = "date"; break;
            case "T": kenm_type = "time"; break;
            case "V": kenm_type = "check0"; break;
            case "N": kenm_type = ((data_dec && data_dec > 0) ? "float" : "number"); break;
            default:  kenm_type = "varchar";
        }

        result = { req_info: req_info
                 , module: module
                 , naam: kenm_naam
                 , type: kenm_type
                 , key: kenm_key
                 , sql: "(" + src + ")"
                 }
        break;
    }
}

if (req_info != "kenmerkwaarde")
{
  var oRs = Oracle.Execute(sql);
  while (!oRs.eof)
  {
    result.lov.push( { key: oRs("key").Value, oms: oRs("oms").Value });
    oRs.MoveNext();
  }
  oRs.Close();
  result.sql = sql;
}


Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
