/*
    $Revision$
    $Id$

    File:           ExchangeAll.js

    Description:    Exchange EWS koppeling
                    Maakt vanuit database een tmp_Exchange_all.bat in de huidige
                    folder met voor elke ruimte met extern_id een regel

    Parameters:     (net als gen_import.wsf)
                    0: customerId
                    1: fac_import_app_key/code voor de EXCHANGE import
                    2: fac_import_app_key/code voor de EXCHSYNC import
*/

var fso = new ActiveXObject("Scripting.FileSystemObject");
if (WScript.Arguments.length < 3)
{
    __Log("Usage: CScript ExchangeAll.js CUST EXCHANGE EXCHSYNC");
    WScript.Quit(1);
}
var customerId = WScript.Arguments(0);
var import_app_key = WScript.Arguments(1);
var sync_app_key = WScript.Arguments(2);

var batfile = "./tmp_Exchange_all.bat";

function __Log(s)
{
    var flog = fso.OpenTextFile("ExchangeAll.js.log", 8 /* ForAppending */, true /* create */);
    flog.WriteLine(s);
    flog.Close();
}

__Log("Starting rev. $Revision$ at " + new Date());

var udl = "../Oracle.udl";
var Oracle = new ActiveXObject("ADODB.Connection");
Oracle.Open('File Name='+udl);

var sql = "SELECT res_ruimte_key,"
        + "       res_ruimte_extern_id,"
        + "       res_ruimte_syncstate"
        + "  FROM res_ruimte"
        + " WHERE res_ruimte_verwijder IS NULL"
        + "   AND res_ruimte_extern_id IS NOT NULL";

var oRs = Oracle.Execute(sql);

var fbat = fso.CreateTextFile(batfile, true /* overwrite */);
while (!oRs.Eof)
{
    var line = 'CScript /NoLogo ..\\..\\..\\utils\\Exchange\\Exchange.js "'
                    + oRs("res_ruimte_extern_id").Value + '" "' + (oRs("res_ruimte_syncstate").Value||"") + '"';
    fbat.WriteLine(line);
    var line = 'Cscript /NoLogo ..\\..\\..\\utils\\gen_import\\gen_import.wsf '
                    + customerId + ' ' + sync_app_key + ' ' + oRs("res_ruimte_key").Value + '>>ExchangeAll.log 2>>&1';
    fbat.WriteLine(line);
    oRs.MoveNext();
}

// Eén import aan het eind; de room-id is toch uit de XML te halen
var line = 'Cscript /NoLogo ..\\..\\..\\utils\\gen_import\\gen_import.wsf '
              + customerId + ' ' + import_app_key + ' >>ExchangeAll.log 2>>&1';
fbat.WriteLine(line);

fbat.Close();
oRs.Close();

__Log("Done at " + new Date());
