Package io.jmix.core
Class MetadataTools
java.lang.Object
io.jmix.core.MetadataTools
Utility class to provide common metadata-related functionality.
Implemented as Spring bean to allow for extension in application projects.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
INTERNALstatic interface
INTERNAL -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final String
protected CurrentAuthentication
protected DatatypeRegistry
static final String
static final String
static final String
static final String
protected Collection<Class<?>>
protected ExtendedEntities
protected InstanceNameProvider
static final String
static final String
protected Messages
protected Metadata
protected List<MetadataExtension>
protected Collection<MetaPropertyPathResolver>
protected PersistentAttributesLoadChecker
static final String
static final String
static final String
static final String
static final String
Not applicable for legacy cuba entities -
Constructor Summary
ConstructorDescriptionDefault constructor used by container at runtime and in server-side integration tests. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Make a shallow copy of an instance.<T> T
copy
(T source) Create a new instance and make it a shallow copy of the instance given.protected static <T> T
createInstance
(Class<T> aClass) protected static Object
createInstanceWithId
(Class<?> entityClass, Object id) void
deepCopy
(Object source, Object destination, MetadataTools.EntitiesHolder entitiesHolder) Copies all property values from source to destination excluding null values.<T> T
deepCopy
(T source) Makes a deep copy of the source entity.boolean
fetchPlanContainsProperty
(FetchPlan fetchPlan, MetaPropertyPath propertyPath) Determine whether the fetch plan contains a property, traversing a fetch plan branch according to the given property path.findDeletedByProperty
(Class<?> clazz) findDeletedDateProperty
(Class<?> clazz) findLastModifiedDateProperty
(Class<?> clazz) protected String
findPropertyByAnnotation
(Class<?> clazz, String annotationName) Formats a value according to the value type.format
(Object value, MetaProperty property) Formats a value according to the property type.getAdditionalProperties
(MetaClass metaClass) Returns all additional properties which are providing classes implement MetadataExtension interfaceCollection<Class<?>>
getCascadeProperties
(MetaClass metaClass, jakarta.persistence.CascadeType type) List<jakarta.persistence.CascadeType>
getCascadeTypes
(MetaProperty metaProperty) 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.getDatabaseColumn
(MetaProperty metaProperty) getDatabaseTable
(MetaClass metaClass) getDeletedDateProperty
(Object entity) getDependsOnProperties
(MetaProperty metaProperty) getDependsOnProperties
(Class<?> entityClass, String property) getEmbeddedProperties
(MetaClass metaClass) getFetchPlanPropertyPaths
(FetchPlan fetchPlan, MetaClass metaClass) Collects all meta-properties of the given meta-class included to the given fetchPlan asMetaPropertyPath
s.getInstanceName
(Object instance) getInstanceNameRelatedProperties
(MetaClass metaClass) Return a collection of properties included into entity's name pattern (seeInstanceName
).getInstanceNameRelatedProperties
(MetaClass metaClass, boolean useOriginal) Return a collection of properties included into entity's name pattern (seeInstanceName
).getMetaAnnotationAttributes
(Map<String, Object> metaAnnotations, Class metaAnnotationClass) <T> T
getMetaAnnotationValue
(MetaProperty metaProperty, Class metaAnnotationClass) getPrimaryKeyName
(MetaClass metaClass) getPrimaryKeyProperty
(MetaClass metaClass) getPrimaryKeyProperty
(Class<?> entityClass) getPropertyEnclosingMetaClass
(MetaPropertyPath propertyPath) Get metaclass that contains metaproperty for passed propertyPath.getPropertyPaths
(MetaClass metaClass) getSoftDeleteProperties
(Class<?> clazz) 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:Id
,JmixId
,EmbeddedId
JmixGeneratedValue
CreatedDate
CreatedBy
LastModifiedDate
LastModifiedBy
DeletedDate
DeletedBy
Version
getUuidPropertyName
(Class<?> clazz) boolean
hasCompositePrimaryKey
(MetaClass metaClass) boolean
hasDbGeneratedPrimaryKey
(MetaClass metaClass) boolean
Checks whether an entity has uuid key (primary or not)protected void
internalTraverseAttributes
(Entity entity, EntityAttributeVisitor visitor, HashSet<Object> visited) protected void
internalTraverseAttributesByFetchPlan
(FetchPlan fetchPlan, Object entity, EntityAttributeVisitor visitor, Map<Object, Set<FetchPlan>> visited, boolean checkLoaded) boolean
isAdditionalProperty
(MetaClass metaClass, String propertyName) 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.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.boolean
isAssignableFrom
(MetaClass metaClass, MetaClass other) boolean
isCacheable
(MetaClass metaClass) boolean
isCascade
(MetaProperty metaProperty) Deprecated.boolean
isEmbedded
(MetaProperty metaProperty) Determine whether the given property denotes an embedded object.boolean
isJpa
(MetaProperty metaProperty) Determine whether the given property is a persistent attribute of a JPA entity or embeddable.boolean
isJpa
(MetaPropertyPath metaPropertyPath) Determine whether all the properties defined by the given property path are persistent attributes of JPA entities or embeddables.boolean
isJpaEmbeddable
(MetaClass metaClass) Determine whether the given meta-class is JPA embeddable.boolean
isJpaEmbeddable
(Class<?> aClass) Determine whether the given entity class is JPA embeddable.boolean
isJpaEntity
(MetaClass metaClass) Determine whether the given meta-class represents a JPA entity.boolean
isJpaEntity
(Class<?> aClass) Determine whether the given class represents a JPA entity.boolean
isLob
(MetaProperty metaProperty) Determine whether the given property is a LOB.boolean
isMethodBased
(MetaProperty metaProperty) Determine whether the given property is a method-based attribute of a JPA entity or embeddable.boolean
isOwningSide
(MetaProperty metaProperty) Determine whether the given property is on the owning side of an association.boolean
isSoftDeletable
(Class<?> entityClass) Determine whether the entity supports Soft Deletion.boolean
isSystem
(MetaProperty metaProperty) Determine whether the given property is system.boolean
isSystemLevel
(MetaClass metaClass) Determine whether the given entity is marked asSystemLevel
.boolean
isSystemLevel
(MetaProperty metaProperty) Determine whether the given property is marked asSystemLevel
.resolveMetaPropertyPath
(MetaClass metaClass, String propertyPath) Returns aMetaPropertyPath
which can include the special MetaProperty for a dynamic attribute.resolveMetaPropertyPathOrNull
(MetaClass metaClass, String propertyPath) Returns aMetaPropertyPath
which can include the special MetaProperty for a dynamic attribute.toPropertyPaths
(Collection<MetaProperty> properties) Converts a collection of properties to collection ofMetaPropertyPath
s containing one property eachvoid
traverseAttributes
(Object entity, EntityAttributeVisitor visitor) Depth-first traversal of the object graph starting from the specified entity instance.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.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.
-
Field Details
-
PRIMARY_KEY_ANN_NAME
- See Also:
-
EMBEDDED_ANN_NAME
- See Also:
-
TEMPORAL_ANN_NAME
- See Also:
-
SYSTEM_ANN_NAME
- See Also:
-
STORE_ANN_NAME
- See Also:
-
LENGTH_ANN_NAME
- See Also:
-
CASCADE_TYPES_ANN_NAME
- See Also:
-
CASCADE_PROPERTIES_ANN_NAME
- See Also:
-
EMBEDDED_PROPERTIES_ANN_NAME
- See Also:
-
UUID_KEY_ANN_NAME
Not applicable for legacy cuba entities- See Also:
-
DELETED_DATE_ANN_NAME
-
DELETED_BY_ANN_NAME
-
LAST_MODIFIED_DATE_ANN_NAME
-
metadata
-
extendedEntities
-
messages
-
instanceNameProvider
-
metadataExtensions
-
currentAuthentication
-
datatypeRegistry
-
persistentAttributesLoadChecker
-
metaPropertyPathResolvers
-
enums
-
-
Constructor Details
-
MetadataTools
public MetadataTools()Default constructor used by container at runtime and in server-side integration tests.
-
-
Method Details
-
format
Formats a value according to the property type.- Parameters:
value
- value of the passed property to formatproperty
- property- Returns:
- formatted value as string
-
format
Formats a value according to the value type.- Parameters:
value
- object to format- Returns:
- formatted value as string
-
getInstanceName
- Parameters:
instance
- instance- Returns:
- Instance name as defined by
InstanceName
ortoString()
.
-
getPrimaryKeyName
- Returns:
- name of a primary key attribute, or null if the entity has no primary key (e.g. embeddable)
-
getPrimaryKeyProperty
- Returns:
- MetaProperty representing a primary key attribute, or null if the entity has no primary key (e.g. embeddable)
-
getPrimaryKeyProperty
- Returns:
- MetaProperty representing a primary key attribute, or null if the entity has no primary key (e.g. embeddable)
-
hasCompositePrimaryKey
- Returns:
- true if passed MetaClass has a composite primary key
-
hasDbGeneratedPrimaryKey
- Returns:
- true if passed MetaClass has a db generated primary key
-
isAssignableFrom
- Returns:
- true if the first MetaClass is equal or an ancestor of the second.
-
isCascade
Deprecated.usegetCascadeTypes(MetaProperty)
insteadDetermine 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
-
getCascadeProperties
public List<MetaProperty> getCascadeProperties(MetaClass metaClass, @Nullable jakarta.persistence.CascadeType type) - Parameters:
metaClass
-type
- - type of operation to find properties.- Returns:
- properties with
CascadeType
containgCascadeType.ALL
or specified type
-
getEmbeddedProperties
- Returns:
- Names of embedded properties or empty list if no such properties contained in
metaClass
-
isSoftDeletable
Determine whether the entity supports Soft Deletion.- Parameters:
entityClass
- entity class- Returns:
true
if the entity has @DeletedDate
field
-
isSystem
Determine whether the given property is system. A property is considered system if it has one of the following annotations:Id
JmixId
JmixGeneratedValue
Version
CreatedDate
CreatedBy
LastModifiedDate
LastModifiedBy
DeletedDate
DeletedBy
-
getSystemProperties
System Properties - is important properties used for identification, audit, soft-delete and optimistic lock purposes More formally, it is properties with annotations:Id
,JmixId
,EmbeddedId
JmixGeneratedValue
CreatedDate
CreatedBy
LastModifiedDate
LastModifiedBy
DeletedDate
DeletedBy
Version
- Returns:
- names of system properties used in Entity determined by
metaClass
parameter
-
isJpa
Determine whether all the properties defined by the given property path are persistent attributes of JPA entities or embeddables. -
isJpa
Determine whether the given property is a persistent attribute of a JPA entity or embeddable. -
isMethodBased
Determine whether the given property is a method-based attribute of a JPA entity or embeddable. -
isEmbedded
Determine whether the given property denotes an embedded object.- See Also:
-
Embedded
EmbeddedId
-
isLob
Determine whether the given property is a LOB.- See Also:
-
Lob
-
isOwningSide
Determine whether the given property is on the owning side of an association. -
isSystemLevel
Determine whether the given entity is marked asSystemLevel
. -
isSystemLevel
Determine whether the given property is marked asSystemLevel
. -
getMetaAnnotationAttributes
-
getMetaAnnotationValue
- 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 instanceproperty
- property nameannotationClass
- 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 classproperty
- property nameannotationClass
- annotation class- Returns:
- true if the annotation is present
-
isJpaEntity
Determine whether the given meta-class represents a JPA entity. -
isJpaEntity
Determine whether the given class represents a JPA entity. -
isJpaEmbeddable
Determine whether the given meta-class is JPA embeddable. -
isJpaEmbeddable
Determine whether the given entity class is JPA embeddable. -
isCacheable
-
getPropertyEnclosingMetaClass
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
Return a collection of properties included into entity's name pattern (seeInstanceName
).- 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 (seeInstanceName
).- Parameters:
metaClass
- entity metaclassuseOriginal
- if true, and if the given metaclass doesn't define aInstanceName
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
- Returns:
- collection of properties owned by this metaclass and all its ancestors in the form of
MetaPropertyPath
s containing one property each
-
toPropertyPaths
Converts a collection of properties to collection ofMetaPropertyPath
s 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 asMetaPropertyPath
s.- Parameters:
fetchPlan
- fetch planmetaClass
- 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
- Returns:
- collection of all meta-classes representing JPA entities
-
getAllJpaEmbeddableMetaClasses
- Returns:
- collection of all meta-classes representing JPA embeddable classes
-
getAllEnums
- Returns:
- collection of all Java enums used as a type of an entity attribute
-
getDatabaseTable
- Returns:
- table name for the given entity, or null if the entity is Embeddable, MappedSuperclass or non-persistent
-
getDatabaseColumn
-
getDependsOnProperties
- Returns:
- list of properties defined in
DependsOnProperties
or empty list
-
getDependsOnProperties
- Returns:
- list of properties defined in
DependsOnProperties
or empty list
-
getDeletedDateProperty
- Returns:
- field annotated with @DeletedDate
- Throws:
IllegalArgumentException
- if entity has no @DeletedDate
field
-
findDeletedDateProperty
- Returns:
- field annotated with @DeletedDate or null if annotation is not present
-
findDeletedByProperty
- Returns:
- field annotated with @DeletedBy or null if annotation is not present
-
findLastModifiedDateProperty
- Returns:
- field annotated with @LastModifiedDate or null if annotation is not present
-
findPropertyByAnnotation
-
getSoftDeleteProperties
- Returns:
- list contains @
DeletedDate
, @DeletedBy
property names if present.
-
hasUuid
Checks whether an entity has uuid key (primary or not)- Returns:
- true if entity's
EntityEntry
implementsEntityEntryHasUuid
-
getUuidPropertyName
-
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 storemetaProperty
- 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 aMetaPropertyPath
which can include the special MetaProperty for a dynamic attribute.- Parameters:
metaClass
- originating meta-classpropertyPath
- path to the attribute- Returns:
- MetaPropertyPath instance
-
resolveMetaPropertyPath
Returns aMetaPropertyPath
which can include the special MetaProperty for a dynamic attribute. Throws an IllegalArgumentException if MetaPropertyPath can't be resolved.- Parameters:
metaClass
- originating meta-classpropertyPath
- path to the attribute- Returns:
- MetaPropertyPath instance
-
traverseAttributes
Depth-first traversal of the object graph starting from the specified entity instance. Visits all attributes.- Parameters:
entity
- entity graph entry pointvisitor
- 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 instanceentity
- entity graph entry pointvisitor
- 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 instanceentity
- entity graph entry pointcheckLoaded
- if true, skips not loaded attributesvisitor
- 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:
-
New instance will not be initialized. Entity id generation, embedded objects creation,
PostConstruct
actions, etc. will not be performed for cloned object (seeEntityInitializer
). - Unfetched or lazy-fetched attributes will not be cloned.
- Parameters:
source
- source instance- Returns:
- new instance of the same Java class as source
-
New instance will not be initialized. Entity id generation, embedded objects creation,
-
copy
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 instancedest
- 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
-
createInstance
-
createInstanceWithId
-
getAdditionalProperties
Returns all additional properties which are providing classes implement MetadataExtension interface- Parameters:
metaClass
- instance MetaClass for getting additional properties- Returns:
- all additional properties
-
isAdditionalProperty
- Parameters:
metaClass
-propertyName
-
-
getCascadeTypes(MetaProperty)
instead