22 Utilities Module

This chapter provides a complete listing and reference for the methods in the OpenScript UtilitiesService Class of Utilitis Module Application Programming Interface (API).

22.1 UtilitiesService API Reference

The following section provides an alphabetical listing of the methods in the OpenScript UtilitiesService API.

22.1.1 Alphabetical Command Listing

The following table lists the UtilitiesService API methods in alphabetical order.

Table 22-1 List of UtilitiesService Methods

Method Description

utilities.getFileService

Gets a file service instance for this Utilities service.

utilities.getSQLService

Gets a SQL service instance for this Utilities service.

utilities.loadCSV

Load the given comma-separated-value file as InputStream into a Table object.

utilities.loadXML

Load a specified XML file as InputStream into an XML tree.

utilities.loadXMLContent

Load a specified XML contents into an XML tree.

utilities.parameters

Convenience method to create a list of parameters for SQL a query.

utilities.saveCSV

Saves a table to a CSV file format.


The following sections provide detailed reference information for each method and enum in the UtilitiesService Class of Utilitis Module Application Programming Interface.


utilities.getFileService

Gets a file service instance for this Utilities service. The FileService provides methods for working with the file system in general.

Format

The utilities.getFileService method has the following command format(s):

utilities.getFileService( );

Returns

the File service instance.

Example

Creates a new file and appends the text strings to the file.

utilities.getFileService().createDestinationFile("myFile.txt", false);
String line1 = "This is a new line 1";
String line2 = "This is a another new line 2";           
String contents = "\n" + line1 + "\n" + line2;
utilities.getFileService().appendStringToFile("myFile.txt", contents);

utilities.getSQLService

Gets a SQL service instance for this Utilities service. SQLService provides methods for working with SQL databases.

Format

The utilities.getSQLService method has the following command format(s):

utilities.getSQLService( );

Returns

the SQL service instance.

Example

Create a database connection, execute SQL statements and query database, then disconnect the database connecton.

//define database
utilities.getSQLService().define("myOracleDB",
 "oracle.jdbc.driver.OracleDriver",
 "jdbc:oracle:thin:@mySystem:1521:mySystem", "username",
 decrypt("ZSL2IzF3WpLh8ydBZYDV3Q=="));
//connect to database
utilities.getSQLService().connect("myOracleDB");
//execute SQL statement
String query = "Create table Employee (ID number(4) not null unique, " +
"FirstName varchar2(40) not null, LastName varchar2(40) not null, " +
"Country varchar2(40), HireDate date)";
info("Query: " + query);
utilities.getSQLService().execute("myOracleDB", query);       
//execute update SQL statement
query = "Insert into Employee (ID, FirstName, LastName, Country, HireDate) " + 
 "Values (101, 'Tom', 'Smith', 'USA', '01-JAN-95')";
utilities.getSQLService().executeUpdate("myOracleDB", query);
//query SQL statement
query = "Select * from Employee";
Table table = utilities.getSQLService().query("myOracleDB", query);
//print table
for (int i=0; i<table.getRowCount(); i++) {
 Row row = table.getRow(i);
String [] rowValue = row.getAll();
String rowContent = "";
for (int col=0; col<rowValue.length; col++)
  rowContent += rowValue[col] + "  ";
info(rowContent);
}
//disconnect from database
utilities.getSQLService().disconnect("myOracleDB");

utilities.loadCSV

Load the given comma-separated-value file as InputStream into a Table object.

Format

The utilities.loadCSV method has the following command format(s):

utilities.loadCSV(filePath);

utilities.loadCSV(filePath);

utilities.loadCSV(url);

utilities.loadCSV(fileInput);

utilities.loadCSV(repository, pathRelToRepository);

Command Parameters

filePath

a File object specifying the file containing CSV-formatted data.

repository

a String specifying the name of repository containing CSV-formatted data file. May contain {{ }} syntax for transforming.

pathRelToRepository

