com.flexive.shared.value
Class FxValue<T,TDerived extends FxValue<T,TDerived>>

java.lang.Object
  extended by com.flexive.shared.value.FxValue<T,TDerived>
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<FxValue>
Direct Known Subclasses:
FxBinary, FxBoolean, FxDate, FxDateRange, FxDateTime, FxDateTimeRange, FxDouble, FxFloat, FxLargeNumber, FxNoAccess, FxNumber, FxReference, FxSelectMany, FxSelectOne, FxString, FxVoid

public abstract class FxValue<T,TDerived extends FxValue<T,TDerived>>
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable<FxValue>

Abstract base class of all value objects. Common base classed is used for multilingual properties, etc.

To check if a value is empty a flag is used for each language resp. the single value. Use the setEmpty() method to explicitly set a value to be empty

Author:
Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
See Also:
Serialized Form

Field Summary
static boolean DEFAULT_MULTILANGUAGE
           
protected  long defaultLanguage
           
protected  java.util.Map<java.lang.Long,java.lang.Boolean> emptyTranslations
           
protected  boolean multiLanguage
           
protected  T singleValue
          Data if multiLanguage is disabled
protected  java.util.Map<java.lang.Long,T> translations
          Data if multiLanguage is enabled
static java.lang.Integer VALUE_NODATA
           
 
Constructor Summary
protected FxValue(boolean multiLanguage, long defaultLanguage, java.util.Map<java.lang.Long,T> translations)
          Constructor
protected FxValue(boolean multiLanguage, long defaultLanguage, T value)
          Constructor
protected FxValue(boolean multiLanguage, java.util.Map<java.lang.Long,T> translations)
          Constructor
protected FxValue(boolean multiLanguage, T value)
          Constructor
protected FxValue(FxValue<T,TDerived> clone)
          Constructor
protected FxValue(long defaultLanguage, boolean multiLanguage)
          Initialize an empty FxValue (used for initalization for XML import, etc.)
protected FxValue(long defaultLanguage, java.util.Map<java.lang.Long,T> translations)
          Constructor
protected FxValue(long defaultLanguage, T value)
          Constructor
protected FxValue(java.util.Map<java.lang.Long,T[]> translations, int pos)
          Constructor - create value from an array of translations
protected FxValue(java.util.Map<java.lang.Long,T> translations)
          Constructor
protected FxValue(T value)
          Constructor
 
Method Summary
 void clearDefaultLanguage()
          Reset the default language to the system language
 void clearValueData()
          Unset value data
 int compareTo(FxValue o)
          A generic comparable implementation based on the value's string representation.
abstract  TDerived copy()
          Creates a copy of the given object (useful if the actual type is unknown).
protected  T copyValue(T value)
          Implement this method for data types that return false from isImmutableValueType().
 boolean equals(java.lang.Object other)
          
 T fromPortableString(java.lang.String value)
          Convert from a portable (not locale specific format)
abstract  T fromString(java.lang.String value)
          Evaluates the given string value to an object of type T.
 T getBestTranslation()
          Get the translation that best fits the current users language.
 T getBestTranslation(FxLanguage language)
          Get the translation that best fits the requested language.
 T getBestTranslation(long lang)
          Get the translation that best fits the requested language.
 T getBestTranslation(UserTicket ticket)
          Get the translation that best fits the requested users language.
 long getDefaultLanguage()
          Get the default language of this value
 T getDefaultTranslation()
          Get a representation of this value in the default translation
abstract  T getEmptyValue()
          Returns an empty value object for this FxValue type.
 T getErrorValue()
          Returns the value that caused isValid() to return false.
 boolean getIsEmpty()
          Like empty(), for JSF EL, since empty cannot be used.
 int getMaxInputLength()
          Returns the maximum input length an input field should have for this value (or -1 for unlimited length).
 java.lang.String getPortableStringValue(T value)
          Converts the given instance of T to a string that can be parsed again by fromPortableString(String).
 long getSelectedLanguage()
          Get the language selected in user interfaces
 java.lang.String getSqlValue()
          Format this FxValue for inclusion in a SQL statement.
 java.lang.String getStringValue(T value)
          Converts the given instance of T to a string that can be parsed again by fromString(String).
 long[] getTranslatedLanguages()
          Get all languages for which translations exist
 T getTranslation(FxLanguage lang)
          Get a String representation of this value in the requested language or an empty String if the translation does not exist
 T getTranslation(long lang)
          Get the translation for a requested language
