// GetFlexfiles.js
// Haalt de bestandsnamen van de flexfiles op.
//
// $Revision$
// $Id$
//
// Opzet in kader van UWVA#33705
// parameters:  flexpath  in welke flexfiles map er gezocht moet worden

var flex   = WScript.Arguments(0);
var nradded = scanFlexFolder(flex);
WScript.Quit(0);

function scanFlexFolder(flexpath)
{
    var nradded = 0;
    var scanDir = "../"+flexpath+(flexpath.slice(flexpath.length-1)=="/"?"":"/");
    WScript.Echo("------------- ["+ scanDir + "] -------------")

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

        try
        {
            var sql = "DELETE FROM UWVA_FLEXFILES"; // TODO: alleen voor flexpath/module
            __Log(sql);
            Oracle.Execute(sql);
            //
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            if (fso.FolderExists(scanDir))
                scanFolder(scanDir, 0);
            else
                __Log("Folder does not exist: "+scanDir);
            //
            Oracle.Close();
        }
        catch (e)
        {
          __Log("Error clearing table: " + e.description);
        };
    }
    catch (e)
    {
        __Log("Cannot open connection with udl: " + udl);
        __Log("Error: " + e.description);
        WScript.Quit(1);
    }
    //
    __Log("Found " + nradded + " files in " + scanDir);
    return nradded;

    function scanFolder(scanDir, lvl)
    {
        //WScript.Echo(lvl+" scanDir:"+scanDir);
        var startFolder = fso.GetFolder(scanDir);
        var fc1 = new Enumerator(startFolder.SubFolders);
        for (; !fc1.atEnd(); fc1.moveNext())
        {
            var fol = fc1.item();
            var fpath = scanDir + fol.Name + "/";
            //
            scanFolder(fpath, lvl+1);
            //
            var currFolder = fso.GetFolder(fpath);
            var fc2 = new Enumerator(currFolder.Files);
            for (; !fc2.atEnd(); fc2.moveNext())
            {
                var fil = fc2.item();
                var fname = fil.Name;
                var fsize = fil.Size;
                var pathArr = fpath.split("/");
                //
                var kenmerk_key = pathArr[pathArr.length-2];
                var object_key  = pathArr[pathArr.length-3];
                var module      = pathArr[pathArr.length-5].toUpperCase();
                var prefix      = "";
                //
                var err = "";
                if (!parseInt(kenmerk_key))
                {
                    err = "kenmerk";
                }
                if (!parseInt(object_key))
                {
                    if (!parseInt(object_key.slice(1)))
                        err = "object";
                    else
                    {
                        prefix = object_key.slice(0,1).toUpperCase();
                        object_key = parseInt(object_key.slice(1));
                    }
                }

                if (!err)
                {
                    try
                    {
                        var sql_i = "INSERT INTO UWVA_FLEXFILES"
                                  + " (module, prefix, object_key, kenmerk_key, file_name, file_size)"
                                  + " VALUES"
                                  + "(" + safe.quoted_sql(module)
                                  + "," + safe.quoted_sql(prefix)
                                  + "," + object_key
                                  + "," + kenmerk_key
                                  + "," + safe.quoted_sql(fname)
                                  + "," + fsize
                                  + ")";
                        Oracle.Execute(sql_i);
                        nradded ++;
                    }
                    catch (e)
                    {
                        __Log("Error:" + e.description);
                        WScript.Echo("Niet toegevoegd: ", module, prefix, object_key, kenmerk_key, fname, fsize);
                    };
                }
                else
                {
                    if (fpath.indexOf("__NEW__") == -1)
                        WScript.Echo("Geen "+err+" in pad: "+ fpath);
                }
            }
        }
    }
}