String specifying the path to the CSV-formatted data file within the named repository. May contain {{ }} syntax for transforming. Must not be null. Any leading file separator on the path, such as / or \, is ignored.

url

a URL object specifying the path to the CSV file.

fileInput

a FileInputStream.

Throws

AbstractScriptException

represents an exception that may be thrown during the execution of a script where the exception should be reported to an end user through the controller.

CSVException

on any exception while loading and reading the CSV file.

Example

Loads a CSV file into a Table object and prints the contents to the Results view.

String filePath = "c:\\fmstocks_data.csv";
InputStream in = null;
in = new FileInputStream(new File(filePath));
Table table = utilities.loadCSV(in);
//Print the CSV file
String columns = "";
int columnNumber = table.getColumns().getColumnCount();
String [] columnNames = table.getColumns().getColumnNames();
for (int index=0; index<columnNumber; index++) 
 columns += columnNames[index] + "  ";
info(columns);
List <Row> rows = table.getRows();
for (int index=0; index<rows.size(); index++) {
 String [] rowValue = rows.get(index).getAll();
String rowContent = "";
for (int columnIndex=0; columnIndex<rowValue.length; columnIndex++)
 rowContent += rowValue[columnIndex] + "  ";
info(rowContent);
}

utilities.loadXML

Load a specified XML file as InputStream into an XML tree.

Format

The utilities.loadXML method has the following command format(s):

utilities.loadXML(filePath);

utilities.loadXML(filePath);

utilities.loadXML(url);

utilities.loadXML(fileInput);

utilities.loadXML(repository, pathRelToRepository);

Command Parameters

filePath

a String specifying the file path to be loaded. May contain {{ }} syntax for transforming.

repository

a String specifying the name of repository containing XML file to load. May contain {{ }} syntax for transforming.

pathRelToRepository

String specifying the path to the XML file within the named repository. May contain {{ }} syntax for transforming. Must not be null.Any leading file separator on the path, such as / or \, is ignored.

url

a URL object specifying the URL.

fileInput

a FileInputStream.

Throws

AbstractScriptException

represents an exception that may be thrown during the execution of a script where the exception should be reported to an end user through the controller.

FileNotFoundException

if the file is not found.

XMLServiceException

if an error occurs loading the XML.

Returns

the root XML element for the given XML or null if XML file doesn't exist.

Example

Loads an XML file into a XML object and prints the contents to the Results view.

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
//[...]
//load XML file by InputStream
String filePath = "c:\\myxmlfile.xml";
InputStream in = null;
in = new FileInputStream(new File(filePath));
XML xml = utilities.loadXML(in); 
info(xml.toString());
info("*** getChildren() ***");
XML [] children = xml.getChildren();
String lsChild = "";
for (int i=0; i<children.length; i++) {
 if (i != 0)
   lsChild += ", ";
 lsChild += children[i].getTagName() + " ";
}
if (lsChild.equals(""))
 info("No child present of xml element " + xml.getTagName());
else
 info("List of all child elements of xml element " + 
   xml.getTagName() + ": " + lsChild);
for (int i=0; i<children.length; i++) {
 XML child = children[i];
// print tag name                       
String tagName = child.getTagName();
info("Tag Name: " + tagName);
//print attributes
String [] allAttr = child.getAttributeNames();
if (allAttr != null) {
 for (int j=0; j<allAttr.length; j++) {
   info("Attribute Name: " + allAttr[j]);
   info("Attribute Value: " + child.getAttribute(allAttr[j]));
  }
 }
else
 info("No attribute");
// print values
if (child.getValue() != null) {
   info("Value: " + child.getValue());
 }
// print parent
info("Parent: \n" + child.getParent());
}

See Also

oracle.oats.scripting.modules.utilities.api.xml.XML


utilities.loadXMLContent

Load a specified XML contents into an XML tree.

Format

The utilities.loadXMLContent method has the following command format(s):

utilities.loadXMLContent(contents);

Command Parameters

