Script units accessing Virtual Entities
From WebRatio WebML Wiki
|
Contents |
Retrieving data from Virtual Entities
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:
#input int categoryOID
import com.webratio.rtx.core.BeanHelper
import org.apache.commons.lang.math.NumberUtils
import com.webratio.rtx.vdb.VirtualEntityTable
import com.webratio.webapp.VCategory
def count = 0
def entityId = "ent10"
/* obtains the model service */
def ms = rtx.getModelService()
/* creates an instance of VirtualEntityTable using the VCategory entity id */
def table = new VirtualEntityTable(entityId, sessionContext, service)
/* extracts the entity key attributes ids */
def attrId = ms.getKeyAttributeIds(entityId)[0]
/* obtains the field name of the key attribute */
def fieldName = ms.getAttributeFieldName(attrId)
/* creates the keys map */
def keyMap = [:]
keyMap[fieldName] = categoryOID
/* obtains the category object finding it by key */
VCategory category = (VCategory) table.findByKey(keyMap)
/* navigates the Category to Product role to obtain the category's products */
def products = category.getVCategory2VProduct()
count = products.size()
if (count != 0) {
count--
}
return count
Note that the method getVCategory2VProduct() 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.
Deleting data from Virtual Entities
As an example, suppose now that we want to add the functionality to delete a product using a Script Unit (even though we can easily use the Delete Unit). Refer to the following model:
The "delete product" functionality can be accomplished by the following script:
#input int productOID import com.webratio.rtx.core.BeanHelper import org.apache.commons.lang.math.NumberUtils import com.webratio.rtx.vdb.VirtualEntityTable import com.webratio.webapp.VProduct def count = 0 def entityId = "ent9" /* obtains the model service */ def ms = rtx.getModelService() /* creates an instance of VirtualEntityTable using the VProducts entity id */ def table = new VirtualEntityTable(entityId, sessionContext, service) /* extracts the entity key attributes ids */ def attrId = ms.getKeyAttributeIds(entityId)[0] /* obtains the field name of the key attribute */ def fieldName = ms.getAttributeFieldName(attrId) /* creates the keys map */ def keyMap = [:] keyMap[fieldName] = productOID /* obtains the product object finding it by key */ VProduct product = (VProduct) table.findByKey(keyMap) /* disconnects the product from all the entity outgoing role */ table.disconnectBeforeDelete(product) /* creates a list of Objects for the delete method */ def objectsToDelete = [] objectsToDelete.add(product) /* deletes from the VProducts table the product received in input */ table.delete(objectsToDelete)
In the following model we are adding the possibility for the user to delete a category and all the products related to it using a Script Unit:
The script that realizes this functionality is as follow:
#input int categoryOID import com.webratio.rtx.core.BeanHelper import org.apache.commons.lang.math.NumberUtils import com.webratio.rtx.vdb.VirtualEntityTable import com.webratio.webapp.VCategory def count = 0 def categoryEntityId = "ent10" def productEntityId = "ent9" /* obtains the model service */ def ms = rtx.getModelService() /* creates an instance of VirtualEntityTable using the VCategory entity id */ def categoryTable = new VirtualEntityTable(categoryEntityId, sessionContext, service) def productTable = new VirtualEntityTable(productEntityId, sessionContext, service) /* extracts the entity key attributes ids */ def attrId = ms.getKeyAttributeIds(categoryEntityId)[0] /* obtains the field name of the key attribute */ def fieldName = ms.getAttributeFieldName(attrId) /* creates the keys map */ def keyMap = [:] keyMap[fieldName] = categoryOID /* obtains the category object finding it by key */ VCategory category = (VCategory) categoryTable.findByKey(keyMap) /* navigates the Category to Product role to obtain the category's products */ def products = category.getVCategory2VProduct() /* disconnects the category from all the entity outgoing role */ categoryTable.disconnectBeforeDelete(category) /* creates a list of Objects for the delete method */ def objectsToDelete = [] objectsToDelete.add(category) /* delete the category */ categoryTable.delete(objectsToDelete) /* delete all the category's products */ productTable.delete(products)
Adding data to Virtual Entities
Now we want to give to the user the possibility to add a new product. Refer to the following model:
The script that implements this functionality is the following:
#input String categoryName import com.webratio.rtx.core.BeanHelper import org.apache.commons.lang.math.NumberUtils import com.webratio.rtx.vdb.VirtualEntityTable import com.webratio.webapp.VCategory def count = 0 def entityId = "ent10" /* obtains the model service */ def ms = rtx.getModelService() /* creates an instance of VirtualEntityTable using the VCategory entity id */ def table = new VirtualEntityTable(entityId, sessionContext, service) /* creating the new VCategory */ VCategory category = new VCategory() category.setCategory(categoryName) /* saving the new category */ table.save(category)
Note that it's not necessary to define the value of the oid since it's auto-generated.
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.
Acme - Accessing Virtual Entities Project




