<% /*
     $Revision$
     $Id$

     File:          api_gen_import.inc
     Description:   API functie voor het importeren van data uit xmlbestanden.
                    Werking werkt naar analogie van gen_import.
     Parameters:    API : new API_func(xxx)
                    data: (text of stream)
     Status:
     Context:       Ook vanuit APPL\MARX\API_mareon_opdracht.asp
*/

// result: { xmldom: object, anyError: boolean }
function api_gen_import(API, data, charset) //
{
    var fileStream = Server.CreateObject("ADODB.Stream");
    if (typeof data == "string")
    {
        fileStream.Type = 2; // adTypeText
        fileStream.Open();
        fileStream.CharSet = charset || "Windows-1252";
        fileStream.WriteText(data);
    }
    else
    {
        fileStream.Type = 1; // adTypeBinary eerst nog
        fileStream.Open();
        fileStream.Write(data);
    }
    if (API.apidata.loglevel & 1 || __Logging > 0)
    {
        var savename = __Log2FileName("{0}_{1}".format(API.APIname, user_key));
        fileStream.SaveToFile(safe.UNC(savename), 2); //  2=adSaveCreateOverWrite
        __DoLog("Import file was saved in {0}".format(savename), "#ff4");
    }

    var import_app_key = API.apidata.options['import_app_key'];
    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,
                                              stylesheet: API.apidata.stylesheet,
                                              keep_backup: true // mits fac_import_app_folder gezet
                                            });
    var import_key = res.import_key;

    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;
        __DoLog("Import (app_key={0}) mislukt: {1}".format(import_app_key, warn), "#ffff00");
        API.error(warn); // Abort met 500-status
    }

    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);

    if (import_key > 0)
    {
        var errorCount = 0; // hoeveel errors?
        var elementDetails = xmlResp.createElement("details");
        var sql = "SELECT imp_log_status,"
                + "       imp_log_omschrijving,"
                + "       imp_log_hint,"
                + "       imp_log_status"
                + "  FROM imp_log"
                + " WHERE fac_import_key = " + import_key
                + "   AND imp_log_status <> 'I'";
        var oRs = Oracle.Execute(sql);
        while (!oRs.Eof)
        {
           var elementDetail = xmlResp.createElement("detail");
           var elementStatus = xmlResp.createElement("status");
           var elementStatusText = xmlResp.createTextNode(oRs("imp_log_status").Value);
           elementStatus.appendChild(elementStatusText);
           elementDetail.appendChild(elementStatus);
           if (oRs("imp_log_omschrijving").Value != null )
           {
              var elementOms = xmlResp.createElement("omschrijving");
              var elementOmsText = xmlResp.createTextNode(oRs("imp_log_omschrijving").Value);
              elementOms.appendChild(elementOmsText);
              elementDetail.appendChild(elementOms);
           }
           if (oRs("imp_log_hint").Value != null )
           {
              var elementHint = xmlResp.createElement("hint");
              var elementHintText = xmlResp.createTextNode(oRs("imp_log_hint").Value);
              elementHint.appendChild(elementHintText);
              elementDetail.appendChild(elementHint);
           }
           if (oRs("imp_log_status").Value != null )
           {
              if (oRs("imp_log_status").Value == 'E' || oRs("imp_log_status").Value == 'F')
                errorCount ++
              var elementStatus = xmlResp.createElement("status");
              var elementStatusText = xmlResp.createTextNode(oRs("imp_log_status").Value);
              elementStatus.appendChild(elementStatusText);
              elementDetail.appendChild(elementStatus);
           }
           elementDetails.appendChild(elementDetail);
           oRs.moveNext();
        }
        oRs.Close();

        // De import database procedure mag in fac_result onder sessionid IMPORT_<importkey)
        // een aantal naam/waarde records aanmaken die als <naam>waarde</naam> in de xml terecht komen
        var sql = "SELECT fac_result_naam,"
                + "       fac_result_waarde"
                + "  FROM fac_result"
                + " WHERE fac_result_sessionid = 'IMPORT_{0}'".format(import_key);
        var oRs = Oracle.Execute(sql);
        if (!oRs.Eof)
        {
           var elementResults = xmlResp.createElement("result");
            while (!oRs.Eof)
            {
                var elementResult = xmlResp.createElement(oRs("fac_result_naam").Value);
                var elementResultText = xmlResp.createTextNode(oRs("fac_result_waarde").Value);
                elementResult.appendChild(elementResultText);
                elementResults.appendChild(elementResult);
                oRs.moveNext();
            }
            elementDetails.appendChild(elementResults);
        }
        oRs.Close();

        FCLTElement.appendChild(elementDetails);
    }

    xmlResp.appendChild(FCLTElement);
    return { xmldom: xmlResp, anyError: errorCount > 0 };
}
%>
