Utility methods

The following methods are available in "db" object that's available in Pre-Query, Post-Query, Server-side Validation Scripts, Object Handlers etc.

Method Name

Usage and examples

assignRoles

assignRoles(String userName, String[] roleCodes) returns List<RPCModelData>

This method can be used to assign one or more roles to a user, using role codes.

String userName = db.getSessionValue("username");

List<RPCModelData> userRolesList = db.assignRoles(userName, new String[]{"ordermanagement"});

cacheGetObjectsByNameValues

cacheGetObjectsByNameValues(String ds, String attr,Object... nameValues)

returns RPCModelData

This method can be used whenever you want to return RPC model data from database and cache it. Instead of querying database every time, it will query once and store values in the cache. The subsequent calls will fetch from the cache instead of querying the database.

Don't cache the data which changes frequently.

Double rsrcId = data.get("rsrcid");

RPCModelData resource = db.cacheGetObjectByNameValues("OJResources","rsrcid",rsrcId);

String rsrcName = resource.get("name");

String isIntenal = resource.get("isinternal");

cleanHtml

cleanHtml(String html)

returns String

createUser

createUser(String userName, String email,String DisplayName) createUser(String userName, String email,String DisplayName, String managerName)

returns RPCModelData This method can be used to create a user.

RPCModelData userData = db.createUser("tonychou","[email protected]","Tony Chou");

dbItemToClientItem

dbItemToClientItem(RPCModelData row)

returns Map<String,Object>

evaluateSelectSQL

evaluateSelectSQL(String sql) returns Object This method can be used to execute a SQL query without any platform specific syntax. This is a plain SQL query execution, and will not do any automatic conversion on result data. User has to take care of casting the result data based on what he is expecting from the query.

double empId = 2001;

String empName = (String) db.evaluateSelectSQL("select emp_name from employee where emp_id = "+ empId);

executeQuery

executeQuery(String sql, Object[] params) returns List<RPCModelData> This method can be used to execute ad-hoc SQL statements with bind variables.

Double empId = data.get("empId");

List<RPCModelData> rows = db.executeQuery("SELECT EMP_NAME empName, DOB, EMP_NO empNo FROM EMP WHERE EMP_ID = ?", new Object[]{ empId });

if (rows.size() > 0) {

RPCModelData firstRow = rows.get(0);

String empName = firstRow.get("empName"); // use the column alias without an underscore

Date dob = firstRow.get("dob");

Double empNo = firstRow.get("empNo"); // all numbers are converted to Double

}

getChangesTextForStreamBody

getChangesTextForStreamBody(RPCModelData data)

getChangesTextForStreamBody(String attributesList, RPCModelData data, RPCModelData dbRow)

returns String

setData

setData(String key, Object value) returns void This method can be used to cache objects, primitive values and retrieve the objects whenever you require. These values are accessible in Pre-Query, Post-Query, and Server side validation scripts. You can use this method to store any global values.

Example 1: Store primitive values

db.setData("Is_Dev_Mode_On","Y");

Example 2: Store objects

RPCModelData appSession = new RPCModelData();

appSession.set("appId",1020);

appSession.set("isOAuth","Y");

db.setData("appsettings",appSession);

<code></code>

Example 3: Store a list of RPC models queried from database

String datasource = "employee";

Map<String,Object> map = new HashMap<String,Object>();

map.put("location","Madison");

int offset = 0;

List<RPCModelData> rows = db.getObjectsByNameValues(datasource,map,offset);

db.setData("emp_by_location_list",rows);

getData

getData(String key) returns Object This method can be used to retrieve the stored objects, primitive values from the cache.

String isDevMode = (String)db.getData("Is_Dev_Mode_On");

List<RPCModelData> empList = (List<RPCModelData>)db.getData("emp_by_location_list");

getDBNextVal

getDBNextVal(String sequenceName) returns Double This method is used to get next value for any sequence.

Double nextValue = db.getDBNextVal("XXRA_MASTER_S");

getDiffHtml

getDiffHtml(RPCModelData data)

getDiffHtml(String oldValue, String newValue)

getDiffHtml(String oldValue, String newValue, int maxLength)

returns String

getLink

getLink(boolean embeded, String linkName, String pageCode, String otherParams)

getLink(boolean embeded,String linkName, String pageCode, String ds, String attr, String cond, Object value)

