Class DBView

java.lang.Object
overit.geocall.sql.DBView
All Implemented Interfaces:
Serializable, JsonSerializable, DBFieldMapper, DBObjectMapper
Direct Known Subclasses:
DBViewWithLikeFilterOnString, IndexedDBView

public class DBView extends Object implements DBObjectMapper, DBFieldMapper, Serializable, JsonSerializable
The class implements the DBView, the logical view of a portion of a database table usually obtained as a result of a query and maintained in memory, and all the methods to manage its content. A DBView object can be also created starting from a DBFields object or a list of strings used as columns name.
The DBView is the logical element whose values can be displayed using the two graphic interface components TableView and ServerTableView
See Also:
  • Field Details

    • INSERTED

      public static final String INSERTED
      Control field that defines that the record of the DBView is an inserted one
      See Also:
    • UPDATED

      public static final String UPDATED
      Control field that defines that the record of the DBView is an updated one
      See Also:
    • DELETED

      public static final String DELETED
      Control field that defines that the record of the DBView is a deleted one
      See Also:
    • POS_INSERTED

      public static final int POS_INSERTED
      Defines the position of the INSERTED control field
      See Also:
    • POS_UPDATED

      public static final int POS_UPDATED
      Defines the position of the UPDATED control field
      See Also:
    • POS_DELETED

      public static final int POS_DELETED
      Defines the position of the DELETED control field
      See Also:
    • FIRST_POS

      public static final int FIRST_POS
      Defines the numbers of the control fields
      See Also:
    • KEEP_ALL

      public static final int KEEP_ALL
      Defines the operation mode of the DBRowComparator. Used to keep all the equal rows.
      See Also:
    • KEEP_FIRST

      public static final int KEEP_FIRST
      Defines the operation mode of the DBRowComparator. Used to keep the first of two equal rows.
      See Also:
    • KEEP_LAST

      public static final int KEEP_LAST
      Defines the operation mode of the DBRowComparator. Used to keep the last of two equal rows.
      See Also:
    • _shift

      protected int _shift
    • noUndo

      protected boolean noUndo
    • roData

      protected boolean roData
    • roView

      protected boolean roView
    • _fieldmap

      protected DBFieldMap _fieldmap
      Generic map object between the fields of a table in memory and a server database table, and vice versa
    • _objectmap

      protected DBObjectMap _objectmap
      General map between Java objects in memory and SQL objects and vice versa
  • Constructor Details

    • DBView

      public DBView()
      Creates an editable DBView
    • DBView

      @Deprecated public DBView(boolean noUndo)
      Deprecated.
      Use DBView
      Parameters:
      noUndo - Boolean value indicating if the DBView should track the changes (updates), so that the undoChanges will throw an IllegalStateException.
    • DBView

      @Deprecated public DBView(Map m)
      Deprecated.
      Legacy mode. Please use DBView(DBFields). For example if you want to clone a DBView dbv just write new DBView(dbv.getDBFields());
      Build a new DBView with the columns as the keys of the passed Map.
      Parameters:
      m - The Map
    • DBView

      public DBView(DBFields dbf)
      Creates a DBView, starting from the DBFields object, which the readonly state that depends on the value passed as parameter
      Parameters:
      dbf - The DBFields (ordered collection of fields) object used to create the DBView
    • DBView

      public DBView(String... cols)
      Creates a DBView, starting from a string array that contains the columns names
      Parameters:
      cols - A variable number of strings to be used as columns names
    • DBView

      public DBView(Collection<String> cols)
      Creates a DBView, starting from a string set that contains the columns names
      Parameters:
      cols - A variable number of strings to be used as columns names
  • Method Details

    • newBaseFields

      public static DBFields newBaseFields()
      Creates a DBFields with the control fields (INSERTED, UPDATED, DELETED)
      Returns:
      A static instance of DBFields
    • setNoUndo

      public DBView setNoUndo()
      Sets this DBView to not track the changes (updates), so that the undoChanges will throw an IllegalStateException. The state is not reversible
      Returns:
      this DBView
    • isNoUndo

      public boolean isNoUndo()
      queries the no-undo state
      Returns:
      the no-undo state
    • isRoData

      public boolean isRoData()
      queries the read-only state
      Returns:
      the read-only state
    • setRoData

      public DBView setRoData()
      Sets this DBView to not be modified in content data. Operations like sorting or filtering are still possible. Every attempt to change the content of a DBView will throw an IllegalStateException The state is not reversible
      Returns:
      this DBView
    • isRoView

      public boolean isRoView()
      queries the read-only state
      Returns:
      the read-only state
    • setRoView

      public DBView setRoView()
      Sets this DBView to not be modified in view features like sorting or filtering. Data content will still be modifiable. Every attempt to change the DBView view (filtering or sorting) will throw an IllegalStateException The state is not reversible
      Returns:
      this DBView
    • setShift

      public void setShift(int missingHeadRows)
      Sets the shift for the DBView, i.e. the number of rows starting from head that must be not considered
      Parameters:
      missingHeadRows - The shift value
    • getShift

      public int getShift()
      Returns the shift value set to the DBView
      Returns:
      The shift value
    • hasMore

      public boolean hasMore()
      Return a boolean value indicating if there's more records to load from the DB.
      Returns:
      true if there's more record to load, false if all records has been loaded
    • addColumn

      public void addColumn(String name)
      Adds a column to the DBView
      Parameters:
      name - The name of the column to be added to the DBView. If there is already a column with the same name in the DBView, an RuntimeException will be raised
    • removeColumn

      public void removeColumn(String name)
      Removes a column from the DBView
      Parameters:
      name - The name of the column to be removed from the DBView. If there isn't a column with this name in the DBView, an RuntimeException will be raised
    • renameColumn

      public void renameColumn(String from, String to)
    • putAttribute

      public void putAttribute(String key, Object o)
      Puts an attribute in the attribute's map of the DBView
      Parameters:
      key - The key of the attribute, that will be used to retrieve the entry from the map
      o - The value of the attribute
    • removeAttribute

      public void removeAttribute(String key)
      Removes from the map of the attributes of the DBView the one defined by the key passed as parameter
      Parameters:
      key - The key of the attribute to remove
    • getAttribute

      public Object getAttribute(String key)
      Returns the attribute of the DBView defined by the key passed as parameter
      Parameters:
      key - The key of the desired attribute
      Returns:
      The value of the attribute that corresponds to the key, or null if in the attribute's map there isn't an entry with the desired key
    • getAttributeNames

      public Set<String> getAttributeNames()
    • matchesFilter

      protected boolean matchesFilter(ArrayList row)
    • matchesFilter

      public static boolean matchesFilter(DBFields fields, Map filter, ArrayList row)
      Returns true if matches the filter, false otherwise
      Parameters:
      fields - The fields of the DBFields
      filter - The map containing the filters
      row - The number that identifies the position of the row
      Returns:
      true if matches the filter, false otherwise
    • calcRows

      protected void calcRows()
    • applyFilterNotOrding

      protected void applyFilterNotOrding()
    • applyFilterOrding

      protected void applyFilterOrding()
    • orderRandom

      public void orderRandom()
      Orders randomly the rows of the DBView
    • setOrder

      public void setOrder(DBRowComparator comparator, int keepMode)
      Sets the DBRowComparator object, and use it to order the rows of the DBView
      Parameters:
      comparator - The DBRowComparator object used to compare the rows
      keepMode - One of KEEP_ALL, KEEP_FIRST, KEEP_LAST
    • getOrder

      public DBRowComparator getOrder()
      Returns the DBRowComparator used to compare the rows of the DBView
      Returns:
      The DBRowComparator used to compare the rows
    • setFilter

      public void setFilter(Map m)
      Sets a filter on the DBView
      Parameters:
      m - The map used as filter
    • getFilter

      public Map getFilter()
      Returns the map used as filter for the current DBView
      Returns:
      The map used as filter
    • addFilter

      public void addFilter(Map m)
      Adds new elements to the filter on the DBView
      Parameters:
      m - The map that must be added to the existent filter (if no filter exists, the map itself is used as new filter)
    • defaultFilter

      public void defaultFilter()
      Resets the user imposed filters
    • removeFilter

      public void removeFilter()
      Removes the map used as for the DBView
    • removeOrder

      public void removeOrder()
      Removes the DBRowComparator object used to compare the rows of the DBView
    • append

      public void append(ResultSet rs) throws SQLException
      Append the ResultSet to the DBView
      Parameters:
      rs - The ResultSet to append
      Throws:
      SQLException - If a database access error occurs, or this method is called on a closed result set, an SQLException is raised
    • cols

      public int cols()
      Returns the number of the columns of the DBView
      Returns:
      The number of the columns
    • rows

      public int rows()
      Returns the number of the rows of the DBView
      Returns:
      The number of the rows, calculated considering also the shift (see setShift(int))
    • empty

      public final void empty()
      Empties the DBView, resetting the rows, setting to 0 the shift (see setShift(int)) and applying the defaultFilter()
    • getRowList

      public ArrayList getRowList(int row)
      Returns The ArrayList that contains the values of the desired row
      Parameters:
      row - The number that identifies the position of the row
      Returns:
      The ArrayList of values of the row
    • getOriginalRowList

      public ArrayList getOriginalRowList(int row)
      Returns The ArrayList that contains the original values of the desired row
      Parameters:
      row - The number that identifies the position of the row
      Returns:
      The ArrayList original values of the row
    • getRowMap

      public HashGetter getRowMap(int row)
      Returns the HashGetter that defines the desired row
      Parameters:
      row - The number that identifies the position of the row
      Returns:
      The map with the association "name of the column" -> "value" that defines the desired row
    • getFieldMap

      @Deprecated public HashGetter getFieldMap()
      Deprecated.
      Returns:
      Returns the HashGetter with the associations key -> position
    • getDBFields

      public DBFields getDBFields()
      Returns a copy of the DBFields of the DBView
      Returns:
      A copy of the DBFields structure of the DBView
    • getDBFields

      public DBFields getDBFields(boolean dropMetadata)
      Returns a copy of the DBFields of the DBView in which the control fields can be removed or not
      Parameters:
      dropMetadata - true to remove the control fields (__i,__u,__d) and update the positions, false otherwise
      Returns:
      A copy of the DBFields structure of the DBView
    • get

      public Object get(int row, String field)
      Returns the object that corresponds to a field in a specific row
      Parameters:
      row - The number that identifies the position of the row in the DBView
      field - The name of the field
      Returns:
      The object that corresponds to the field in the row
    • set

      public void set(int row, String field, Object o)
      Sets an object to a field in a specific row
      Parameters:
      row - The number that identifies the position of the row in the DBView
      field - The name of the field
      o - The object to set
    • put

      public Object put(int row, String field, Object value)
      Puts an object to a field in a specific row
      Parameters:
      row - The number that identifies the position of the row in the DBView
      field - The name of the field
      value - The object to set
      Returns:
      The object previously at the specified position
    • getFieldPos

      public int getFieldPos(String field)
      Returns the position of the field referring to the DBFields of the DBView
      Parameters:
      field - The name of the field
      Returns:
      The position of the field, or -1 if the field does not exist
    • getOriginalMap

      public HashGetter getOriginalMap(int row)
      Returns The HashGetter that contains the original map of values of the desired row
      Parameters:
      row - The number that identifies the position of the row
      Returns:
      The map that contains the original values of the row
    • getRowMap

      protected HashGetter getRowMap(List l)
    • getRowMap

      protected static HashGetter getRowMap(List l, DBFields dbf)
    • delete

      public void delete(int row)
      Removes a row from the DBView
      Parameters:
      row - The number that identifies the position of the row to be removed from the DBView
    • insert

      public void insert(Map<String,? extends Object> m)
      Insert a row as the last row of the DBView
      Parameters:
      m - The map that defines the row that must be inserted
    • insert

      public void insert(int pos, Map<String,? extends Object> m)
      Insert a row into a specific position of the DBView
      Parameters:
      pos - The position in which we want to insert the row (null to insert as the last row)
      m - The map that defines the row that must be inserted
    • append

      public void append(ArrayList l)
      Append a row as the last row of the DBView
      Parameters:
      l - The ArrayList that defines the row to append
    • append0

      public void append0(ArrayList l)
      Append a row as the first row of the DBView
      Parameters:
      l - The ArrayList that defines the row to append
    • append

      public void append(DBView dbv)
      Append a DBView to the current one
      Parameters:
      dbv - The DBView to append
    • removeMapTags

      protected void removeMapTags(Map m)
    • doInsert

      protected void doInsert(Integer pos, Map<String,? extends Object> m)
    • update

      public void update(int pos, Map<String,? extends Object> m)
      Updates a row of the DBView
      Parameters:
      pos - The position of the row to update
      m - The map that contains the new values for the row
    • applyChanges

      public void applyChanges()
      Makes the changes (insertions, updates and deletions) effective
    • undoChanges

      public void undoChanges()
      Undoes the changes (insertions, updates and deletions). If the DBView is in readonly state, the changes cannot be canceled, and an RuntimeException is raised
    • getDBFieldMap

      public DBFieldMap getDBFieldMap()
      Returns the DBFieldMap
      Specified by:
      getDBFieldMap in interface DBFieldMapper
      Returns:
      The map of objects used in this instance
    • setDBFieldMap

      public void setDBFieldMap(DBFieldMap fieldMap)
      Sets the DBFieldMap
      Specified by:
      setDBFieldMap in interface DBFieldMapper
      Parameters:
      fieldMap - The map of objects used in this instance
    • haveDBFieldMap

      public boolean haveDBFieldMap()
      Checks the presence of a correct DBFieldMap
      Specified by:
      haveDBFieldMap in interface DBFieldMapper
      Returns:
      true if the instance contains a correct map of object (that is not null), false otherwise
    • getDBObjectMap

      public DBObjectMap getDBObjectMap()
      Returns the DBObjectMap contained in the instance
      Specified by:
      getDBObjectMap in interface DBObjectMapper
      Returns:
      The map of objects used in this instance
    • setDBObjectMap

      public void setDBObjectMap(DBObjectMap objectMap)
      Sets the DBObjectMap contained in the instance
      Specified by:
      setDBObjectMap in interface DBObjectMapper
      Parameters:
      objectMap - The map of objects used in this instance
    • haveDBObjectMap

      public boolean haveDBObjectMap()
      Checks the presence of a valid DBObjectMap contained in the instance. Implements the DBObjectMapper interface.
      Specified by:
      haveDBObjectMap in interface DBObjectMapper
      Returns:
      true if the instance contains a map of object that is not null, false otherwise
    • find

      public int find(String field, Object value)
      Search a specific value for a field of the DBView
      Parameters:
      field - The name of the column of the DBView in which perform the search
      value - The value searched
      Returns:
      The number of the first rows in which there is the value for the specific column, or -1 if the search is not successful
    • seek

      public int seek(String field, Object value, boolean exactFind)
      Search a specific object for a field of the DBView
      Parameters:
      field - The name of the column of the DBView in which perform the search
      value - The value searched
      exactFind - true if the object found must be equals to the searched one (see Comparable), false otherwise
      Returns:
      The number of the first rows in which there is the value for the specific column, or -1 if the search is not successful
    • toString

      public String toString()
      Returns the string representation of the DBView
      Overrides:
      toString in class Object
      Returns:
      The string representation of the DBView
    • resultingOperation

      public static String resultingOperation(HashGetter hCurrRow)
      Returns the string that defines the status of the operations for the specific HashGetter that defines a row of the DBView
      Parameters:
      hCurrRow - The HashGetter that defines the row
      Returns:
      One of INSERTED, UPDATED, DELETED or null if the status is not correct
    • getLongSet

      public LongSet getLongSet(String column)
      Creates a LongSet populating it with the values of a column of the DBView
      Parameters:
      column - Name of the column from which extract the values
      Returns:
      LongSet The LongSet that contains the extracted values