How To Templatize a File

Table of Contents


Any file within a techstack package can be turned into a template file.  A template file is usually required when the domain model or project properties contains information that is needed to cause conditional output within a template file.

Accessing the Domain Model

During processing, Harbormaster parses a domain model into a single common meta-model. Next, this meta-model is stored  within the Apache Velocity context and assigned to a variable by the name of $aib.  The Harbormaster meta-model is a set of Java classes that are used to encapsulate and allow for navigation through a domain model.  The meta-model contains an extensive set of methods to simplify getting details within a domain model. Take a few minutes to explore the Java docs for this class.

The following are methods of interest:

method namedescriptionexample
public ClassObject getClassObject()Returns the current entity being processed by Harbormaster$aib.getClassObject()
public ClassObject getClassObject(String className)Returns the an entity from the model that matches the provided name. Null if nothing found$aib.getClassObject(“Customer”)
public List getClassesToGenerate()Returns a list of the entities in the model$aib.getClassesToGenerate()
public List getEnumClassesToGenerate()
Returns a list of the enumerated in the model$aib.getEnumClassesToGenerate()

Accessing the Project Properties

During processing, the complete set of project properties is made available within the Apache Velocity context by referencing a variable by the name of $aib.  The following are methods of interest:

method namedescriptionexample
String getParam(String paramKey)Returns the corresponding project property value. Use dot notation to return nested values.$aib.getParam(“spring.entity-store-type”) where spring is a root level declaration and entity-store-type</i< is one level below it.
public Map<String,String> getParams()Returns all the declared project properties as key/value pairings.
public Map<String,String>   getParamsByPrefix(String prefix,boolean stripPrefix)Returns all the key/value pairs for a given prefix$aib.getParamsByPrefix(“spring”,true)will return a the key/value pairs for the properties declared for spring.
public String getApplicationName()Returns the declared application name, which is the same as getParam(“”)$aib.getApplicationName()


The following are some examples of how to make use of and navigate the domain model within a template file:

Getting all entities in the model


Using the current entity


Using attributes

#set( $includePKs = false )
#foreach( $attribute in $classObject.getDirectAttributes( $includePKs ) )
#set( $attributeName = $Utils.lowercaseFirstLetter( $attribute.getName() ) )
#set( $attributeType = $attribute.getType() )
private ${attributeType} ${attributeName};
#end###foreach( $attribute in $class.getDirectAttributes( $includePKs ) )

Using associations

#set( $className = $classObject.getName())
#foreach( $multiAssociation in $classObject.getMultipleAssociations() )
#set( $roleName = $multiAssociation.getRoleName() )
#set( $childType = $multiAssociation.getType() )
#set( $alias = ${multiAssociation.getAddToCommandAlias()} )
* save ${roleName} on ${className}
* @param command ${alias}
public void addTo${roleName}( @RequestBody(required=true) ${alias} command ) {
try {
${className}BusinessDelegate.get${className}Instance().addTo${roleName}( command ); 
catch( Exception exc ) {
LOGGER.log( Level.WARNING, "Failed to add to Set $roleName", exc );

Using query methods

#foreach( $query in $aib.getQueriesToGenerate(${className}) )
#foreach( $handler in $query.getHandlers() )
#set( $method = $handler.getMethodObject() )
#if ( ${method.hasArguments()} )
#set( $queryName = $Utils.capitalizeFirstLetter( $handler.getName() ) )
#set( $argType = ${method.getArguments().getArgs().get(0).getType()} )
#set( $argName = ${method.getArguments().getArgs().get(0).getName()} )
#set( $returnType = ${method.getArguments().getReturnType()} )