Package io.jmix.core

Class MetadataTools

java.lang.Object
io.jmix.core.MetadataTools

@Component("core_MetadataTools") public class MetadataTools extends Object
Utility class to provide common metadata-related functionality.

Implemented as Spring bean to allow for extension in application projects.

  • Field Details

  • Constructor Details

    • MetadataTools

      public MetadataTools()
      Default constructor used by container at runtime and in server-side integration tests.
  • Method Details

    • format

      public String format(@Nullable Object value, MetaProperty property)
      Formats a value according to the property type.
      Parameters:
      value - value of the passed property to format
      property - property
      Returns:
      formatted value as string
    • format

      public String format(@Nullable Object value)
      Formats a value according to the value type.
      Parameters:
      value - object to format
      Returns:
      formatted value as string
    • getInstanceName

      public String getInstanceName(Object instance)
      Parameters:
      instance - instance
      Returns:
      Instance name as defined by InstanceName or toString().
    • getPrimaryKeyName

      @Nullable public String getPrimaryKeyName(MetaClass metaClass)
      Returns:
      name of a primary key attribute, or null if the entity has no primary key (e.g. embeddable)
    • getPrimaryKeyProperty

      @Nullable public MetaProperty getPrimaryKeyProperty(MetaClass metaClass)
      Returns:
      MetaProperty representing a primary key attribute, or null if the entity has no primary key (e.g. embeddable)
    • getPrimaryKeyProperty

      @Nullable public MetaProperty getPrimaryKeyProperty(Class<?> entityClass)
      Returns:
      MetaProperty representing a primary key attribute, or null if the entity has no primary key (e.g. embeddable)
    • hasCompositePrimaryKey

      public boolean hasCompositePrimaryKey(MetaClass metaClass)
      Returns:
      true if passed MetaClass has a composite primary key
    • hasDbGeneratedPrimaryKey

      public boolean hasDbGeneratedPrimaryKey(MetaClass metaClass)
      Returns:
      true if passed MetaClass has a db generated primary key
    • isAssignableFrom

      public boolean isAssignableFrom(MetaClass metaClass, MetaClass other)
      Returns:
      true if the first MetaClass is equal or an ancestor of the second.
    • isCascade

      @Deprecated public boolean isCascade(MetaProperty metaProperty)
      Deprecated.
      Determine whether an object denoted by the given property is merged into persistence context together with the owning object. This is true if the property is ManyToMany, or if it is OneToMany with certain CascadeType defined.
    • getCascadeTypes

      public List<javax.persistence.CascadeType> getCascadeTypes(MetaProperty metaProperty)
    • getCascadeProperties

      public List<MetaProperty> getCascadeProperties(MetaClass metaClass, @Nullable javax.persistence.CascadeType type)
      Parameters:
      metaClass -
      type - - type of operation to find properties.
      Returns:
      properties with CascadeType containg CascadeType.ALL or specified type
    • getEmbeddedProperties

      public List<String> getEmbeddedProperties(MetaClass metaClass)
      Returns:
      Names of embedded properties or empty list if no such properties contained in metaClass
    • isSoftDeletable

      public boolean isSoftDeletable(Class<?> entityClass)
      Determine whether the entity supports Soft Deletion.
      Parameters:
      entityClass - entity class
      Returns:
      true if the entity has @DeletedDate field
    • isSystem

      public boolean isSystem(MetaProperty metaProperty)
      Determine whether the given property is system. A property is considered system if it has one of the following annotations:
    • getSystemProperties

      public List<String> getSystemProperties(MetaClass metaClass)
      System Properties - is important properties used for identification, audit, soft-delete and optimistic lock purposes More formally, it is properties with annotations:
      Returns:
      names of system properties used in Entity determined by metaClass parameter
    • isJpa

      public boolean isJpa(MetaPropertyPath metaPropertyPath)
      Determine whether all the properties defined by the given property path are persistent attributes of JPA entities or embeddables.
      See Also:
    • isJpa

      public boolean isJpa(MetaProperty metaProperty)
      Determine whether the given property is a persistent attribute of a JPA entity or embeddable.
    • isEmbedded

      public boolean isEmbedded(MetaProperty metaProperty)
      Determine whether the given property denotes an embedded object.
      See Also:
      • Embedded
      • EmbeddedId
    • isLob

      public boolean isLob(MetaProperty metaProperty)
      Determine whether the given property is a LOB.
      See Also:
      • Lob
    • isOwningSide

      public boolean isOwningSide(MetaProperty metaProperty)
      Determine whether the given property is on the owning side of an association.
    • isSystemLevel

      public boolean isSystemLevel(MetaClass metaClass)
      Determine whether the given entity is marked as SystemLevel.
    • isSystemLevel

      public boolean isSystemLevel(MetaProperty metaProperty)
      Determine whether the given property is marked as SystemLevel.
    • getMetaAnnotationAttributes

      public Map<String,Object> getMetaAnnotationAttributes(Map<String,Object> metaAnnotations, Class metaAnnotationClass)
    • getMetaAnnotationValue

      public <T> T getMetaAnnotationValue(MetaProperty metaProperty, Class metaAnnotationClass)
      Returns:
      annotation value for specified metaProperty and annotation
    • isAnnotationPresent

      public boolean isAnnotationPresent(Object object, String property, Class<? extends Annotation> annotationClass)
      Determine whether the given annotation is present in the object's class or in any of its superclasses.
      Parameters:
      object - entity instance
      property - property name
      annotationClass - annotation class
    • isAnnotationPresent

      public boolean isAnnotationPresent(Class javaClass, String property, Class<? extends Annotation> annotationClass)
      Determine whether the given annotation is present in the object's class or in any of its superclasses.
      Parameters:
      javaClass - entity class
      property - property name
      annotationClass - annotation class
      Returns:
      true if the annotation is present
    • isJpaEntity

      public boolean isJpaEntity(MetaClass metaClass)
      Determine whether the given meta-class represents a JPA entity.
    • isJpaEntity

      public boolean isJpaEntity(Class<?> aClass)
      Determine whether the given class represents a JPA entity.
    • isJpaEmbeddable

      public boolean isJpaEmbeddable(MetaClass metaClass)
      Determine whether the given meta-class is JPA embeddable.
    • isJpaEmbeddable

      public boolean isJpaEmbeddable(Class<?> aClass)
      Determine whether the given entity class is JPA embeddable.
    • isCacheable

      public boolean isCacheable(MetaClass metaClass)
    • getPropertyEnclosingMetaClass

      public MetaClass getPropertyEnclosingMetaClass(MetaPropertyPath propertyPath)
      Get metaclass that contains metaproperty for passed propertyPath. Resolves real metaclass for property in consideration of inherited entity classes and extended classes.
      Parameters:
      propertyPath - Property path
      Returns:
      metaclass
    • getInstanceNameRelatedProperties

      @Nonnull public Collection<MetaProperty> getInstanceNameRelatedProperties(MetaClass metaClass)
      Return a collection of properties included into entity's name pattern (see InstanceName).
      Parameters:
      metaClass - entity metaclass
      Returns:
      collection of the name pattern properties
    • getInstanceNameRelatedProperties

      @Nonnull public Collection<MetaProperty> getInstanceNameRelatedProperties(MetaClass metaClass, boolean useOriginal)
      Return a collection of properties included into entity's name pattern (see InstanceName).
      Parameters:
      metaClass - entity metaclass
      useOriginal - if true, and if the given metaclass doesn't define a InstanceName and if it is an extended entity, this method tries to find a name pattern in an original entity
      Returns:
      collection of the name pattern properties
    • getPropertyPaths

      public Collection<MetaPropertyPath> getPropertyPaths(MetaClass metaClass)
      Returns:
      collection of properties owned by this metaclass and all its ancestors in the form of MetaPropertyPaths containing one property each
    • toPropertyPaths

      public Collection<MetaPropertyPath> toPropertyPaths(Collection<MetaProperty> properties)
      Converts a collection of properties to collection of MetaPropertyPaths containing one property each
    • getFetchPlanPropertyPaths

      public Collection<MetaPropertyPath> getFetchPlanPropertyPaths(FetchPlan fetchPlan, MetaClass metaClass)
      Collects all meta-properties of the given meta-class included to the given fetchPlan as MetaPropertyPaths.
      Parameters:
      fetchPlan - fetch plan
      metaClass - meta-class
      Returns:
      collection of paths
    • fetchPlanContainsProperty

      public boolean fetchPlanContainsProperty(@Nullable FetchPlan fetchPlan, MetaPropertyPath propertyPath)
      Determine whether the fetch plan contains a property, traversing a fetch plan branch according to the given property path.
      Parameters:
      fetchPlan - fetch plan instance. If null, return false immediately.
      propertyPath - property path defining the property
    • getAllJpaEntityMetaClasses

      public Collection<MetaClass> getAllJpaEntityMetaClasses()
      Returns:
      collection of all meta-classes representing JPA entities
    • getAllJpaEmbeddableMetaClasses

      public Collection<MetaClass> getAllJpaEmbeddableMetaClasses()
      Returns:
      collection of all meta-classes representing JPA embeddable classes
    • getAllEnums

      public Collection<Class<?>> getAllEnums()
      Returns:
      collection of all Java enums used as a type of an entity attribute
    • getDatabaseTable

      @Nullable public String getDatabaseTable(MetaClass metaClass)
      Returns:
      table name for the given entity, or null if the entity is Embeddable, MappedSuperclass or non-persistent
    • getDatabaseColumn

      @Nullable public String getDatabaseColumn(MetaProperty metaProperty)
    • getDependsOnProperties

      public List<String> getDependsOnProperties(Class<?> entityClass, String property)
      Returns:
      list of properties defined in DependsOnProperties or empty list
    • getDependsOnProperties

      public List<String> getDependsOnProperties(MetaProperty metaProperty)
      Returns:
      list of properties defined in DependsOnProperties or empty list
    • getDeletedDateProperty

      public String getDeletedDateProperty(Object entity) throws IllegalArgumentException
      Returns:
      field annotated with @DeletedDate
      Throws:
      IllegalArgumentException - if entity has no @DeletedDate field
    • findDeletedDateProperty

      @Nullable public String findDeletedDateProperty(Class<?> clazz)
      Returns:
      field annotated with @DeletedDate or null if annotation is not present
    • findDeletedByProperty

      @Nullable public String findDeletedByProperty(Class<?> clazz)
      Returns:
      field annotated with @DeletedBy or null if annotation is not present
    • findLastModifiedDateProperty

      @Nullable public String findLastModifiedDateProperty(Class<?> clazz)
      Returns:
      field annotated with @LastModifiedDate or null if annotation is not present
    • findPropertyByAnnotation

      @Nullable protected String findPropertyByAnnotation(Class<?> clazz, String annotationName)
    • getSoftDeleteProperties

      public List<String> getSoftDeleteProperties(Class<?> clazz)
      Returns:
      list contains @DeletedDate, @DeletedBy property names if present.
    • hasUuid

      public boolean hasUuid(MetaClass metaClass)
      Checks whether an entity has uuid key (primary or not)
      Returns:
      true if entity's EntityEntry implements EntityEntryHasUuid
    • getUuidPropertyName

      @Nullable public String getUuidPropertyName(Class<?> clazz)
    • getCrossDataStoreReferenceIdProperty

      @Nullable public String getCrossDataStoreReferenceIdProperty(String thisStore, MetaProperty metaProperty)
      If the given property is a reference to an entity from different data store, returns the name of a persistent property which stores the identifier of the related entity.
      Parameters:
      thisStore - name of a base data store
      metaProperty - property
      Returns:
      name of the ID property or null if the given property is not a cross-datastore reference or it does not satisfy the convention of declaring related properties for such references
    • resolveMetaPropertyPathOrNull

      @Nullable public MetaPropertyPath resolveMetaPropertyPathOrNull(MetaClass metaClass, String propertyPath)
      Returns a MetaPropertyPath which can include the special MetaProperty for a dynamic attribute.
      Parameters:
      metaClass - originating meta-class
      propertyPath - path to the attribute
      Returns:
      MetaPropertyPath instance
    • resolveMetaPropertyPath

      public MetaPropertyPath resolveMetaPropertyPath(MetaClass metaClass, String propertyPath)
      Returns a MetaPropertyPath which can include the special MetaProperty for a dynamic attribute. Throws an IllegalArgumentException if MetaPropertyPath can't be resolved.
      Parameters:
      metaClass - originating meta-class
      propertyPath - path to the attribute
      Returns:
      MetaPropertyPath instance
    • traverseAttributes

      public void traverseAttributes(Object entity, EntityAttributeVisitor visitor)
      Depth-first traversal of the object graph starting from the specified entity instance. Visits all attributes.
      Parameters:
      entity - entity graph entry point
      visitor - the attribute visitor implementation
    • traverseAttributesByFetchPlan

      public void traverseAttributesByFetchPlan(FetchPlan fetchPlan, Object entity, EntityAttributeVisitor visitor)
      Depth-first traversal of the object graph by the fetch plan starting from the specified entity instance. Visits attributes defined in the fetch plan.
      Parameters:
      fetchPlan - fetchPlan instance
      entity - entity graph entry point
      visitor - the attribute visitor implementation
    • traverseAttributesByFetchPlan

      public void traverseAttributesByFetchPlan(FetchPlan fetchPlan, Object entity, boolean checkLoaded, EntityAttributeVisitor visitor)
      Depth-first traversal of the object graph by the fetch plan starting from the specified entity instance. Visits attributes defined in the fetch plan.
      Parameters:
      fetchPlan - fetchPlan instance
      entity - entity graph entry point
      checkLoaded - if true, skips not loaded attributes
      visitor - the attribute visitor implementation
    • copy

      public <T> T copy(T source)
      Create a new instance and make it a shallow copy of the instance given.

      This method copies attributes according to the metadata.


      WARNING:

      1. New instance will not be initialized. Entity id generation, embedded objects creation, PostConstruct actions, etc. will not be performed for cloned object (see EntityInitializer).
      2. Unfetched or lazy-fetched attributes will not be cloned.
      Parameters:
      source - source instance
      Returns:
      new instance of the same Java class as source
    • copy

      public void copy(Object source, Object dest)
      Make a shallow copy of an instance.

      This method copies attributes according to the metadata.

      The source and destination instances don't have to be of the same Java class or metaclass. Copying is performed in the following scenario: get each source property and copy the value to the destination if it contains a property with the same name and it is not read-only.

      Parameters:
      source - source instance
      dest - destination instance
    • deepCopy

      public <T> T deepCopy(T source)
      Makes a deep copy of the source entity. All referenced entities and collections will be copied as well.
    • deepCopy

      public void deepCopy(Object source, Object destination, MetadataTools.EntitiesHolder entitiesHolder)
      Copies all property values from source to destination excluding null values.
    • internalTraverseAttributes

      protected void internalTraverseAttributes(Entity entity, EntityAttributeVisitor visitor, HashSet<Object> visited)
    • internalTraverseAttributesByFetchPlan

      protected void internalTraverseAttributesByFetchPlan(FetchPlan fetchPlan, Object entity, EntityAttributeVisitor visitor, Map<Object,Set<FetchPlan>> visited, boolean checkLoaded)
    • createInstance

      protected static <T> T createInstance(Class<T> aClass)
    • createInstanceWithId

      protected static Object createInstanceWithId(Class<?> entityClass, Object id)
    • getAdditionalProperties

      public Set<MetaProperty> getAdditionalProperties(MetaClass metaClass)
      Returns all additional properties which are providing classes implement MetadataExtension interface
      Parameters:
      metaClass - instance MetaClass for getting additional properties
      Returns:
      all additional properties
    • isAdditionalProperty

      public boolean isAdditionalProperty(MetaClass metaClass, String propertyName)
      Parameters:
      metaClass -
      propertyName -