abstract  java.lang.Class<T> getValueClass()
          Return the class instance of the value type.
 java.lang.Integer getValueData()
          Get attached value data (optional, if not set will return VALUE_NODATA).
 java.lang.Integer getValueDataRaw()
          Get attached value data (optional, if not set will return VALUE_NODATA) including any bit-coded flags.
 java.lang.String getXPath()
          Get the XPath for this value - the XPath is optional and can be an empty String if not explicitly assigned!
 java.lang.String getXPathName()
          Returns the name of the value from the xpath.
 boolean hasDefaultLanguage()
          Is a default value set for this FxValue?
 int hashCode()
          
 boolean hasValueData()
          Are additional value data set for this value instance?
protected  boolean isAcceptsEmptyDefaultTranslations()
           
 boolean isDefaultLanguage(long language)
          Check if the passed language is the default language
 boolean isEmpty()
          Is this value empty?
 boolean isImmutableValueType()
          Return true if T is immutable (e.g.
 boolean isMultiLanguage()
          Is this value available for multiple languages?
 boolean isReadOnly()
          Is this value editable by the user? This always returns true except it is a FxNoAccess value or flagged as readOnly
 boolean isTranslationEmpty(FxLanguage lang)
          Check if the translation for the given language is empty
 boolean isTranslationEmpty(long lang)
          Check if the translation for the given language is empty
 boolean isValid()
          Returns true if this value is valid for the actual type (e.g.
 boolean isValid(FxLanguage language)
          Returns true if the translation for the given language is valid.
 boolean isValid(long languageId)
          Returns true if the translation for the given language is valid.
 void removeLanguage(long language)
          Remove the translation for the given language
 void setChangeListener(FxValueChangeListener changeListener)
          Set the change listener
 void setDefaultLanguage(long defaultLanguage)
          Set the default language.
 void setDefaultLanguage(long defaultLanguage, boolean force)
          Set the default language.
 FxValue setDefaultTranslation(T translation)
          Set the translation in the default language.
 TDerived setEmpty()
          Mark this FxValue as empty
 void setEmpty(long language)
          Mark the entry for the given language as empty
 void setMaxInputLength(int maxInputLength)
          Set the maximum input length for this value (-1 for unlimited length).
 void setReadOnly()
          One-time operation to flag this FxValue as read only.
 FxValue setSelectedLanguage(long selectedLanguage)
          Set the user selected language
 TDerived setTranslation(FxLanguage lang, T translation)
          Set the translation for a language or override the single language value if this value is not flagged as multi language enabled
 TDerived setTranslation(long language, T value)
          Set the translation for a language or override the single language value if this value is not flagged as multi language enabled.
 void setValue(T value)
          For multilanguage values, set the default translation.
 TDerived setValueData(java.lang.Integer valueData)
          Attach additional data to this value instance
 TDerived setXPath(java.lang.String XPath)
          Set the XPath (unless readonly)
 TDerived setXPath(java.lang.String xpathPrefix, java.lang.String xpath)
          Set the XPath (unless readonly)
 java.lang.String toString()
          
 boolean translationExists(long languageId)
          Does a translation exist for the given language?
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_MULTILANGUAGE

public static final boolean DEFAULT_MULTILANGUAGE
See Also:
Constant Field Values

VALUE_NODATA

public static final java.lang.Integer VALUE_NODATA

multiLanguage

protected boolean multiLanguage

defaultLanguage

protected long defaultLanguage

translations

protected java.util.Map<java.lang.Long,T> translations
Data if multiLanguage is enabled


emptyTranslations

protected java.util.Map<java.lang.Long,java.lang.Boolean> emptyTranslations

singleValue

protected T singleValue
Data if multiLanguage is disabled

Constructor Detail

FxValue

protected FxValue(boolean multiLanguage,
                  long defaultLanguage,
                  java.util.Map<java.lang.Long,T> translations)
Constructor

Parameters:
multiLanguage - multilanguage value?
defaultLanguage - the default language
translations - HashMap containing language->translation mapping

FxValue

protected FxValue(long defaultLanguage,
                  boolean multiLanguage)
Initialize an empty FxValue (used for initalization for XML import, etc.)

Parameters:
defaultLanguage - default language
multiLanguage - multilanguage value?

FxValue

protected FxValue(long defaultLanguage,
                  java.util.Map<java.lang.Long,T> translations)
Constructor

Parameters:
defaultLanguage - the default language
translations - HashMap containing language->translation mapping

FxValue

protected FxValue(boolean multiLanguage,
                  java.util.Map<java.lang.Long,T> translations)
Constructor

Parameters:
multiLanguage - multilanguage value?
translations - HashMap containing language->translation mapping

FxValue

protected FxValue(java.util.Map<java.lang.Long,T> translations)
Constructor

Parameters:
translations - HashMap containing language->translation mapping

FxValue

protected FxValue(java.util.Map<java.lang.Long,T[]> translations,
                  int pos)
Constructor - create value from an array of translations

Parameters:
translations - HashMap containing language->translation mapping
pos - position (index) in the array to use

FxValue

protected FxValue(boolean multiLanguage,
                  long defaultLanguage,
                  T value)
Constructor

Parameters:
multiLanguage - multilanguage value?
defaultLanguage - the default language and the language for the value
value - single initializing value

FxValue

protected FxValue(long defaultLanguage,
                  T value)
Constructor

Parameters:
defaultLanguage - the default language
value - single initializing value

FxValue

protected FxValue(boolean multiLanguage,
                  T value)
Constructor

Parameters:
multiLanguage - multilanguage value?
value - single initializing value

FxValue

protected FxValue(T value)
Constructor

Parameters:
value - single initializing value

FxValue

protected FxValue(FxValue<T,TDerived> clone)
Constructor

Parameters:
clone - original FxValue to be cloned
Method Detail

getXPath

public java.lang.String getXPath()
Get the XPath for this value - the XPath is optional and can be an empty String if not explicitly assigned!

Returns:
XPath (optional! can be an empty String)

getXPathName

public java.lang.String getXPathName()
Returns the name of the value from the xpath.

If the xpath is an empty string the name will also return an emptry String.

Returns:
the property name

setXPath

public TDerived setXPath(java.lang.String XPath)
Set the XPath (unless readonly)

Parameters:
XPath - the XPath to set, will be ignored if readonly
Returns:
this

setXPath

public TDerived setXPath(java.lang.String xpathPrefix,
                         java.lang.String xpath)
Set the XPath (unless readonly)

Parameters:
xpathPrefix - the xpath prefix (e.g. instance PK or type)
xpath - the XPath
Returns:
this
Since:
3.2.0

setReadOnly

public void setReadOnly()
One-time operation to flag this FxValue as read only. This is not reversible!


setEmpty

public TDerived setEmpty()
Mark this FxValue as empty

Returns:
this

setEmpty

public void setEmpty(long language)
Mark the entry for the given language as empty

Parameters:
language - the language to flag as empty

getValueClass

public abstract java.lang.Class<T> getValueClass()
Return the class instance of the value type.

Returns:
the class instance of the value type.

fromString

public abstract T fromString(java.lang.String value)
Evaluates the given string value to an object of type T.

Parameters:
value - string value to be evaluated
Returns:
the value interpreted as T

fromPortableString

public T fromPortableString(java.lang.String value)
Convert from a portable (not locale specific format)

Parameters:
value - portable string value to be evaluated
Returns:
the value interpreted as T
Since:
3.1.6

getStringValue

public java.lang.String getStringValue(T value)
Converts the given instance of T to a string that can be parsed again by fromString(String).

Parameters:
value - the value to be converted
Returns:
a string representation of the given value that can be parsed again using fromString(String).

getPortableStringValue

public java.lang.String getPortableStringValue(T value)
Converts the given instance of T to a string that can be parsed again by fromPortableString(String).

Parameters:
value - the value to be converted
Returns:
a string representation of the given value that can be parsed again using fromPortableString(String).
Since:
3.1.6

copy

public abstract TDerived copy()
Creates a copy of the given object (useful if the actual type is unknown).

Returns:
a copy of the given object (useful if the actual type is unknown).

copyValue

protected T copyValue(T value)
Implement this method for data types that return false from isImmutableValueType().

The default implementation returns the argument as-is and throws an IllegalArgumentException when the container class has mutable value types.

Parameters:
value - the value to be copied (not null)
Returns:
an independent copy of value
Since:
3.2.0

isImmutableValueType

public boolean isImmutableValueType()
Return true if T is immutable (e.g. java.lang.String). This prevents cloning of the translations in copy constructors.

Returns:
true if T is immutable (e.g. java.lang.String)

isReadOnly

public boolean isReadOnly()
Is this value editable by the user? This always returns true except it is a FxNoAccess value or flagged as readOnly

Returns:
if this value editable?
See Also:
FxNoAccess

isValid

public boolean isValid()
Returns true if this value is valid for the actual type (e.g. if a FxNumber property actually contains only valid numbers).

Returns:
true if this value is valid for the actual type

isValid

public boolean isValid(long languageId)
Returns true if the translation for the given language is valid. An empty translation is always valid.

Parameters:
languageId - the language ID
Returns:
true if the translation for the given language is valid
Since:
3.1

isValid

public boolean isValid(FxLanguage language)
Returns true if the translation for the given language is valid. An empty translation is always valid.

Parameters:
language - the language
Returns:
true if the translation for the given language is valid
Since:
3.1

getErrorValue

public T getErrorValue()
                throws java.lang.IllegalStateException
Returns the value that caused isValid() to return false. If isValid() is true, a RuntimeException is thrown.

Returns:
the value that caused the validation via isValid() to fail
Throws:
java.lang.IllegalStateException - if the instance is valid and the error value is undefined

getDefaultTranslation

public T getDefaultTranslation()
Get a representation of this value in the default translation

Returns:
T

getTranslation

public T getTranslation(long lang)
Get the translation for a requested language

Parameters:
lang - requested language
Returns:
translation or an empty String if it does not exist

getTranslation

public T getTranslation(FxLanguage lang)
Get a String representation of this value in the requested language or an empty String if the translation does not exist

Parameters:
lang - requested language id
Returns:
T translation

getBestTranslation

public T getBestTranslation(long lang)
Get the translation that best fits the requested language. The requested language is queried and if it does not exist the default translation is returned

Parameters:
lang - requested best-fit language
Returns:
best fit translation

getBestTranslation

public T getBestTranslation(FxLanguage language)
Get the translation that best fits the requested language. The requested language is queried and if it does not exist the default translation is returned

Parameters:
language - requested best-fit language
Returns:
best fit translation

getBestTranslation

public T getBestTranslation(UserTicket ticket)
Get the translation that best fits the requested users language. The requested users language is queried and if it does not exist the default translation is returned

Parameters:
ticket - UserTicket to obtain the users language
Returns:
best fit translation

getBestTranslation

public T getBestTranslation()
Get the translation that best fits the current users language. The user language is obtained from the FxContext thread local.

Returns:
best fit translation

getTranslatedLanguages

public long[] getTranslatedLanguages()
Get all languages for which translations exist

Returns:
languages for which translations exist

translationExists

public boolean translationExists(long languageId)
Does a translation exist for the given language?

Parameters:
languageId - language to query
Returns:
translation exists

getIsEmpty

public boolean getIsEmpty()
Like empty(), for JSF EL, since empty cannot be used.

Returns:
true if the value is empty

isEmpty

public boolean isEmpty()
Is this value empty?

Returns:
if value is empty

isTranslationEmpty

public boolean isTranslationEmpty(FxLanguage lang)
Check if the translation for the given language is empty

Parameters:
lang - language to check
Returns:
if translation for the given language is empty

isTranslationEmpty

public boolean isTranslationEmpty(long lang)
Check if the translation for the given language is empty

Parameters:
lang - language to check
Returns:
if translation for the given language is empty

getSelectedLanguage

public long getSelectedLanguage()
Get the language selected in user interfaces

Returns:
selected language

setSelectedLanguage

public FxValue setSelectedLanguage(long selectedLanguage)
                            throws FxNoAccessException
Set the user selected language

Parameters:
selectedLanguage - selected language ID
Returns:
self
Throws:
FxNoAccessException - if the selected Language is not contained

setTranslation

public final TDerived setTranslation(long language,
                                     T value)
Set the translation for a language or override the single language value if this value is not flagged as multi language enabled. This method cannot be overridden since it not only accepts parameters of type T, but also of type String for web form handling.

Parameters:
language - language to set the translation for
value - translation
Returns:
this

setTranslation

public TDerived setTranslation(FxLanguage lang,
                               T translation)
Set the translation for a language or override the single language value if this value is not flagged as multi language enabled

Parameters:
lang - language to set the translation for
translation - translation
Returns:
this

setValue

public void setValue(T value)
For multilanguage values, set the default translation. For single language values, set the value.

Parameters:
value - the value to be stored

setDefaultTranslation

public FxValue setDefaultTranslation(T translation)
Set the translation in the default language. For single-language values, sets the value.

Parameters:
translation - the default translation
Returns:
this

getDefaultLanguage

public long getDefaultLanguage()
Get the default language of this value

Returns:
default language

getMaxInputLength

public int getMaxInputLength()
Returns the maximum input length an input field should have for this value (or -1 for unlimited length).

Returns:
the maximum input length an input field should have for this value

setMaxInputLength

public void setMaxInputLength(int maxInputLength)
Set the maximum input length for this value (-1 for unlimited length).

Parameters:
maxInputLength - the maximum input length for this value (-1 for unlimited length).

setDefaultLanguage

public void setDefaultLanguage(long defaultLanguage)
Set the default language. It will only be set if a translation in the requested default language exists!

Parameters:
defaultLanguage - requested default language

setDefaultLanguage

public void setDefaultLanguage(long defaultLanguage,
                               boolean force)
Set the default language. Will have no effect if the value is not multi language enabled

Parameters:
defaultLanguage - requested default language
force - if true, the default language will also be updated if no translation exists (for UI input)

clearDefaultLanguage

public void clearDefaultLanguage()
Reset the default language to the system language


hasDefaultLanguage

public boolean hasDefaultLanguage()
Is a default value set for this FxValue?

Returns:
default value set

isDefaultLanguage

public boolean isDefaultLanguage(long language)
Check if the passed language is the default language

Parameters:
language - the language to check
Returns:
passed language is the default language

removeLanguage

public void removeLanguage(long language)
Remove the translation for the given language

Parameters:
language - the language to remove the translation for

isMultiLanguage

public boolean isMultiLanguage()
Is this value available for multiple languages?

Returns:
value available for multiple languages

isAcceptsEmptyDefaultTranslations

protected boolean isAcceptsEmptyDefaultTranslations()

getSqlValue

public java.lang.String getSqlValue()
Format this FxValue for inclusion in a SQL statement. For example, a string is wrapped in single quotes and escaped properly (' --> ''). For multilanguage values the default translation is used. If the value is empty (@link #isEmpty()), a runtime exception is thrown.

Returns:
the formatted value

getEmptyValue

public abstract T getEmptyValue()
Returns an empty value object for this FxValue type.

Returns:
an empty value object for this FxValue type.

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object other)

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()

Overrides:
hashCode in class java.lang.Object

compareTo

public int compareTo(FxValue o)
A generic comparable implementation based on the value's string representation.

Specified by:
compareTo in interface java.lang.Comparable<FxValue>
Parameters:
o - the other object
Returns:
see Comparable.compareTo(T).

getValueData

public java.lang.Integer getValueData()
Get attached value data (optional, if not set will return VALUE_NODATA). As value data might contain some bit-coded flags in the future, it is not certain if the full Integer range will be available as some bits might be masked out.

Returns:
attached value data, if not set will return VALUE_NODATA
Since:
3.1.4

getValueDataRaw

public java.lang.Integer getValueDataRaw()
Get attached value data (optional, if not set will return VALUE_NODATA) including any bit-coded flags. Internal use only!

Returns:
raw attached value data, if not set will return VALUE_NODATA
Since:
3.1.4

setValueData

public TDerived setValueData(java.lang.Integer valueData)
Attach additional data to this value instance

Parameters:
valueData - value data to attach
Returns:
this
Since:
3.1.4

clearValueData

public void clearValueData()
Unset value data

Since:
3.1.4

hasValueData

public boolean hasValueData()
Are additional value data set for this value instance?

Returns:
value data set
Since:
3.1.4

setChangeListener

public void setChangeListener(FxValueChangeListener changeListener)
Set the change listener

Parameters:
changeListener - change listener
Since:
3.1.6