org.jminor.framework.domain
Class EntityUtil

java.lang.Object
  extended by org.jminor.framework.domain.EntityUtil

public final class EntityUtil
extends Object

A static utility class containing helper methods for working with Entity instances.


Method Summary
static List<Entity> copyEntities(List<Entity> entities)
           
static Entity createEntity(String entityID, ValueProvider<Property,Object> valueProvider)
           
static Entity createRandomEntity(String entityID, Map<String,Entity> referenceEntities)
           
static Collection<Object> getDistinctPropertyValues(String propertyID, List<Entity> entities)
          Returns a Collection containing the distinct values of propertyID from the given entities.
static Deserializer<Entity> getEntityDeserializer()
           
static Serializer<Entity> getEntitySerializer()
           
static List<Entity> getModifiedEntities(Collection<Entity> entities)
           
static List<Entity.Key> getPrimaryKeys(Collection<Entity> entities)
           
static List<Entity.Key> getPrimaryKeys(Collection<Entity> entities, boolean originalValue)
           
static List<Property> getProperties(String entityID, Collection<String> propertyIDs)
           
static List<Object> getPropertyValues(List<Entity.Key> keys)
          Retrieves the values of the given keys, assuming they are single column keys.
static List<Object> getPropertyValues(Property property, List<Entity> entities)
           
static List<Object> getPropertyValues(Property property, List<Entity> entities, boolean includeNullValues)
           
static List<Object> getPropertyValues(String propertyID, List<Entity> entities)
           
static List<Object> getPropertyValues(String propertyID, List<Entity> entities, boolean includeNullValues)
           
static Object getRandomValue(Property property, Map<String,Entity> referenceEntities)
           
static List<Property> getSortedProperties(String entityID, Collection<String> propertyIDs)
           
static List<Property> getUpdatableProperties(String entityID)
           
static Map<String,Collection<Entity>> hashByEntityID(Collection<Entity> entities)
          Returns a Map containing the given entities hashed by their entityIDs
static Map<Entity.Key,Entity> hashByPrimaryKey(List<Entity> entities)
          Hashes the given entities by their primary key
static Map<Object,Collection<Entity>> hashByPropertyValue(String propertyID, Collection<Entity> entities)
          Returns a Map containing the given entities hashed by the value of the property with ID propertyID
static Map<String,Collection<Entity.Key>> hashKeysByEntityID(Collection<Entity.Key> keys)
          Returns a Map containing the given entity keys hashed by their entityIDs
static boolean isPrimaryKeyModified(Collection<Entity> entities)
           
static Entity randomize(Entity entity, boolean includePrimaryKey, Map<String,Entity> referenceEntities)
           
static Map<Entity.Key,Object> setPropertyValue(String propertyID, Object value, Collection<Entity> entities)
          Sets the value of the property with ID propertyID to value in the given entities
static void sort(List<? extends Property> properties)
          Sorts the given properties by caption, or if that is not available, property ID
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getModifiedEntities

public static List<Entity> getModifiedEntities(Collection<Entity> entities)
Parameters:
entities - the entities
Returns:
a List of entities that have been modified

hashByPrimaryKey

public static Map<Entity.Key,Entity> hashByPrimaryKey(List<Entity> entities)
Hashes the given entities by their primary key

Parameters:
entities - the entities to hash
Returns:
the hashed entities

getPrimaryKeys

public static List<Entity.Key> getPrimaryKeys(Collection<Entity> entities)
Parameters:
entities - the entities
Returns:
a List containing the primary keys of the given entities

getPrimaryKeys

public static List<Entity.Key> getPrimaryKeys(Collection<Entity> entities,
                                              boolean originalValue)
Parameters:
entities - the entities
originalValue - if true then the original value of the primary key is used
Returns:
a List containing the primary keys of the given entities

getPropertyValues

public static List<Object> getPropertyValues(List<Entity.Key> keys)
Retrieves the values of the given keys, assuming they are single column keys.

Parameters:
keys - the keys
Returns:
the actual property values of the given keys

getPropertyValues

public static List<Object> getPropertyValues(String propertyID,
                                             List<Entity> entities)
Parameters:
propertyID - the ID of the property for which to retrieve the values
entities - the entities from which to retrieve the property value
Returns:
a List containing the values of the property with the given ID from the given entities, null values are included

getPropertyValues

public static List<Object> getPropertyValues(String propertyID,
                                             List<Entity> entities,
                                             boolean includeNullValues)
Parameters:
propertyID - the ID of the property for which to retrieve the values
entities - the entities from which to retrieve the property value
includeNullValues - if true then null values are included
Returns:
a List containing the values of the property with the given ID from the given entities

getPropertyValues

public static List<Object> getPropertyValues(Property property,
                                             List<Entity> entities)
