The data type API
The classes in gw.datatype form the core of the data type API. Most of
the time, you do not need to use data types directly, as Guidewire uses
these internally in the system. However, there can be cases in which you
need to access a data type, typically to determine the constraints
information.
Retrieving the data type for a property
To retrieve the data type for a property, you could look up the annotation on the property. You could then look up the data type reflectively, using the name and parameters properties of the annotation. However, this is a cumbersome process. As a convenience, use the following method instead:
gw.datatype.DataTypes.get(gw.lang.reflect.IAnnotatedFeatureInfo)For example:
var property = Claim.Type.TypeInfo.getProperty("ClaimNumber")
var claimNumberDataType = DataTypes.get(property)
The gw.datatype.DataTypes.get(gw.lang.reflect.IAnnotatedFeatureInfo) method also has provides some performance optimizations. Therefore, Guidewire recommends that you use this method rather than looking up the annotation directly from the property.
Retrieving a particular data type in Gosu
If you need an instance of a particular data
type, use the corresponding method on gw.datatype.DataTypes.
A static method exists on this type for each data type in the system.
Some data types have two methods:
- One method that takes all parameters
- One method that takes only the required parameters
For example:
var varcharDataType = DataTypes.varchar(10)
var encryptedVarcharDataType = DataTypes.varchar(10,
/* validator */ null,
logicalSize */ null,
/* encryption */ true,
/* trimwhitespace */ null)
Retrieving a data type reflectively
In rare cases, you may need to look up a data type reflectively. To do this, you need the name of the data type, and a map containing the parameters for the data type. For example:
var varcharDataType = DataTypes.get("varchar", { "size" -> "10" })Using the IDataType methods
After you have a data type, you can access its various aspects using one of the asXXXDataType methods, which are:
asConstrainedDataType() : IConstrainedDataTypeasPersistentDataType() : IPersistentDataTypeasPresentableDataType() : IPresentableDataType
For example, suppose that you want to determine the maximum length of a property:
var claim : Claim = ...
var claimNumberProperty = Claim.Type.TypeInfo.getProperty("ClaimNumber")
var claimNumberDataType = DataTypes.get(claimNumberProperty)
var maxLength = claimNumberDataType.asConstrainedDataType().getLength(claim, claimNumberProperty)
It may seem odd that the getLength(java.lang.Object, gw.lang.reflect.IPropertyInfo) method (in this example) takes the claim and the claim number property. The reason for this is that the constraint and presentation aspects of data types are dynamic, meaning that they are based on context.
Many of the methods on gw.datatype.IConstrainedDataType
and gw.datatype.IPresentableDataType
take a context object, representing the owner of the property with the
data type, along with the property in question. This allows the implementation
to provide different behavior, based on the context. If you do not have
the context object or property, then you can pass null for either of these arguments.
If you implement a data type, then you must handle the case in which the context is unknown.
