Package org.cyclos.model.utils
Class ModelHelper
- java.lang.Object
-
- org.cyclos.model.utils.ModelHelper
-
public class ModelHelper extends Object
Helper for handlingCurrencyAmountDTOs
-
-
Field Summary
Fields Modifier and Type Field Description static StringLOCATOR_QUOTEstatic intMAX_INTERNAL_NAME_LENGTHThe maximum length for internal namesstatic intMAX_NAME_LENGTHThe maximum length for names
-
Constructor Summary
Constructors Constructor Description ModelHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static InternalAccountOwneraccountOwner(String principal)Creates a newInternalAccountOwner.static AmountSummaryVOamountSummary(BigDecimal sum, BigDecimal average, int count, CurrencyVO currency)Creates an amount summarystatic ChannelVOchannel(String internalName)static voidclearIds(Iterable<? extends EntityModel> models)Sets the ids of all models to nullstatic <S extends Bean,T extends Bean>
voidcopyProperties(S source, T target)It copies from source to target (apply a bean over another bean) traversing all source's properties.
If a property is not found in the target bean or has a different type then it's ignored.static CurrencyAmountDTOcurrencyAmount(CurrencyVO currency, BigDecimal amount)Creates a newCurrencyAmountDTO.static CurrencyAmountRangeDTOcurrencyRange(CurrencyVO currency, BigDecimal min, BigDecimal max)Creates a newCurrencyAmountRangeDTO.static DatePeriodDTOdatePeriod(DateTime begin, DateTime end)Creates a newDatePeriodDTOstatic DecimalRangeDTOdecimalRange(BigDecimal min, BigDecimal max)Creates a newDecimalRangeDTOstatic DimensionsDTOdimensions(int width, int height)Creates a newDimensionsDTOstatic booleanequals(LatLongDTO first, LatLongDTO second)Check if both coordinates are the samestatic StringescapeIdentifierIfNeeded(String value)When the given value is a valid Long, return it escaped with a trailing '.static StringgetActualInternalNameOrId(EntityVO vo)Returns the internal name of the given VO if it is valid (no nested paths contains a null part).static Set<String>getAllowedMimeTypes(Collection<FileMimeType> allowedMimeTypes, Collection<String> otherMimeTypes)Expand the list of allowed mime types into the actual mime types as stringstatic StringgetEmailValidationMesssage(UserValidationResult result, MessageProducer mProducer)static ObjectgetFirstNonEmpty(Bean bean, Collection<? extends Property<?,?>> properties)Returns the first non-empty of the given properties, according toObjectHelper.isNotEmpty(Object)static List<String>getFriendlyMimeTypes(Collection<String> mimeTypes, MessageProducer messageProducer)Returns a list of user-friendly displayed mime typesstatic List<String>getFriendlyMimeTypes(Collection<FileMimeType> mimes, Collection<String> otherTypes, MessageProducer messageProducer)Returns a list of user-friendly displayed mime typesstatic <T extends Bean>
List<T>getHierarchyPath(T bean, Property<T,T> parentProperty)Returns a list with the path from the hierarchy root up to the given object, having the given parent property to resolve an object's parent.static <T extends Bean>
intgetLevel(T bean, Property<T,T> parentProperty)Returns the level of a given hierarchical bean.static LatLongDTOgetLocation(IAddress address)Returns the location for a given addressstatic StringgetPublicRegistrationMesssage(UserRegistrationResult result, MessageProducer mProducer)static Comparator<UserLocatorVO>getUserLocatorComparator()static Stringidentifier(AccountOwner owner, boolean numericValueAsLocator)Returns the string identifier of an account owner: SYSTEM for system, EXTERNAL for external and id for users.static IntegerRangeDTOintegerRange(Integer min, Integer max)Creates a newIntegerRangeDTOstatic StringinternalNameOrId(IEntity entity)For a given entity model, returns the internal name, if available, otherwise the idstatic booleanisEmpty(DatePeriodDTO period)Returns whether the given date period is empty: null or having both begin and end as nullstatic booleanisEmpty(Range<?> range)Returns whether the given range is empty: null or having both min and maxas nullstatic <T extends Bean>
booleanisEmpty(T bean, Property<?,? super T>... properties)static booleanisValid(IInternalNamedEntity entity)static booleanisValid(UserLocatorVO user)Returns whether the given user locator is validstatic booleanisValid(DistanceDTO distance)Returns whether the given distance is validstatic booleanisValid(FileSizeDTO size)Returns whether the given size and unit are validstatic booleanisValid(LatLongDTO latLong)Returns whether the given latitude and longitude are validstatic booleanisValid(TimeIntervalDTO interval)Returns whether the given time interval is validstatic LatLongDTOlatLong(double latitude, double longitude)Creates a newLatLongDTOstatic LatLongDTOlatLong(BigDecimal latitude, BigDecimal longitude)Creates a newLatLongDTOstatic StringlocatorText(String text)Prepares the given text to be used as locator.static Property<?,? super OperatorWithFieldsVO>operatorWithFieldsProperty(BasicProfileField profileField)Returns the property fromOperatorWithFieldsVOwhich matches the given profile field, or null if not applicablestatic <E extends EntityModel>
EremoveId(E model)Set null to the id property and return the same instance.static ImageNatureresolveImageNature(ImageVO image)Returns the image nature for the givenImageVOstatic UserLocatorVOself()Returns aUserLocatorVOreferencing the logged userstatic StringstringFromVO(EntityVO vo)Returns the locator string from the givenEntityVO.static StringstringFromVO(EntityVO vo, boolean numericValueAsLocator)Returns the locator string from the givenEntityVO.static StringstringOrIdFromVO(EntityVO vo)Same asstringFromVO(EntityVO, boolean), however, if no string can be resolved, returns the idstatic StringstringOrIdFromVO(EntityVO vo, boolean numericValueAsLocator)Same asstringFromVO(EntityVO, boolean), however, if no string can be resolved, returns the idstatic TimeIntervalDTOtimeInterval(int amount, TimeField field)Creates a newTimeIntervalDTOstatic <VO extends EntityVO>
Set<VO>toVOs(Class<VO> voClass, Collection<? extends IEntity> entities)Returns a set with VOs filled up only with idsstatic StringunqualifiedInternalNameOrId(IEntity entity)For a given entity model, returns the internal name, if available, otherwise the idstatic StringunqualifyInternalName(String internalName)Returns the unqualified internal name, for example, "a.b.c" returns "c".static StringunqualifyInternalName(IInternalNamedEntity entity)Returns the unqualified internal name of the given entity.static UserLocatorVOuserLocator(String text)Returns a UserLocatorVO from the given text, allowing idsstatic UserLocatorVOuserLocator(String text, boolean allowId)Returns a UserLocatorVO from the given text, optionally allowing idsstatic Property<?,? super UserWithFieldsVO>userWithFieldsProperty(BasicProfileField profileField)Returns the property fromUserWithFieldsVOwhich matches the given profile field, or null if not applicablestatic <VO extends EntityVO>
VOvoFromString(Class<VO> voClass, String value)Converts a textual value to a VO.static <VO extends EntityVO>
List<VO>voListFromString(Class<VO> voClass, Collection<String> values)Returns each string element as a VO in a new set.static <VO extends EntityVO>
Set<VO>vosFromString(Class<VO> voClass, Collection<String> values)Returns each string element as a VO in a new set.
-
-
-
Field Detail
-
MAX_NAME_LENGTH
public static final int MAX_NAME_LENGTH
The maximum length for names- See Also:
- Constant Field Values
-
MAX_INTERNAL_NAME_LENGTH
public static final int MAX_INTERNAL_NAME_LENGTH
The maximum length for internal names- See Also:
- Constant Field Values
-
LOCATOR_QUOTE
public static final String LOCATOR_QUOTE
- See Also:
- Constant Field Values
-
-
Method Detail
-
accountOwner
public static InternalAccountOwner accountOwner(String principal)
Creates a newInternalAccountOwner. If the given principal is blank, returns null. If "SYSTEM", thenSystemAccountOwneris returned. Otherwise, returns aUserLocatorVOwith the given principal.
-
amountSummary
public static AmountSummaryVO amountSummary(BigDecimal sum, BigDecimal average, int count, CurrencyVO currency)
Creates an amount summary
-
clearIds
public static void clearIds(Iterable<? extends EntityModel> models)
Sets the ids of all models to null
-
copyProperties
public static <S extends Bean,T extends Bean> void copyProperties(S source, T target)
It copies from source to target (apply a bean over another bean) traversing all source's properties.
If a property is not found in the target bean or has a different type then it's ignored.- Throws:
IllegalArgumentException- if source, target or both are null.
-
currencyAmount
public static CurrencyAmountDTO currencyAmount(CurrencyVO currency, BigDecimal amount)
Creates a newCurrencyAmountDTO. WARNING: The givenCurrencyVOmust contain at least the precision information, otherwise this method will round the amount with precision 0.
-
currencyRange
public static CurrencyAmountRangeDTO currencyRange(CurrencyVO currency, BigDecimal min, BigDecimal max)
Creates a newCurrencyAmountRangeDTO.
-
datePeriod
public static DatePeriodDTO datePeriod(DateTime begin, DateTime end)
Creates a newDatePeriodDTO
-
decimalRange
public static DecimalRangeDTO decimalRange(BigDecimal min, BigDecimal max)
Creates a newDecimalRangeDTO
-
dimensions
public static DimensionsDTO dimensions(int width, int height)
Creates a newDimensionsDTO
-
equals
public static boolean equals(LatLongDTO first, LatLongDTO second)
Check if both coordinates are the same
-
escapeIdentifierIfNeeded
public static String escapeIdentifierIfNeeded(String value)
When the given value is a valid Long, return it escaped with a trailing '. Otherwise return the value as is.
-
getActualInternalNameOrId
public static String getActualInternalNameOrId(EntityVO vo)
Returns the internal name of the given VO if it is valid (no nested paths contains a null part). Otherwise, returns the id as string
-
getAllowedMimeTypes
public static Set<String> getAllowedMimeTypes(Collection<FileMimeType> allowedMimeTypes, Collection<String> otherMimeTypes)
Expand the list of allowed mime types into the actual mime types as string
-
getEmailValidationMesssage
public static String getEmailValidationMesssage(UserValidationResult result, MessageProducer mProducer)
-
getFirstNonEmpty
public static Object getFirstNonEmpty(Bean bean, Collection<? extends Property<?,?>> properties)
Returns the first non-empty of the given properties, according toObjectHelper.isNotEmpty(Object)
-
getFriendlyMimeTypes
public static List<String> getFriendlyMimeTypes(Collection<FileMimeType> mimes, Collection<String> otherTypes, MessageProducer messageProducer)
Returns a list of user-friendly displayed mime types
-
getFriendlyMimeTypes
public static List<String> getFriendlyMimeTypes(Collection<String> mimeTypes, MessageProducer messageProducer)
Returns a list of user-friendly displayed mime types
-
getHierarchyPath
public static <T extends Bean> List<T> getHierarchyPath(T bean, Property<T,T> parentProperty)
Returns a list with the path from the hierarchy root up to the given object, having the given parent property to resolve an object's parent. For example, if the given object is A1.1.1, the result is [A, A1, A1.1, A1.1.1]
-
getLevel
public static <T extends Bean> int getLevel(T bean, Property<T,T> parentProperty)
Returns the level of a given hierarchical bean. The given property needs to resolve the parent bean. Objects with no parent have level 1
-
getLocation
public static LatLongDTO getLocation(IAddress address)
Returns the location for a given address
-
getPublicRegistrationMesssage
public static String getPublicRegistrationMesssage(UserRegistrationResult result, MessageProducer mProducer)
-
getUserLocatorComparator
public static Comparator<UserLocatorVO> getUserLocatorComparator()
-
identifier
public static String identifier(AccountOwner owner, boolean numericValueAsLocator)
Returns the string identifier of an account owner: SYSTEM for system, EXTERNAL for external and id for users.
-
integerRange
public static IntegerRangeDTO integerRange(Integer min, Integer max)
Creates a newIntegerRangeDTO
-
internalNameOrId
public static String internalNameOrId(IEntity entity)
For a given entity model, returns the internal name, if available, otherwise the id
-
isEmpty
public static boolean isEmpty(DatePeriodDTO period)
Returns whether the given date period is empty: null or having both begin and end as null
-
isEmpty
public static boolean isEmpty(Range<?> range)
Returns whether the given range is empty: null or having both min and maxas null
-
isEmpty
@SafeVarargs public static <T extends Bean> boolean isEmpty(T bean, Property<?,? super T>... properties)
- Parameters:
properties- list of Properties to be checked for empty/null- Returns:
- true if observable is null or all of the specified properties are null or empty (see
ObjectHelper.isEmpty(Object)).
-
isValid
public static boolean isValid(DistanceDTO distance)
Returns whether the given distance is valid
-
isValid
public static boolean isValid(FileSizeDTO size)
Returns whether the given size and unit are valid
-
isValid
public static boolean isValid(IInternalNamedEntity entity)
-
isValid
public static boolean isValid(LatLongDTO latLong)
Returns whether the given latitude and longitude are valid
-
isValid
public static boolean isValid(TimeIntervalDTO interval)
Returns whether the given time interval is valid
-
isValid
public static boolean isValid(UserLocatorVO user)
Returns whether the given user locator is valid
-
latLong
public static LatLongDTO latLong(BigDecimal latitude, BigDecimal longitude)
Creates a newLatLongDTO
-
latLong
public static LatLongDTO latLong(double latitude, double longitude)
Creates a newLatLongDTO
-
locatorText
public static String locatorText(String text)
Prepares the given text to be used as locator. Basically removes leading and trailing single quotes, if any.
-
operatorWithFieldsProperty
public static Property<?,? super OperatorWithFieldsVO> operatorWithFieldsProperty(BasicProfileField profileField)
Returns the property fromOperatorWithFieldsVOwhich matches the given profile field, or null if not applicable
-
removeId
public static <E extends EntityModel> E removeId(E model)
Set null to the id property and return the same instance.
-
resolveImageNature
public static ImageNature resolveImageNature(ImageVO image)
Returns the image nature for the givenImageVO
-
self
public static UserLocatorVO self()
Returns aUserLocatorVOreferencing the logged user
-
stringFromVO
public static String stringFromVO(EntityVO vo)
Returns the locator string from the givenEntityVO. For example, forUserLocatorVO, returns the principal. Will never return the id. If the VO has no representing string, returns null.
-
stringFromVO
public static String stringFromVO(EntityVO vo, boolean numericValueAsLocator)
Returns the locator string from the givenEntityVO. For example, forUserLocatorVO, returns the principal. Will never return the id. If the VO has no representing string, returns null. ForUserLocatorVOif ´numericValueAsLocator´ is true and the principal is numeric it will be prefixed with "\'", this way it can be used as locator, otherwise it will be treated as an id and the location will fail.
-
stringOrIdFromVO
public static String stringOrIdFromVO(EntityVO vo)
Same asstringFromVO(EntityVO, boolean), however, if no string can be resolved, returns the id
-
stringOrIdFromVO
public static String stringOrIdFromVO(EntityVO vo, boolean numericValueAsLocator)
Same asstringFromVO(EntityVO, boolean), however, if no string can be resolved, returns the id
-
timeInterval
public static TimeIntervalDTO timeInterval(int amount, TimeField field)
Creates a newTimeIntervalDTO
-
toVOs
public static <VO extends EntityVO> Set<VO> toVOs(Class<VO> voClass, Collection<? extends IEntity> entities)
Returns a set with VOs filled up only with ids
-
unqualifiedInternalNameOrId
public static String unqualifiedInternalNameOrId(IEntity entity)
For a given entity model, returns the internal name, if available, otherwise the id
-
unqualifyInternalName
public static String unqualifyInternalName(IInternalNamedEntity entity)
Returns the unqualified internal name of the given entity.- See Also:
unqualifyInternalName(String)
-
unqualifyInternalName
public static String unqualifyInternalName(String internalName)
Returns the unqualified internal name, for example, "a.b.c" returns "c".
-
userLocator
public static UserLocatorVO userLocator(String text)
Returns a UserLocatorVO from the given text, allowing ids
-
userLocator
public static UserLocatorVO userLocator(String text, boolean allowId)
Returns a UserLocatorVO from the given text, optionally allowing ids
-
userWithFieldsProperty
public static Property<?,? super UserWithFieldsVO> userWithFieldsProperty(BasicProfileField profileField)
Returns the property fromUserWithFieldsVOwhich matches the given profile field, or null if not applicable
-
voFromString
public static <VO extends EntityVO> VO voFromString(Class<VO> voClass, String value)
Converts a textual value to a VO. If the value is null, returns null. Else, if the value is a number, use it as id. Otherwise, use it as either internal name, principal (if a userLocatorVO) or account number (if an AccountVO)
-
voListFromString
public static <VO extends EntityVO> List<VO> voListFromString(Class<VO> voClass, Collection<String> values)
Returns each string element as a VO in a new set.- See Also:
voFromString(Class, String)
-
vosFromString
public static <VO extends EntityVO> Set<VO> vosFromString(Class<VO> voClass, Collection<String> values)
Returns each string element as a VO in a new set.- See Also:
voFromString(Class, String)
-
-