Class EntityModelUtils
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
static class
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
appendDefaultSorting
(Query query, EntityModel eModel) Appends the default sorting rules of the entity model, if any.static boolean
appendDefaultSorting
(Query query, EntityModel eModel, String path) Appends the default sorting rules of the entity model, if any.static boolean
appendDefaultSorting
(Query query, EntityModel eModel, String path, boolean flip) Appends the default sorting rules of the entity model, if any.static Query
appendSorting
(Query query, String... orderBy) Append to a potential existing sorting of a query, previously added sorting information will have the highest sorting prioritystatic boolean
appendSorting
(Query query, @Nullable List<? extends Sorting> list) Appends the given sorting rules on the query, if any.static boolean
appendSorting
(Query query, @Nullable List<? extends Sorting> list, String path, boolean flip) Appends the given sorting rules on the query, if any.static @Nullable EntityModel
dryResolveRelationModel
(EntityModel model, Path path) Resolve a path on the entity model only (no actual entities involved)static @Nullable EntityModel
dryResolveRelationModel
(EntityModel model, String pathString) Resolve a path on the entity model only (no actual entities involved)dryResolveRelationPath
(EntityModel model, Path path) Resolve a path on the entity model only (no actual entities involved), check create permissions along the waydryResolveRelationPath
(EntityModel model, String pathString) Resolve a path on the entity model only (no actual entities involved), check create permissions along the waydryResolveRelationPaths
(EntityModel model, Path path) Resolve a path on the entity model only (no actual entities involved), check create permissions along the waystatic boolean
entitiesEqual
(@Nullable Entity first, @Nullable Entity second) Evaluate if two entities are equal.static URI
getEntityUri
(Entity entity) static @Nullable org.joda.time.DateTime
getLastModTime
(Entity entity) static @Nullable String
getLastModUser
(Entity entity) Returns the time and user of the last modification.getLocalizedFieldNames
(String baseFieldName, EntityModel entityModel) static Object
getLocalizedValue
(Entity entity, String fieldName, Locale locale) Get the value of a localized field for a specific localegetUnlocalizedValueMap
(Entity entity) Get an unlocalized value map, i.e.getUnlocalizedValueMap
(Entity entity, Predicate<String> pathIncludePredicate) static long
getVersion
(Entity entity) Returns the 'version' of an entity, or 0 if the entity type does not use an auto-version field.static boolean
prependDefaultSorting
(Query query, EntityModel model, String path) static boolean
prependDefaultSorting
(Query query, EntityModel model, String path, boolean flip) static Query
prependSorting
(Query query, String... orderBy) Prepend to a potential existing sorting of a query, the first orderBy path in the passed list will have the highest sorting priority.static boolean
prependSorting
(Query query, @Nullable List<? extends Sorting> list, String path, boolean flip) Prepends the given sorting rules on the query, if any.static @Nullable Entity
resolvePathToEntity
(Entity entity, String pathString) Resolve a path of relations to an individual entity.static EntityList
resolvePathToEntityList
(Entity entity, String pathString) Resolve a path of relations to an entity list.static EntityList
resolvePathToEntityList
(Entity entity, String pathString, @Nullable String orderBy) Resolve a path of relations to an individual entity.static @Nullable String
resolvePathToStringValue
(Entity entity, String pathString, ModelTypeAdapter.LocaleAndTimeZone localeAndTimeZone) Resolve a path of relations to a single field value or the defaultdisplay if last part is a relation.static @Nullable TypedValue
<?> resolvePathToValue
(Entity entity, String pathString) Resolve a path of relations to a single field value.static <T> T
resolvePathToValue
(Entity entity, String pathString, Class<T> expectedType) Resolve a path of relations to a single field value.static Entity
resolvePathWithoutLastStep
(Entity entity, Path path) Resolve a path of relations (without the last step) to an entity.static Entity
resolvePathWithoutLastStep
(Entity entity, Path path, boolean doResolveToManyRelations) Resolve a path of relations (without the last step) to an entity.static Entity
resolvePathWithoutLastStep
(Entity entity, Path path, boolean doResolveToManyRelations, Consumer<EntityModelUtils.RelationStep> stepConsumer) Resolve a path of relations (without the last step) to an entity.static EntityList
resolvePathWithSelectorsToEntityList
(Entity entity, String pathString) Resolve a path of relations to an entity list.static void
setLocalizedValue
(Entity entity, String fieldName, Locale locale, @Nullable Object value) Set value to a localized field for the given localestatic void
setLocalizedValueForAll
(Entity entity, String fieldName, @Nullable Object value) Set value to all localized fields of entitystatic void
setNonNullableToEmpty
(Entity entity) Sets all non-nullable fields to the type's default value.static void
setNonNullableToEmpty
(Entity entity, Predicate<FieldModel> fieldPredicate) static void
sortByKeys
(Query query, EntityModel eModel, boolean prepend) Adds the sortings rule to order by the primary key columns.static boolean
sortByLastUpdated
(Query query, EntityModel eModel, boolean prepend) Adds the sorting rule to order so that the last touched object comes first.static boolean
sortBySorting
(Query query, EntityModel eModel, boolean prepend) Adds the sorting rule to order by the sorting column if the entity has it.static Entity
unwrap a potentially wrapped entity
-
Method Details
-
getVersion
Returns the 'version' of an entity, or 0 if the entity type does not use an auto-version field. -
getLastModTime
- Returns:
- Null if the entity does not support the feature OR no ts has been set yet in the current state (not sure if that can happen).
-
getLastModUser
Returns the time and user of the last modification. -
appendSorting
Appends the given sorting rules on the query, if any.This is useful for when you get sorting rules in by parameters, not when constructing the sorting in code.
- Parameters:
query
- A query, it may have sorting on it already or not.list
- The list, as received from EntityModel.getVisualisation().getDefaultSorting()- Returns:
true
if sorting was applied,false
if not.- See Also:
-
appendSorting
public static boolean appendSorting(Query query, @Nullable @Nullable List<? extends Sorting> list, String path, boolean flip) Appends the given sorting rules on the query, if any.This is useful for when you get sorting rules in by parameters, not when constructing the sorting in code.
- Parameters:
query
- A query, it may have sorting on it already or not.list
- The list, as received from EntityModel.getVisualisation().getDefaultSorting()path
- a path to prepend each order item- Returns:
true
if sorting was applied,false
if not.- See Also:
-
prependSorting
public static boolean prependSorting(Query query, @Nullable @Nullable List<? extends Sorting> list, String path, boolean flip) Prepends the given sorting rules on the query, if any.This is useful for when you get sorting rules in by parameters, not when constructing the sorting in code.
- Parameters:
query
- A query, it may have sorting on it already or not.list
- The list, as received from EntityModel.getVisualisation().getDefaultSorting()path
- a path to prepend each order item- Returns:
true
if sorting was applied,false
if not.- See Also:
-
appendDefaultSorting
Appends the default sorting rules of the entity model, if any.If there are no default sorting rules defined in the entity (xml config) then the method checks if the entity contains a field of type 'sorting', and uses it.
- Parameters:
query
- A query, it may have sorting on it already or not.eModel
- The model of the entity to order by, must match the query.- Returns:
true
if sorting was applied,false
if not.- See Also:
-
appendSorting
Append to a potential existing sorting of a query, previously added sorting information will have the highest sorting priority- Parameters:
query
- the query, the same object is returnedorderBy
- a list of paths to sort the query by, prefix '-' or suffix ' desc' may be used to sortOrdering.Direction.DESCENDING
- Returns:
- the same query object
-
prependSorting
Prepend to a potential existing sorting of a query, the first orderBy path in the passed list will have the highest sorting priority.- Parameters:
query
- the query, the same object is returnedorderBy
- a list of paths to sort the query by, prefix '-' or suffix ' desc' may be used to sortOrdering.Direction.DESCENDING
- Returns:
- the same query object
-
appendDefaultSorting
Appends the default sorting rules of the entity model, if any.If there are no default sorting rules defined in the entity (xml config) then the method checks if the entity contains a field of type 'sorting', and uses it.
- Parameters:
query
- A query, it may have sorting on it already or not.eModel
- The model of the entity to order by, must match the query.path
- a path to prepend each order item- Returns:
true
if sorting was applied,false
if not.- See Also:
-
appendDefaultSorting
public static boolean appendDefaultSorting(Query query, EntityModel eModel, String path, boolean flip) Appends the default sorting rules of the entity model, if any.If there are no default sorting rules defined in the entity (xml config) then the method checks if the entity contains a field of type 'sorting', and uses it.
- Parameters:
query
- A query, it may have sorting on it already or not.eModel
- The model of the entity to order by, must match the query.path
- a path to prepend each order item- Returns:
true
if sorting was applied,false
if not.- See Also:
-
prependDefaultSorting
-
prependDefaultSorting
public static boolean prependDefaultSorting(Query query, EntityModel model, String path, boolean flip) -
sortByLastUpdated
Adds the sorting rule to order so that the last touched object comes first.- Parameters:
query
- A query, it may have sorting on it already or not.eModel
- The model of the entity to order by, must match the query.prepend
- true=prepend, false=append the rule- Returns:
true
if sorting was applied (the entity type used the auto-fields),false
if not.
-
sortBySorting
Adds the sorting rule to order by the sorting column if the entity has it.- Parameters:
query
- A query, it may have sorting on it already or not.eModel
- The model of the entity to order by, must match the query.prepend
- true=prepend, false=append the rule- Returns:
true
if sorting was applied (the entity has the sorting field),false
if not.
-
sortByKeys
Adds the sortings rule to order by the primary key columns.- Parameters:
query
- A query, it may have sorting on it already or not.eModel
- The model of the entity to order by, must match the query.prepend
- true=prepend, false=append the rule
-
setNonNullableToEmpty
Sets all non-nullable fields to the type's default value.WARNING: - atm this also sets db-generated values to the type's default value. - atm this also sets key fields to the type's default value, for example 0 for a long. In most cases this is not desired, but doesn't affect much. It probably fires events that are not desired. Also, the validation must be ignored (which would be the case if the values were left as null). I expect this to change. At least I suggest to ignore key fields. Asking if a value is db-generated requires to ask the backend, see EntityFacade.
-
setNonNullableToEmpty
-
dryResolveRelationModel
@Nullable public static @Nullable EntityModel dryResolveRelationModel(EntityModel model, String pathString) Resolve a path on the entity model only (no actual entities involved)- Parameters:
model
- the modelpathString
- a path- Returns:
- the target EntityModel the path points to or null if the path cannot be resolved to an EntityModel
-
dryResolveRelationModel
Resolve a path on the entity model only (no actual entities involved)- Parameters:
model
- the modelpath
- a path- Returns:
- the target EntityModel the path points to or null if the path cannot be resolved to an EntityModel
-
dryResolveRelationPath
public static EntityModelUtils.DryResolveResult dryResolveRelationPath(EntityModel model, String pathString) Resolve a path on the entity model only (no actual entities involved), check create permissions along the way- Parameters:
model
- starting entity modelpathString
- a path to resolve on the entity model- Returns:
- DryResolveResult containing the final entity model and field
-
dryResolveRelationPath
public static EntityModelUtils.DryResolveResult dryResolveRelationPath(EntityModel model, Path path) Resolve a path on the entity model only (no actual entities involved), check create permissions along the way- Parameters:
model
- starting entity modelpath
- a path to resolve on the entity model- Returns:
- DryResolveResult containing the final entity model and field
-
dryResolveRelationPaths
public static List<EntityModelUtils.DryResolveResult> dryResolveRelationPaths(EntityModel model, Path path) Resolve a path on the entity model only (no actual entities involved), check create permissions along the way- Parameters:
model
- starting entity modelpath
- a path to resolve on the entity model- Returns:
- a list containing DryResolveResult for each step of the path that was resolved
-
resolvePathToValue
@Nullable public static @Nullable TypedValue<?> resolvePathToValue(Entity entity, String pathString) throws NoEntityToLoadException Resolve a path of relations to a single field value. All relations on the path may only be n:1 relations.- Parameters:
entity
- the starting entitypathString
- a string containing an arbitrary path to follow from the starting entity. Must end in a fieldname.- Returns:
- the field value corresponding to the specified path
- Throws:
NoEntityToLoadException
-
resolvePathToValue
@Nullable public static <T> T resolvePathToValue(Entity entity, String pathString, Class<T> expectedType) throws NoEntityToLoadException Resolve a path of relations to a single field value. All relations on the path may only be n:1 relations.- Parameters:
entity
- the starting entitypathString
- a string containing an arbitrary path to follow from the starting entity. Must end in a fieldname.- Returns:
- the field value corresponding to the specified path
- Throws:
NoEntityToLoadException
-
resolvePathToStringValue
@Nullable public static @Nullable String resolvePathToStringValue(Entity entity, String pathString, @Nullable ModelTypeAdapter.LocaleAndTimeZone localeAndTimeZone) throws NoEntityToLoadException Resolve a path of relations to a single field value or the defaultdisplay if last part is a relation. All relations on the path may only be n:1 relations. *- Parameters:
entity
- the starting entitypathString
- a string containing an arbitrary path to follow from the starting entity.- Returns:
- the field value corresponding to the specified path
- Throws:
NoEntityToLoadException
-
resolvePathWithSelectorsToEntityList
public static EntityList resolvePathWithSelectorsToEntityList(Entity entity, String pathString) throws PersistException, NoEntityToLoadException Resolve a path of relations to an entity list. All relations on the path may only be n:1 relations except to-many relations with selectors are alowed and the last relation which can be either 1:n or n:1- Parameters:
entity
- the starting entitypathString
- a string containing an arbitrary path to follow from the starting entity- Returns:
- the entity list corresponding to the specified relation path
- Throws:
PersistException
NoEntityToLoadException
-
resolvePathToEntityList
public static EntityList resolvePathToEntityList(Entity entity, String pathString) throws PersistException, NoEntityToLoadException Resolve a path of relations to an entity list. All relations on the path may only be n:1 relations except for the last relation which can be either 1:n or n:1- Parameters:
entity
- the starting entitypathString
- a string containing an arbitrary path to follow from the starting entity- Returns:
- the entity list corresponding to the specified relation path
- Throws:
PersistException
NoEntityToLoadException
-
resolvePathToEntity
@Nullable public static @Nullable Entity resolvePathToEntity(Entity entity, String pathString) throws PersistException, NoEntityToLoadException Resolve a path of relations to an individual entity. All relations on the path may only be n:1 relations except for the last relation which can be either 1:n or n:1. This method will throw an exception if the last relation resolves to more than one entity.- Parameters:
entity
- the starting entitypathString
- a string containing an arbitrary path to follow from the starting entity- Returns:
- the entity list corresponding to the specified relation path
- Throws:
PersistException
NoEntityToLoadException
-
resolvePathToEntityList
public static EntityList resolvePathToEntityList(Entity entity, String pathString, @Nullable @Nullable String orderBy) throws PersistException, NoEntityToLoadException Resolve a path of relations to an individual entity. All relations on the path may only be n:1 relations except for the last relation which can be either 1:n or n:1. This method will throw an exception if the last relation resolves to more than one entity. If the path is just "." then the entity will be returned.- Parameters:
entity
- the starting entitypathString
- a string containing an arbitrary path to follow from the starting entityorderBy
- a string that indicates how the result should be sorted- Returns:
- the entity list corresponding to the specified relation path
- Throws:
PersistException
NoEntityToLoadException
-
resolvePathWithoutLastStep
public static Entity resolvePathWithoutLastStep(Entity entity, Path path) throws NoEntityToLoadException Resolve a path of relations (without the last step) to an entity. All relations on the path may only be n:1 relations.- Parameters:
entity
- the starting entitypath
- the path object to follow- Throws:
NoEntityToLoadException
-
resolvePathWithoutLastStep
public static Entity resolvePathWithoutLastStep(Entity entity, Path path, boolean doResolveToManyRelations) throws NoEntityToLoadException Resolve a path of relations (without the last step) to an entity.- Parameters:
entity
- the starting entitypath
- the path object to followdoResolveToManyRelations
- Set this to true to resolve to-many relations as well. Nevertheless, the to-many relations must contain exactly 1 related entity.- Returns:
- the entity corresponding to the path without the last step
- Throws:
NoEntityToLoadException
-
resolvePathWithoutLastStep
public static Entity resolvePathWithoutLastStep(Entity entity, Path path, boolean doResolveToManyRelations, Consumer<EntityModelUtils.RelationStep> stepConsumer) throws NoEntityToLoadException Resolve a path of relations (without the last step) to an entity.- Parameters:
entity
- the starting entitypath
- the path object to followdoResolveToManyRelations
- Set this to true to resolve to-many relations as well. Nevertheless, the to-many relations must contain exactly 1 related entity.stepConsumer
- A consumer which is called for each step in the path. The consumer is called before the relation is resolved.- Returns:
- the entity corresponding to the path without the last step
- Throws:
NoEntityToLoadException
-
getEntityUri
-
getUnlocalizedValueMap
Get an unlocalized value map, i.e. it consists only values of the current locale and the field names do not contain locale suffix -
getUnlocalizedValueMap
-
setLocalizedValue
public static void setLocalizedValue(Entity entity, String fieldName, Locale locale, @Nullable @Nullable Object value) Set value to a localized field for the given locale- Parameters:
entity
- an entityfieldName
- a basename of a localized fieldlocale
- a localevalue
- a value to be set on the field
-
getLocalizedValue
Get the value of a localized field for a specific locale- Parameters:
entity
- an entityfieldName
- a basename of a localized fieldlocale
- a locale- Returns:
- the localized value of the field
-
setLocalizedValueForAll
public static void setLocalizedValueForAll(Entity entity, String fieldName, @Nullable @Nullable Object value) Set value to all localized fields of entity- Parameters:
entity
- an entityfieldName
- a basename of a localized fieldvalue
- a value to be set on all localized fields
-
entitiesEqual
public static boolean entitiesEqual(@Nullable @Nullable Entity first, @Nullable @Nullable Entity second) Evaluate if two entities are equal. Entities are considered equal if they represent the same database record. This is the case when the models and keys of both entities are the same. Conception entities are considered equal, if they have the same underlying entity object.
Note: there is no explicit equals method in any of theEntity
implementations to not interfere with existing persist-core code. This method is a helper method to determine equality of two entities in client code.- Parameters:
first
- first entitysecond
- second entity- Returns:
- true if the entities are equal.
-
unwrap
unwrap a potentially wrapped entity- Parameters:
entity
- the entity- Returns:
- an entity object not wrapped in a
AbstractEntityWrapper
-
getLocalizedFieldNames
-