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

    File:           cad_thema.asp

    Description:    fac_management aanroep van model_cad_thema

    Context:

    Notes:
*/
%>
<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../mgt/mgt_tools.inc" -->
<!-- #include file="../api2/model_cad_thema.inc" -->
<%
var this_model = model_cad_thema;

this_model.hook_pre_put = function(params, jsondata, key)
{   // update
    refresh_legenda(jsondata);
}
this_model.hook_pre_post = function(params, jsondata)
{   // insert
    refresh_legenda(jsondata);
}

scaffolding(this_model,
    {
        "search": {
            "autosearch": true,
            "filters": [
                "name",
                "legenda",
                "type_place",
                "type_situation"
                ]
        },
        "list": {
            "columns": [
                "id",
                "name",
                "legenda",
                "type_place",
                "type_situation"
            ]
          }
    });


    function refresh_legenda(data)
    {
        if (data._generate == 1)
            GenerateLegenda(data);
        if (data._update == 1)
            UpdateLegenda(data);
    }

    function GenerateLegenda(data)
    {   // Genereer een legenda aan de hand van voorkomende waarden.
        //
        var rndSQL = "to_char(dbms_utility.get_hash_value(WAARDE,0,power(2,24)), '0XXXXX')";
        var legendaName = "Legenda_" + Date.parse(new Date());
        var sql = "INSERT INTO cad_legenda"
                + " (cad_legenda_omschrijving, cad_legenda_discreet)"
                + " VALUES"
                + "(" + safe.quoted_sql(legendaName)
                + ", 1)";
        Oracle.Execute(sql);

        sql = "SELECT cad_legenda_key"
            + "  FROM cad_legenda"
            + " WHERE cad_legenda_omschrijving = " + safe.quoted_sql(legendaName)
        var oRs = Oracle.Execute(sql);
        var legenda_key = oRs("cad_legenda_key").Value;

        sql = "INSERT INTO cad_legendawaarde"
            + " ( cad_legenda_key"
            + " , cad_legendawaarde_value"
            + " , cad_legendawaarde_rgb"
            + " , cad_legendawaarde_omschr"
            + " ) SELECT DISTINCT " + legenda_key
            + "        , waarde"
            + "        , " + rndSQL
            + "        , waarde"
            + "     FROM " + data.view_name

        Oracle.Execute(sql);

        data.legenda = legenda_key;
    }

    function UpdateLegenda(data)
    {   // Breid een legenda uit aan de hand van voorkomende nieuwe waarden.
        var rndSQL = "to_char(dbms_utility.get_hash_value(WAARDE,0,power(2,24)), '0XXXXX')";

        var sql = "INSERT INTO cad_legendawaarde"
                + " ( cad_legenda_key"
                + " , cad_legendawaarde_value"
                + " , cad_legendawaarde_rgb"
                + " , cad_legendawaarde_omschr"
                + " ) SELECT DISTINCT " + data.legenda
                + "        , waarde"
                + "        , " + rndSQL
                + "        , waarde"
                + "     FROM " + data.view_name + " thv"
                + "    WHERE waarde IS NOT NULL "
                + "      AND NOT EXISTS (SELECT cl.cad_legendawaarde_value"
                                    + "    FROM cad_legendawaarde cl"
                                    + "   WHERE cl.cad_legendawaarde_value = thv.waarde"
                                    + "     AND cl.cad_legenda_key = " + data.legenda
                                    + " )";
        Oracle.Execute(sql);
    }
%>
