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

     File:          api_gen_import.asp
     Description:   API voor het importeren van data uit xmlbestanden.
                    Werking werkt naar analogie van gen_import.
     Parameters:    Upload van een xml
     Status:
     Context:
     Notes:         Technisch is xml niet per se noodzakelijk, CSV kunnen we ook aan
*/
    DOCTYPE_Disable = 1;
    THIS_FILE = "appl/api/api_gen_import.asp";
%>
<!-- #include file="../../appl/Shared/common.inc" -->
<!-- #include file="../../appl/api/api.inc" -->
<!-- #include file="../../appl/shared/FlexFiles.inc" -->
<script language="javascript" src="../imp/imp_shared.js" runat="server"></script>
<%
    // We sturen het antwoord in UTF-8.
    Session.Codepage = 65001;
    Response.Charset = 'utf-8';

    var API = new API_func();

    var import_app_key = API.apidata.options['import_app_key'];
    var bytes = Request.TotalBytes;
    if (bytes == 0)
    {
        __DoLog("api_gen_import empty body posted", "#ffff00");
        Response.Write("Error: no data posted for API import");
        Response.End; // Grof maar anders AiAi, dat is nog erger
    }

    fileStream = Server.CreateObject("ADODB.Stream");
    fileStream.Type = 1; // adTypeBinary eerst nog
    fileStream.Open();
    fileStream.Write(Request.BinaryRead(bytes));

    if (API.apidata.loglevel & 1)
        fileStream.SaveToFile(Server.MapPath(rooturl + "/temp/") + "/" + customerId + "_" + API.APIname + ".tmp", 2); //  2=create

    var res = impReadStream(fileStream, import_app_key,
                                            { fac_home: Server.MapPath(rooturl + "/") + "/",
                                              filepathname: "API " + API.APIname,
                                              customerId: customerId,
                                              keep_old: 300, // Parallelle import 300 seconden ondersteunen
                                              user_key: user_key,
                                              keep_backup: true // mits fac_import_app_folder gezet
                                            });
    if (res.success)
    {
        var warn = L("lcl_imp_read_lines") + res.read_lines;
        var res = impProcessStream(res.import_key, { customerId: customerId,
                                                     user_key: user_key });
    }
    else
        var warn = res.warning;

    var xmlResp = new ActiveXObject("MSXML2.DOMDocument.6.0");
    xmlResp.appendChild(xmlResp.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""));

    // The response
    var FCLTElement = xmlResp.createElement("facilitor");

    var elementResult = xmlResp.createElement("result");
    var elementResultText = xmlResp.createTextNode(warn);
    elementResult.appendChild(elementResultText);
    FCLTElement.appendChild(elementResult);
    xmlResp.appendChild(FCLTElement);

    Response.ContentType = "text/xml";
    Response.Write(xmlResp.xml);
%>