getLink(boolean embeded, String linkName, String pageCode, String ds, String attr, String cond, Object value, String attr2, String cond2, Object value2)

getLink(String linkName, String pageCode, String otherParams)

getLink(String linkName, String pageCode, String ds, String attr, String cond, Object value)

getLink(String linkName, String pageCode, String ds, String attr, String cond, Object value, String attr2, String cond2, Object value2)

returns String

getNextVal

getNextVal(String sequenceName)

returns String

getObjectByNameValues

getObjectByNameValues(String ds, Object... nameValues)

returns RPCModelData This method can be used to get a single RPC model by passing attribute names, attribute values and datasource. It can be used to get a row from the database.

You need not construct the Where Clause; based on the passed attributes and value, it will construct the Where Clause and query the table.

String datasource = "employee";

RPCModelData row = db.getObjectByNameValues(datasource,"ename","Rob Benada","location","madison");

getObjectsByNameValues(String ds, int offset, Object... nameValues) returns List<RPCModelData> This method can be used to get a list of RPC models.

String datasource = "employee";

int offset = 0;

RPCModelData row = db.getObjectsByNameValues(datasource,offset,"location","madison");

getObjectByPKValues

getObjectByPKValues(String ds, Object... params) returns RPCModelData This method will return a single RPC model data from the database based on passed primary key values. No need to pass the attribute names.

Example 1: If the employee table uses a single column (username) as the primary key

String datasource = "employee";

RPCModelData row = db.getObjectByPKValues(datasource,"robbenada");

Example 2: If the employee table uses a composite primary key (username + email)

String datasource = "employee";

RPCModelData row = db.getObjectByPKValues(datasource,"[email protected]","robbenada");

getObjects

getObjects(String ds, String whereClause, List params) returns List<RPCModelData> This method can be used to get a list of RPC models from the database by passing the datasource, Where Clause and a list of parameter values.

StringBuilder empIdsString = new StringBuilder();

Integer[] empIds = new Integer[]{1001,1002,1003,104};

<code></code>

List params = new ArrayList();

for (int i=0; i<empIds.length; i++) {

if (empIds[i] == null || "null".equals(empIds[i]) || "".equals(empIds[i])) {

continue;

}

if (i > 0) {

empIdsString.append(", ");

}

empIdsString.append("?");

params.add(new Double(empIds[i]));

}

String whereClause = "#empId# IN ("+empIdsString.toString()+")";

//where #empId# attribute name should always enclosed with #

List<RPCModelData> list = db.getObjects("Employee",whereClause, params);

getObjectByNameValues

getObjectByNameValues(String ds, Map<String,Object> map, int offset) returns RPCModelData This method can be used to fetch data from the table, by using a map with key value pairs of attribute names and values. You can specify the offset value as 0.

String datasource = "employee";

Map<String,Object> map = new HashMap<String,Object>();

map.put("ename","Rob Benada");

map.put("location","Madison");

int offset = 0;

RPCModelData row = db.getObjectByNameValues(datasource,map,offset);

getObjectsByNameValues(String ds, Map<String,Object> map, int offset, int limit)

returns List<RPCModelData>

This is an overloaded method for getting list of data objects (RPC models) from the table by passing attribute names, attribute values as a map. You can pass the offset and limit to get the paginated result.

String datasource = "employee";

Map<String,Object> map = new HashMap<String,Object>();

map.put("location","Madison");

int offset = 0;

int limit = 200;

RPCModelData row = db.getObjectByNameValues(datasource,map,offset,limit);

<code></code>

getObjectsByNameValues(String ds, Map<String,Object> map, int offset)

returns List<RPCModelData>

This method is used to get a list of RPC models by using attribute name, attribute value in a map.

String datasource = "employee";

Map<String,Object> map = new HashMap<String,Object>();

map.put("location","Madison");

int offset = 0;

List<RPCModelData> rows = db.getObjectsByNameValues(datasource,map,offset);

getRoleEmails

getRoleEmails(String roleName) returns String This method can be used to get the email addresses of all the users under a specific role as a string with comma separated values.

String emailAddressesWithCommaSeparatedValues = db.getRoleEmails("Manager");

setSessionData

setSessionData(String key, Object value) returns void This method can be used to set temporary values which can be accessed in Pre-Query, Post-Query and Server-side validations scripts.

Example 1: Setting primitive values

