Script units accessing Virtual Entities

From Wiki

Jump to: navigation, search

The Script Unit has multiple usages: transform input in output, access database and access virtual entities. Consider the article Script units accessing database and reproduce the same situation but using two virtual entities: VCategory and VProduct and the 1:N relationship VProduct_VCategory

As in the database example, suppose we want to know how many products belong to the category of the selected product, in addition to the selected one. We use an XMLOut unit in order to export the data stored in the database and an Adapter unit in order to create the right XML that is used to populate the virtual tables through the XMLIn unit. Your site view can appear as in the image below.

The script code to retrieve the number of products is:

 1: //inputs=categoryOID
 2:
 3: import com.webratio.rtx.core.BeanHelper
 4: import org.apache.commons.lang.math.NumberUtils
 5: import com.webratio.rtx.vdb.VirtualEntityTable
 6: import com.webratio.webapp.VCategory
 7:
 8: def count = 0
 9: def entityId = "ent10"
10: def ms = rtx.getModelService()
11: def table = new VirtualEntityTable(entityId, sessionContext, service)
12: def attrId = ms.getKeyAttributeIds(entityId)[0]
13: def fieldName = ms.getAttributeFieldName(attrId)
14: def value = BeanHelper.asString(categoryOID)
15: def keyMap = [:]
16: keyMap[fieldName] = NumberUtils.toInt(value)
17: VCategory category = (VCategory) table.findByKey(keyMap)
18: def products = category.getVCategory2VProduct()
19: count = products.size()
20: if (count != 0) {
21: 	count--
22: }
23: return count
  • Defines the inputs of the script (line 1).
  • Defines the id of the VCategory entity (line 9).
  • Obtains the model service. The rtx is the shared runtime manager. It is an implicit variable (line 10)
  • Creates an instance of VirtualEntityTable using the VCategory entity id (line 11)
  • Extracts the entity key attributes ids and selects the first one, because we know that the key of the entity VCategory is a single key (line 12).
  • Obtains the field name of the key attribute and the string value of the input (categoryOID) and fills the key map (lines 13-16)
  • Retrieves the VCategory object from the table finding it by key (line 17)
  • Retrieves the list of products navigating the role VCategory_2_VProduct (line 18). Note that the methodgetVCategory_2_VProduct() is a method of the generated class VCategory that you can find in <pathToApplication>/WEB-INF/classes/com/webratio/webapp folder where all classes and hibernate config files are stored. If the name of the entity or of the roles are changed the script must be updated using the same names.
  • Returns the result (line 23).

VirtualEntityTable class

The VirtualEntityTable class provides some methods that can be used in scripts to retrieve the necessary information from virtual entities. The table below indexes these methods.

Constructor
VirtualEntityTable(String entity, Map sessionCtx, RTXService service)
Constructs a new wrapper.
Parameters:
  • entity - the identifier of an entity.
  • sessionCtx - the session context.
  • service - the runtime service.

Throws:

  • RTXException


Method Summary
void connect(Object object, Object targetObject, String roleId)
Connects the given object to the target object depending on the role id.
Parameters:
  • object - the object to connect
  • targetObject - the object to which connect the given object
  • roleId - the role to be used for the connection

Throws:

  • RTXException
boolean contains(Object object)
Returns true if the given object is already present into this virtual table.
Parameters:
  • object - the volatile object

Returns:

  • true if the given object is already present into this virtual table
void delete(Collection objects)
Deletes all the given objects.
Parameters:
  • objects - the collection of objects to be deleted
void disconnect(Object object, Object targetObject, String roleId)
Disconnects the given object from the target object depending on the role id.
Parameters:
  • object - the object to disconnect
  • targetObject - the object from which disconnect the given object
  • roleId - the role to be used for the disconnection

Throws:

  • RTXException
void disconnectBeforeDelete(Object object)
Disconnects the given object from all the entity outgoing roles.
Parameters:
  • objects - the volatile object instance

Throws:

  • RTXException
List findByFields(Map fieldMap)
Returns all objects depending on the given field values.
Parameters:
  • fieldMap - the map containing all field values by field name

Returns:

  • the list of objects depending on the given field values.

Throws:

  • RTXException
Object findByKey(Map key)
Finds and returns the object instance by the given key attributes values.
Parameters:
  • key - the map containing the key attribute values by field name

Returns:

  • the object instance.

Throws:

  • RTXException
Set getConnectedObjects(Object object, String roleId)
Retrieves all the connected objects depending on the role id.
Parameters:
  • object - the volatile object instance
  • roleId - the role to be used to retrieve connected objects

Returns:

  • the set of connected objects depending on the role id.

Throws:

  • RTXException
String getEntityId()
Gets the entity identifier.
Returns:
  • the entity id.
Map getKeyMap(Object object)
Computes and returns the key attributes map of the given volatile object.
Parameters:
  • object - the volatile object instance

Returns:

  • the key attributes map of the given volatile object

Throws:

  • RTXException
Object getRow(index)
Returns the object instance at the specified index.
Parameters:
  • index - the index of the row.

Returns:

  • the object instance at the specified index.
List getRows()
Retrieves the list of beans, creating a new list (and storing it in the session context) if not found.
Returns:
  • the list of entity beans.
void save(Object object)
Saves a new object in the virtual database. If the associated entity is not a top level entity, the instance is also saved in the list of ancestor entity instances.
Parameters:
  • object - the new object to save.

Throws:

  • RTXException
int size()
Gets the size of the virtual table.
Returns:
  • the number of rows.

Download the example

Click on the following link to download a sample project containing all the resources to test this article.

Acme - Accessing Virtual Entities Project

Personal tools