*/
protected QueryExpression getIteratorStatement(StateManager ownerSM)
{
final ClassLoaderResolver clr = ownerSM.getObjectManager().getClassLoaderResolver();
final Class elementCls = clr.classForName(elementType);
QueryExpression stmt = null;
if (emd != null &&
emd.getDiscriminatorStrategy() != null &&
emd.getDiscriminatorStrategy() != DiscriminatorStrategy.NONE &&
containerTable instanceof DatastoreClass)
{
// Inverse Map where Key is PC and uses discriminator
if (storeMgr.getOMFContext().getTypeManager().isReferenceType(clr.classForName(elementType)))
{
// Take the metadata for the first implementation of the reference type
String[] clsNames = storeMgr.getOMFContext().getMetaDataManager().getClassesImplementingInterface(elementType, clr);
Class[] cls = new Class[clsNames.length];
for (int j=0; j<clsNames.length; j++)
{
cls[j] = clr.classForName(clsNames[j]);
}
stmt = new DiscriminatorIteratorStatement(clr,
cls, true, this.storeMgr, true).getQueryStatement(null);
}
else
{
stmt = new DiscriminatorIteratorStatement(clr, new Class[] {elementCls},
true, this.storeMgr, true).getQueryStatement(null);
}
iterateUsingDiscriminator = true;
// Add inner join to the container table
DatastoreClass sourceTable = storeMgr.getDatastoreClass(elementType, clr);
ScalarExpression sourceExpr = sourceTable.getIDMapping().newScalarExpression(stmt, stmt.getMainTableExpression());
LogicSetExpression teTargetElement = stmt.newTableExpression(containerTable, valueIdentifier);
ScalarExpression targetExpr = elementMapping.newScalarExpression(stmt, teTargetElement);
stmt.innerJoin(sourceExpr, targetExpr, teTargetElement, true);
}
else
{
// Join to key table if this is join table, or value table
boolean sourceJoin = (mapType == MAP_TYPE_JOIN || mapType == MAP_TYPE_KEY_IN_VALUE);
stmt = new UnionIteratorStatement(clr, elementCls, true, this.storeMgr,
elementCls, elementMapping, containerTable, sourceJoin, null,
true, false).getQueryStatement(null);
}
// Apply condition on owner field to filter by owner
if (containerTable instanceof SCOTable || storeMgr.getOMFContext().getTypeManager().isSupportedType(elementType) ||
mapType == MAP_TYPE_VALUE_IN_KEY)
{
// SCO cases/primitiveKeys just provide the owner id on their table
ScalarExpression ownerExpr = ownerMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
ScalarExpression ownerVal = ownerMapping.newLiteral(stmt, ownerSM.getObject());
stmt.andCondition(ownerExpr.eq(ownerVal), true);
}
else
{
// FCO cases/non-primitiveKeys provide the owner id on the value table
ScalarExpression ownerExpr = ownerMapping.newScalarExpression(stmt, stmt.getTableExpression(valueIdentifier));
ScalarExpression ownerVal = ownerMapping.newLiteral(stmt, ownerSM.getObject());
stmt.andCondition(ownerExpr.eq(ownerVal), true);
}
return stmt;
}