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

    File:           api_SOAP.asp
    Description:    API om een SOAP-achtige service te bieden
    Parameters:
    Context:
    Notes:          Zie UWVA/KASPRS voor voorbeeld gebruik
    TODO:           Parameters uit Request-SOAP bericht ook via een XSL er uit vogelen

*/
    DOCTYPE_Disable = 1;
    THIS_FILE = "appl/api/api_soap.asp";
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/xml_converter.inc" -->
<!-- #include file="../api/api.inc" -->

<%
    var API = new API_func();

    Session.Codepage = 65001;
    Response.Charset = 'utf-8';

    // Zet de loglevel in de API op 1 om de xml te loggen naar de TEMP folder
    function XML2TEMP(xml, postfix)
    {
        if (!API.apidata.loglevel) return;
        var in_file = Server.MapPath("./temp") + "/" + customerId + "_" + API.APIname + "_" + postfix + "_";
        var jsDate = new Date();
        var s = String(jsDate.getFullYear()) + padout(jsDate.getMonth() + 1) + padout(jsDate.getDate())
              + padout(jsDate.getHours()) + padout(jsDate.getMinutes()) + padout(jsDate.getSeconds())
        in_file = in_file + s + ".xml";
        //Response.Write(in_file);Response.End;

        var fs  = Server.CreateObject("Scripting.FileSystemObject");
        var ts  = fs.CreateTextFile(in_file, true, -1); // -1 voor de Unicode
        ts.WriteLine (xml);                             // Anders hier Invalid procedure call or argument
        ts.Close();
    }

    Session("logging") = API.apidata.loglevel||0;

    var xmlReq = Server.CreateObject("MSXML2.DOMDocument.6.0");
    xmlReq.load(Request);

    XML2TEMP(xmlReq.xml, "IN");

    var xmlnode = xmlReq.getElementsByTagName("xmlnode")[0].text;
    var beginTag = xmlReq.getElementsByTagName("begindatumtijd")[0].text;
    var eindTag = xmlReq.getElementsByTagName("einddatumtijd")[0].text;
    //var ekey = xmlReq.getElementsByTagName("key")[0].text;
    __Log("xmlnode: " + xmlnode);
    __Log("From: " + beginTag);
    __Log("To: " + eindTag);

    var key = -1;
    var xtrakey = -1;
    var stylesheet = API.apidata.stylesheet;
    var srtnotificatie = "";

    // TODO: View configureerbaar maken
    var whichSQL = "SELECT DISTINCT fac_srtnotificatie_xmlnode xmlnode, fac_tracking_refkey "
                 + "  FROM fac_tracking ft, fac_srtnotificatie fs "
                 + " WHERE     ft.fac_srtnotificatie_key = fs.fac_srtnotificatie_key "
                 + "       AND fs.fac_srtnotificatie_xmlnode = " + safe.quoted_sql(xmlnode)
                 + "       AND fac_tracking_datum BETWEEN TO_DATE("+safe.quoted_sql(beginTag)+", 'YYYY-MM-DD HH24:MI:SS')"
                 + "                                  AND TO_DATE("+safe.quoted_sql(eindTag)+", 'YYYY-MM-DD HH24:MI:SS')"
// UWVA sneller                 + "       AND fac_tracking_key >= 11376454"
                 + " GROUP BY fac_srtnotificatie_xmlnode, fac_tracking_refkey";
    // whichSQL = "SELECT * FROM (" + whichSQL + ") WHERE ROWNUM < 5";

    var oRsW = Oracle.Execute(whichSQL);
    var xml_nodes_arr = [];
    var xml_nodes_dom = new ActiveXObject("MSXML2.DOMDocument.6.0");

    var FCLTElement = xml_nodes_dom.createElement("facilitor");
    var headerDone = false;
    where = "";
    while (!oRsW.Eof)
    {
        var xml_content = make_xml({ xmlnode: oRsW("xmlnode").Value,
                                     key: oRsW("fac_tracking_refkey").Value,
                                     xtrakey: xtrakey,
                                     where: where
                                   });
        // Loggen van de individuele XMLNODE's
        // XML2TEMP(xml_content, "XML_" +  oRsW("xmlnode").Value + "_" + oRsW("fac_tracking_refkey").Value);

        __Log("XML '" + oRsW("xmlnode").Value + "' voor key " + oRsW("fac_tracking_refkey").Value + " is " + xml_content.length + " karakters");

        xml_nodes_arr.push(xml_content); // Eén node
        var xmldoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
        xmldoc.async = false;
        xmldoc.loadXML(xml_content);
        xmldoc.setProperty("SelectionLanguage", "XPath");
        if (!headerDone)
        {
            node = xmlReq.selectSingleNode("facilitor/getinfo");
            //var FCLTElement = xml_nodes_dom.createElement("begintimestamp");
            var sql = "SELECT (TO_DATE("+safe.quoted_sql(beginTag)+", 'YYYY-MM-DD HH24:MI:SS')"
                    +              " - TO_DATE ('01-01-1970', 'DD-MM-YYYY')) * 86400 FROM DUAL"
            var ts = Oracle.Execute(sql);
            var tNode = xml_nodes_dom.createTextNode(ts(0).value);
            bnode = xml_nodes_dom.createElement("begintimestamp");
            bnode.appendChild(tNode)
            node.appendChild(bnode);
            FCLTElement.appendChild(node);

            sql = "SELECT (TO_DATE("+safe.quoted_sql(eindTag)+", 'YYYY-MM-DD HH24:MI:SS')"
                    +              " - TO_DATE ('01-01-1970', 'DD-MM-YYYY')) * 86400 FROM DUAL"
            ts = Oracle.Execute(sql);
            tNode = xml_nodes_dom.createTextNode(ts(0).value);
            bnode = xml_nodes_dom.createElement("eindtimestamp");
            bnode.appendChild(tNode)
            node.appendChild(bnode);
            FCLTElement.appendChild(node);

            node = xmldoc.selectSingleNode("facilitor/header");
            FCLTElement.appendChild(node);
            node = xmldoc.selectSingleNode("facilitor/lcl"); // TODO: Eigenlijk wil je deze mergen over alle xmlNodes heen
            FCLTElement.appendChild(node);
            headerDone = true;
        }
        node = xmldoc.selectSingleNode("facilitor/" + oRsW("xmlnode").Value);
        FCLTElement.appendChild(node);

        oRsW.moveNext();
    }
    oRsW.Close();
    xml_nodes_dom.appendChild(FCLTElement);

    XML2TEMP(xml_nodes_dom.xml, "DATAXML");

    Response.ContentType = "text/xml";
    mode = xmlnode; // is dit logisch? PF: volgens mij helemaal niet
    STR2Stream(xml_nodes_dom.xml, stylesheet, Response, mode, srtnotificatie);

    Session.Abandon(); // Voor de zekerheid
%>