contents

The XML contents to be loaded.May contain {{ }} syntax for transforming.

Throws

AbstractScriptException

represents an exception that may be thrown during the execution of a script where the exception should be reported to an end user through the controller.

XMLServiceException

if an error occurs loading the XML.

Example

Loads an XML string into an XML object and prints the contents to the Results view.

//load XML file by file name string.
String contents = "<GroceryStore><StoreName>Fourth Coffee</StoreName>" +
  "<Departments><Department Name=\"Breads\">" +
  "<Item ID=\"B2\" Type=\"Muffin\"><Name>Blueberry Muffin</Name>" +
  "<Price>3.99</Price><New/></Item>" +
  "</Department></Departments></GroceryStore>";
XML xml = utilities.loadXMLContent(contents);
info(xml.toString());

See Also

oracle.oats.scripting.modules.utilities.api.xml.XML


utilities.parameters

Convenience method to create a list of parameters for SQL a query.

Format

The utilities.parameters method has the following command format(s):

utilities.parameters(obj, param);

Command Parameters

obj

a List<Object> of parameters for SQL a query.

param

Optional list of parameters for the SQL query List<Object>.

Returns

a new List<String>

Example

Creates a database connection, execute SQL statements and query database with parameters object, then disconnect the database connecton.

//define database
utilities.getSQLService().define("myOracleDB",
 "oracle.jdbc.driver.OracleDriver",
 "jdbc:oracle:thin:@mySystem:1521:mySystem", "username",
 decrypt("ZSL2IzF3WpLh8ydBZYDV3Q=="));
//connect to database
utilities.getSQLService().connect("myOracleDB");
//execute SQL statement
String query = "Create table Employee (ID number(4) not null unique, " +
"FirstName varchar2(40) not null, LastName varchar2(40) not null, " +
"Country varchar2(40), HireDate date)";
info("Query: " + query);
utilities.getSQLService().execute("myOracleDB", query); 
//execute update SQL statement
query = "Update Employee SET LastName = 'Davis' 
   WHERE ID = ? and LastName = ?";
info("Query: " + query);
utilities.getSQLService().executeUpdate("myOracleDB", query, 
   utilities.parameters(101, "Smith"));
//query SQL statement
query = "Select * from Employee";
Table table = utilities.getSQLService().query("myOracleDB", query);
//print table
for (int i=0; i<table.getRowCount(); i++) {
 Row row = table.getRow(i);
 String [] rowValue = row.getAll();
 String rowContent = "";
 for (int col=0; col<rowValue.length; col++)
   rowContent += rowValue[col] + "  ";
 info(rowContent);
}
//disconnect from database
utilities.getSQLService().disconnect("myOracleDB");

utilities.saveCSV

Saves a table to a CSV file format.

Format

The utilities.saveCSV method has the following command format(s):

utilities.saveCSV(table, csvFilename, overwrite);

utilities.saveCSV(table, repository, pathRelToRepository, overwrite);

Command Parameters

table

a Table object.

csvFilename

a String specifying the full path and name of a file where the Table is saved. May contain {{ }} syntax for transforming.

overwrite

a Boolean that specifies whether or not to overwrite an existing file.

repository

a String specifying the name of repository where the Table is to be saved. May contain {{ }} syntax for transforming. Must not be null.

pathRelToRepository

String specifying the path to the file within the named repository.May contain {{ }} syntax for transforming. Must not be null. Any leading file separator on the path, such as / or \, is ignored.

Throws

AbstractScriptException

on any error when saving.

IOException

file input/oputput exception.

Example

Saves a CSV Table object to a new file in overwrite mode

String repository = "Default";
String pathRelToRepository = "Data/fmstocks_data.csv";
String newCSVFile = "new_fmstocks_data.csv";
boolean overwrite = true;
Table table = utilities.loadCSV(repository, pathRelToRepository);
utilities.saveCSV(table, repository, pathRelToRepository, overwrite);