// Smoelen.js
// Importeer foto's en plaats ze in de flexfiles in de smoelen folderstructuur
//
// $Revision$
// $Id$
//
// Smoelen import
//
// Opzet in kader van UWVA#19552
// Twee parameters: UDL voor database connectie en pad naar import(webdav) folder

var str = WScript.Arguments(0); // udlpath
var imppad = WScript.Arguments(1); // pad naar import(webdav) folder
var smlpath = "../flexfiles/PRS/P0000___/P"; // + pKey + "/SML/";
var smlpath = "../flexfiles/PRS/"

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

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

fso = new ActiveXObject("Scripting.FileSystemObject")

startFolder = fso.GetFolder(imppad)
var fc1 = new Enumerator(startFolder.Files);
for (; !fc1.atEnd(); fc1.moveNext())
{
  fil = fc1.item();
  WScript.Echo("File: " + fil);
  filename = fil.Name.toUpperCase();
  if (filename.match(/.*\.jpg$/i))
  {
      fname = filename.substring(0, filename.length-4).toUpperCase();
      sql = "select prs_perslid_key, prs_perslid_voornaam, prs_perslid_naam"
          + "  from prs_perslid"
          + " where prs_perslid_oslogin = '"+fname.replace(/\'/g,"''")+"'";
      oRs = Oracle.Execute(sql)
      if (!oRs.Eof)
      {
        WScript.Echo("Foto " + fil.name + " hoort bij key " + oRs("prs_perslid_key").Value + ": " + oRs("prs_perslid_voornaam").Value + " " + oRs("prs_perslid_naam").Value);
        var AttachPath = smlpath + subfolderKey("P", oRs("prs_perslid_key").Value) + "/SML/";
        CreateFullPath(AttachPath);
        WScript.Echo("  verplaatsen naar " + AttachPath);
        if (fso.FileExists(AttachPath + filename))
        {
            WScript.Echo("  deleting previous file");
            fso.DeleteFile(AttachPath + filename);
        }
        fso.MoveFile (fil, AttachPath);

        // Thumb maken
        var thumbpath = AttachPath + "thumb/";
        var oIMG = new ActiveXObject("SLNKDWF.ImageConvert");

        oIMG.Open(AttachPath + filename);
        // WScript.Echo("w=" + oIMG.Width + " h=" + oIMG.Height)
        maxPhotoW = 150;
        maxPhotoH = 200;
        if (oIMG.Width > maxPhotoW || oIMG.Height > maxPhotoH)
        {
            if (oIMG.Width / oIMG.Height > maxPhotoW / maxPhotoH)
            {
                oIMG.Height = oIMG.Height / oIMG.Width * maxPhotoW;
                oIMG.Width = maxPhotoW;
            }
            else
            {
                oIMG.Width = oIMG.Width / oIMG.Height * maxPhotoH;
                oIMG.Height = maxPhotoH;
            }
            oIMG.SaveAs(AttachPath + filename);
        }

        // Thumb altijd
        maxThumbW = 60;
        maxThumbH = 80;
        if (oIMG.Width / oIMG.Height > maxThumbW / maxThumbH)
        {
          oIMG.Height = oIMG.Height / oIMG.Width * maxThumbW;
          oIMG.Width = maxThumbW;
        }
        else
        {
          oIMG.Width = oIMG.Width / oIMG.Height * maxThumbH;
          oIMG.Height = maxThumbH;
        }
        CreateFullPath(AttachPath + "thumb/");
        oIMG.SaveAs(AttachPath + "thumb/" + filename);

      }
  }
  else
    WScript.Echo("   skipping");
}

function CreateFullPath(sPath)
{
    var oFS = new ActiveXObject("Scripting.FileSystemObject");
    if (!oFS.FolderExists(sPath))
    {
        while (!oFS.FolderExists(sPath))
        {
            var sParent =sPath;
            while (!oFS.FolderExists(sParent))
            {
               var sChild = sParent;
               var sParent = oFS.GetParentFolderName(sChild);
            }
            oFolder = oFS.CreateFolder(sChild)
        }
    }
}

function subfolderKey(pNiveau, pKey)
{
  var keyStr = "0000000" + pKey;
  var subfolder = pNiveau + keyStr.substr(keyStr.length-7,4) + "___/" + pNiveau + pKey;
  return subfolder;
}

