Package org.openbravo.dal.core
Class DalUtil
- java.lang.Object
-
- org.openbravo.dal.core.DalUtil
-
public class DalUtil extends Object
Utility class used by the dal layer. It contains several copy methods and methods to retrieve the EntityName and id of an object without forcing a load by Hibernate.- Author:
- mtaal
-
-
Field Summary
Fields Modifier and Type Field Description static String
DOT
static String
FIELDSEPARATOR
-
Constructor Summary
Constructors Constructor Description DalUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static BaseOBObject
copy(BaseOBObject source)
Copies one object and its children.static BaseOBObject
copy(BaseOBObject source, boolean copyChildren)
Copies one object.static BaseOBObject
copy(BaseOBObject source, boolean copyChildren, boolean resetId)
Copies one object.static BaseOBObject
copy(BaseOBObject source, boolean copyChildren, boolean resetId, Map<BaseOBObject,BaseOBObject> fromTo)
Copies a single object, including its children (if the relevant parameter is set to true), and resets the id in the copied object (if the relevant parameter is set to true).static List<BaseOBObject>
copyAll(List<BaseOBObject> source)
Copies a BaseOBObject and all its children (seeparent
).static List<BaseOBObject>
copyAll(List<BaseOBObject> source, boolean resetId)
Copies a BaseOBObject and all its children (seeparent
) are copied recursively.static BaseOBObject
copyToTarget(BaseOBObject source, BaseOBObject target, boolean copyChildren, List<String> notCopiedProperties)
Copies one object into another existing object.static BaseOBObject
copyToTarget(BaseOBObject source, BaseOBObject target, boolean copyChildren, Map<BaseOBObject,BaseOBObject> fromTo, List<String> notCopiedProperties)
Copies a single object into an existing target object, including its children (if the relevant parameter is set to true), all properties are copied except of the ID and the parent properties.static String
getEntityName(Class<?> clz)
static String
getEntityName(Object o)
Returns the entity name (seeEntity.getName()
without loading/resolving the object.static Serializable
getId(Object o)
Deprecated.It is not needed to used this method for lazy loading of id in a proxy.static Property
getProperty(String tableName, String dbColumnName)
Finds a property using the db column name and table name.static Property
getPropertyByTableId(String tableId, String dbColumnName)
Finds a property using the db column name and table id.static Property
getPropertyFromPath(Entity entity, String propertyPath)
Translates a so-called property path to a property.static Serializable
getReferencedPropertyValue(Property referencingProperty, Object referedObject)
Is used when the value of the foreign-key column should be retrieved.static Object
getValueFromPath(BaseOBObject bob, String propertyPath)
Translates a so-called property path to a property.static void
repairReferences(Map<BaseOBObject,BaseOBObject> fromTo)
Convenience method usefull for when implementing a custom copy process.static void
sortByIdentifier(List<? extends BaseOBObject> objects)
Sorts the passed list according to the identifier of the objects.
-
-
-
Field Detail
-
FIELDSEPARATOR
public static final String FIELDSEPARATOR
- See Also:
- Constant Field Values
-
DOT
public static final String DOT
- See Also:
- Constant Field Values
-
-
Method Detail
-
sortByIdentifier
public static void sortByIdentifier(List<? extends BaseOBObject> objects)
Sorts the passed list according to the identifier of the objects.- Parameters:
objects
- the objects (must be BaseOBObjects) to sort.
-
getPropertyFromPath
public static Property getPropertyFromPath(Entity entity, String propertyPath)
Translates a so-called property path to a property. The passed entity is the starting entity. For example the property: organization.name and entity: Product will result in the Organization.name property to be returned.- Parameters:
entity
- the start entity for the property pathpropertyPath
- the property path, dot-separated property names- Returns:
- the found property
-
getProperty
public static Property getProperty(String tableName, String dbColumnName)
Finds a property using the db column name and table name.
-
getPropertyByTableId
public static Property getPropertyByTableId(String tableId, String dbColumnName)
Finds a property using the db column name and table id.
-
getValueFromPath
public static Object getValueFromPath(BaseOBObject bob, String propertyPath)
Translates a so-called property path to a property. The passed entity is the starting entity. For example the property: organization.name and entity: Product will result in the Organization.name property to be returned.- Parameters:
bob
- the start entity for the property pathpropertyPath
- the property path, dot-separated property names- Returns:
- the found value
-
copyAll
public static List<BaseOBObject> copyAll(List<BaseOBObject> source)
Copies a BaseOBObject and all its children (seeparent
). Referenced objects are not copied. The id of the copied objects are all set to null.- Parameters:
source
- the list of BaseOBObject which will be copied- Returns:
- the copied BaseOBObject, child objects are also copied
-
copyAll
public static List<BaseOBObject> copyAll(List<BaseOBObject> source, boolean resetId)
Copies a BaseOBObject and all its children (seeparent
) are copied recursively. Referenced objects are not copied. The resetId parameter controls if the id of the copied objects is nullified.- Parameters:
source
- the list of BaseOBObject which will be copiedresetId
- if true then the id's of the copied objects will be nullified- Returns:
- the copied BaseOBObject, child objects are also copied
-
copy
public static BaseOBObject copy(BaseOBObject source)
Copies one object and its children. The id of the copied object (and its children) is set to null.- Parameters:
source
- the object to be copied- Returns:
- the copied object
-
copy
public static BaseOBObject copy(BaseOBObject source, boolean copyChildren)
Copies one object. The copyChildren parameter controls if the children of the object are also copied. The id of the copied object (and its children) is set to null.- Parameters:
source
- the object to be copiedcopyChildren
- this parameter controls if the children of the source are also copied (recursively)- Returns:
- the copied object
-
copy
public static BaseOBObject copy(BaseOBObject source, boolean copyChildren, boolean resetId)
Copies one object. The copyChildren parameter controls if the children of the object are also copied. The resetId parameter controls if the id of the copied object (and its children) is set to null.- Parameters:
source
- the object to be copiedcopyChildren
- this parameter controls if the children of the source are also copied (recursively)resetId
- if true then the id's of the copied objects will be nullified- Returns:
- the copied object
-
repairReferences
public static void repairReferences(Map<BaseOBObject,BaseOBObject> fromTo)
Convenience method usefull for when implementing a custom copy process. Walks through all copied objects and if there are references to objects which have been copied then the references are moved to the copied object See issue https://issues.openbravo.com/view.php?id=8651 which is related to this method.- Parameters:
fromTo
- map with copy from - copy to relation
-
copy
public static BaseOBObject copy(BaseOBObject source, boolean copyChildren, boolean resetId, Map<BaseOBObject,BaseOBObject> fromTo)
Copies a single object, including its children (if the relevant parameter is set to true), and resets the id in the copied object (if the relevant parameter is set to true). The source and its copy are added to the fromTo map.- Parameters:
source
- the object to copycopyChildren
- if true then all the child objects (which have this object as parent) are copied recursivelyresetId
- if true then the id of the copied object is set to nullfromTo
- the map which maintains the relation between the to-copy and the copied object- Returns:
- the copied object
- See Also:
copy(BaseOBObject, boolean, boolean)
,repairReferences(Map)
-
copyToTarget
public static BaseOBObject copyToTarget(BaseOBObject source, BaseOBObject target, boolean copyChildren, List<String> notCopiedProperties)
Copies one object into another existing object. The id and the parent properties of the target object remain unchanged. The copyChildren parameter controls if the children of the object are also copied. The resetId parameter controls if the id of the copied object (and its children) is set to null.- Parameters:
source
- the object to be copiedtarget
- the object where the original properties will be copiedcopyChildren
- this parameter controls if the children of the source are also copied (recursively)notCopiedProperties
- list of property names that will not be copied into the target- Returns:
- the copied object
-
copyToTarget
public static BaseOBObject copyToTarget(BaseOBObject source, BaseOBObject target, boolean copyChildren, Map<BaseOBObject,BaseOBObject> fromTo, List<String> notCopiedProperties)
Copies a single object into an existing target object, including its children (if the relevant parameter is set to true), all properties are copied except of the ID and the parent properties. The source and its copy are added to the fromTo map.- Parameters:
source
- the object to copytarget
- the object where the original properties will be copiedcopyChildren
- if true then all the child objects (which have this object as parent) are copied recursivelyfromTo
- the map which maintains the relation between the to-copy and the copied objectnotCopiedProperties
- list of property names that will not be copied into the target- Returns:
- the copied object
- See Also:
copy(BaseOBObject, boolean, boolean)
,repairReferences(Map)
-
getId
@Deprecated public static Serializable getId(Object o)
Deprecated.It is not needed to used this method for lazy loading of id in a proxy. Kept only for backwards compatibility.
-
getEntityName
public static String getEntityName(Object o)
Returns the entity name (seeEntity.getName()
without loading/resolving the object. If the object is a Hibernate proxy then the entity name is retrieved through the proxy and not directly from the object. This prevent unwanted loading of the object.- Parameters:
o
- the object to get the entity name for, can be a Hibernate proxy or aBaseOBObject
- Returns:
- the entity name
-
getReferencedPropertyValue
public static Serializable getReferencedPropertyValue(Property referencingProperty, Object referedObject)
Is used when the value of the foreign-key column should be retrieved. In java the value of a reference is an entity. This method is usefull to get to the actual database foreign key value (often the primary key of the referenced object). This method has a special implementation to prevent unwanted loading of Hibernate Proxies. This loading is not required if only the id of the referenced object needs to be returned.- Parameters:
referencingProperty
- the property which models the association, it is a property of the refering object (the owner)referedObject
- the entity to which is being refered- Returns:
- the id of the referedObject (most of the time) or if the foreign key is to another property then the value of that property is returned
-
-