Class 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
    • Constructor Detail

      • DalUtil

        public DalUtil()
    • 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 path
        propertyPath - 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.
        Parameters:
        tableName - the table name, is used to find the Entity
        dbColumnName - is used to find the Property
        Returns:
        a Property or null if no property found
      • getPropertyByTableId

        public static Property getPropertyByTableId​(String tableId,
                                                    String dbColumnName)
        Finds a property using the db column name and table id.
        Parameters:
        tableId - the table id, is used to find the Entity
        dbColumnName - is used to find the Property
        Returns:
        a Property or null if no property found
      • 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 path
        propertyPath - 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 (see parent). 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 (see parent) 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 copied
        resetId - 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 copied
        copyChildren - 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 copied
        copyChildren - 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 copy
        copyChildren - if true then all the child objects (which have this object as parent) are copied recursively
        resetId - if true then the id of the copied object is set to null
        fromTo - 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 copied
        target - the object where the original properties will be copied
        copyChildren - 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 copy
        target - the object where the original properties will be copied
        copyChildren - if true then all the child objects (which have this object as parent) are copied recursively
        fromTo - the map which maintains the relation between the to-copy and the copied object
        notCopiedProperties - 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 (see Entity.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 a BaseOBObject
        Returns:
        the entity name
      • getEntityName

        public static String getEntityName​(Class<?> clz)
      • 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