Example: Data Model Class shows an example data model class.
The data model class must extend the DataObject class and use the [
Model] annotation tag. Typically, you create the data model class as a separate class or package in your plug-in module. This package must import the package
com.vmware.core.model.DataObject from the vSphere Web Client SDK.
The type tag in the
[Model] annotation is optional. If you specify the
type tag in the annotation, the data model class can retrieve properties only for objects of the specified type. You can create a data model class to request common properties for multiple object types, or you can use the
type tag in the
[Model] annotation to make the data model class specific to a particular vSphere object type.
In Example: Data Model Class, the class
MyVmData has a
[Model] annotation and extends
DataObject. In the example, the object type
VirtualMachine is specified, indicating that the data model retrieves information on Virtual Machine objects.
In the data model class, you must specify a class property for each data property to retrieve through DAM. When you declare each property, you must include a [Model] annotation with a
property tag. The annotation associates the class property with the associated data property for the vSphere object. When you use the data model class in a data request, DAM retrieves the data specified in the annotation from the vSphere environment, and assigns the value to the associated class property.
In Example: Data Model Class, the class
MyVmData includes the
myVmName and
myVmPowerState annotated properties. Each property includes an annotation that specifies the DAM data property associated with the class property.
Example: Data Model Class with a Related Object Property Annotation shows a data model class with a property annotated for the DAM to retrieve data from a related object. In the example, the property
myVmHostName is annotated for the DAM to retrieve the
name property for the host object related to the target virtual machine object.
You can retrieve properties over multihop relationships by specifying each hop separated by a comma. Example: Data Model Class with a Multi-hop Relationship Annotation shows a property annotated to retrieve data over a multi-hop relationship. In the example, the
myDatacenterName property is annotated to retrieve the
name property for a Datacenter object associated with the Host for a given Virtual Machine.
Omitting the property tag implicitly declares that the class member variable is a data model of the same object type as the parent data model. If the
relation tag is specified, however, the class property is assumed to be a data model for the object type specified in the
relation tag.
Example: Nested Data Model Classes shows a data model class with two class properties that are nested data models. The first property,
myVmHardwareData, is another data model for Virtual Machine objects, the same as the parent data model. The second property,
myHostDetails, is a data model for Host objects.
Important If the related object for which you want to retrieve data is a user-defined custom object, you must use a different syntax in your [Model(relation="..."] annotation. Your
[Model] annotation must explicitly specify both the custom object type, and the nested data model that you use to define that type.
Example: Related Data Model for Custom Object Type shows a data model class that uses a nested data model for a user-defined custom Chassis object.
In the example, the [Model(relation="..."] annotation specifies the exact object type for
Chassis as comexample:Chassis. The annotation also includes an explicit
nestedModel attribute that specifies the fully-qualified class name for the Chassis data model. Use this syntax for any custom object type that you include in a data model class.
Your mediator requests data from DAM by dispatching data request events. To dispatch the events in the DAM API, your mediator class declaration must include an [Event] annotation for each type of event it dispatches, and the class must extend the base SDK class
EventDispatcher. See
Example 5-16, on page 54. In the class methods, you use the
dispatchEvent() method to dispatch each data request event.
Your mediator class can listen for, and handle, data response events by annotating a class method with the [ResponseHandler] annotation. You specify the type of event for which the method listens in the
[ResponseHandler] annotation. See
Example 5-16, on page 54.
Data Request and Data Response Events contains the types of data request events, the resulting data response events, and the associated result types included in the DAM API.
Example 5-16, on page 54, shows an example mediator class that interfaces with the DAM by dispatching data request events and handling data response events. The class
MyDataViewMediator extends the
EventDispatcher base class, and the declaration has an
[Event] annotation for each of the data request events that the class can dispatch.
The DataByConstraintRequest specifies the constraint using the
createConstraintForRelationship method. In the example, the constraint specifies the virtual machine objects related to the host object specified by
hostRef, which represent all virtual machines related to the given host.
Data response events contain an additional error property that you can use to obtain more information about the results of a failed data request. The
error property is available as an optional parameter to the response handler method.
Example 5-17, on page 55, shows a code excerpt that demonstrates the use of this property.
A data update specification is included in the SDK base class DataRequestInfo. You must create an instance of
DataRequestInfo and set the
DataUpdateSpec property, and then pass the
DataRequestInfo object when you create your data request. See
Example 5-18, on page 56.
To create an implicit binding, you use the newImplicitInstance() method to construct the
DataUpdateSpec object. To create an explicit binding, you use the
newExplicitInstance() method.
Example 5-18, on page 56, shows an example mediator class that includes the
DataRequestInfo and
DataUpdateSpec objects to create an automatic binding in the data request. In the example, the
DataUpdateSpec is constructed using the
newImplicitInstance() method, resulting in an implicit binding.
In the example, the function onStart dispatches the initial
DataByModelRequest. The initial request includes a
DataRequestInfo object with an implicit
DataUpdateSpec. The function
onDataRetrieved is annotated to handle the response, and updates a UI control with the returned data. Because of the data update specification, the DAM calls
onDataRetrieved when the data changes.