<% /*
    $Revision$
    $Id$

    File:           model_notes.inc

    Description:    notes model.
    Parameters:
    Context:

    Notes:          table en fields worden dynamisch per module bepaald
*/

/*
alle notes in 1 view
CREATE OR REPLACE VIEW fac_v_notes
( module, key, parent_key, writer_key, aanmaak, omschrijving)
AS
SELECT 'MLD'
     , mld_melding_note_key
     , mld_melding_key
     , prs_perslid_key
     , mld_melding_note_aanmaak
     , mld_melding_note_omschrijving
  FROM mld_melding_note
UNION
SELECT 'ORD'
     , mld_opdr_note_key
     , mld_opdr_key
     , prs_perslid_key
     , mld_opdr_note_aanmaak
     , mld_opdr_note_omschrijving
  FROM mld_opdr_note
UNION
SELECT 'FIN'
     , fin_factuur_note_key
     , fin_factuur_key
     , prs_perslid_key
     , fin_factuur_note_aanmaak
     , fin_factuur_note_omschrijving
  FROM fin_factuur_note
UNION
SELECT 'CNT'
     , cnt_contract_note_key
     , cnt_contract_key
     , prs_perslid_key
     , cnt_contract_note_aanmaak
     , cnt_contract_note_omschrijving
  FROM cnt_contract_note
UNION
SELECT 'PRJ'
     , prj_scenario_note_key
     , prj_scenario_key
     , prs_perslid_key
     , prj_scenario_note_aanmaak
     , prj_scenario_note_omschrijving
  FROM prj_scenario_note
*/

%>
<!-- #include file="../mld/mld.inc" -->
<%

function model_notes(module)
{
    this.module =       module;
    // this.table =        "fac_v_notes";
    this.records_name = "notes";
    this.record_name =  "note";

    var tabel = {}; // De tabel met notes die aangepast moet worden.
    switch (module)
    {
        case "MLD":
        {   tabel.naam   = "mld_melding_note";
            tabel.id     = "mld_melding_note_key";
            tabel.seq    = "mld_s_mld_melding_note_key";
            tabel.parent = "mld_melding";
            break;
        }
        case "ORD":
        {   tabel.naam   = "mld_opdr_note";
            tabel.id     = "mld_opdr_note_key";
            tabel.seq    = "mld_s_mld_opdr_note_key";
            tabel.parent = "mld_opdr";
            break;
        }
        case "FIN":
        {   tabel.naam   = "fin_factuur_note";
            tabel.id     = "fin_dactuur_note_key";
            tabel.seq    = "fin_s_fin_factuur_note_key";
            tabel.parent = "fin_factuur";
            break;
        }
        case "CNT":
        {   tabel.naam   = "cnt_contract_note";
            tabel.id     = "cnt_contract_note_key";
            tabel.seq    = "cnt_s_cnt_contract_note_key";
            tabel.parent = "cnt_contract";
            break;
        }
        case "PRJ":
        {   tabel.naam   = "prj_scenario_note";
            tabel.id     = "prj_scenario_note_key";
            tabel.seq    = "prj_s_prj_scenario_note_key";
            tabel.parent = "prj_scenario";
            break;
        }
        default:
            api2.error(500, "Missing module parameter");
    }
    this.table = tabel.naam;
    this.primary = tabel.parent + "_note_key";
    this.fields =
            { "id"          : { dbs: tabel.id,                     typ: "key",     seq:     tabel.seq,     filter: "exact" },
              "author"      : { dbs: "prs_perslid_key",            typ: "key",     foreign: "prs_perslid", filter: "exact" },
              "description" : { dbs: tabel.naam + "_omschrijving", typ: "varchar",                         filter: "like"  },
              "createdate"  : { dbs: tabel.naam + "_aanmaak",      typ: "datetime"},
              "parent"      : { dbs: tabel.parent + "_key",        typ: "key",                             filter: "exact" }
        };

    this.list = {
            "columns": [
              "id",
              "author",
              "description",
              "createdate"
            ]
          };

    function _check_authorization (params, method)
        {
            params.message = "";

            if (params.filter.module == "MLD")
            {
                var autfunction = (params.filter.scope == "fe"? "WEB_MLDFOF" : "WEB_MLDBOF");
                params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch

                switch (method)
                {
                    case "GET":
                        if (params.filter.parent_key)
                        {
                            var mld_key = params.filter.parent_key;
                            var this_mld = mld.func_enabled_melding(mld_key, params);
                            user.auth_required_or_abort(this_mld.canReadNotes);
                        }
                        break;
                    case "DELETE":
                        // Notes mogen niet worden verwijderd.
                        // en verder met de autorisatie van PUT...
                    case "PUT":
                        var mld_key = params.filter.parent;
                        var this_mld = mld.func_enabled_melding(mld_key, params);
                        user.auth_required_or_abort(this_mld.canWriteNotes);
                        params.isNew = false;
                        break;
                    case "POST":
                        var mld_key = params.filter.parent_key;
                        var this_mld = mld.func_enabled_melding(mld_key, params);
                        user.auth_required_or_abort(!this_mld.canWriteNotes);
                        params.isNew = true;
                        break;
                }
                params.func_enabled = this_mld || {};
            }
            else
            {
                // Voor alle andere modules toevoegen niet toestaan.
                if (method == "POST")
                    user.auth_required_or_abort(false);
            }
        }

    // TODO: REST_GET niet (rechtstreeks) toestaan?
    this.REST_GET = function _GET(params)
        {
            var query = api2.sqlfields(params, this );
            var wheres = api2.sqlfilter(params, this);
            query.wheres = query.wheres.concat(wheres);

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

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

            return json;
        };

    this.REST_PUT = function (params, jsondata, the_key) /* update note */
        {
            // Een note kan alleen worden aangepast door de persoon die hem geschreven heeft.
            // Alleen van de meest recente note bij een (melding/opdracht/...) kan de omschrijving worden aangepast.
            //
            var dbfields = api2.update_fields(params, this, jsondata); // Build updater

            var wheres = [ this.id.dbs + " = " + the_key];
            wheres.push("prs_perslid = " + user_key);

            var xxxUpd = buildTrackingUpdate(this.table, wheres.join(" AND " ), dbfields, { noValidateToken: true });

            Oracle.Execute(xxxUpd.sql);

            return { key: the_key };
        };

    this.REST_POST = function (params, jsondata) /* new note */
        {
            params.filter.parent_key = jsondata.parent;
            _check_authorization(params, "POST");
            //
            var dbfields = api2.update_fields(params, this, jsondata); // Build updater
            dbfields["id"] = { dbs: tabel.id, typ: "key", seq: tabel.seq };

            var xxxIns = buildInsert(this.table, dbfields, { noValidateToken: true });
            var new_key = xxxIns.sequences[tabel.id];

            Oracle.Execute(xxxIns.sql);

            return { key: new_key };
        }

     // REST_DELETE = function (params, the_key) /* delete note doen we niet */
}
%>
