Extending the DataBuilder class

To extend the DatabBuilder class, use the following syntax:

class MyNewBuilder extends DataBuilder<BuilderEntity, BuilderType> {
   ...
}

The DataBuilder class takes the following parameters:

Parameter

Description

BuilderEntity

Type of entity created by the builder. The create method requires this parameter so that it can return a strongly-typed value and, so that other builder methods can declare strongly-typed parameters.

BuilderType

Type of the builder itself. The with methods require this on the DataBuilder class so that it can return a strongly-typed builder value (to facilitate the chaining of with methods).

If you choose to extend the DataBuilder class (gw.api.databuilder.DataBuilder), place your newly created builder class in the gw.api.databuilder package in the Studio Tests folder. Start any method that you define in your new builder with one of the following recommended words:

Initial word

Indicates

on

A link to a parent. For example, PolicyPeriod is on an Account, so the method is onAccount(Account account).

as

A property that holds only a single state. For example, asSmallBusiness or asAgencyBill.

with

The single element or property to be set. For example, the following sets a FirstName property:

    withFirstName("Joe")

Your configuration methods can set properties by calling DataBuilder.set and DataBuilder.addArrayElement. You can provide property values as any of the following:

  • Simple values.
  • Beans to be used as subobjects.
  • Other builders, which PolicyCenter uses to create subobjects if it calls your builder's create method.
  • Instances of gw.api.databuilder.ValueGenerator. For example, an instance that generates a different value to satisfy uniqueness constraints for each instance constructed.

DataBuilder.set and DataBuilder.addArrayElement optionally accept an integer order argument that determines how PolicyCenter configures that property on the target object. (PolicyCenter processes properties in ascending order.) If you do not provide an order for a property, Studio uses DataBuilder.DEFAULT_ORDER as the order for that property. PolicyCenter processes properties with the same order value (for example, all those that do not have an order) in the order in which they are set on the builder.

In most cases, Guidewire recommends that you omit the order value as you are implement builder configuration methods. This enables callers of your builder to select the execution order through the order of the configuration method calls.

Constructors for builders can call set, and similar methods to set up default values. These are useful to satisfy null constraints so it is possible to commit built objects to the database. However, Guidewire generally recommends that you limit the number of defaults. This is so that you have the maximum control over the target object.

Other DataBuilder classes

The gw.api.databuilder package also includes gw.api.databuilder.ValueGenerator. You can use this class to generate a different value for each instance constructed to satisfy uniqueness constraints. The databuilder package includes ValueGenerator class variants for generating unique integers, strings, and typekeys.

  • gw.api.databuilder.SequentialIntegerGenerator 
  • gw.api.databuilder.SequentialStringGenerator 
  • gw.api.databuilder.SequentialTypeKeyGenerator 

Custom builder populators

Ideally, all building can be done through simple property setters, using the DataBuilder.set or DataBuilder.addArrayElements methods. However, you may want to define more complex logic, if these methods do not suffice. To achieve this, you can define a custom implementation of gw.api.databuilder.populator.BeanPopulator and pass it to DataBuilder.addPopulator. Guidewire provides an abstract implementation, AbstractBeanPopulator, to support short anonymous BeanPopulator objects.

The following example uses an anonymous subclass of AbstractBeanPopulator to call the withCustomSetting method. This code passes the group to the constructor, and the code inside of execute only accesses it through the vals argument. This allows the super-class to handle packaging details.

public MyEntityBuilder withCustomSetting(group : Group) {
  
  addPopulator(new AbstractBeanPopulator<MyEntity>(group) {
  
    function execute(e : MyEntity, vals : Object[]) {
      e.customGroupSet(vals[0] as Group)
    }
  })
  return this
}

The AbstractBeanPopulator class automatically converts builders to beans. That is, if you pass a builder to the constructor of AbstractBeanPopulator, it returns the bean that it builds in the execute method. The following example illustrates this.

public MyEntityBuilder withCustomSetting(groupBuilder : DataBuilder<Group, ?>) : MyEntityBuilder {
 
  addPopulator(new AbstractBeanPopulator<MyEntity>(groupBuilder) {

     function execute(e : MyEntity, vals : Object[]) {
      e.customGroupSet(vals[0] as Group)
     }
  })
  return this
}