Page 1 of 1

Jobsort based on XML metadata

Posted: Sat Feb 01, 2014 2:23 pm
by telgrin
Hi, i tried to modify a example script for my flow, but something is wrong in the code.

The error-message is : Error in line 23 of script : Error. Use of undefined variable 'theXML'



It was a flow for xmp and i changed only xmp to xml. Has anyone an idea what i have to change to get this working?







// Is invoked each time a new job arrives in one of the input folders for the flow element.

// The newly arrived job is passed as the second parameter.

function jobArrived( s : Switch, job : Job )

{

// Get the xml dataset associated with this job

var theXml = job.getDataset("XML");



// Only search for a valid outgoing connection if we have XML information in the job in the first place

var theResultConnection = null;

if (theXML != null) {



// Get the value of the XML property we're looking for

var theSortKey = s.getPropertyValue('argSortExpression');

var theSortValue = theXML.getString( theSortKey , null );

if (theSortValue != null) {

s.log( 1, theSortKey + " = " + theSortValue );

theResultConnection = getConnectionByName( s, theSortValue );

} else {

s.log( 1, "XML information for " + theSortKey + " was not found for this job" );

}

} else {

s.log( 1, "No XML information found associated with this job" );

}



// If we found a connection, move the job there. If not move it to the data error connections

if (theResultConnection != null) {

job.sendTo( theResultConnection, job.getPath() );

s.log( 1, "Connection with correct name found; moved job" );

} else {

job.sendToData( 3, job.getPath() );

s.log( 1, "No valid connection found, moved job to error output" );

}

}





// ==================================================================================================

// This function gets the full list of outgoing connections for the given script element and searches through this list for a connection with a given name.

// If such a connection is found it is returned, if nothing is found this function returns null.

// ==================================================================================================

function getConnectionByName( s: Switch, connectionName: String ): Connection

{

var theConnections = s.getOutConnections();

for (var i = 0; i < theConnections.getCount(); i++) {



// Get this connection

var theConnection = theConnections.at(i);

if (theConnection.getName() == connectionName) {

return theConnection;

}

}

return null;

}

Jobsort based on XML metadata

Posted: Mon Feb 03, 2014 9:58 am
by freddyp
Javascript variables are case-sensitive:



var theXml = ...

if (theXML ...



That is indeed not going to work.



Freddy

Jobsort based on XML metadata

Posted: Mon Feb 03, 2014 10:33 pm
by telgrin
Hi, i changed it, sadly i have a new error.



Error in line 14 of script : TypeError. 'getString' undefined or not a function.



For me is looks good, but somewhere must be a little bug.



Here is my flow: https://www.dropbox.com/s/497taif1k8ih9 ... enshot.jpg



// Is invoked each time a new job arrives in one of the input folders for the flow element.

// The newly arrived job is passed as the second parameter.

function jobArrived( s : Switch, job : Job )

{

// Get the xml dataset associated with this job

var theXml = job.getDataset("Xml");



// Only search for a valid outgoing connection if we have XML information in the job in the first place

var theResultConnection = null;

if (theXml != null) {



// Get the value of the XML property we're looking for

var theSortKey = s.getPropertyValue('argSortExpression');

var theSortValue = theXml.getString( theSortKey , null );

if (theSortValue != null) {

s.log( 1, theSortKey + " = " + theSortValue );

theResultConnection = getConnectionByName( s, theSortValue );

} else {

s.log( 1, "XML information for " + theSortKey + " was not found for this job" );

}

} else {

s.log( 1, "No XML information found associated with this job" );

}



// If we found a connection, move the job there. If not move it to the data error connections

if (theResultConnection != null) {

job.sendTo( theResultConnection, job.getPath() );

s.log( 1, "Connection with correct name found; moved job" );

} else {

job.sendToData( 3, job.getPath() );

s.log( 1, "No valid connection found, moved job to error output" );

}

}





// ==================================================================================================

// This function gets the full list of outgoing connections for the given script element and searches through this list for a connection with a given name.

// If such a connection is found it is returned, if nothing is found this function returns null.

// ==================================================================================================

function getConnectionByName( s: Switch, connectionName: String ): Connection

{

var theConnections = s.getOutConnections();

for (var i = 0; i < theConnections.getCount(); i++) {



// Get this connection

var theConnection = theConnections.at(i);

if (theConnection.getName() == connectionName) {

return theConnection;

}

}

return null;

}



And her is my XML file:



<?xml version="1.0" encoding="utf-8"?>

<import version="1.0">

<pos>

<artikel>

<options>

<auflage>50 Stck</auflage>

<papier>300_Bilderdruck_matt</papier>

</options>

</artikel>

</pos>

</import>





Jobsort based on XML metadata

Posted: Mon Feb 03, 2014 11:30 pm
by dkelly
When you created the script did you add a property named 'argSortExpression'?



Also, I made 2 changes to your script



function jobArrived( s : Switch, job : Job )

{

// Get the xml dataset associated with this job

var theXml = job.getDataset("Xml");



// Only search for a valid outgoing connection if we have XML information in the job in the first place

var theResultConnection = null;

if (theXml.hasValidData( )) {



// Get the value of the XML property we're looking for

var theSortKey = s.getPropertyValue('argSortExpression');

var theSortValue = theXml.evalToString( theSortKey , null );

if (theSortValue != null) {

s.log( 1, theSortKey + " = " + theSortValue );

theResultConnection = getConnectionByName( s, theSortValue );

} else {

s.log( 1, "XML information for " + theSortKey + " was not found for this job" );

}

} else {

s.log( 1, "No XML information found associated with this job" );

}



// If we found a connection, move the job there. If not move it to the data error connections

if (theResultConnection != null) {

job.sendTo( theResultConnection, job.getPath() );

s.log( 1, "Connection with correct name found; moved job" );

} else {

job.sendToData( 3, job.getPath() );

s.log( 1, "No valid connection found, moved job to error output" );

}

}


Jobsort based on XML metadata

Posted: Tue Feb 04, 2014 10:59 am
by freddyp
I see no reason to use a script for what you are doing. On each of the connections you can easily put a condition with variable, where on the left-hand side you have:

[Metadata.Text:Path="/import/pos/artikel/options/papier",Dataset="Xml",Model=XML]



As an operator you have "Starts with".



And on the right-hand side you have "300_" (or another combination of operator and string that is more suited).



Freddy

Jobsort based on XML metadata

Posted: Tue Feb 04, 2014 11:12 am
by telgrin
Thank you both for your help, i will consider your tips. Thanks a lot.