Script units accessing Virtual Entities

From WebRatio WebML Wiki

Jump to: navigation, search


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:
  • 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


Related articles:
Category Difficulty Refers
Context Parameters Web Model Beginner Get Unit
Set Unit
Reset Unit
Create Unit Web Model Beginner Create Unit
Create a "Hello world" application Web Model Beginner Site View
WebML
WebRatio
Create a "Simple data centric" web application Web Model Beginner Site View
WebML
WebRatio
Custom Locations and Master pages Web Model Beginner Master Page
Custom Location
… further results












Did you find this article useful? Please rate it!

Rating: 0.0/5 (0 votes cast)

Personal tools