// ******************************************
// * $Id$
// *
// *   flexfile_converter()
// *
// * 1. Stores path and filename in table fac_bijlagen.
// * 2. Links fac_bijlagen record to flexfile.
// *
// ******************************************
(function ()
    {
        var L = {
            main: function ()
            {
                /* global */ PUO_LOGPATH = "./";
                Log2File(0, "*> process");
                var result = {message: "", success: false};
                try
                {
                    var path = L.getRootPath();
                    Log2File(0, path);
                    L.scanFolder(path, 0);
                    result.success = true;
                }
                catch(e)
                {
                    Log2File(0, "Error in process: " + e.description);
                    result.message = "Error in process: " + e.description;
                }
                Log2File(0, "*< process");
                return result;
            },

            scanFolder: function _scanFolder(thisPath, lvl)
            {
                var ffso = new ActiveXObject("Scripting.FileSystemObject");
                var thisFolder = ffso.GetFolder(thisPath);
                var enumFolder = new Enumerator(thisFolder.SubFolders);
                for (; !enumFolder.atEnd(); enumFolder.moveNext())
                {
                    var folder = enumFolder.item();
                    if (folder.name == "DWF" || folder.name == "CAD")
                    {
                        Log2File(0, "Skipping " + thisPath + folder.name);
                        continue;
                    }
                    if (folder.name.match(/__NEW__/))
                    {
                        Log2File(0, "Skipping " + thisPath + folder.name);
                        continue;
                    }


                    var nextPath = thisPath + folder.name + "/";

                    L.scanFolder(nextPath, lvl+1);

                    var nextFolder = ffso.GetFolder(nextPath);
                    var enumFile = new Enumerator(nextFolder.Files);
                    for (; !enumFile.atEnd(); enumFile.moveNext())
                    {
                        var thisFile = enumFile.item();
                        L.processFile(nextPath, thisFile);
                    }
                }
            },

            processFile: function _processFile(thisPath, thisFile)
            {
                var flex_obj = L.getFlexKenmerk(thisFile);

                if (flex_obj)
                {
                    try
                    {
                        var sql = "SELECT COUNT(*) cnt"
                                + "   FROM fac_bijlagen"
                                + "  WHERE fac_bijlagen_module = " + safe.quoted_sql(flex_obj.module)
                                + "    AND fac_bijlagen_kenmerk_key = " + flex_obj.kenmerk_key
                                + "    AND fac_bijlagen_refkey = " + flex_obj.deel_key
                                + "    AND fac_bijlagen_disk_directory = " + safe.quoted_sql(flex_obj.file_path)
                                + "    AND fac_bijlagen_filename = " + safe.quoted_sql(flex_obj.file_name)
                        var oRs = Oracle.Execute(sql);
                        if (oRs("cnt").Value > 0)
                        {
                            Log2File(0, "DUBBEL: m:"+flex_obj.module +" p:"+flex_obj.niveau +" d:"+flex_obj.deel_key+" k:"+flex_obj.kenmerk_key +" f:"+flex_obj.file_name);
                            return;
                        }
                        //Log2File(0, " z:"+flex_obj.file_size+" t:"+toDateTimeString(flex_obj.file_date)+":"+flex_obj.file_path);
                    }
                    catch (e)
                    {
                        Log2File(0, "Niet kunnen vinden:  m:"+flex_obj.module +" p:"+flex_obj.niveau +" d:"+flex_obj.deel_key +" k:"+flex_obj.kenmerk_key +" f:"+thisFile.Name +" z:"+thisFile.Size + "e:" + e.description);
                    };
                    try
                    {
                        var oCrypto = new ActiveXObject("SLNKDWF.Crypto");
                        var sql = "BEGIN "
                                + "  flx.setflexbijlage"
                                + "      ( " + safe.quoted_sql(flex_obj.module)
                                + "      , " + flex_obj.kenmerk_key
                                + "      , " + flex_obj.deel_key
                                + "      , " + safe.quoted_sql(flex_obj.file_path)
                                + "      , " + "NULL"
                                + "      , " + safe.quoted_sql(flex_obj.file_name)
                                + "      , " + flex_obj.file_size
                                + "      , TO_DATE('" + toDateTimeString(flex_obj.file_date) + "', 'yyyy-mm-dd hh24:mi:ss')"
                                + "      , " + safe.quoted_sql(oCrypto.hex_sha1_file(flex_obj.digestpath))
                                + "      );"
                                + " END;";
                        Oracle.Execute(sql);
                        Log2File(0, "TOEGEVOEGD: m:"+flex_obj.module +" p:"+flex_obj.niveau +" d:"+flex_obj.deel_key+" k:"+flex_obj.kenmerk_key +" f:"+flex_obj.file_name);
                        //Log2File(0, " z:"+flex_obj.file_size+" t:"+toDateTimeString(flex_obj.file_date)+":"+flex_obj.file_path);
                    }
                    catch (e)
                    {
                        Log2File(0, "Niet toegevoegd: m:"+flex_obj.module +" p:"+flex_obj.niveau +" d:"+flex_obj.deel_key +" k:"+flex_obj.kenmerk_key +" f:"+thisFile.Name +" z:"+thisFile.Size + "e:" + e.description);
                    };
                }
            },

            getFlexKenmerk: function _getFlexKenmerk(fileObj)
            {
                var v_path = fileObj.ParentFolder.Path.replace(/\\/g, "/");
                var v_path_arr = v_path.split("/");
                var objFlex = null;

                var v_kenmerk_key = v_path_arr[v_path_arr.length-1];
                var v_deel_key    = v_path_arr[v_path_arr.length-2];
                var v_module      = v_path_arr[v_path_arr.length-4].toUpperCase();
                var v_prefix = "";

                var err = "";
                if (!parseInt(v_kenmerk_key))
                {
                    err = "kenmerk";
                }
                if (!parseInt(v_deel_key))
                {
                    if (!parseInt(v_deel_key.slice(1)))
                        err = "object";
                    else
                    {
                        v_prefix = v_deel_key.slice(0,1).toUpperCase();
                        v_deel_key = parseInt(v_deel_key.slice(1));
                    }
                }

                if (!err)
                {
                    objFlex = { module: v_module
                              , niveau: v_prefix
                              , deel_key: v_deel_key
                              , kenmerk_key: v_kenmerk_key
                              , file_path: v_path.substr(L.getRootPath().length)
                              , file_name: fileObj.Name
                              , file_size: fileObj.Size
                              , file_date: new Date(fileObj.DateCreated)
                              , digestpath: v_path + "/" + fileObj.Name
                              };
                }
                else
                {
                    Log2File(0, "Geen "+err+" in pad: "+ v_path);
                }
                return objFlex;
            },

            getRootPath: function _getRootPath()
            {
                var path = S("flexfilespath")
                if (path.substr(path.length-1,1) != "/" )
                    path += "/";
                //      Log2File(0, path);
                return path;
            }
        }
        return L;
    }
).call();
