Script with iterating database call...

Post Reply
BuckXUK
Newbie
Posts: 16
Joined: Thu Mar 01, 2012 8:43 pm

Script with iterating database call...

Post by BuckXUK »

Hi Everyone



I'm trying to build a script element that will evaluate a directory of jobs, pull key values out of each included file's name and look up two values in a database to determine the file's sort priority. I'll then push that priority to an array for sorting and releasing the jobs into the flow in the correct order.



Here is the script:





const cDebug = -1;

const cError = 3;

const cSuccess = 1;



var jPriority = null;



function pageNumCompare(a, b)

{

return a.pageNum - b.pageNum;

}



function addFilePage(theArray, filename, pagenum)

{

var fp = new Object();

fp.fileName = filename;

fp.pageNum = pagenum;

theArray.push(fp);

}



function jobArrived( s : Switch, job : Job )

{

var myFilePageArray = new Array();



var path = job.getPath();

job.log(1, "path: " + path);

var directory = new Dir(path);

var fileList = directory.entryList("*.pdf", Dir.Files, Dir.Name);

var fileListCount = fileList.length;

job.log(1, "file count: " + fileListCount);



var i;

for (i=0; i < fileListCount; i++) {

var fileName = fileList;

var fileNameSplit = fileName.split("_"); // 1014181_300g_A6-fd-ct_4-4_26.pdf

var stkVal = fileNameSplit[2];

var lytVal = fileNameSplit[3];

var jPriority = chkPriority(stkVal,lytVal);

addFilePage(myFilePageArray, fileName, parseInt(jPriority));

}



job.log(1, "Sorting...");

myFilePageArray.sort(pageNumCompare);



job.log(1, "Sorted list");

for (i=0; i < myFilePageArray.length; i++) {

job.log(1, myFilePageArray.fileName);

job.sendToData(path + "/" + myFilePageArray.fileName);

}

}



function chkPriority(stk,lyt)

{

var datasource = new DataSource();

var theSuccess = datasource.connect("Elanders","","");



var stmt = new Statement(datasource);



var dquery = "SELECT Priority FROM main.`Lookup` WHERE Stock='" + stk + "' AND Layout='" + lyt + "'";



stmt.execute(dquery);



while(stmt.isRowAvailable())

{

stmt.fetchRow();

var jPriority = stmt.getString("Priority");

return jPriority;

}



}





The problem seems to be in the way the first function calls the second function as each of the functions work independently of each other. However when I run the script in SwitchScripter with a test directory set up in a fixture, the SwitchScripter blows up everytime.



When I run it inside a Switch flow, I get a ScriptExecutor has quit unexpectedly error.



Any assistance anyone might be able to offer as to what I'm doing wrong would be greatly appreciated.



Thanks in advance.
Regards,

William Buckingham
BuckXUK
Newbie
Posts: 16
Joined: Thu Mar 01, 2012 8:43 pm

Script with iterating database call...

Post by BuckXUK »

False alarm...I have it working.



Sorry.
Regards,

William Buckingham
Post Reply