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
Nested ClassesModifier and TypeClassDescriptionstatic classINTERNALstatic interfaceINTERNAL -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringprotected CurrentAuthenticationprotected DatatypeRegistrystatic final Stringstatic final Stringstatic final Stringstatic final Stringprotected Collection<Class<?>>protected ExtendedEntitiesprotected InstanceNameProviderstatic final Stringstatic final Stringprotected Messagesprotected Metadataprotected List<MetadataExtension>protected Collection<MetaPropertyPathResolver>protected PersistentAttributesLoadCheckerstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringNot applicable for legacy cuba entities -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor used by container at runtime and in server-side integration tests. -
Method Summary
Modifier and TypeMethodDescriptionvoidMake a shallow copy of an instance.<T> Tcopy(T source) Create a new instance and make it a shallow copy of the instance given.protected static <T> TcreateInstance(Class<T> aClass) protected static ObjectcreateInstanceWithId(Class<?> entityClass, Object id) voiddeepCopy(Object source, Object destination, MetadataTools.EntitiesHolder entitiesHolder) Copies all property values from source to destination excluding null values.<T> TdeepCopy(T source) Makes a deep copy of the source entity.booleanfetchPlanContainsProperty(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 StringfindPropertyByAnnotation(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, javax.persistence.CascadeType type) List<javax.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 asMetaPropertyPaths.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> TgetMetaAnnotationValue(MetaClass metaClass, Class<?> metaAnnotationClass) <T> TgetMetaAnnotationValue(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,EmbeddedIdJmixGeneratedValueCreatedDateCreatedByLastModifiedDateLastModifiedByDeletedDateDeletedByVersiongetUuidPropertyName(MetaClass metaClass) Gets name of uuid key property.getUuidPropertyName(Class<?> clazz) Gets name of uuid key property.booleanhasCompositePrimaryKey(MetaClass metaClass) booleanhasDbGeneratedPrimaryKey(MetaClass metaClass) booleanChecks whether an entity has uuid key (primary or not)protected voidinternalTraverseAttributes(Entity entity, EntityAttributeVisitor visitor, HashSet<Object> visited) protected voidinternalTraverseAttributesByFetchPlan(FetchPlan fetchPlan, Object entity, EntityAttributeVisitor visitor, Map<Object, Set<FetchPlan>> visited, boolean checkLoaded) booleanisAdditionalProperty(MetaClass metaClass, String propertyName) booleanisAnnotationPresent(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.booleanisAnnotationPresent(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.booleanisAssignableFrom(MetaClass metaClass, MetaClass other) booleanisCacheable(MetaClass metaClass) booleanisCascade(MetaProperty metaProperty) Deprecated.booleanisEmbedded(MetaProperty metaProperty) Determine whether the given property denotes an embedded object.booleanisJpa(MetaProperty metaProperty) Determine whether the given property is a persistent attribute of a JPA entity or embeddable.booleanisJpa(MetaPropertyPath metaPropertyPath) Determine whether all the properties defined by the given property path are persistent attributes of JPA entities or embeddables.booleanisJpaEmbeddable(MetaClass metaClass) Determine whether the given meta-class is JPA embeddable.booleanisJpaEmbeddable(Class<?> aClass) Determine whether the given entity class is JPA embeddable.booleanisJpaEntity(MetaClass metaClass) Determine whether the given meta-class represents a JPA entity.booleanisJpaEntity(Class<?> aClass) Determine whether the given class represents a JPA entity.booleanisLob(MetaProperty metaProperty) Determine whether the given property is a LOB.booleanisMethodBased(MetaProperty metaProperty) Determine whether the given property is a method-based attribute of a JPA entity or embeddable.booleanisOwningSide(MetaProperty metaProperty) Determine whether the given property is on the owning side of an association.booleanisSoftDeletable(Class<?> entityClass) Determine whether the entity supports Soft Deletion.booleanisSystem(MetaProperty metaProperty) Determine whether the given property is system.booleanisSystemLevel(MetaClass metaClass) Determine whether the given entity is marked asSystemLevel.booleanisSystemLevel(MetaProperty metaProperty) Determine whether the given property is marked asSystemLevel.resolveMetaPropertyPath(MetaClass metaClass, String propertyPath) Returns aMetaPropertyPathwhich can include the special MetaProperty for a dynamic attribute.resolveMetaPropertyPathOrNull(MetaClass metaClass, String propertyPath) Returns aMetaPropertyPathwhich can include the special MetaProperty for a dynamic attribute.toPropertyPaths(Collection<MetaProperty> properties) Converts a collection of properties to collection ofMetaPropertyPaths containing one property eachvoidtraverseAttributes(Object entity, EntityAttributeVisitor visitor) Depth-first traversal of the object graph starting from the specified entity instance.voidtraverseAttributesByFetchPlan(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.voidtraverseAttributesByFetchPlan(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
InstanceNameortoString().
-
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 javax.persistence.CascadeType type) - Parameters:
metaClass-type- - type of operation to find properties.- Returns:
- properties with
CascadeTypecontaingCascadeType.ALLor 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:
trueif the entity has @DeletedDatefield
-
isSystem
Determine whether the given property is system. A property is considered system if it has one of the following annotations:IdJmixIdJmixGeneratedValueVersionCreatedDateCreatedByLastModifiedDateLastModifiedByDeletedDateDeletedBy
-
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,EmbeddedIdJmixGeneratedValueCreatedDateCreatedByLastModifiedDateLastModifiedByDeletedDateDeletedByVersion
- Returns:
- names of system properties used in Entity determined by
metaClassparameter
-
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:
-
EmbeddedEmbeddedId
-
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:
- meta-annotation value for specified metaClass and annotation
-
getMetaAnnotationValue
- Returns:
- meta-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 aInstanceNameand 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
MetaPropertyPaths containing one property each
-
toPropertyPaths
Converts a collection of properties to collection ofMetaPropertyPaths 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 asMetaPropertyPaths.- 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
DependsOnPropertiesor empty list
-
getDependsOnProperties
- Returns:
- list of properties defined in
DependsOnPropertiesor empty list
-
getDeletedDateProperty
- Returns:
- field annotated with @DeletedDate
- Throws:
IllegalArgumentException- if entity has no @DeletedDatefield
-
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, @DeletedByproperty names if present.
-
hasUuid
Checks whether an entity has uuid key (primary or not)- Returns:
- true if entity's
EntityEntryimplementsEntityEntryHasUuid
-
getUuidPropertyName
Gets name of uuid key property.- Parameters:
clazz- entity java class- Returns:
- uuid property name or null if it doesn't exist. Throws exception if there is no MetaClass for the provided java class.
-
getUuidPropertyName
Gets name of uuid key property.- Parameters:
metaClass- entity MetaClass- Returns:
- uuid property name or null if it doesn't exist
-
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 aMetaPropertyPathwhich can include the special MetaProperty for a dynamic attribute.- Parameters:
metaClass- originating meta-classpropertyPath- path to the attribute- Returns:
- MetaPropertyPath instance
-
resolveMetaPropertyPath
Returns aMetaPropertyPathwhich 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,
PostConstructactions, 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