Class DefaultDisplayFormatter

java.lang.Object
name.matthewgreet.strutscommons.util.DefaultDisplayFormatter
All Implemented Interfaces:
DisplayFormatter

public class DefaultDisplayFormatter extends Object implements DisplayFormatter

Default library for formatting a record to a view helper in Struts 2. Sets string, string collection or string array display fields from record fields with the same name but formatted according to the conversion annotations, such as IntegerConversion, on display fields, or default converter if no annotation.

For each display field, the exact behaviour depends on it, the matching record field, and conversion annotation. Combinations not shown here aren't allowed.

Display formatting modes
Record field type Display field type Converter annotation Notes
Single value Single value string Converter or none If no converter set, uses default single value converter
Collection Single value string Collection converter or none If no collection converter set, uses default collection converter
Array String array Converter or none If no converter set, uses default converter
Array String collection Converter or none If no converter set, uses default converter
Collection String array Converter or none If no converter set, uses default converter
Collection String collection Converter or none If no converter set, uses default converter

Miscellaneous notes.

  • Constructor Details

    • DefaultDisplayFormatter

      public DefaultDisplayFormatter()
  • Method Details

    • checkFieldClass

      protected <T> boolean checkFieldClass(Class<?> conversionClass, Class<?> fieldClass)
      Returns whether a converter's target data type can convert to the form field's data type.
    • filterDisplayField

      protected boolean filterDisplayField(Field displayField)
      Returns whether display field is allowed to receive a formatted value, which is not annotated with DisableFormatting.
    • formatInit

      protected void formatInit()
      Initialisation that needs to be done before formatting function is run.
    • getAnnotationEntries

      protected <T> InterceptorCommonLibrary.AnnotationEntries<T> getAnnotationEntries(Field displayField, PolicyLookup policyLookup)
      Returns all recognised annotations and their linked policies.
    • getDefaultCollectionConverter

      protected <T> CollectionConverter<?,T> getDefaultCollectionConverter(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage)
      Returns default collection converter for a collection display field, or null if none available.
    • getDefaultConverter

      protected <T> Converter<?,T> getDefaultConverter(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage)
      Returns default converter for a single value display field, or null if none available.
    • getDefaultConverterFromArray

      protected <T> Converter<?,T> getDefaultConverterFromArray(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage)
      Returns default converter for each item of an array display field, or null if none available.
    • getDefaultConverterFromCollection

      protected <T> Converter<?,T> getDefaultConverterFromCollection(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage)
      Returns default converter for each item of a collection display field, or null if none available.
    • getDisplayFieldSources

      protected Collection<DefaultDisplayFormatter.DisplayFieldSource> getDisplayFieldSources(Collection<Field> displayFields, Collection<Field> recordFields)
      Returns every display field that will receive a formatted value and the record field containing the value to be formatted.
    • getDisplayFieldUsage

      protected <T> DefaultDisplayFormatter.DisplayFieldUsage<T> getDisplayFieldUsage(Field recordField, Field displayField, PolicyLookup policyLookup)
      Analyses converter annotations on display field and returns configured formatter to formatted from source field display field, or null if not applicable.
    • getDisplayFormatMode

      protected DefaultDisplayFormatter.DisplayFormatMode getDisplayFormatMode(DefaultDisplayFormatter.DisplayFieldUsage<?> displayFieldUsage)
      Returns mode for formatting a record field to the matching display field. The following combinations are recognised, the rest unsupported.
      Recognised record and display field types and converters
      Record field Display field Converters Mode
      Array Array Single value or default ARRAY_TO_ARRAY
      Array Collection Single value or default ARRAY_TO_COLLECTION
      Collection Array Single value or default COLLECTION_TO_ARRAY
      Collection Collection Single value or default COLLECTION_TO_COLLECTION
      Collection Single value Collection or default COLLECTION_TO_SINGLE
      Single value Single value Single value or default SINGLE_TO_SINGLE
    • getProperties

      protected Collection<Field> getProperties(Class<?> type)
      Returns all properties of a class, even private ones, whether directly declared or inherited.
    • getTypeFromCollectionField

      protected <T> Class<T> getTypeFromCollectionField(Field displayField)
      Returns element type of a collection-based form field.
    • makeCollectionForRecipient

      protected <T> Collection<T> makeCollectionForRecipient(Class<?> recipientClass)
      Returns an empty, modifiable collection from a collection type.
    • processDisplayFieldUsageArrayToArray

      protected <T> void processDisplayFieldUsageArrayToArray(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Sets array display field from each member of an array record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty array.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageArrayToCollection

      protected <T> void processDisplayFieldUsageArrayToCollection(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Sets collection display field from each member of an array record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty collection.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageArrayToSingle

      protected <T> void processDisplayFieldUsageArrayToSingle(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Sets single display field from first member of an array record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty string.

      This is not actually used, not considered useful, and exists for the sake of completeness.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageCollectionToArray

      protected <T> void processDisplayFieldUsageCollectionToArray(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Sets array display field from each member of a collection record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty array.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageCollectionToCollection

      protected <T> void processDisplayFieldUsageCollectionToCollection(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Sets collection display field from each member of a collection record field formatted using converter. If no converter is set, uses default converter for source field. If no default converter and source is a string collection, copies source values, otherwise sets empty collection.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageCollectionToSingle

      protected <T> void processDisplayFieldUsageCollectionToSingle(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception
      Sets single display field from collection record field formatted using collection converter. If no collection converter is set, uses default collection converter for source field. If no default converter, sets empty string.
      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther1

      protected <T> void processDisplayFieldUsageOther1(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER1 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER1 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther10

      protected <T> void processDisplayFieldUsageOther10(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER10 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER10 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther2

      protected <T> void processDisplayFieldUsageOther2(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER2 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER2 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther3

      protected <T> void processDisplayFieldUsageOther3(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER3 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER3 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther4

      protected <T> void processDisplayFieldUsageOther4(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER4 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER4 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther5

      protected <T> void processDisplayFieldUsageOther5(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER5 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER5 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther6

      protected <T> void processDisplayFieldUsageOther6(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER6 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER6 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther7

      protected <T> void processDisplayFieldUsageOther7(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER7 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER7 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther8

      protected <T> void processDisplayFieldUsageOther8(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER8 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER8 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageOther9

      protected <T> void processDisplayFieldUsageOther9(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for OTHER9 formatting mode and does nothing. This exists so subclasses can override it, as well as #getDisplayFormatMode, to define OTHER9 formatting mode.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageSingleToArray

      protected <T> void processDisplayFieldUsageSingleToArray(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Sets array display field from from single record field formatted using configured converter. If no converter is set, uses default converter for source field. If no default converter and source is a string, copies source value, otherwise uses empty string. If the formatted value is not an empty string, sets an array of just that value, otherwise sets an empty array.

      This is not actually used, not considered useful and exists for the sake of completeness.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageSingleToCollection

      protected <T> void processDisplayFieldUsageSingleToCollection(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Sets collection display field from from single record field formatted using configured converter. If no converter is set, uses default converter for source field. If no default converter and source is a string, copies source value, otherwise uses empty string. If the formatted value is not an empty string, sets a collection of just that value, otherwise sets an empty collection.

      This is not actually used, not considered useful and exists for the sake of completeness.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageSingleToSingle

      protected <T> void processDisplayFieldUsageSingleToSingle(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception
      Sets single display field from from single record field formatted using configured converter. If no converter is set, uses default converter for source field. If no default converter and source is a string, copies source value, otherwise sets empty string.
      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • processDisplayFieldUsageUnsupported

      protected <T> void processDisplayFieldUsageUnsupported(Object record, Object display, DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage) throws Exception

      Called for UNSUPPORTED formatting mode and does nothing. This exists so subclasses can override it.

      Parameters:
      record - Record containing unformatted field values. Can be null.
      display - View helper containing fields to receive formatted values.
      displayFieldUsage - Source and target fields and converter configured from annotation.
      Throws:
      Exception
    • getPolicyLookup

      public PolicyLookup getPolicyLookup()
      Returns policy lookup by annotation helper used in validation.
    • setPolicyLookup

      public void setPolicyLookup(PolicyLookup policyLookup)
    • updateDisplay

      public void updateDisplay(Class<?> recordClass, Object record, Object display)

      Sets string fields of display from fields of record with the same name but formatted according to the conversion annotations on display fields, or default converter if no annotation. If record is null, recipient display fields are set to empty string. This function is useful for displaying database records.

      Miscellaneous notes.

      • Source field type must apply to converter's type.
      • Recipient field must be a string, not even string array or string collection.
      • Custom converters only for formatting are best derived from AbstractCustomFormatterSupport or AbstractCustomCollectionFormatterSupport.
      • The default converters for booleans and enumerations don't format to user friendly text, so custom converters are better.
      • Does not follow associations.
      Specified by:
      updateDisplay in interface DisplayFormatter