org.jpox.store.rdbms.scostore
Class JoinListStore

java.lang.Object
  extended byorg.jpox.store.rdbms.scostore.BaseContainerStore
      extended byorg.jpox.store.rdbms.scostore.ElementContainerStore
          extended byorg.jpox.store.rdbms.scostore.AbstractCollectionStore
              extended byorg.jpox.store.rdbms.scostore.AbstractListStore
                  extended byorg.jpox.store.rdbms.scostore.JoinListStore
All Implemented Interfaces:
CollectionStore, ListStore, Store

public class JoinListStore
extends org.jpox.store.rdbms.scostore.AbstractListStore

Representation of the backing store for a Normal List. Uses a Join table, so we use 3 tables - owner table, join table and element table.

Version:
$Revision: 1.5 $

Nested Class Summary
 
Nested classes inherited from class org.jpox.store.rdbms.scostore.ElementContainerStore
ElementContainerStore.ElementInfo
 
Field Summary
protected  java.lang.String addStmt
           
protected  java.lang.String indexOfStmt
           
protected  java.lang.String lastIndexOfStmt
           
protected  java.lang.String listName
           
protected  java.lang.String removeAtStmt
           
protected  java.lang.String setStmt
           
protected  java.lang.String shiftStmt
           
 
Fields inherited from class org.jpox.store.rdbms.scostore.AbstractCollectionStore
containsStmt
 
Fields inherited from class org.jpox.store.rdbms.scostore.ElementContainerStore
clearStmt, clr, containerTable, dba, elementInfo, elementMapping, elementsAreEmbedded, elementsAreSerialised, elementType, elmIdentifier, emd, iterateUsingDiscriminator, LOCALISER, orderMapping, ownerFieldMetaData, ownerMapping, relationDiscriminatorMapping, relationDiscriminatorValue, sizeStmt, storeMgr, thisIdentifier
 
Constructor Summary
JoinListStore(AbstractPropertyMetaData fmd, CollectionTable joinTable, ClassLoaderResolver clr)
          Constructor.
 
Method Summary
 void add(StateManager sm, int index, java.lang.Object element)
          Method to add an element to the List.
 boolean add(StateManager sm, java.lang.Object element)
          Method to add an element to the List.
 boolean addAll(StateManager sm, java.util.Collection elements)
          Method to add a collection of elements to the List.
 boolean addAll(StateManager sm, java.util.Collection c, boolean initialising)
          Method to add all elements from a Collection to the List.
 boolean addAll(StateManager sm, int index, java.util.Collection c)
          Method to add all elements from a Collection to the List.
 java.lang.Object get(StateManager sm, int index)
          Method to retrieve an element from the List.
protected  java.lang.String getIndexOfStmt()
          Generate statement for getting the index of an item.
protected  java.lang.String getIndicesOfStmt(java.util.Collection elements)
          Generates the statement for getting the indices of a collection of element.
protected  QueryExpression getIteratorStatement(StateManager ownerSM, int start_index, int end_index)
          Accessor for the iterator statement to retrieve element(s) in a range from the List.
protected  java.lang.String getLastIndexOfStmt()
          Generates the statement for getting the index of the last item.
protected  java.lang.String getRemoveAtStmt()
          Generates the statement for removing an item.
protected  java.lang.String getSetStmt()
          Generates the statement for setting an item.
protected  java.lang.String getShiftStmt()
          Generates the statement for shifting items.
 int indexOf(StateManager sm, java.lang.Object element)
          Accessor for the indexOf an object in the List.
protected  void initialiseStatements()
          Method to initialise the statements being used.
protected  boolean internalAdd(StateManager sm, int start, boolean atEnd, java.util.Collection c, boolean initialising)
          Internal method to add element(s) to the List.
protected  java.util.List internalGetRange(StateManager sm, int start, int end)
          Utility to retrieve (as a List) the elements for the List in a range depending on the specified start/end positions.
protected  void internalRemoveAt(StateManager sm, int index, java.lang.String stmt)
          Internal method to remove an object at a location in the List.
 java.util.Iterator iterator(StateManager sm)
          Accessor for an iterator through the list elements.
 ScalarExpression joinElementsTo(QueryExpression stmt, QueryExpression parentStmt, JavaTypeMapping ownerMapping, LogicSetExpression ownerTe, DatastoreIdentifier listRangeVar, java.lang.Class filteredElementType, ScalarExpression elmExpr, DatastoreIdentifier elementRangeVar)
          Method used in queries when contains() has been invoked.
 int lastIndexOf(StateManager sm, java.lang.Object element)
          Method to retrieve the last index of an object in the list.
 java.util.ListIterator listIterator(StateManager sm)
          Accessor for an iterator through the list elements.
 java.util.ListIterator listIterator(StateManager ownerSM, int start)
          Accessor for an iterator through the list elements.
 QueryExpression newQueryStatement(StateManager sm, java.lang.String candidateClass)
          Utility to return a new QueryStatement.
