<% /*
    $Revision$
    $Id$

    File:           model_cad_tekening.inc

    Description:    Model voor plattegronden
    Parameters:
    Context:

    Notes:          WIP
*/

    DOCTYPE_Disable = true;
    THIS_FILE = "appl/api/model_cad_tekening.inc";

%>
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../Shared/flexfiles.inc" -->
<!-- #include file='../CAD/contour_update.inc' -->
<%

function model_cad_tekening(niveau, params)
{
    // Niveau; [V]erdieping (supported), [T]errein (unsupported), [L]ocatie (unsupported)
    params = params || {};
    this.module =       "CAD";
    this.table =        "cad_tekening";
    this.primary =      "cad_tekening_key";
    this.records_name = "drawings";
    this.record_name =  "drawing";
    this.autfunction =  (params.internal ? false : "WEB_CADFOF"); // we controleren zelf
    this.fields = {
        "id"            : { dbs: "cad_tekening_key",            typ: "key" },
        "name"          : { dbs: "cad_tekening_filenaam",       typ: "varchar" },
        "floor"         : { dbs: "alg_verdieping_key",          typ: "key",     insertonly: true },
        "location"      : { dbs: "alg_locatie_key",             typ: "key",     insertonly: true },
        "importdate"    : { dbs: "cad_tekening_geimporteerd",   typ: "date"}
    };

    // this.REST_GET = function _GET(params) {
    //     // Unsupported
    // };

    this.REST_PUT = function (params, jsondata, the_key) {
        if (!("content" in jsondata)) {
            api2.error(500, "Missing content");
        }
        if (!("name" in jsondata)) { // Dan gebruiken we de huidige naam
            var sql = "SELECT cad_tekening_filenaam"
                    + "  FROM cad_tekening"
                    + " WHERE cad_tekening_key = " + the_key;
            var oRs = Oracle.Execute(sql);
            jsondata.name = oRs("cad_tekening_filenaam").Value;
            oRs.Close();
        }
        if (!("encoding" in jsondata)) {
            jsondata.encoding = "base64"; // Default base64
        }

        // Save on filesystem
        _upload(jsondata);

        var safefilename = safe.filename(jsondata.name);

        // Save in DB (link file to floor)
        var cadfields = [
            { dbs: "cad_tekening_filenaam", typ: "varchar", val: safefilename.replace(/\.dwf$/i, "") }
        ];
        var sql = buildUpdate("cad_tekening", cadfields, { noValidateToken: true }) + " cad_tekening_key = " + the_key;
        Oracle.Execute(sql);

        _procesDWF(the_key, jsondata.floor, safefilename);

        return { key: the_key, warning: "" };
    };

    this.REST_POST = function (params, jsondata) {
        var result = {};
        // Geen key meegegeven, maar we hebben wel maar altijd 1 cad_tekening-record per entiteit
        // Dus laten we eerst opzoeken of we echt niet willen updaten maar inserten;
        var sql = "SELECT cad_tekening_key"
                + "     , cad_tekening_filenaam"
                + "  FROM cad_tekening"
                + " WHERE cad_tekening_verwijder IS NULL"
                + "   AND alg_verdieping_key = " + jsondata.floor; // Momenteel alleen niveau [V]erdieping supported
        var oRs = Oracle.Execute(sql);
        result.cad_tek_key = -1;
        if (!oRs.EoF) {
            result.cad_tek_key = oRs("cad_tekening_key").Value;
            if (!("name" in jsondata)) { // Dan gebruiken we de huidige naam
                jsondata.name = oRs("cad_tekening_filenaam").Value;
            }
        }
        oRs.Close();
        if (result.cad_tek_key > -1) {
            return this.REST_PUT(params, jsondata, result.cad_tek_key);
        }

        // inclusief nieuw cad_tekening-record
        if (!("name" in jsondata)) {
            api2.error(500, "Missing name");
        }
        if (!("content" in jsondata)) {
            api2.error(500, "Missing content");
        }
        if (!("encoding" in jsondata)) {
            jsondata.encoding = "base64"; // Default base64
        }

        // Save on filesystem
        _upload(jsondata);

        var safefilename = safe.filename(jsondata.name);

        // Save in DB (link file to floor)
        var cadfields = [
            { dbs: "cad_tekening_key",      typ: "key",     seq: "cad_s_cad_tekening_key" },
            { dbs: "cad_tekening_filenaam", typ: "varchar", val: safefilename.replace(/\.dwf$/i, "") },
            { dbs: "alg_locatie_key",       typ: "key",     val: getLocation(jsondata.floor) },
            { dbs: "alg_verdieping_key",    typ: "key",     val: jsondata.floor },
            { dbs: "cad_tekening_type",     typ: "varchar", val: "P" }
        ];
        var cadIns = buildInsert("cad_tekening", cadfields, { noValidateToken: true });
        cad_tek_key = cadIns.sequences["cad_tekening_key"];
        var err = Oracle.Execute(cadIns.sql, true);
        if (err.friendlyMsg) {
            result.warning = err.friendlyMsg;
        } else {
            _procesDWF(cad_tek_key, jsondata.floor, safefilename);
        }
        return result;
    };

    this.REST_DELETE = function (params, jsondata, the_key) {
    };

    function _upload(jsondata) {
        var props = flexProps("FGII");
        var safefilename = safe.filename(jsondata.name);
        if (safefilename.split(".").length === 1) { // Geen extensie? Voeg .dwf toe
            safefilename += ".dwf";
        }
        encodedString2File(props.AttachPath + safefilename, jsondata.content, "bin." + jsondata.encoding);
    }

    function _procesDWF(cad_tek_key, floor_key, safefilename) {
        // Scan naar concept
        scanDWF(cad_tek_key, 1);

        // Concept tekening naar actuele tekening
        var result = contour.make_actual(cad_tek_key, safefilename.replace(/\.dwf$/, ""));
        if (result.success) {
            // Gescande oppervlakte naar actuele oppervlakte
            contour.doorvoeren_opp(false, { floor_key: floor_key });
        }
    }

    function getLocation(verdieping_key) {
        var sql = "SELECT g.alg_locatie_key"
                + "  FROM alg_gebouw g"
                + "     , alg_verdieping v"
                + " WHERE g.alg_gebouw_key = v.alg_gebouw_key"
                + "   AND v.alg_verdieping_key = " + verdieping_key;
        var oRs = Oracle.Execute(sql);
        var result = -1;
        if (!oRs.EoF) {
            result = oRs("alg_locatie_key").Value;
        }
        oRs.Close();
        return result;
    }
}
%>
