Class Utility
- java.lang.Object
-
- com.znize.platform.entity.persistence.Utility
-
public class Utility extends java.lang.ObjectPersistence entity utilities- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description Utility()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends PersistenceEntity>
java.util.List<java.lang.Long>collectionToIdList(java.util.Collection<T> entities)Get the list of id(s) of the entities.static <T extends PersistenceEntity>
java.util.Set<java.lang.Long>collectionToIdSet(java.util.Collection<T> entities)Get the set of id(s) of the entities.static <T extends PersistenceEntity>
java.lang.StringcollectionToIdString(java.util.Collection<T> entities, java.lang.String separator)Get the string of id(s) of the entities, separated by the specified separator.static <T extends NormativeId>
java.lang.StringcollectionToNidString(java.util.Collection<T> entities, java.lang.String separator)Get the string of nid(s) of the NormativeId entities, separated by the specified separator.static intcompareEntityAttrValues(EntityAttrValueSupport entity1, EntityAttrValueSupport entity2)If the entityAttrValues of entity1 is superSet of those of entity2, return -1; If the entityAttrValues of entity2 is superSet of those of entity1, return 1; if they are equal, return 0; otherwise return 2.static intcompareEntityAttrValues(java.util.List<EntityAttrValue> entityAttrValues1, java.util.List<EntityAttrValue> entityAttrValues2)If the entityAttrValues1 is superSet of those of entity2, return -1; If the entityAttrValues2 is superSet of those of entity1, return 1; if they are equal, return 0; otherwise return 2.static <T extends PersistenceEntity>
java.util.Map<java.lang.Long,T>createPersistenceEntityMapById(java.util.Collection<T> entities)Create a map from entity id to entity.static <T extends PersistenceEntity>
HierarchygetBrowseByHierarchy(T entity, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)Get the browseBy hierarchy of the entity.static <T extends PersistenceEntity>
PropertyDescriptorgetBrowseByProperty(java.lang.Class<T> entityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)Get the browseBy hierarchy property of the entity type.static java.util.List<java.lang.Class<? extends ChildEntity>>getChildEntityTypes(java.lang.Class<?> parentEntityType, java.lang.Class matchType, boolean persistentOnly)Get all the child entity types whose parent type is the specified parentEntityType.static intgetColumnLength(java.lang.Class entityType, java.lang.String property)Get the property column length of @Column annotation.static <T extends PersistenceEntity>
HierarchygetEntityAttrSourceHierarchy(T entity, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)Get the hierarchy which defines EntityAttr for the entity.static <T extends PersistenceEntity>
PropertyDescriptorgetEntityAttrSourceProperty(java.lang.Class<T> entityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)Get the hierarchy property of the entity type, which defines EntityAttrstatic java.lang.StringgetIdProperty(java.lang.Class<? extends PersistenceEntity> type)Get id property name for the entity type Hierarchy: hierarchyId if it is annotated as unique. NormativeId: nid if it is annotated as unique. Else: idstatic java.lang.StringgetIdString(PersistenceEntity entity)Get entity id as string.static java.lang.StringgetJoinColumnName(java.lang.Class entityType, java.lang.String propertyName)Get the name of @JoinColumn annotation of the property.static java.lang.StringgetMappedEntityName(java.lang.Class entityType)Get entity name from Entity annotation.static <T extends ChildEntity>
PersistenceEntitygetParentEntity(T childEntity, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)Get parent entity of a ChildEntity.static <T extends ChildEntity>
java.lang.ClassgetParentEntityType(java.lang.Class<T> childEntityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)Get parent entity type of a ChildEntity type.static <T extends ChildEntity>
PropertyDescriptorgetParentPropertyDescriptor(java.lang.Class<T> childEntityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)Get the PropertyDescriptor of a ChildEntity property that refers to its parent entity.static <T extends Hierarchy>
TgetPersistenceEntityByHierarchyId(java.util.Collection<T> entities, java.lang.String hierarchyId)Get entity by hierarchyIdstatic <T extends PersistenceEntity>
TgetPersistenceEntityById(java.util.Collection<T> entities, java.lang.Long id)Get entity by idstatic <T extends NormativeId>
TgetPersistenceEntityByNid(java.util.Collection<T> entities, java.lang.String nid)Get entity by nid.static java.lang.ClassgetPropertyTargetType(java.lang.reflect.Method readMethod, boolean returnMemberTypeForCollection)Get property type.static java.lang.StringgetPropertyValue(EntityI18NSupport entity, java.lang.String property, java.util.Locale locale)Get the property value by the locale using EntityI18Ns of the entity.static java.lang.StringgetPropertyValue(java.util.Map<java.lang.String,java.util.Map<java.lang.String,EntityI18N>> entityI18NMap, java.lang.String property, java.util.Locale locale)Get the property value by the locale using EntityI18Ns of the entity.static java.lang.reflect.Method[]getReadMethods(java.lang.Class cls, java.lang.String propertyNamePath)Get a list of read methods for the property name path.static <T extends ChildEntity>
java.lang.ClassgetRootEntityType(java.lang.Class<T> childEntityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)Get the root owning entity type of a ChildEntity type.static <T extends PersistenceEntity>
booleanisIdAssignedByUser(java.lang.Class<T> entityType)Is the entity id auto-generated or assigned by user?static booleanisPropertyTransient(java.lang.Class entityType, java.lang.String property)Is the property of the entityType transient by annotation Transient?static <T extends PersistenceEntity>
java.util.List<T>sortById(java.util.Collection<T> entities, boolean ascending)Sort entities by id.static java.util.List<java.lang.String>splitNamePath(java.lang.String namePath)Split dotted name path as a name liststatic <T extends PersistenceEntity>
java.util.Set<java.lang.Long>stringToIdSet(java.lang.String value, java.lang.String separator)Convert id string to id set.static java.lang.StringtruncatePropertyValue(java.lang.Class entityType, java.lang.String property, java.lang.String value, boolean appendDots)Truncate property value to its max length.
-
-
-
Method Detail
-
collectionToIdString
public static <T extends PersistenceEntity> java.lang.String collectionToIdString(java.util.Collection<T> entities, java.lang.String separator)
Get the string of id(s) of the entities, separated by the specified separator.For example, the entities with id: 10,2,9, and separator is /, then return 10/2/9.
- Parameters:
entities- a collection of entitiesseparator- id separator in the string- Returns:
- a string of entity id(s). return null if the set of entities is null or empty.
-
collectionToNidString
public static <T extends NormativeId> java.lang.String collectionToNidString(java.util.Collection<T> entities, java.lang.String separator)
Get the string of nid(s) of the NormativeId entities, separated by the specified separator.For example, the entities with nid: 010,002,009, and separator is /, then return 010/002/009.
- Parameters:
entities- a collection of entitiesseparator- id separator in the string- Returns:
- a string of entity nid(s). return null if the set of entities is null or empty.
-
collectionToIdList
public static <T extends PersistenceEntity> java.util.List<java.lang.Long> collectionToIdList(java.util.Collection<T> entities)
Get the list of id(s) of the entities.- Parameters:
entities- a collection of entities- Returns:
- a list of entity id(s). return null if the collection of entities is null.
-
collectionToIdSet
public static <T extends PersistenceEntity> java.util.Set<java.lang.Long> collectionToIdSet(java.util.Collection<T> entities)
Get the set of id(s) of the entities.- Parameters:
entities- a collection of entities- Returns:
- a set of entity id(s). return null if the collection of entities is null.
-
stringToIdSet
public static <T extends PersistenceEntity> java.util.Set<java.lang.Long> stringToIdSet(java.lang.String value, java.lang.String separator)
Convert id string to id set. For example, "122,123,124,125" to {122,123,124,125}- Type Parameters:
T- PersistenceEntity- Parameters:
value- id stringseparator- id separator in the string- Returns:
- a set of id(s). Id(s) are of java.lang.Long type.
-
splitNamePath
public static java.util.List<java.lang.String> splitNamePath(java.lang.String namePath)
Split dotted name path as a name list- Parameters:
namePath- name path such as a.b.c- Returns:
- name components such as [a,b,c]
-
getIdProperty
public static java.lang.String getIdProperty(java.lang.Class<? extends PersistenceEntity> type)
Get id property name for the entity type- Hierarchy: hierarchyId if it is annotated as unique.
- NormativeId: nid if it is annotated as unique.
- Else: id
- Parameters:
type- entity type- Returns:
- id property name
-
getIdString
public static java.lang.String getIdString(PersistenceEntity entity)
Get entity id as string.- Hierarchy: hierarchyId
- NormativeId: nid
- Other types: id
- Parameters:
entity- a PersisteneEntity- Returns:
- id value as string
- See Also:
getIdProperty(Class)
-
getMappedEntityName
public static java.lang.String getMappedEntityName(java.lang.Class entityType)
Get entity name from Entity annotation.- Parameters:
entityType- entity type- Returns:
- entity name. return null if it is not a mapped entity type.
-
getJoinColumnName
public static java.lang.String getJoinColumnName(java.lang.Class entityType, java.lang.String propertyName)Get the name of @JoinColumn annotation of the property.- Parameters:
entityType- the entity type of the propertypropertyName- property name- Returns:
- JoinColumn name. return null if the property or its @JoinColumn annotation is not found.
-
sortById
public static <T extends PersistenceEntity> java.util.List<T> sortById(java.util.Collection<T> entities, boolean ascending)
Sort entities by id.- Parameters:
entities- the collection of entities to set that will not be changed.ascending- ascending or descending- Returns:
- the ordered list of entities
-
isIdAssignedByUser
public static <T extends PersistenceEntity> boolean isIdAssignedByUser(java.lang.Class<T> entityType)
Is the entity id auto-generated or assigned by user?If the getId() method has annotation @GeneratedValue, then it is auto-generated.
- Parameters:
entityType- entity type- Returns:
- true if id is assigned by user
-
getPersistenceEntityById
public static <T extends PersistenceEntity> T getPersistenceEntityById(java.util.Collection<T> entities, java.lang.Long id)
Get entity by id- Parameters:
entities- a collection of entitiesid- entity id- Returns:
- the entity with the id. return null if not found.
-
getPersistenceEntityByNid
public static <T extends NormativeId> T getPersistenceEntityByNid(java.util.Collection<T> entities, java.lang.String nid)
Get entity by nid.- Parameters:
entities- a collection of NormativeId entitiesnid- entity normativeId- Returns:
- the entity with the nid. return null if not found.
-
getPersistenceEntityByHierarchyId
public static <T extends Hierarchy> T getPersistenceEntityByHierarchyId(java.util.Collection<T> entities, java.lang.String hierarchyId)
Get entity by hierarchyId- Parameters:
entities- a list of entitieshierarchyId- entity hierarchyId- Returns:
- entity with the hierarchyId
-
createPersistenceEntityMapById
public static <T extends PersistenceEntity> java.util.Map<java.lang.Long,T> createPersistenceEntityMapById(java.util.Collection<T> entities)
Create a map from entity id to entity.- Parameters:
entities- a collection of entities- Returns:
- a map.
-
getColumnLength
public static int getColumnLength(java.lang.Class entityType, java.lang.String property)Get the property column length of @Column annotation.- Parameters:
entityType- entity typeproperty- property name- Returns:
- property column length. return -1 if not defined.
-
truncatePropertyValue
public static java.lang.String truncatePropertyValue(java.lang.Class entityType, java.lang.String property, java.lang.String value, boolean appendDots)Truncate property value to its max length.- Parameters:
entityType- entity typeproperty- property namevalue- the property value to truncateappendDots- whether to append three dots at the end of the truncated value- Returns:
- truncated value, or the value itself if the value is not greater than its max length
- Since:
- 5.21
-
getPropertyValue
public static java.lang.String getPropertyValue(EntityI18NSupport entity, java.lang.String property, java.util.Locale locale)
Get the property value by the locale using EntityI18Ns of the entity. If EntityI18N is not found for the locale, return property value of the entity. Locale fallback is supported by matching the most specific locale.- Parameters:
entity- EntityI18NSupportproperty- the property for which to get valuelocale- a Locale- Returns:
- property value
-
getPropertyValue
public static java.lang.String getPropertyValue(java.util.Map<java.lang.String,java.util.Map<java.lang.String,EntityI18N>> entityI18NMap, java.lang.String property, java.util.Locale locale)
Get the property value by the locale using EntityI18Ns of the entity. If EntityI18N is not found for the locale, return null. Locale fallback is supported by matching the most specific locale.- Parameters:
entityI18NMap- entityI18N Map (property name -> locale -> EntityI18N).property- the property for which to get valuelocale- locale- Returns:
- property value
-
getParentPropertyDescriptor
public static <T extends ChildEntity> PropertyDescriptor getParentPropertyDescriptor(java.lang.Class<T> childEntityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)
Get the PropertyDescriptor of a ChildEntity property that refers to its parent entity. Check the cache first if the cache is not null.Its read method is annotated with ParentEntity.
- Parameters:
childEntityType- property descriptor cachecache- class reflection cache, can be null.- Returns:
- PropertyDescriptor instance.
- Throws:
SystemRuntimeException- throw if child entityType does not have a getter method with annotation ParentEntity
-
getParentEntity
public static <T extends ChildEntity> PersistenceEntity getParentEntity(T childEntity, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)
Get parent entity of a ChildEntity. A ChildEntity type is expected to have a getter method annotated with ParentEntity and ManyToOne. The getter method returns its parent entity.- Parameters:
childEntity- child entitycache- type reflection cache that can be null- Returns:
- parent entity
- Throws:
SystemRuntimeException- throw if @ParentEntity is not properly annotated.- Since:
- 5.16
-
getParentEntityType
public static <T extends ChildEntity> java.lang.Class getParentEntityType(java.lang.Class<T> childEntityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)
Get parent entity type of a ChildEntity type. A ChildEntity type is expected to have a getter method annotated with ParentEntity and ManyToOne. The getter method returns its parent entity.- Parameters:
childEntityType- child entity typecache- type reflection cache that can be null- Returns:
- parent entity type.
- Throws:
SystemRuntimeException- throw if @ParentEntity is not properly annotated.
-
getBrowseByProperty
public static <T extends PersistenceEntity> PropertyDescriptor getBrowseByProperty(java.lang.Class<T> entityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)
Get the browseBy hierarchy property of the entity type.- Type Parameters:
T- entity type- Parameters:
entityType- an entity typecache- type reflection cache that can be null- Returns:
- PropertyDescriptor object. return null if not found.
- Since:
- 5.17
- See Also:
HierarchySupport.browseByProperty()
-
getBrowseByHierarchy
public static <T extends PersistenceEntity> Hierarchy getBrowseByHierarchy(T entity, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)
Get the browseBy hierarchy of the entity.- Type Parameters:
T- entity type- Parameters:
entity- an entitycache- type reflection cache that can be null- Returns:
- Hierarchy entity. return null if not found.
- Since:
- 5.17
- See Also:
HierarchySupport.browseByProperty()
-
getEntityAttrSourceProperty
public static <T extends PersistenceEntity> PropertyDescriptor getEntityAttrSourceProperty(java.lang.Class<T> entityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)
Get the hierarchy property of the entity type, which defines EntityAttr- Type Parameters:
T- entity type- Parameters:
entityType- an entity typecache- type reflection cache that can be null- Returns:
- PropertyDescriptor object. return null if not found.
- Since:
- 5.17
- See Also:
HierarchySupport.entityAttrSourceProperty()
-
getEntityAttrSourceHierarchy
public static <T extends PersistenceEntity> Hierarchy getEntityAttrSourceHierarchy(T entity, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)
Get the hierarchy which defines EntityAttr for the entity.- Type Parameters:
T- entity type- Parameters:
entity- an entitycache- type reflection cache that can be null- Returns:
- Hierarchy entity. return null if not found.
- Since:
- 5.17
- See Also:
HierarchySupport.entityAttrSourceProperty()
-
getRootEntityType
public static <T extends ChildEntity> java.lang.Class getRootEntityType(java.lang.Class<T> childEntityType, java.util.Map<java.lang.Class,java.util.Map<java.lang.String,PropertyDescriptor>> cache)
Get the root owning entity type of a ChildEntity type. For example, A is a ChildEntity of B, B is a ChildEntity type of C, and C is a ChildEntity type of D. D is not a ChildEntity type. Then D is the root entity type of A, B and C.- Parameters:
childEntityType- child entity typecache- type reflection cache- Returns:
- root entity type.
- Since:
- 5.4
- See Also:
getParentEntityType(Class, Map)
-
getChildEntityTypes
public static java.util.List<java.lang.Class<? extends ChildEntity>> getChildEntityTypes(java.lang.Class<?> parentEntityType, java.lang.Class matchType, boolean persistentOnly)
Get all the child entity types whose parent type is the specified parentEntityType. An entity of parent type has a collection of entities of a child type.For example, if Order is parent type, and OrderItem is one of its child entity type.
@ParentType({OrderItem.class}) public class Order { @OneToMany public List<OrderItem> getOrderItems() { } } public class OrderItem implements ChildEntity { @ParentEntity @ManyToOne public Order getOrder() { } }If a parent entity type is not annotated with ParentEntity, reflection will be used to get all its child entity types with some performance overhead.- Parameters:
parentEntityType- parent entity typematchType- the super(or itself) type that matches Class.isAssignableFrom(Class). if null, all ChildEntity types.persistentOnly- if true, return only those child entity types that is O/R mapped with @Entity annotation.- Returns:
- a list of child entity types. return empty list if not found.
-
compareEntityAttrValues
public static int compareEntityAttrValues(EntityAttrValueSupport entity1, EntityAttrValueSupport entity2)
If the entityAttrValues of entity1 is superSet of those of entity2, return -1; If the entityAttrValues of entity2 is superSet of those of entity1, return 1; if they are equal, return 0; otherwise return 2.If every attribute value of entity1 is found in the entity2, then entity1 is the super set of entity2. That is, the entity1 has fewer attributes and more generic in scope than entity2.
- Parameters:
entity1- an EntityAttrValueSupportentity2- another EntityAttrValueSupport to compare with- Returns:
- -1, 0, 1, 2
- Since:
- 5.4
-
compareEntityAttrValues
public static int compareEntityAttrValues(java.util.List<EntityAttrValue> entityAttrValues1, java.util.List<EntityAttrValue> entityAttrValues2)
If the entityAttrValues1 is superSet of those of entity2, return -1; If the entityAttrValues2 is superSet of those of entity1, return 1; if they are equal, return 0; otherwise return 2.If every attribute value of entityAttrValues1 is found in the entityAttrValues2, then entityAttrValues1 is the super set of entityAttrValues2. That is, the entityAttrValues1 has fewer attributes and more generic in scope than entityAttrValues2.
- Parameters:
entityAttrValues1- an attribute value listentityAttrValues2- another attribute value list to compare with- Returns:
- -1, 0, 1, 2
- Since:
- 5.5.1
-
getPropertyTargetType
public static java.lang.Class getPropertyTargetType(java.lang.reflect.Method readMethod, boolean returnMemberTypeForCollection)Get property type. The return type of a property's read method may be different from mapped target type of the property using the targetEntity of a JPA annotation including @OneToOne, @ManyToOne, @OneToMany and @ManyToMany.If checking memberType for @OneToMany or @ManyToMany, return memberType.
- Parameters:
readMethod- property read methodreturnMemberTypeForCollection- if true, for collection property @OneToMany, targetEntity is its memberType- Returns:
- target type
-
getReadMethods
public static java.lang.reflect.Method[] getReadMethods(java.lang.Class cls, java.lang.String propertyNamePath)Get a list of read methods for the property name path. The type of a property is the targetEntity of JPA annotation @OneToOne, @ManyToOne, @OneToMany and @ManyToMany if defined. otherwise it is the return type of its read method.Collection type on the path is not supported except the leaf property.
For example, foo.bar.baz, return the read methods: getFoo(), getBar(), getBaz().
- Parameters:
cls- the root object typepropertyNamePath- property name path such as a.b.c- Returns:
- an array of property read methods.
- Since:
- 5.4
-
isPropertyTransient
public static boolean isPropertyTransient(java.lang.Class entityType, java.lang.String property)Is the property of the entityType transient by annotation Transient?- Parameters:
entityType- entity typeproperty- property name- Returns:
- true if transient, false otherwise
- Since:
- 5.20
-
-