protected  void prepareIndicesOfStmt(StateManager sm, java.sql.PreparedStatement ps, java.util.Collection elements)
          Method to prepare the indicesOf statement for use.
 java.lang.Object remove(StateManager sm, int index)
          Method to remove an object at a location in the List.
 boolean remove(StateManager sm, java.lang.Object element)
          Method to remove an element from the List.
 boolean removeAll(StateManager sm, java.util.Collection elements)
          Remove all elements from a collection from the association owner vs elements.
protected  void removeAt(StateManager sm, int index)
          Method to remove an element from the specified position
 java.lang.Object set(StateManager sm, int index, java.lang.Object element, boolean allowDependentField)
          Method to set an object in the List.
 java.util.List subList(StateManager sm, int start, int end)
          Method to retrieve a list of elements in a range.
 
Methods inherited from class org.jpox.store.rdbms.scostore.AbstractCollectionStore
contains, getContainsStmt, getExistsSubquery, getSizeSubquery, getUpdateEmbeddedElementStmt, updateEmbeddedElement
 
Methods inherited from class org.jpox.store.rdbms.scostore.ElementContainerStore
clear, executeQuery, executeUpdate, getAddStmt, getClearStmt, getElementInformationForClass, getElementType, getOwnerMapping, getSizeStmt, getStateManagerForEmbeddedPCObject, getStoreManager, hasOrderMapping, isEmbeddedMapping, newResultObjectFactory, populateElementDiscriminatorInStatement, populateElementInStatement, populateEmbeddedElementFieldsInStatement, populateOrderInStatement, populateOwnerInStatement, populateRelationDiscriminatorInStatement, size, validateElementForReading, validateElementForWriting, validateElementType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jpox.store.scostore.CollectionStore
clear, contains, getElementType, getExistsSubquery, getSizeSubquery, hasOrderMapping, newResultObjectFactory, size, updateEmbeddedElement
 
Methods inherited from interface org.jpox.store.scostore.Store
getOwnerMapping, getStoreManager
 

Field Detail

listName

protected java.lang.String listName

addStmt

protected java.lang.String addStmt

removeAtStmt

protected java.lang.String removeAtStmt

setStmt

protected java.lang.String setStmt

shiftStmt

protected java.lang.String shiftStmt

indexOfStmt

protected java.lang.String indexOfStmt

lastIndexOfStmt

protected java.lang.String lastIndexOfStmt
Constructor Detail

JoinListStore

public JoinListStore(AbstractPropertyMetaData fmd,
                     CollectionTable joinTable,
                     ClassLoaderResolver clr)
Constructor.

Parameters:
joinTable - The join table
clr - ClassLoader resolver
Method Detail

getIteratorStatement

protected QueryExpression getIteratorStatement(StateManager ownerSM,
                                               int start_index,
                                               int end_index)
Accessor for the iterator statement to retrieve element(s) in a range from the List. This has 5 modes

Parameters:
ownerSM - The StateManager
start_index - The start position in the List.
end_index - The end position in the List.
Returns:
The QueryStatement.

newQueryStatement

public QueryExpression newQueryStatement(StateManager sm,
                                         java.lang.String candidateClass)
Utility to return a new QueryStatement.

Parameters:
sm - The state manager
candidateClass - The base class
Returns:
The QueryStatement

joinElementsTo

public ScalarExpression joinElementsTo(QueryExpression stmt,
                                       QueryExpression parentStmt,
                                       JavaTypeMapping ownerMapping,
                                       LogicSetExpression ownerTe,
                                       DatastoreIdentifier listRangeVar,
                                       java.lang.Class filteredElementType,
                                       ScalarExpression elmExpr,
                                       DatastoreIdentifier elementRangeVar)
Method used in queries when contains() has been invoked.

Parameters:
stmt - The Query Statement
parentStmt - the parent Query Statement. If there is no parent, parentStmt must be equals to stmt
ownerMapping - the mapping for the owner.
ownerTe - Table Expression for the owner
listRangeVar - The range variable for the "List" table.
filteredElementType - The Class Type for the filtered element
elmExpr - The Expression for the element
elementRangeVar - The SQL alias, or "range variable", to assign to the expression or to the element table.
Returns:
expression to the join