Parameters:
property - the the property for which to retrieve the values
entities - the entities from which to retrieve the property value
Returns:
a List containing the values of the property with the given ID from the given entities, null values are included

getPropertyValues

public static List<Object> getPropertyValues(Property property,
                                             List<Entity> entities,
                                             boolean includeNullValues)
Parameters:
property - the the property for which to retrieve the values
entities - the entities from which to retrieve the property value
includeNullValues - if true then null values are included
Returns:
a List containing the values of the property with the given ID from the given entities

getDistinctPropertyValues

public static Collection<Object> getDistinctPropertyValues(String propertyID,
                                                           List<Entity> entities)
Returns a Collection containing the distinct values of propertyID from the given entities. If the entities list is null an empty Collection is returned.

Parameters:
propertyID - the ID of the property for which to retrieve the values
entities - the entities from which to retrieve the values
Returns:
a Collection containing the distinct property values

setPropertyValue

public static Map<Entity.Key,Object> setPropertyValue(String propertyID,
                                                      Object value,
                                                      Collection<Entity> entities)
Sets the value of the property with ID propertyID to value in the given entities

Parameters:
propertyID - the ID of the property for which to set the value
value - the value
entities - the entities for which to set the value
Returns:
the old property values mapped to their respective primary key

hashByPropertyValue

public static Map<Object,Collection<Entity>> hashByPropertyValue(String propertyID,
                                                                 Collection<Entity> entities)
Returns a Map containing the given entities hashed by the value of the property with ID propertyID

Parameters:
propertyID - the ID of the property which value should be used for mapping
entities - the entities to map by property value
Returns:
a Map of entities hashed by property value

hashByEntityID

public static Map<String,Collection<Entity>> hashByEntityID(Collection<Entity> entities)
Returns a Map containing the given entities hashed by their entityIDs

Parameters:
entities - the entities to map by entityID
Returns:
a Map of entities hashed by entityID

hashKeysByEntityID

public static Map<String,Collection<Entity.Key>> hashKeysByEntityID(Collection<Entity.Key> keys)
Returns a Map containing the given entity keys hashed by their entityIDs

Parameters:
keys - the entity keys to map by entityID
Returns:
a Map of entity keys hashed by entityID

getProperties

public static List<Property> getProperties(String entityID,
                                           Collection<String> propertyIDs)
Parameters:
entityID - the entity ID
propertyIDs - the property IDs
Returns:
a list containing the properties identified by the given propertyIDs

getSortedProperties

public static List<Property> getSortedProperties(String entityID,
                                                 Collection<String> propertyIDs)
Parameters:
entityID - the entity ID
propertyIDs - the property IDs
Returns:
the given properties sorted by caption, or if that is not available, property ID

sort

public static void sort(List<? extends Property> properties)
Sorts the given properties by caption, or if that is not available, property ID

Parameters:
properties - the properties to sort

getUpdatableProperties

public static List<Property> getUpdatableProperties(String entityID)
Parameters:
entityID - the entity ID
Returns:
a list containing all updatable properties associated with the given entity ID

copyEntities

public static List<Entity> copyEntities(List<Entity> entities)
Parameters:
entities - the entities to copy
Returns:
deep copies of the entities

createRandomEntity

public static Entity createRandomEntity(String entityID,
                                        Map<String,Entity> referenceEntities)
Parameters:
entityID - the entity ID
referenceEntities - entities referenced by the given entity ID
Returns:
a Entity instance containing randomized values, based on the property definitions

createEntity

public static Entity createEntity(String entityID,
                                  ValueProvider<Property,Object> valueProvider)
Parameters:
entityID - the entity ID
valueProvider - the value provider
Returns:
an Entity instance initialized with values provided by the given value provider

randomize

public static Entity randomize(Entity entity,
                               boolean includePrimaryKey,
                               Map<String,Entity> referenceEntities)
Parameters:
entity - the entity to randomize
includePrimaryKey - if true then the primary key values are include
referenceEntities - entities referenced by the given entity
Returns:
the entity with randomized values

getRandomValue

public static Object getRandomValue(Property property,
                                    Map<String,Entity> referenceEntities)
Parameters:
property - the property
referenceEntities - entities referenced by the given property
Returns:
a random value

getEntitySerializer

public static Serializer<Entity> getEntitySerializer()
Returns:
a Serializer, if one is available on the classpath

getEntityDeserializer

public static Deserializer<Entity> getEntityDeserializer()
Returns:
a Deserializer, if one is available on the classpath

isPrimaryKeyModified

public static boolean isPrimaryKeyModified(Collection<Entity> entities)
Parameters:
entities - the entities to check
Returns:
true if any of the given entities has a modified primary key