SearchCriteria

Let's say you're got a nice simple assembly line that has just two connectors; one brining data in and the other sending it out. You're in the 'Before Add' hook of the outbound connector when you realize you really should be looking up any legacy records that might already be there for this entry.

You can go back and add another connector, work attributes and flow control. But if it's simple, you can just look it up right in the hook by reusing the connector you're already in creating a search criteria.

When you create a Criteria, you're basically translating :

select * from schema.table where COLUMN = VALUE
     to: 
theCriteria.addCriteria("COLUMN",theCriteria.EXACT,"VALUE")

You could use straight up SQL, but this creating these end-point independent Criteria is the TDI way to do it. Turning the '=' into 'theCriteria.EXACT' looks a little funny, but it's how you select from the predefined operators that mean different things in different contexts.

If you only expect or care to get one result back:

var theCriteria = new com.ibm.di.server.SearchCriteria();

theCriteria.addCriteria("MD_DATA_SOURCE",theCriteria.EXACT,"OPM");
theCriteria.addCriteria("MD_NETID",theCriteria.EXACT,"gattis");

var theEntry = thisConnector.getConnector().findEntry(theCriteria);

task.logmsg("Found " + theEntry.getAttribute("SOME_DB_COL").getValue() );


But this will bomb anytime there is anything other than one result. So handle it like so:

var pid = [];
var theCriteria = new com.ibm.di.server.SearchCriteria();

theCriteria.addCriteria("MD_DATA_SOURCE",theCriteria.EXACT,"OPM");
theCriteria.addCriteria("MD_NETID",theCriteria.EXACT,"gattis");


var source = thisConnector.getConnector();

var theEntry = source.findEntry(theCriteria);
var theCount = source.getFindEntryCount();

if ( theCount == 0 ) {
    pid = null;
}
else if ( theCount == 1 ) {
    pid[0] = theEntry.getAttribute("MD_OUID").getValue();
}
else if ( theCount > 1 ) {

    theEntry = source.getNextFindEntry();

    while (theEntry ) {
        pid.push(theEntry.getAttribute("MD_OUID").getValue() );
        theEntry = source.getNextFindEntry();
    

}



Note - you'll sometimes see people use the int '61' for the search criteria. This is the literal value of theCriteria.EXACT and I expect people use it because it's not at all clear that you need to get it by referring to the object's own static field as  'theCriteria.EXACT' 




http://www.stephen-swann.co.uk/javadoc/tdi7.1/com/ibm/di/connector/ConnectorInterface.html#findEntry(com.ibm.di.server.SearchCriteria)



Comments