<% /*
    $Revision$
    $Id$

    File:           model_issueobjects.inc

    Description:    Meldingobjecten model
    Parameters:
    Context:

    Notes:
*/

model_issueobjects =
{
    table: "mld_melding_object",
    primary: "mld_melding_object_key",
    records_name: "issueobjects",
    record_name: "issueobject",
    fields: {"id"             : { dbs: "mld_melding_object_key", typ: "key",  filter: "exact" },
             "object"         : { dbs: "ins_deel_key",           typ: "key",  foreign: "ins_deel" },
             "mld_melding_key": { dbs: "mld_melding_key",        typ: "key" }
            },

    list: { columns: ["object"] },

    _analyze_fields: function (dbfields, params, jsondata) /* analyseer inkomende data, common voor PUT en POST */
    {
        if (jsondata.id)
        {
            // Bestaande mld_melding_object: PUT
        }
        else
        {   // Nieuwe mld_melding_object: POST
            // Controleer of de combinatie melding_key/object_key al bestaat. Zo ja, dan niet toevoegen.
            var mld_obj_key = -1;
            var sql = "SELECT mld_melding_object_key"
                    + "  FROM mld_melding_object"
                    + " WHERE mld_melding_object_verwijder IS NULL"
                    + "   AND mld_melding_key = " + params.filter.id
                    + "   AND ins_deel_key = " + dbfields[0].val; // TODO: unsafe?
            var oRs = Oracle.Execute(sql);
            if (!oRs.eof)
            {
                mld_obj_key = oRs("mld_melding_object_key").Value;
                params.isNew = false;
            }
            oRs.Close();
        }
    },

    _validate_fields: function (dbfields, params, jsondata) /* valideer dbfields, alle constraints die niet door de database worden afgevangen */
    {
    },

    REST_GET: function _GET(params)
        {
            // Kan alleen vanuit model_issue aangeroepen worden. De autorisatie is daar al geregeld.
            var query = api2.sqlfields(params, model_issueobjects );

            query.wheres.push("mld_melding_object_verwijder IS NULL");

            var wheres = api2.sqlfilter(params, model_issueobjects);
            query.wheres = query.wheres.concat(wheres);

            var sql = "SELECT " + query.selects.join(", ")
                    + "  FROM " + query.tables.join(", ")
                    + " WHERE " + query.wheres.join(" AND " );

            var json = api2.sql2json (params, sql, model_issueobjects );

            return json;
        },

    REST_PUT: function (params, jsondata, the_key) /* update object */
        {
            // Kan alleen vanuit model_issue aangeroepen worden. De autorisatie is daar al geregeld.
            var dbfields = api2.update_fields(params, model_issueobjects, jsondata); // Build updater
            model_issueobjects._analyze_fields(dbfields, params, jsondata);

            var mldUpd = buildTrackingUpdate("mld_melding_object", " mld_melding_object_key = " + the_key, dbfields, { noValidateToken: true });
            Oracle.Execute(mldUpd.sql);

            // Nu geen api2.process_includes() meer aanroepen, anders ga je de objecten zelf ook nog aanpassen.

            return { key: the_key };
        },

    REST_POST: function (params, jsondata, parent_key) /* new object */
        {
            // Kan alleen vanuit model_issue aangeroepen worden. De autorisatie is daar al geregeld.
            params.isNew = true; // negeer eventuele bestaande keys
            var dbfields = api2.update_fields(params, model_issueobjects, jsondata); // Build updater
            model_issueobjects._analyze_fields(dbfields, params, jsondata);

            var new_key = null;
            if (params.isNew)
            {
                dbfields["issue"] = { dbs: "mld_melding_key",        typ: "key", val: parent_key };
                dbfields["id"] = { dbs: "mld_melding_object_key", typ: "key", seq: "mld_s_mld_object_key" };

                var mldIns = buildInsert("mld_melding_object", dbfields, { noValidateToken: true });
                new_key = mldIns.sequences["mld_melding_object_key"];
                Oracle.Execute(mldIns.sql);
            }

            return { key: new_key };
        },

    REST_DELETE: function (params, the_key) /* delete object */
        {
            // Kan alleen vanuit model_issue aangeroepen worden. De autorisatie is daar al geregeld.
            var sql = "DELETE FROM mld_melding_object WHERE mld_melding_object_key = " + the_key;
            Oracle.Execute(sql);
        }
}
%>