Class DBViewUtils

java.lang.Object
overit.geocallapp.utilities.dataaccess.DBViewUtils

public class DBViewUtils extends Object
Utility class providing helper methods for working with DBView objects.
This class offers a collection of static utility methods that simplify common operations on DBView objects, such as cloning, transformation, and manipulation.
It provides standardized approaches for handling these operations with consistent behavior.
Since:
1.0
  • Method Details

    • cloneDBView

      public static DBView cloneDBView(DBView dbv)
      Creates a deep copy of the given DBView. This method creates a complete copy of the provided DBView, including its structure (field definitions) and all data rows. The resulting DBView is independent of the original, so changes to one will not affect the other.
      Parameters:
      dbv - the DBView to clone
      Returns:
      a new independent copy of the original DBView
    • addColumns

      public static void addColumns(DBView dbv, Collection<String> columns)
      Adds a collection of columns to the given DBView. This method adds each column from the provided collection to the DBView if it doesn't already exist.
      Parameters:
      dbv - the DBView to add columns to
      columns - the collection of column names to add
    • addColumns

      public static void addColumns(DBView dbv, String[] columns)
      Adds an array of columns to the given DBView. It checks if each column is already present in the DBView before adding it, to avoid duplicate columns.
      Parameters:
      dbv - the DBView to add columns to
      columns - the array of column names to add
    • setDefaultValue

      public static void setDefaultValue(DBView dbv, String column, Object value)
      Sets a default value for a specific column on each record of a DBView if the column is empty or null.
      Parameters:
      dbv - the DBView containing the records to process
      column - the name of the column to check and potentially set
      value - the default value to set if the column is empty or null
    • fillDBView

      public static void fillDBView(DBView toFill, Iterable<? extends DynO> items)
      Populates a DBView with data from a collection of DynO objects. Note that this method assumes the DBView already has the necessary columns defined. If the DBView needs to have columns added dynamically based on the DynO objects, use toDBView(Iterable) instead.
      Parameters:
      toFill - the DBView to populate with data
      items - the collection of DynO objects to add as rows
    • toDBView

      public static DBView toDBView(Iterable<? extends DynO> items)
      Converts a collection of DynO objects into a new DBView.
      Parameters:
      items - the collection of DynO objects to convert to a DBView
      Returns:
      a new DBView containing the data from the DynO objects
    • toDBView

      public static DBView toDBView(String itemObjectColumn, Iterable<? extends DynO> items)
      Converts a collection of DynO objects into a new DBView with an optional reference column. If itemObjectColumn is not null, each row in the DBView will also include a column with that name containing a reference to the original DynO object.
      Parameters:
      itemObjectColumn - the name of the column to store references to the original DynO objects, or null if not needed
      items - the collection of DynO objects to convert to a DBView
      Returns:
      a new DBView containing the data from the DynO objects
    • toDBView

      public static <T extends DynO> DBView toDBView(DynOExtractor<T> extractor, String itemObjectColumn, Iterable<T> items)
      Converts a collection of DynO objects into a new DBView with an optional reference column. If itemObjectColumn is not null, each row in the DBView will also include a column with that name containing a reference to the original DynO object.
      Parameters:
      extractor - the DynOExtractor used to convert the DynO objects into Map
      itemObjectColumn - the name of the column to store references to the original DynO objects, or null if not needed
      items - the collection of DynO objects to convert to a DBView
      Returns:
      a new DBView containing the data from the DynO objects
    • toDBView

      public static DBView toDBView(Collection<? extends Map<String,Object>> items)
      Converts a collection of maps into a new DBView. Each map in the collection is added as a row in the DBView, with the map keys corresponding to column names and map values corresponding to cell values.
      Parameters:
      items - the collection of maps to convert to a DBView
      Returns:
      a new DBView containing the data from the maps
    • toDBView

      public static DBView toDBView(Iterable<? extends DynO> items, String tableName)
      Converts a collection of DynO objects into a new DBView with columns from a database table. System fields (those starting with "__" or named DATASTAMP, LOGIN, or ACTION) are excluded.
      Parameters:
      items - the collection of DynO objects to convert to a DBView
      tableName - the name of the database table whose fields should be used as columns
      Returns:
      a new DBView containing the data from the DynO objects with columns from the table
    • toList

      public static List<HashGetter> toList(DBView dbv)
      Converts a DBView into a list of HashGetter objects. The resulting list preserves the order of rows from the original DBView.
      Parameters:
      dbv - the DBView to convert
      Returns:
      a list of HashGetter objects, each representing a row from the DBView
    • toList

      public static <T extends DynO> List<T> toList(DBView dbv, Class<T> clazz)
      Converts a DBView into a list of DynO objects of the specified class.
      Type Parameters:
      T - the type of DynO objects to create
      Parameters:
      dbv - the DBView to convert
      clazz - the class of DynO objects to instantiate
      Returns:
      a list of DynO objects of the specified class, each representing a row from the DBView
    • toList

      public static <T extends DynO> List<T> toList(DBView dbv, DynOCreator<T> creator)
      Converts a DBView into a list of DynO objects using a custom creator.
      Type Parameters:
      T - the type of DynO objects to create
      Parameters:
      dbv - the DBView to convert
      creator - the DynOCreator to use for instantiating objects
      Returns:
      a list of DynO objects created by the specified creator, each representing a row from the DBView
    • extractObjects

      public static <T> List<T> extractObjects(DBView dbv, String itemObjectColumn)
      Extracts values from a specific column of a DBView into a typed list.
      Type Parameters:
      T - the target type of the extracted values
      Parameters:
      dbv - the DBView containing the data
      itemObjectColumn - the name of the column to extract values from
      Returns:
      a list of values extracted from the specified column, cast to type T
    • toMap

      public static <T extends DynO> Map<Long,T> toMap(DBView dbv, Class<T> clazz, Function<? super T,Long> keyMapper)
      Converts a DBView into a map of DynO objects keyed by a derived value.
      Type Parameters:
      T - the type of DynO objects to create
      Parameters:
      dbv - the DBView to convert
      clazz - the class of DynO objects to instantiate
      keyMapper - a function that extracts the key from each DynO object
      Returns:
      a map of DynO objects keyed by the value extracted by the key mapper
    • toPageResponse

      public static <T extends DynO> PageResponse<T> toPageResponse(DBView in, DynOCreator<T> creator, int pageNumber)
      Converts a DBView into a PageResponse with pagination information.
      Type Parameters:
      T - the type of DynO objects to create
      Parameters:
      in - the DBView to convert
      creator - the DynOCreator to use for instantiating objects
      pageNumber - the current page number (1-based)
      Returns:
      a PageResponse containing DynO objects and pagination information
    • mapCompare

      public static boolean mapCompare(Map<String,Object> firstMap, Map<String,Object> secondMap, String[] excludedFields)
      Compares two maps to check if they have different values, excluding specified fields. This method returns true if any value in the second map (except for excluded fields) is different from the corresponding value in the first map. In addition to the specified excluded fields, this method always excludes system fields (LOGIN, ACTION, and DATASTAMP) from the comparison.
      Parameters:
      firstMap - the first map to compare
      secondMap - the second map to compare against the first
      excludedFields - an array of field names to exclude from the comparison
      Returns:
      true if the maps have different values for any common key (except excluded fields), false if all common keys have the same values
    • mapCompare

      public static boolean mapCompare(Map<String,Object> firstMap, Map<String,Object> secondMap)
      Compares two maps to check if they have different values for common keys. This method returns true if any value in the second map is different from the corresponding value in the first map.
      Parameters:
      firstMap - the first map to compare
      secondMap - the second map to compare against the first
      Returns:
      true if the maps have different values for any common key, false if all common keys have the same values
    • hgCompare

      public static boolean hgCompare(HashGetter hgBefore, HashGetter hg, String[] excludedFields)
      Compares two HashGetter objects to check if they have different values, excluding specified fields. It returns true if any value in the second HashGetter (except for excluded fields) is different from the corresponding value in the first HashGetter.
      Parameters:
      hgBefore - the first HashGetter to compare
      hg - the second HashGetter to compare against the first
      excludedFields - an array of field names to exclude from the comparison
      Returns:
      true if the HashGetters have different values for any common key (except excluded fields), false if all common keys have the same values
    • getChangedKeys

      public static List<String> getChangedKeys(HashGetter hgBefore, HashGetter hg, String[] excludedFields)
      Identifies which keys have different values between two HashGetter objects. It returns a list of keys where the values differ between the two objects.
      Parameters:
      hgBefore - the first HashGetter to compare, can be null
      hg - the second HashGetter to compare against the first
      excludedFields - an array of field names to exclude from the comparison
      Returns:
      a list of keys where the values differ between the two HashGetter objects
    • removeRowsInValues

      public static DBView removeRowsInValues(DBView dbv, String fieldName, Collection<?> values)
      Creates a new DBView with only the rows where the value of the specified field is among the given values.
      Parameters:
      dbv - the starting DBView
      fieldName - the name of the field to use for comparisons
      values - the values to include
      Returns:
      a new filtered DBView
    • removeRowsNotInValues

      public static DBView removeRowsNotInValues(DBView dbv, String fieldName, Collection<?> values)
      Creates a new DBView with only the rows where the value of the specified field is NOT among the given values.
      Parameters:
      dbv - the starting DBView
      fieldName - the name of the field to use for comparisons
      values - the values to exclude
      Returns:
      a new filtered DBView