initialiseStatements

protected void initialiseStatements()
Method to initialise the statements being used.

Overrides:
initialiseStatements in class AbstractCollectionStore

iterator

public java.util.Iterator iterator(StateManager sm)
Accessor for an iterator through the list elements.

Specified by:
iterator in interface CollectionStore
Specified by:
iterator in class ElementContainerStore
Parameters:
sm - State Manager for the container.
Returns:
The Iterator

listIterator

public java.util.ListIterator listIterator(StateManager sm)
Accessor for an iterator through the list elements.

Specified by:
listIterator in interface ListStore
Parameters:
sm - State Manager for the container.
Returns:
The List Iterator

listIterator

public java.util.ListIterator listIterator(StateManager ownerSM,
                                           int start)
Accessor for an iterator through the list elements.

Specified by:
listIterator in interface ListStore
Parameters:
ownerSM - State Manager for the container.
start - The start point in the list.
Returns:
The List Iterator

add

public boolean add(StateManager sm,
                   java.lang.Object element)
Method to add an element to the List.

Specified by:
add in interface CollectionStore
Parameters:
sm - The state manager
element - The element to remove
Returns:
Whether it was added successfully.

add

public void add(StateManager sm,
                int index,
                java.lang.Object element)
Method to add an element to the List.

Specified by:
add in interface ListStore
Parameters:
sm - The state manager.
index - The location to add at
element - The element to add.

addAll

public boolean addAll(StateManager sm,
                      java.util.Collection elements)
Method to add a collection of elements to the List.

Specified by:
addAll in interface CollectionStore
Parameters:
sm - The state manager
elements - The elements to remove
Returns:
Whether they were added successfully.

addAll

public boolean addAll(StateManager sm,
                      int index,
                      java.util.Collection c)
Method to add all elements from a Collection to the List.

Specified by:
addAll in interface ListStore
Parameters:
sm - The state manager
index - The location to add at
c - The collection
Returns:
Whether it was successful

addAll

public boolean addAll(StateManager sm,
                      java.util.Collection c,
                      boolean initialising)
Method to add all elements from a Collection to the List.

Specified by:
addAll in interface ListStore
Parameters:
sm - The state manager
c - The collection
initialising - Whether we are initialising the list with these elements
Returns:
Whether it was successful

get

public java.lang.Object get(StateManager sm,
                            int index)
Method to retrieve an element from the List.

Specified by:
get in interface ListStore
Parameters:
sm - The state manager
index - The location of the element.
Returns:
The object

indexOf

public int indexOf(StateManager sm,
                   java.lang.Object element)
Accessor for the indexOf an object in the List.

Specified by:
indexOf in interface ListStore
Parameters:
sm - The state manager
element - The element.
Returns:
The index

lastIndexOf

public int lastIndexOf(StateManager sm,
                       java.lang.Object element)
Method to retrieve the last index of an object in the list.

Specified by:
lastIndexOf in interface ListStore
Parameters:
sm - The state manager.
element - The object
Returns:
The last index

remove

public boolean remove(StateManager sm,
                      java.lang.Object element)
Method to remove an element from the List.

Specified by:
remove in interface CollectionStore
Parameters:
sm - The state manager
element - The element to remove.
Returns:
Whether it was removed successfully.

removeAll

public boolean removeAll(StateManager sm,
                         java.util.Collection elements)
Remove all elements from a collection from the association owner vs elements. TODO : Change the query to do all in one go for efficiency. Currently removes an element and shuffles the indexes, then removes an element and shuffles the indexes, then removes an element and shuffles the indexes etc ... a bit inefficient !!!

Specified by:
removeAll in interface CollectionStore
Parameters:
sm - State Manager for the container
elements - Collection of elements to remove
Returns:
Whether the database was updated

remove

public java.lang.Object remove(StateManager sm,
                               int index)
Method to remove an object at a location in the List.

Specified by:
remove in interface ListStore
Parameters:
sm - The state manager.
index - The location
Returns:
The object that was removed.

set

public java.lang.Object set(StateManager sm,
                            int index,
                            java.lang.Object element,
                            boolean allowDependentField)
Method to set an object in the List.

Specified by:
set in interface ListStore
Parameters:
sm - The state manager
index - The item index
element - What to set it to.
allowDependentField - Whether to allow dependent field deletes
Returns:
The value before setting.

subList

public java.util.List subList(StateManager sm,
                              int start,
                              int end)
Method to retrieve a list of elements in a range.

Specified by:
subList in interface ListStore
Parameters:
sm - The state manager.
start - From index (inclusive)
end - To index (exclusive)
Returns:
Sub List of elements in this range.

