Home > OracleUCM > Search UCM (Stellent) With Groovy

Search UCM (Stellent) With Groovy

July 9, 2009

In a previous article we talked about using Groovy to execute the PING_SERVER service of our SOA enabled Content Server. Interesting, but fairly useless you might say. Let's see how we can conduct a search using Groovy and then access specific metadata from those search results.

You may want to keep the RIDC JavaDoc link handy as you explore Groovy Integration further.

First, the script, and then some discussion.

The Script

// Import needed classes from Remote Intradoc Jarimport oracle.stellent.ridc.IdcClientManagerimport oracle.stellent.ridc.IdcContext
 
// Create the client for request/response, connect directly to serverclient = (new IdcClientManager()).createClient("idc://localhost:4444")
 
// Create a user/security contextuserContext = new IdcContext("sysadmin", "idc")
 
// Setup the request, search for two pieces of contentreq = client.createBinder()req.putLocal("IdcService", "GET_SEARCH_RESULTS")req.putLocal("QueryText", "")req.putLocal("ResultCount", "2")
 
// Get the responseresp = client.sendRequest(userContext, req).getResponseAsBinder()
 
// Dump out the local data of the responseprintln "LocalData:" resp.getLocalData().keySet().each{ println " $it = ${resp.getLocalData().get(it)}"}
 
// Dump out the names of the resultsets in the responseprintln "\nResultSets:"resp.getResultSetNames().each{ println " $it"}
 
// Dump out dDocTitle for each piece of content in the responseprintln "\nTitles:"rsSearchResults = resp.getResultSet("SearchResults")rsSearchResults.getRows().each{ println " ${it.get('dDocTitle')}"}
 
// Wrap it up!println "\nDone"
 

Closures

The above script makes extensive use of a language feature in Groovy known as a closure. Closures are not specific to Groovy and they are certainly not a new concept. They are found in many other programming languages. One such language you may be very familiar with is JavaScript!

In this particular case we are using the "each" closure function. Here is an excerpt from the sample script:

println "LocalData:"resp.getLocalData().keySet().each{ println " $it = ${resp.getLocalData().get(it)}"}
 

See that "each" statement in the second line? The word each followed by curly brackets forges the wrapping closure and the code inside is what becomes executed when the closure is invoked. In this case, our closure is invoked for each key in the Key Set of the Local Data collection. Within the closure you can use the "it" variable (as in iterator) to access each key.

If you have something against the name "it" for the variable you can alter the variable name. As an example, let's change the syntax so the variable will be named "key", try this:

println "LocalData:"resp.getLocalData().keySet().each{ key -> println " $key = ${resp.getLocalData().get(key)}"} 
 

This is also very helpful when using a closure within a closure. An example? Sure. What if you wanted to loop the resultsets and print the value for each column for each row of each resultset? You're code might use nested closures and look something like this:

resp.getResultSetNames().each{ println "\nResult Set '$it':" resp.getResultSet(it).getRows().each { row ->  resp.getResultSet(it).getFields().each {  field ->  println "\t${field.getName()} = ${row.get(field.getName())}" } } print("\t----------------")}
 

Additional Sample Scripts

Here are two additional Groovy Scripts you can download and try out:

Search & Target Title, and Search & List Result Sets

Advertisement
Categories: OracleUCM Tags:
Follow

Get every new post delivered to your Inbox.