db.setSessionData("customerid",1200);

Example 2: Setting objects into session

RPCModelData newemployee = new RPCModelData();

newemployee.set("ename","Tony Chou");

newemployee.set("location","California");

db.setSessionData("custemployee",newemployee);

CloudIO Platform is stateless and hence any session data set is not guaranteed to be available across requests. This method is primarily intended to share data between Pre-Query & Post-Query or between Server Side Validation Scripts within the same transaction.

getSessionData

getSessionData(String key) returns Object This method can be used to access the values that are set using setSessionData() method.

String customerId = db.getSessionData("customerid");

getSessionValue

getSessionValue(String fieldName) returns Object This method can be used to access the implicit session attributes like clienttimezone, username, userid, displayname, domainname. etc.

getURL

getURL()

getURL(boolean embedded, String pageCode, String otherParams)

getURL(String pageCode, String otherParams)

returns String

getUserDisplayName

getUserDisplayName(java.lang.Double userId) returns String This method can be used to get display name of the logged in user.

String displayName = db.getUserDisplayName(userid);

// where userid is implicit variable which is available in the scriptUtil (db)

getUserEmailAddress

getUserEmailAddress(java.lang.Double userId) returns String This method can be used to get the email of a logged in user.

String emailAddress = db.getUserEmail(userid);

// where userid is implicit variable which is available in the scriptUtil (db)

getUserUserName

getUserUserName(Double userId) returns String This method can be used to get the user name for any user by passing userid.

double otherUserId = 10001;

String userName = db.getUserUserName(otherUserId);

isObjectExists

isObjectExists(String ds, Map<String,Object> map) returns boolean This method can be used to check if the specific data row exists in the database table. It will take the datasource name and a key value pair of attribute names and values as arguments and return true or false.

Map<String,Object> map = new HashMap<String,Object>();

map.put("empId","10021");

boolean isExistRow = db.isObjectExists("employee",map);

performFTPActions

performFTPActions(String hostName, String uName, String pwd, Double fileId, String destFolder, String action)

returns void

postObjectToDB

postObjectToDB(String ds, RPCModelData data)

returns RPCModelData

<em></em>

This method can be used to insert an RPC Model object into a datasource. You can use this method to post data into to the table from Pre-Query,Post-Query and Server side validation scripts. You need to construct a RPCModelData with keys(attributeNames) and values (attributeValues) and pass it to the method along with datasource name.

RPCModelData employee = new RPCModelData();

employee.set("ename","Bob Thomas");

employee.set("location","New York");

employee.set("age",36);

<code></code>

String ds = "employee";

RPCModelData result = db.postObjectToDB(ds, employee);

postObjectsToDB

postObjectsToDB(String ds, List<RPCModelData> data) returns List<RPCModelData>

<em></em>

This method can be used to insert multiple RPC model objects into a datasource as individual rows. You can use this method to post data into to the table from Pre-Query,Post-Query and Server side validation scripts. You need to construct a RPCModelData array with keys(attributeNames) and values (attributeValues) and pass it to the method along with datasource name.

List<RPCModelData> empList = new ArrayList<RPCModelData>();

RPCModelData employee = new RPCModelData();

employee.set("ename","Rob Benada");

employee.set("location","Madison");

employee.set("age",58);

empList.add(employee);

<code></code>

employee = new RPCModelData();

employee.set("ename","Jeff Kirk");

employee.set("location","Madison");

employee.set("age",48);

empList.add(employee);

<code></code>

String ds = "employee";

List<RPCModelData> result = db.postObjectsToDB(ds, empList);

publishSystemNotification

publishSystemNotification(String subject, String body, String roleIds,String userIds) returns void This method can be used to publish notifications as an admin. These notifications are inserted in the notification tables.You can pass the role ids as a comma separated string.

<em></em>

To add notifications manually, use the Platform screen 'Notifications' under Developer Menu.

Date commentedDate = data.get("commenteddate");

String commentedBy = db.getSessionValue("displayname");

<code></code>

StringBuilder post = new StringBuilder();

post.append("<b>"+commentedBy+" Commented on the current task on "+commentedDate+"</br>");

post.append(comments);

<code></code>

db.publishSystemNotification("New note",post.toString(),"1001,2005",null);

publishUserNotification

publishUserNotification(String subject, String body, String roleIds, String userIds) returns void This is the overloaded method to publish notifications as a logged in user.

