7.4 NamingStrategy

NamingStrategy implementation are responsible to derive default names of generated Artefacts (e.g column and table names), given the respective model element.

/**
 * Strategy interface for determining various names, like column and table
 * names, given the respective model elements. This interface is used from the
 * default workflow and templates during a generator run.
 * 
 * Implementations use this to implement project-scoped naming standards.
 */
public interface NamingStrategy {
	/**
	 * Return a table name for an {@code Dao}
	 * 
	 * @param dao
	 *            the dao instance to generate the table name for
	 * @return a table name for the given dao
	 */
	String getTableName(Dao dao);

	/**
	 * Return the alias name for an {@code Dao}.
	 * 
	 * @param dao the dao instance to generate the table alias for
	 * @return an alias name for the given dao
	 */
	String getQualifier(Dao dao);

	/**
	 * Return the column name for an <code>Attribute</code>.
	 * 
	 * @param entity the entity of the given attribute 
	 * @param feature the feature to calculate the column for
	 * @return a column name for the given feature
	 */
	String getColumnName(Entity entity, Attribute feature);

	/**
	 * Return the column name for a nested (embeddable) <code>Attribute</code>.
	 * 
	 * @param entity the entity of the given attribute 
	 * @param feature the feature to calculate the column for
	 * @param nestedfeature
	 * @return a column name for the given nested feature
	 */
	String getColumnName(Entity entity, Attribute feature, Attribute nestedfeature);

	/**
	 * Return the implementation package name for the given modeElement.
	 * 
	 * @param modeElement
	 *            the modeElement instance to calculate the package for
	 * @return the implementation package name for the given modeElement
	 */
	String getPackageName(ModelElement modeElement);

	/**
	 * Return the interface package name for the given modeElement.
	 * 
	 * @param modeElement
	 *            the modeElement instance to calculate the package for
	 * @return the interface package name for the given modeElement
	 */
	String getInterfacePackageName(ModelElement modeElement);

}

Projects can implement this interface or extend from the built-in DefaultNamingStrategy to enforce project-specific naming standards. The generator determines the current NamingStragy based on the value of the domainModel.namingstrategy.class property. Within eclipse a specific NamingStrategy is easily configured by the means of standard eclipse project properties as already described in the previous section (see Properties). The following listing gives an example for such a project specific NamingStrategy implementation and shows the customized generator output.

public class ProjectNamingStrategy extends DefaultNamingStrategy {

	@Override
	public String getTableName(Dao dao) {
		return String.format("T_%s", dao.getEntity().getName().toUpperCase());
	}

	@Override
	public String getColumnName(Entity entity, Attribute attribute) {
		String entityName = entity.getName().toUpperCase();
		String attributeTypeName = attribute.getType().getDataType().getName();
		String attributeName = attribute.getName().toUpperCase();
		return String.format("C_%S_%s_%s", attributeTypeName, entityName, attributeName);
	}

	@Override
	public String getColumnName(Entity entity, Attribute feature, Attribute nestedFeature) {
		String embeddableName = feature.getName().toUpperCase();
		String attributeName = nestedFeature.getName().toUpperCase();
		return String.format("C_%s_%s", embeddableName, attributeName);
	}
}

Note: you'll have to include the following additional jars: dsl-core-version.jar, dsl-dom-version.jar and emfs ecore and common jars.

Based on the example Customer demo model and the previous custom naming strategy the generator would create the following column and table names.

 <class name="org.openxma.demo.customer.model.impl.CustomerImpl"
           table="T_CUSTOMER" discriminator-value="CSTMR">
        <id name="oid" access="property">
            <column name="C_STRING_CUSTOMER_OID" >
                <comment><![CDATA[technical identifier of CustomerDao]]></comment>
            </column>
            <generator class="assigned" />
        </id>
        <version name="version" access="field" unsaved-value="null" type="timestamp">
        	<column name="C_DATE_CUSTOMER_VERSION"/>
        </version>
		<property name="firstName" not-null="true">
			<column name="C_STRING_CUSTOMER_FIRSTNAME"/>
		</property>
		<property name="lastName" not-null="true">
			<column name="C_STRING_CUSTOMER_LASTNAME"/>
		</property>
		<property name="emailAddress" not-null="true">
			<column name="C_STRING_CUSTOMER_EMAILADDRESS"/>
		</property>
		<property name="birthDate" not-null="true">
			<column name="C_DATE_CUSTOMER_BIRTHDATE"/>
		</property>
		<component name="invoiceAddress" class="org.openxma.demo.customer.Address" access="field">
			<property name="streetName">
		    	<column name="C_INVOICEADDRESS_STREETNAME" />
			</property>
			<property name="streetNumber">
		    	<column name="C_INVOICEADDRESS_STREETNUMBER" />
			</property>
			<property name="zip">
		    	<column name="C_INVOICEADDRESS_ZIP" />
			</property>
			<property name="city">
		    	<column name="C_INVOICEADDRESS_CITY" />
			</property>
			<property name="country">
		    	<column name="C_INVOICEADDRESS_COUNTRY" />
			</property>
		</component>
		<component name="deliveryAddress" class="org.openxma.demo.customer.Address" access="field">
			<property name="streetName">
		    	<column name="C_DELIVERYADDRESS_STREETNAME" />
			</property>
			<property name="streetNumber">
		    	<column name="C_DELIVERYADDRESS_STREETNUMBER" />
			</property>
			<property name="zip">
		    	<column name="C_DELIVERYADDRESS_ZIP" />
			</property>
			<property name="city">
		    	<column name="C_DELIVERYADDRESS_CITY" />
			</property>
			<property name="country">
		    	<column name="C_DELIVERYADDRESS_COUNTRY" />
			</property>
		</component>
    </class>