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, according to converter annotations or default converter on the view helper fields, in Struts 2. Converter annotations are found in name.matthewgreet,strutscommons.annotation (which allows custom converters). This must be executed in a Struts 2 Action context, whether in Action code or in an Interceptor.

Such converter annotations are for converting a single string value to a single value form field or collection form field, so this library only supports formatting a single record field to a single display field, formatting a collection record field to a single display field. However, this library is designed with extensions in mind. See getDisplayFormatMode(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>).

  • 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.
    • getAnnotationEntries

      protected <T> InterceptorCommonLibrary.AnnotationEntries<T> getAnnotationEntries(Field displayField)
      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 form field or display field, or null if none available.
    • getDefaultConverter

      protected <T> Converter<?,T> getDefaultConverter(DefaultDisplayFormatter.DisplayFieldUsage<T> displayFieldUsage)
      Returns default converter for a single value form field or 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)
      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. Only SINGLE_TO_SINGLE and COLLECTION_TO_SINGLE are actually used and the rest regarded as inconsistent with converter annotation usage. Subclasses can override this support other modes.
    • 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.

      This is not actually used and exists for future use.

      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.

      This is not actually used and exists for future use.

      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.

      This is not actually used and exists for future use.

      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.

      This is not actually used and exists for future use.

      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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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(name.matthewgreet.strutscommons.util.DefaultDisplayFormatter.DisplayFieldUsage<?>), 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
    • 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