// $Revision$
// $Id$
//
// Periodic Notifications Generator for Facilitor
//
// To be scheduled at a webserver, requires access to the Facilitor database
//
// Invokes the internal notificationjobsgenerator to process the query-jobs as
// defined by the entries in fac_notificatie_job, like reminder-jobs or
// time-scheduled events. These notifications are messages meant for humans
// (e-mails, SMS), but also tech-messages to others systems. Therefor it is possible
// to use various notification-queues, typically scheduled at different intervals.
//
// This needs to be scheduled at the desired interval, the default being every hour.
// Note that depending on the jobs these may take some time, hence it is important that
// this tasks and the jobs are performance-aware, as is anything.
//
// Roept de inwendige notificationjobsgenerator aan, die controleert of er
// notificaties te doen zijn en deze dan ook in de queue plaatst. Deze moet periodiek
// aangeroepen worden. De beheerder kan middels de parameters in fac_notificatie_job
// de periodiciteit etc. van de jobs aanpassen, in veelvouden van de frequentie waarop
// deze taak wordt aangeroepen
//
// Parameters:
//  udlfile     voor database connectie
//  CustId      voor het logpad
//  QueueId     optioneel voor specifieke jobqueue (fac_notificatie_job_queue), anders 'DEFAULT'

var fso = new ActiveXObject("Scripting.FileSystemObject");

function __Log(s, params)
{
    var params = params || {}; // default logging met timestamp.
    params.severity = params.severity || "I";
    var logPath = "../../../temp/" + CustId;
    var flog = fso.OpenTextFile(logPath + "/gennotify.log", 8 /* ForAppending */, true /* create */);
    flog.WriteLine(toDateTimeString(new Date) + ";" + params.severity + ";" + s.replace(/\n/g, "<nl>"));
    flog.Close();
}

function isOffline()
{
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    if (fso.FileExists("../offline.html"))
        return true;
    if (fso.FileExists("../../offline.html"))
        return true;
    if (fso.FileExists("../../../temp/offline.html"))
        return true;

    return false;
}

if (isOffline())
{
    WScript.Echo("Facilitor is offline, gen_notify skipped"); // geen __Log omdat CustId nog niet bekend is
    WScript.Quit(1);
}

var udlfile = WScript.Arguments(0);
if (WScript.Arguments.length >= 2)
    var QueueId = WScript.Arguments(1);
else
    var QueueId = 'DEFAULT';

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

var sql = "SELECT fac_version_cust FROM fac_version";
var oRs = Oracle.Execute(sql);
var CustId = oRs(0).Value;
oRs.Close();

if (QueueId == CustId) // vroeger was de tweede parameter verplicht CustId. Tegenwoordig niet meer maar zorg voor backwards compatibiliteit
    QueueId = 'DEFAULT';

__Log("Starting " + CustId + "/" + QueueId + " at " + new Date());
var WshNetwork = new ActiveXObject("WScript.Network");
__Log("User: " + WshNetwork.UserDomain + "\\" + WshNetwork.UserName);

try
{
    sql = "BEGIN fac.initsession(NULL); fac.putnotificationjobs('"+QueueId+"'); END;";
    oRs = Oracle.Execute(sql)
    var sql = "UPDATE fac_version"
            + "   SET fac_version_notify_date = SYSDATE";
    Oracle.Execute(sql);
}
catch(e)
{
    __Log("ERROR: " + e.description, {severity: "E"})
    throw e;
}

function padout(number)
{ return (number < 10) ? "0" + number : number;
}
// Noot: altijd jaar vooraan, goed voor logfiles, niet bedoeld voor presentatie
function toDateString(jsDate)
{
    return padout(jsDate.getFullYear())  + "-" + padout(jsDate.getMonth() + 1) + "-" + padout(jsDate.getDate());
}
function toDateTimeString(jsDate)
{
    return toDateString(jsDate) + " "
         + padout(jsDate.getHours()) + ":" + padout(jsDate.getMinutes())   + ":" + padout(jsDate.getSeconds());
}

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