net.java.ao.schema
Class PluralizedNameConverter

java.lang.Object
  extended by net.java.ao.schema.AbstractTableNameConverter
      extended by net.java.ao.schema.PluralizedNameConverter
All Implemented Interfaces:
TableNameConverter

public class PluralizedNameConverter
extends AbstractTableNameConverter

A simple table name converter which imposes a set of regular-expression rules to pluralize names generated by the delegate converter. Unlike most table name converters, this one does not actually generate a table name from a class. Rather, it takes a name generated by another converter and applies some hard-coded rules to pluralize the result. (these rules are contained within the "/net/java/ao/schema/englishPluralRules.properties" file). The default delegate converter is CamelCaseTableNameConverter

Unfortunately, due to complexities innate to the English language, automatic pluralization is never very accurate. The sheer number of irregular nouns, coupled with the different conjugations which can unrecognizably mangle even an "every day" noun makes it extremely difficult to define generic rules which can control pluralization. ActiveObjects attempts to uphold the highest standards of quality in its pluralization engine by imposing rigorous tests using a wide variety of dictionary words. However, the pluralization should be considered experimental at best. Applications using pluralization in a production environment will often have the need to define rules manually (thus the AbstractTableNameConverter.addPatternMapping(String, String) method). It is for these reasons that pluralization is not enabled by default (as it is in other ORMs).

To enable table name pluralization, simply set the name converter for the EntityManager in question to an instance of this class. Thus:

EntityManager em = ...
 m.setTableNameConverter(new PluralizedNameConverter());

Although this class will delegate to any valid TableNameConverter implementation, it will provide optimal functionality when used in conjunction with a subclass of AbstractTableNameConverter.

Author:
Daniel Spiewak

Constructor Summary
PluralizedNameConverter()
          Creates a new pluralizing table name converter using a new instance of CamelCaseTableNameConverter as a delegate.
PluralizedNameConverter(TableNameConverter delegateConverter)
          Creates a new pluralizing table name converter using the specified name converter as a delegate.
 
Method Summary
protected  String convertName(Class<? extends RawEntity<?>> entity)
          Performs the actual operation of converting a class type into a proper table name.
 TableNameConverter getDelegate()
          Returns the delegate name converter used for all pre-pluralization conversions.
protected  String postProcessName(String back)
          Performs second-step post-processing on converted names.
 String toString()
           
 
Methods inherited from class net.java.ao.schema.AbstractTableNameConverter
addClassMapping, addClassMappings, addPatternMapping, addPatternMappings, getName, processName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PluralizedNameConverter

public PluralizedNameConverter()
Creates a new pluralizing table name converter using a new instance of CamelCaseTableNameConverter as a delegate. Suitable for most use-cases.


PluralizedNameConverter

public PluralizedNameConverter(TableNameConverter delegateConverter)
Creates a new pluralizing table name converter using the specified name converter as a delegate. This constructor provides for chaining with another name converter, thus allowing a great deal of flexibility in pluralizing table names of various conventions.

Parameters:
delegateConverter - The table name converter to which raw table name requests will be delegated (with the results being pluralized by this class). Should be an instance of AbstractTableNameConverter.
Method Detail

getDelegate

public TableNameConverter getDelegate()
Returns the delegate name converter used for all pre-pluralization conversions.


convertName

protected String convertName(Class<? extends RawEntity<?>> entity)
Description copied from class: AbstractTableNameConverter

Performs the actual operation of converting a class type into a proper table name. This method need not concern itself with particulars of @Table annotations or pattern mappings. All of these things are handled in the AbstractTableNameConverter.getName(Class) method in the superclass implementation.

This method must run as fast as possible and should be thread-safe. If necessary, caching may be employed, but it is also within scope for such caching to be utilized within the superclass. As such, the implementation of this method should be kept extremely simple. Also note that this method may not be called for every entity type which must be converted (due to mappings, @Table overrides, etc).

Specified by:
convertName in class AbstractTableNameConverter
Parameters:
entity - The entity type which must be converted.
Returns:
The unique table name which corresponds to the given type.

postProcessName

protected String postProcessName(String back)
Description copied from class: AbstractTableNameConverter

Performs second-step post-processing on converted names. This method is called to perform last-stage conventions enforcement prior to utilization of the table name. This allows implementations to impose sweeping conventions such as "all table names should be uppercase" and so on. Any minor or name-specific conventions should be enforced within the AbstractTableNameConverter.processName(String) method.

The main reason for this method's existence is actually to allow for table name pluralizing on conversions imposing odd conventions, such as all uppercase or all lowercase. This method should be invoked as the last step of any table name conversion (with the exception of class mappings and @Table annotations) to allow for extremely broad-brush changes. The default implementation simply returns the passed name.

Overrides:
postProcessName in class AbstractTableNameConverter
Parameters:
back - The generated table name which should be processed to suit large-scale conventions.
Returns:
An (optionally) modified version of the given table name.

toString

public String toString()
Overrides:
toString in class AbstractTableNameConverter


Copyright © 2007-2011. All Rights Reserved.