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

    File:               fac_edit_api_save
    Description:        SUBMIT-form

    Parameters:
    Context:
    Note:
*/
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/save2db.inc" -->
<!-- #include file="../Shared/json2.js" -->

<% FCLTHeader.Requires({ plugins:["jQuery"] }) %>
<%
var api_key = getQParamInt("api_key", -1 );

var hasFACFAC = user.checkAutorisation("WEB_FACFAC");

var viewoptions = getFParam("fac_api_options_json", "");
if (viewoptions)
{
    try
    {
        viewoptions = JSON.stringify(eval("(" + viewoptions + ")")); // test en formatteer plat
        if (viewoptions == "null")
            viewoptions = "";
    }
    catch (e)
    {
        abort_with_warning("Invalid JSON "  + L("lcl_fac_api_options") + "\n" + e.description);
    }
}
var viewjson = getFParam("fac_api_viewmapping_json", "");
if (viewjson)
{
    try
    {
        viewjson = JSON.stringify(eval("(" + viewjson + ")")); // test en formatteer plat
        if (viewjson == "null")
            viewjson = "";
    }
    catch (e)
    {
        abort_with_warning("Invalid JSON "  + L("lcl_fac_api_viewmapping") + "\n" + e.description);
    }
}

var fields =  [ { dbs: "fac_api_name",             typ: "varchar",  frm: "fac_api_name" },
                { dbs: "fac_api_filepath",         typ: "varchar",  frm: "fac_api_filepath" },
                { dbs: "fac_api_loglevel",         typ: "number",   frm: "fac_api_loglevel" },
                { dbs: "fac_usrrap_key",           typ: "key",      frm: "fac_usrrap_key" },
                { dbs: "fac_api_viewmapping_json", typ: "varchar",  val: viewjson, len: 4000},
                { dbs: "fac_api_stylesheet",       typ: "varchar",  frm: "fac_api_stylesheet"},
                { dbs: "fac_import_app_key",       typ: "key",      frm: "fac_import_app_key"},
                { dbs: "fac_api_options_json",     typ: "varchar",  val: viewoptions, len: 4000},
                { dbs: "fac_api_omschrijving",     typ: "varchar",  frm: "fac_api_omschrijving", len: 60}];

var warning = "";
if (api_key > 0)
{
  sql = buildUpdate("fac_api", fields)
      + "     fac_api_key = " + api_key;
  var err = Oracle.Execute(sql, true);

  if (err.friendlyMsg)
      abort_with_warning(err.friendlyMsg);
}
else
{
  fields.push({  dbs: "fac_api_key",  typ: "key", seq: "fac_s_fac_api_key" });
  var regIns = buildInsert("fac_api", fields);
  var api_key = regIns.sequences["fac_api_key"];
  sql = regIns.sql;
  var err = Oracle.Execute(sql, true);
  if (err.friendlyMsg)
    abort_with_warning(err.friendlyMsg);
}

doneParams = { api_key: api_key,
               warning: warning,
               success: true
             }
Response.Write(JSON.stringify(doneParams));
Response.End;
%>