Date commentedDate = data.get("commenteddate");

String commentedBy = db.getSessionValue("displayname");

String roleId = "1001;2005";

String userId = "2098;4098";

<code></code>

StringBuilder post = new StringBuilder();

post.append("<b>"+commentedBy+" Commented on the current task on "+commentedDate+"</br>");

post.append(comments);

<code></code>

<code></code>

db.publishUserNotification("New Task Comment",post.toString(),roleId,userId);

queueEmails

queueEmails(String es, String eb)

returns void

sendEmail

sendEmail(String toEmails, String ccEmails, String es, String eb, String custTemplate) returns void This method can be used to send emails. If custTemplate is set to 'N' then the the default template is used.

db.sendEmail("[email protected],[email protected]", null, subject, sb.toString(), "N"); // use comma separted values to send emails for multiple users.

sendEmail(String toEmails, String ccEmails, String bccEmails,String es, String eb, String custTemplate) returns void This method can be used to send email with blind copies (bcc emails). db.sendEmail("[email protected],[email protected]", null, "[email protected]", subject, sb.toString(), "N"); // use comma separated values to send emails for multiple users. sendEmail(String toEmails, String ccEmails, String es, String eb, Date startDate, String custTemplate)

returns void

<em></em>

This method can be used to schedule emails.

Date d = new Date("12/12/2013");

db.sendEmail("[email protected],[email protected]", null, subject, sb.toString(), "N"); // use comma separted values to send emails for multiple users.

public void sendEmail(String toEmails, String ccEmails, String bccEmails, String es, String eb, String custTemplate,Long fileId)

returns void

This method can be used for sending emails with single attachment.

Double fileId = data.get("attachmentId");

db.sendEmail("[email protected],[email protected]", null, null,subject, sb.toString(), "N",fileId);

// where fileId is a reference of a file stored in RA_FILES.

public void sendEmail(String toEmails, String ccEmails, String bccEmails, String es, String eb, String custTemplate,List<Long> fileIds) returns void

<em></em>

This method can be used for sending emails with multiple attachments.

Double fileId1 = 10002;

Doubel fileId2 = 10004;

List<Long> fileIds = new ArrayList<Long>();

fileIds.add(fileId1);

fileIds.add(fileId2);

db.sendEmail("[email protected],[email protected]", null, null,subject, sb.toString(), "N",fileIds);

// where fileId is a reference of a file that is stored in RA_FILES.

trunc

trunc(Date date)

returns java.util.Date

confirmationMessageBox

public void confirmationMessageBox(String confirmationMessage, String confirmationOptions, double confirmationSequence, String confirmationToken, String cancelButtonText)

returns void

executeUpdate

executeUpdate(String sql, Object[] params) throws SQLException

returns int

executeQueryDouble

executeQueryDouble( String sql, Object[] params) throws SQLException

returns Double

executeQueryObject

executeQueryObject( String sql, Object[] params) throws SQLException

returns Object

setProfileValue

setProfileValue(Double userId, String code, String value)

returns void

getProfile

getProfile(Double userId, String profileCode)

getProfile(String profileCode)

returns String

resetPassword

resetPassword(String userName, String emailAddress, boolean sendEmailAndNotification)

returns String

doHttpGet

doHttpGet(RPCModelData data, String url, Map headers, String errorNode, String respRootNode) returns List<RPCModelData>

This method can be used to do a server call to a third party URL using the GET method. Example: RPCModelData data = new RPCModelData();

data.set("issueId", "1123"); Map headers = new HashMap();

headers.set("Autorization", "Token");

headers.set("token", "m56nksjdDyu82374"); String url = "githib.com/issues";

List<RPCModelData> result = db.doHttpPost(data, url, headers, "error", "description");

doHttpPost

doHttpPost(RPCModelData data, String url, Map headers, String errorNode, String respRootNode)

returns List<RPCModelData>

<em></em>

This method can be used to do a server call to a third party URL using the POST method. Example: RPCModelData data = new RPCModelData();

data.set("issueId", "1123"); Map headers = new HashMap();

headers.set("Autorization", "Token");

headers.set("token", "m56nksjdDyu82374"); String url = "githib.com/issues";

List<RPCModelData> result = db.doHttpPost(data, url, headers, "error", "description");

Last updated

Was this helpful?