org.jpox.store.rdbms.scostore
Class FKListStore

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.FKListStore
All Implemented Interfaces:
CollectionStore, ListStore, Store

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

Representation of an Inverse List as part of a relationship. This class is used where you have a 1-N and the tables are not joined via a join table. There is an owner table and an element table, and the element table has a column being the id of the owner table. This is in contrast to NormalListStore which represents 1-N relationships using a join table. There are 2 possible uses here

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
FKListStore(AbstractPropertyMetaData fmd, RDBMSManager storeMgr, 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.
 void clear(StateManager ownerSM)
          Method to clear the List.
 java.lang.Object get(StateManager sm, int index)
          Method to retrieve an element from the List.
protected  java.lang.String getClearNullifyStmt()
          Generates the statement for clearing items by nulling the owner link out.
protected  java.lang.String getClearStmt()
          Generate statement for clearing the container.
protected  java.lang.String getContainsStmt()
          Generate statement for retrieving the contents of the Collection.
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 getRemoveAtNullifyStmt()
          Generates the statement for removing an item by nulling it out.
protected  java.lang.String getRemoveAtStmt()
          Generates the statement for removing an item.
protected  java.lang.String getSetStmt()
          Generates the statement for setting an item to be at a position.
protected  java.lang.String getShiftStmt()
          Generates the statement for shifting items.
protected  java.lang.String getSizeStmt()
          Generate statement for getting the size of the container.
protected  java.lang.String getUnsetStmt()
          Generates the statement for unsetting an item from a list position.
 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 startAt, boolean atEnd, java.util.Collection c, boolean initialising)
          Internal method for adding an item 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 method to return a new QueryStatement for retrieval of the elements of this List.
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)
          Internal method to remove an object at a location in the List.
 java.lang.Object set(StateManager sm, int index, java.lang.Object element, boolean allowDependentField)
          Method to set an object in the List at a position.
 java.util.List subList(StateManager sm, int start, int end)
          Method to retrieve a list of elements in a range.
protected  void validateElementForWriting(StateManager sm, java.lang.Object element)
          Method to validate that an element is valid for writing to the datastore.
 
Methods inherited from class org.jpox.store.rdbms.scostore.AbstractCollectionStore
contains, getExistsSubquery, getSizeSubquery, getUpdateEmbeddedElementStmt, updateEmbeddedElement
 
Methods inherited from class org.jpox.store.rdbms.scostore.ElementContainerStore
executeQuery, executeUpdate, getAddStmt, getElementInformationForClass, getElementType, getOwnerMapping, getStateManagerForEmbeddedPCObject, getStoreManager, hasOrderMapping, isEmbeddedMapping, newResultObjectFactory, populateElementDiscriminatorInStatement, populateElementInStatement, populateEmbeddedElementFieldsInStatement, populateOrderInStatement, populateOwnerInStatement, populateRelationDiscriminatorInStatement, size, validateElementForReading, 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
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

FKListStore

public FKListStore(AbstractPropertyMetaData fmd,
                   RDBMSManager storeMgr,
                   ClassLoaderResolver clr)
Constructor.

Parameters:
fmd - Field MetaData for the field that this represents
storeMgr - The Store Manager in use
clr - The ClassLoaderResolver
Method Detail

getSetStmt

protected java.lang.String getSetStmt()
Generates the statement for setting an item to be at a position.
 UPDATE LISTTABLE SET OWNERCOL=?, INDEXCOL = ? [,DISTINGUISHER=?]
 WHERE ELEMENTCOL = ?
 

Returns:
The Statement for setting an item

getUnsetStmt

protected java.lang.String getUnsetStmt()
Generates the statement for unsetting an item from a list position.
 
 UPDATE LISTTABLE SET OWNERCOL=NULL, INDEXCOL = NULL [, DISTINGUISHER = NULL]
 WHERE OWNERCOL = ? AND INDEXCOL = ? [AND DISTINGUISHER = ?]
 

Returns:
The Statement for unsetting an item

set

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

Parameters:
sm - The state manager
index - The item index
element - What to set it to.
allowDependentField - Whether to enable dependent-field deletes during the set
Returns:
The value before setting.

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.

internalAdd

protected boolean internalAdd(StateManager sm,
                              int startAt,
                              boolean atEnd,
                              java.util.Collection c,
                              boolean initialising)
Internal method for adding an item to the List.

