Calling REST Services in WebCenter Enterprise Capture Client Scripts

For customizing WebCenter Enterprise Capture, the JavaScript engine included with the Java Runtime Environment can be used. The developer’s guide for WebCenter Enterprise Capture contains two sample client scripts that can be used to modify the standard behavior of the Capture client that runs in the browser. The two samples do the following tasks:

Sample 1: This sample script customizes client behavior in the following ways:

  • Prevents the client user from leaving a metadata field if the entry contains the word “test”
  • Prevents the user from entering an asterisk in any metadata field.
  • Outputs event information to the java console, such as coordinates after a user right-mouse-drags a selection on an image.

Sample 2: This sample script customizes client behavior in the following ways:

  • Uses the BatchScanBegin function to restrict files that can be imported to those with a .TIF extension only.
  • Uses the DBSearchResults function to modify the results of a database lookup so that only the first result is used, and prevents the results list from displaying.

These scripts create a starting point for further client side customizations in Enterprise Capture. To extend the samples a bit further, the following script displays how to call a web service and populate a client profile field automatically. This example pulls a stock quote from a Yahoo Finance public REST API to demo how to call the REST service, parse the JSON response, and set the Capture field value.

 

Note that using the JavaScript engine, packages and classes that need to be included can be imported in either of the following ways:

  importClass(java.awt.event.KeyEvent);
  importPackage(java.io, java.net);

 

For the example script that follows, the java.io and java.net packages are required, since calling the web service will use the classes URL and HttpUrlConnection. To read the JSON response, an InputStreamReader and BufferedReader classes are used.

importClass(java.awt.event.KeyEvent);
importPackage(java.io, java.net);

function DocumentSelected(event) {
  DocumentSelectedEvent println("DocumentSelected: " + event.getDocument().getTitle());
  var MY_FIELD = "MetaField"; // The name of the metadata field to populate
  var document = event.getDocument();
  var batch = document.getParentBatch();
  var fieldDef = batch.getWorkspace().getFieldDefinitions().findByName(MY_FIELD);

  if (fieldDef != null) {
    var fieldId = fieldDef.getId();
    var fields = document.getFields();
    var field = fields.get(fieldId);

    //Setup URL to call Yahoo Finance service
    var yqlHostAndContext = "http://query.yahooapis.com/v1/public/yql";
    var yqlQuery = "select * from yahoo.finance.quotes where symbol in ('ORCL')";
    var yqlEnv = "http://datatables.org/alltables.env";
    //Encode uri parameters
    var urlStr = yqlHostAndContext + "?q=" + encodeURIComponent(yqlQuery) + "&env=" + encodeURIComponent(yqlEnv);

    println(urlStr);

    var url = new URL(urlStr);
    var conn = url.openConnection(); //Returns HttpUrlConnection
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "application/json");

    var responseCode = conn.getResponseCode();
    println("Sending 'GET' request to URL : " + url);
    println("Response Code : " + responseCode);
    if(responseCode == "200"){
      var is = conn.getInputStream();
      var isr = new InputStreamReader(is);
      var br = new BufferedReader(isr);
      var response = new java.lang.StringBuffer();
      var line;
      while ((line = br.readLine()) != null) {
        response.append(line);
      }
      br.close();
      conn.disconnect();

      println(response);
      var jsonObj = JSON.parse(response);
      var price = jsonObj.query.results.quote.LastTradePriceOnly;

      //Set the field value
      field.setValue(price);
    }
    else{
      //Could throw an error instead, but for sample script just setting meta field to a generic error string so user can see it.
      field.setValue("ERROR getting stock price. HTTP Response code:" + responseCode );
    }

    // Save the document data
    document.persist();
  }
}

 

When the script succeeds, the last trading price value of the ORCL stock will be placed automatically into the field (MetaField).

metafield

When troubleshooting client side scripts, see the Java Console. This is where ECMA errors or other syntax errors will appear. Note that when updating client scripts, the Enterprise Capture managed server does not need to be restarted. Logout of the client profile and login once again, and the updated client script will load.

javaconsole

 

Oracle Support has additional as-is samples for customizing and enhancing the Enterprise Capture import processor, recognition processor, and client profiles. See these knowledge base articles for other example scripts.

    • Document 1900716.1 – Enterprise Capture Recognition Processor Script to Modify Database Lookup Search Value does not Work
    • Document 1909021.1 – Sample Import Processor Script (Email Jobs Only) for Converting PDF to TIFF Using GhostScript
    • Document 1664009.1 – Enterprise Capture Sample Script to Display Message in FieldGotFocus Event
    • Document 1901189.1 – Enterprise Capture Sample Client Script to Perform DB Lookup on FieldLostFocus Event
    • Document 1672851.1 – Sample Enterprise Capture Client Script to Show Field Properties in FieldGotFocus Event
    • Document 1662311.1 – Sample Import Processor Script to Rename a Batch to Match a FileName
    • Document 1665817.1 – How to Access a Batch Name in the Enterprise Capture Recognition Processor Script
    • Document 1639684.1 – Sample Enterprise Capture Script to Populate Field in Client Profile with UserName in DocumentSelected Event
    • Document 1677731.1 – Sample Enterprise Capture Client Script to Connect to the Database using JDBC/ODBC Bridge
    • Document 1918874.1 – Sample Enterprise Capture Recognition Processor Script to Retain Existing Meta-Data for New Documents
    • Document 1943347.1 – Sample Enterprise Capture Client Script to Copy Meta Data on PreBatchRelease Event (Sticky Fields)
    • Document 1934031.1 – Sample Enterprise Capture Import Processor Script to Set Meta Data Value in “preCreateDocument” Event
    • Document 1917387.1 – How To Get The Total Number Of Pages In A Document Using Scripts Within Enterprise Capture
    • Document 1614232.1 – Enterprise Capture Sample Script to Add a User ID to a Batch Name at Scan Time
    • Document 1936687.1 – Sample Enterprise Capture Client Script to Unlock a Batch and Make Available for Other Users (Skips Post Processing Step)
    • Document 1594882.1 – How to Execute Enterprise Capture WLST Commands
    • Document 1946580.1 – Sample Enterprise Capture Client Script to Call A REST Web Service and Populate a Field With the Result
    • Document 1946581.1 – Sample Enterprise Capture Client Script to Call Documents Cloud Service Using Jersey

 

Add Your Comment