getIndexOfStmt

protected java.lang.String getIndexOfStmt()
Generate statement for getting the index of an item.
 SELECT INDEXCOL FROM LISTTABLE
 WHERE OWNERCOL=? 
 AND ELEMENTCOL=? 
 [AND EMBEDDEDFIELD1=? AND EMBEDDEDFIELD2=? AND ...]
 [AND DISTINGUISHER=?]
 ORDER BY INDEXCOL
 

Returns:
The Statement for getting the index of an item

getLastIndexOfStmt

protected java.lang.String getLastIndexOfStmt()
Generates the statement for getting the index of the last item.
 
 SELECT INDEXCOL FROM LISTTABLE 
 WHERE OWNERCOL=? 
 AND ELEMENTCOL=?
 [AND EMBEDDEDFIELD1=? AND EMBEDDEDFIELD2=? AND ...]
 [AND DISTINGUISHER=?]
 ORDER BY INDEXCOL DESC
 

Returns:
The Statement for getting the last item

getRemoveAtStmt

protected java.lang.String getRemoveAtStmt()
Generates the statement for removing an item.
 
 DELETE FROM LISTTABLE
 WHERE OWNERCOL = ? 
 AND INDEXCOL = ?
 [AND DISTINGUISHER=?]
 

Returns:
The Statement for removing an item from a position

getSetStmt

protected java.lang.String getSetStmt()
Generates the statement for setting an item.
 UPDATE LISTTABLE SET [ELEMENTCOL = ?]
 [EMBEDDEDFIELD1=?, EMBEDDEDFIELD2=?, ...]
 WHERE OWNERCOL = ? 
 AND INDEXCOL = ?
 [AND DISTINGUISHER=?]
 

Returns:
The Statement for setting an item

getShiftStmt

protected java.lang.String getShiftStmt()
Generates the statement for shifting items.
 
 UPDATE LISTTABLE SET ELEMENTCOL = ? 
 WHERE OWNERCOL = ? 
 AND INDEXCOL = ?
 [AND DISTINGUISHER=?]
 

Returns:
The Statement for shifting elements

getIndicesOfStmt

protected java.lang.String getIndicesOfStmt(java.util.Collection elements)
Generates the statement for getting the indices of a collection of element. Order into descending index order (highest first) so they will NOT be in the same order as they appear in the input collection "elements".
 SELECT INDEXCOL FROM LISTTABLE 
 WHERE (OWNERCOL=? AND ELEMENT_COL=? [AND DISTINGUISHER=?]) OR
       (OWNERCOL=? AND ELEMENT_COL=? [AND DISTINGUISHER=?]) OR
       (OWNERCOL=? AND ELEMENT_COL=? [AND DISTINGUISHER=?]) 
 ORDER BY INDEXCOL DESC
 

Parameters:
elements - The elements to retrieve the indices for.
Returns:
The Statement for getting the indices of the collection.

prepareIndicesOfStmt

protected void prepareIndicesOfStmt(StateManager sm,
                                    java.sql.PreparedStatement ps,
                                    java.util.Collection elements)
Method to prepare the indicesOf statement for use. Populates the various parameters. This is required because the query is built dynamically depending on the number of elements to retrieve the indices for.

Parameters:
sm - State Manager of the container.
ps - The Prepared Statement
elements - Collection of elements

removeAt

protected void removeAt(StateManager sm,
                        int index)
Method to remove an element from the specified position

Parameters:
sm - The State Manager for the list
index - The index of the element

internalRemoveAt

protected void internalRemoveAt(StateManager sm,
                                int index,
                                java.lang.String stmt)
Internal method to remove an object at a location in the List.

Parameters:
sm - The state manager.
index - The location
stmt - The statement to remove the element from the List

internalAdd

protected boolean internalAdd(StateManager sm,
                              int start,
                              boolean atEnd,
                              java.util.Collection c,
                              boolean initialising)
Internal method to add element(s) to the List.

Parameters:
sm - The state manager
start - The start location (if required)
atEnd - Whether to add the element at the end
c - The collection of objects to add.
initialising - Whether we are initialising the list with these elements
Returns:
Whether it was successful

internalGetRange

protected java.util.List internalGetRange(StateManager sm,
                                          int start,
                                          int end)
Utility to retrieve (as a List) the elements for the List in a range depending on the specified start/end positions.

Parameters:
sm - State Manager for the container
start - Start position in the List.
end - End position in the List.
Returns:
The List of elements matching the range specification.


Copyright © -2007 . All Rights Reserved.