Class EntityModelUtils

java.lang.Object
ch.tocco.nice2.persist.core.api.util.EntityModelUtils

public class EntityModelUtils extends Object
Static utility functions for dealing with entities and relations.
  • Method Details

    • getVersion

      public static long getVersion(Entity entity)
      Returns the 'version' of an entity, or 0 if the entity type does not use an auto-version field.
    • getLastModTime

      @Nullable public static @Nullable org.joda.time.DateTime getLastModTime(Entity entity)
      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

      @Nullable public static @Nullable String getLastModUser(Entity entity)
      Returns the time and user of the last modification.
    • appendSorting

      public static boolean appendSorting(Query query, @Nullable @Nullable List<? extends Sorting> list)
      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

      public static boolean appendDefaultSorting(Query query, EntityModel eModel)
      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

      public 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 priority
      Parameters:
      query - the query, the same object is returned
      orderBy - a list of paths to sort the query by, prefix '-' or suffix ' desc' may be used to sort Ordering.Direction.DESCENDING
      Returns:
      the same query object
    • prependSorting

      public 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.
      Parameters:
      query - the query, the same object is returned
      orderBy - a list of paths to sort the query by, prefix '-' or suffix ' desc' may be used to sort Ordering.Direction.DESCENDING
      Returns:
      the same query object
    • appendDefaultSorting

      public static boolean appendDefaultSorting(Query query, EntityModel eModel, String path)
      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

      public static boolean prependDefaultSorting(Query query, EntityModel model, String path)
    • prependDefaultSorting

      public static boolean prependDefaultSorting(Query query, EntityModel model, String path, boolean flip)
    • sortByLastUpdated

      public static boolean sortByLastUpdated(Query query, EntityModel eModel, boolean prepend)
      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

      public static boolean sortBySorting(Query query, EntityModel eModel, boolean prepend)
      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

      public static void sortByKeys(Query query, EntityModel eModel, boolean prepend)
      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

      public static void setNonNullableToEmpty(Entity entity)
      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

      public static void setNonNullableToEmpty(Entity entity, Predicate<FieldModel> fieldPredicate)
    • 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 model
      pathString - a path
      Returns:
      the target EntityModel the path points to or null if the path cannot be resolved to an EntityModel
    • dryResolveRelationModel

      @Nullable public static @Nullable EntityModel dryResolveRelationModel(EntityModel model, Path path)
      Resolve a path on the entity model only (no actual entities involved)
      Parameters:
      model - the model
      path - 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 model
      pathString - 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 model
      path - 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 model
      path - 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 entity
      pathString - 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 entity
      pathString - 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 entity
      pathString - 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 entity
      pathString - 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 entity
      pathString - 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 entity
      pathString - 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 entity
      pathString - a string containing an arbitrary path to follow from the starting entity
      orderBy - 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 entity
      path - 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 entity
      path - the path object to follow
      doResolveToManyRelations - 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 entity
      path - the path object to follow
      doResolveToManyRelations - 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

      public static URI getEntityUri(Entity entity)
    • getUnlocalizedValueMap

      public static Map<String,Object> getUnlocalizedValueMap(Entity entity)
      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

      public static Map<String,Object> getUnlocalizedValueMap(Entity entity, Predicate<String> pathIncludePredicate)
    • 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 entity
      fieldName - a basename of a localized field
      locale - a locale
      value - a value to be set on the field
    • getLocalizedValue

      public static Object getLocalizedValue(Entity entity, String fieldName, Locale locale)
      Get the value of a localized field for a specific locale
      Parameters:
      entity - an entity
      fieldName - a basename of a localized field
      locale - 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 entity
      fieldName - a basename of a localized field
      value - 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 the Entity 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 entity
      second - second entity
      Returns:
      true if the entities are equal.
    • unwrap

      public static Entity unwrap(Entity entity)
      unwrap a potentially wrapped entity
      Parameters:
      entity - the entity
      Returns:
      an entity object not wrapped in a AbstractEntityWrapper
    • getLocalizedFieldNames

      public static List<String> getLocalizedFieldNames(String baseFieldName, EntityModel entityModel)