// GetCatalogus.js
// Haalt catalogus op mbv http. Kan daarna via GEN_IMPORT ingelezen worden
//
// $Revision$
// $Id$
//
// Opzet in kader van UWVA#14582
// Twee parameters: UDL voor database connectie en optioneel proxy-ip

var str = WScript.Arguments(0); // udlpath

if (WScript.Arguments.length > 1)
    var proxyip=WScript.Arguments(1);
else
    var proxyip="";

var debugFiddler = false;

// Haal van URL CatalogURL een catalogus met naam CatalogName en sla op in bestand CatalogFile
function GetCatalogus(CatalogURL, CatalogName, CatalogFile)
{
    var XMLReq=  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                +"<facilitor>"
                +"  <catalog_request>"
                +"      <name>"+CatalogName+"</name>"
                +"  </catalog_request>"
                +"</facilitor>";

    var objXMLHTTP = new ActiveXObject("MSXML2.ServerXMLHTTP.6.0")

    var SXH_PROXY_SET_PROXY = 2;
    if (debugFiddler)
    {
        var SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS = 13056;
        objXMLHTTP.setProxy(SXH_PROXY_SET_PROXY, "127.0.0.1:8888");
        objXMLHTTP.setOption(2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS);
    }
    else
        if (proxyip!="")
        {
            objXMLHTTP.setProxy(SXH_PROXY_SET_PROXY, proxyip)
        }

    objXMLHTTP.open("PUT", CatalogURL, false);
    objXMLHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
    objXMLHTTP.send(XMLReq);
    if (objXMLHTTP.status==200)
    {   // Geldigheid XML wordt gecontroleerd door GEN_IMPORT
        WScript.Echo("Gelukt, status = " + objXMLHTTP.status);
        oStream = new ActiveXObject("ADODB.Stream");
        oStream.Open();
        oStream.Type = 1; // adTypeBinary
        oStream.Write(objXMLHTTP.responseBody); // responseText geeft encoding problemen!
        oStream.SaveToFile (CatalogFile, 2); // adSaveCreateOverWrite
        oStream.Close();
    }
    else
    {
        WScript.Echo("Mislukt, status = " + objXMLHTTP.status);
        WScript.Echo(objXMLHTTP.statusText);
    }
}

var udlstr = 'File Name='+str;
var Oracle = new ActiveXObject("ADODB.Connection");
WScript.Echo(udlstr);
Oracle.Open(udlstr);

var sql = "ALTER SESSION SET nls_territory='AMERICA'";
var oRs = Oracle.Execute(sql)

// prs_bedrijf_xmlcatalogus_loc
sql =  "SELECT   fia.ins_discipline_key, "
     + "         pb.prs_bedrijf_xmlcatalogus_loc, pb.prs_bedrijf_order_mode, "
     + "         bd.ins_discipline_omschrijving, bd.ins_discipline_key "
     + "    FROM FAC_IMPORT_APP fia, "
     + "         bes_discipline bd, "
     + "         INS_SRTGROEP isg, "
     + "         INS_SRTDEEL isd, "
     + "         PRS_BEDRIJF pb "
     + "   WHERE fac_import_app_code = 'CATALOGUS' " // TODO: Wildcard?
     + "     AND fia.ins_discipline_key = bd.ins_discipline_key "
     + "     AND bd.ins_discipline_key = isg.ins_discipline_key "
     + "     AND isg.ins_srtgroep_key = isd.ins_srtgroep_key "
     + "     AND isd.prs_bedrijf_key = pb.prs_bedrijf_key "
     + "     AND pb.prs_bedrijf_xmlcatalogus_loc IS NOT NULL "
     + "GROUP BY fia.ins_discipline_key, "
     + "         pb.prs_bedrijf_xmlcatalogus_loc, "
     + "         pb.prs_bedrijf_order_mode, "
     + "         bd.ins_discipline_key, "
     + "         bd.ins_discipline_omschrijving "

//WScript.Echo (sql)

WScript.Echo(new Date());
try
{
    oRs = Oracle.Execute(sql);
    while (!oRs.eof)
    {
        WScript.Echo("Inlezen catalogus '" + oRs("ins_discipline_omschrijving") + "' van " + oRs("prs_bedrijf_xmlcatalogus_loc"))
        var filename = "./catalog" + oRs("ins_discipline_key") + ".xml";
        CatalogURL = oRs("prs_bedrijf_xmlcatalogus_loc");
        GetCatalogus(CatalogURL, oRs("ins_discipline_omschrijving"), filename)
        oRs.MoveNext();
    }
    WScript.Quit(0);
}
catch (e)
{
    WScript.Echo("Serieuze fout in GetCatalogus.js: " + e.description)
    WScript.Quit(1);
}