Parameters:
sm - The state manager
startAt - The start position
atEnd - Whether to add at the end
c - The Collection of elements to add.
initialising - Whether we are initialising the list with these elements
Returns:
Whether it was successful

getRemoveAtNullifyStmt

protected java.lang.String getRemoveAtNullifyStmt()
Generates the statement for removing an item by nulling it out. When there is only a single element table the statement will be
 UPDATE LISTTABLE SET OWNERCOL = NULL, INDEXCOL = NULL
 WHERE OWNERCOL = ?
 AND INDEXCOL = ?
 [AND DISTINGUISHER = ?]
 
and when there are multiple element tables the statement will be
 UPDATE ? SET OWNERCOL=NULL, INDEXCOL=NULL
 WHERE OWNERCOL=?
 AND INDEXCOL=?
 [AND DISTINGUISHER = ?]
 

Returns:
The Statement for removing an item from a position

remove

public boolean remove(StateManager sm,
                      java.lang.Object element)
Method to remove an element from the List. Differs from the JoinTable list in that duplicate positions are not possible for an element so it has a simpler job to remove it.

Parameters:
sm - The state manager
element - The element to remove.
Returns:
Whether it was removed successfully.

removeAt

protected void removeAt(StateManager sm,
                        int index)
Internal method to remove an object at a location in the List. Differs from the Normal List in that it nulls out the owner FK.

Parameters:
sm - The state manager.
index - The location

getClearNullifyStmt

protected java.lang.String getClearNullifyStmt()
Generates the statement for clearing items by nulling the owner link out. The statement will be
 UPDATE LISTTABLE SET OWNERCOL=NULL, INDEXCOL=NULL [,DISTINGUISHER=NULL]
 WHERE OWNERCOL=? [AND DISTINGUISHER=?]
 
when there is only one element table, and will be
 UPDATE ? SET OWNERCOL=NULL, INDEXCOL=NULL [,DISTINGUISHER=NULL]
 WHERE OWNERCOL=? [AND DISTINGUISHER=?]
 
when there is more than 1 element table.

Returns:
The Statement for clearing items for the owner.

getSizeStmt

protected java.lang.String getSizeStmt()
Generate statement for getting the size of the container. The order part is only present when an order mapping is used. The discriminator part is only present when the element type has inheritance strategy of "superclass-table" and is Inverse.
 SELECT COUNT(*) FROM CONTAINERTABLE
 WHERE OWNERCOL=?
 [AND ORDERCOL IS NOT NULL]
 [AND (DISCRIMINATOR=? OR DISCRMINATOR=? OR DISCRIMINATOR=?)]
 
The discriminator part includes all subclasses of the element type

Overrides:
getSizeStmt in class ElementContainerStore
Returns:
The Statement returning the size of the container.

getContainsStmt

protected java.lang.String getContainsStmt()
Generate statement for retrieving the contents of the Collection. The discriminator part is only present when the element type has inheritance strategy of "superclass-table" and is Inverse.
 
 SELECT OWNERCOL FROM COLLECTIONTABLE 
 WHERE OWNERCOL=?
 AND ELEMENTCOL = ?
 [AND DISCRIMINATOR = ?]
 

Overrides:
getContainsStmt in class AbstractCollectionStore
Returns:
Statement for retrieving the contents of the Collection.

getClearStmt

protected java.lang.String getClearStmt()
Generate statement for clearing the container.
 DELETE FROM CONTAINERTABLE
 WHERE OWNERCOL = ?
 

Overrides:
getClearStmt in class ElementContainerStore
Returns:
Statement for clearing the container.

clear

public void clear(StateManager ownerSM)
Method to clear the List. This is called by the List.clear() method and also when the container object is being deleted and the elements are to be removed (maybe for dependent field).

Specified by:
clear in interface CollectionStore
Overrides:
clear in class ElementContainerStore
Parameters:
ownerSM - The state manager

validateElementForWriting

protected void validateElementForWriting(StateManager sm,
                                         java.lang.Object element)
Method to validate that an element is valid for writing to the datastore.

Overrides:
validateElementForWriting in class ElementContainerStore
Parameters:
sm - StateManager for the List
element - The element to validate

newQueryStatement

public QueryExpression newQueryStatement(StateManager sm,
                                         java.lang.String candidateClass)
Utility method to return a new QueryStatement for retrieval of the elements of this List.

Parameters:
sm - StateManager for this object
candidateClass - Class for the element end of the link.
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

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.

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

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

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

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.