Script units accessing Virtual Entities
From Wiki
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 method
getVCategory_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:
Throws:
|
| Method Summary | ||
|---|---|---|
void |
connect(Object object, Object targetObject, String roleId)Connects the given object to the target object depending on the role id. |
Parameters:
Throws:
|
boolean |
contains(Object object)Returns true if the given object is already present into this virtual table. |
Parameters:
Returns:
|
void |
delete(Collection objects)Deletes all the given objects. |
Parameters:
|
void |
disconnect(Object object, Object targetObject, String roleId)Disconnects the given object from the target object depending on the role id. |
Parameters:
Throws:
|
void |
disconnectBeforeDelete(Object object)Disconnects the given object from all the entity outgoing roles. |
Parameters:
Throws:
|
List |
findByFields(Map fieldMap)Returns all objects depending on the given field values. |
Parameters:
Returns:
Throws:
|
Object |
findByKey(Map key)Finds and returns the object instance by the given key attributes values. |
Parameters:
Returns:
Throws:
|
Set |
getConnectedObjects(Object object, String roleId)Retrieves all the connected objects depending on the role id. |
Parameters:
Returns:
Throws:
|
String |
getEntityId()Gets the entity identifier. |
Returns:
|
Map |
getKeyMap(Object object)Computes and returns the key attributes map of the given volatile object. |
Parameters:
Returns:
Throws:
|
Object |
getRow(index)Returns the object instance at the specified index. |
Parameters:
Returns:
|
List |
getRows()Retrieves the list of beans, creating a new list (and storing it in the session context) if not found. |
Returns:
|
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:
Throws:
|
int |
size()Gets the size of the virtual table. |
Returns:
|
Download the example
Click on the following link to download a sample project containing all the resources to test this article.

