Setting values of String properties in entity instances

Setting a value on a String data type property on an entity instance causes special behavior that does not happen for String properties on classes. When you set a String value on a database-backed entity type property:

  • Gosu removes spaces from the beginning and end of the String value. This behavior is configurable.
  • If the result is the empty String (""), Gosu sets the value to null instead of the empty String. This behavior is not configurable

In both cases, this change happens immediately as you set the value. This change is not part of committing the data to the database. If you get the property value after setting it, the value is already updated.

For example:

var obj = new temp1()          // New normal Gosu or Java object
var entityObj = new Address()  // New entity instance

// Set String property on a regular object 
obj.City = "           San Francisco      "
display("Gosu", obj.City)
obj.City = "    "
display("Gosu", obj.City)
obj.City = null
display("Gosu", obj.City)

// Set String property on an entity instance
entityObj.City = "           San Francisco      "
display("entity", entityObj.City)
entityObj.City = "    "
display("entity", entityObj.City)
entityObj.City = null
display("entity", entityObj.City)

function display (intro : String, t : String) {
  print (intro + " object " + (t == null ? "NULL" : "\"" + t + "\""))
}

This code prints:

Gosu object "           San Francisco      "
Gosu object ""
Gosu object NULL
entity object "San Francisco"
entity object NULL
entity object NULL

Note that the entity property has no initial or trailing spaces in the first case, and is set to null in the other cases.

If you want to test a String variable to see if it has content or is either null or empty, use the HasContent method.

Configuring whitespace removal for entity text properties

You can control whether PolicyCenter trims whitespace on a database-backed String property on an entity type. Set the trimwhitespace column parameter in the data model definition of the String column. For columns that you define as type="varchar", Gosu trims leading and trailing spaces by default.

To prevent PolicyCenter from trimming whitespace before committing a String property to the database, include the trimwhitespace column parameter in the column definition, and set the parameter to false. The XML definition for a column that does not trim whitespace looks like the following lines:

<column 
  desc="Primary email address associated with the contact." 
  name="EmailAddress1" 
  type="varchar">
  <columnParam name="size" value="60"/>
  <columnParam name="trimwhitespace" value="false"/>
</column>

The parameter controls only whether PolicyCenter trims leading and trailing spaces. You cannot configure whether PolicyCenter coerces an empty string to null.

For both trimming and converting empty String values to null, the change happens immediately that you set the value.

See also