if (candidateAlias != null)
{
candidateAliasId = storeMgr.getIdentifierFactory().newDatastoreContainerIdentifier(candidateAlias);
}
ClassLoaderResolver clr = ec.getClassLoaderResolver();
List<DatastoreClass> candidateTables = new ArrayList<DatastoreClass>();
if (cmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE)
{
DatastoreClass candidateTable = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
if (candidateTable != null)
{
candidateTables.add(candidateTable);
}
if (subclasses)
{
Set<String> subclassNames =
storeMgr.getSubClassesForClass(cmd.getFullClassName(), subclasses, clr);
if (subclassNames != null)
{
Iterator<String> subclassIter = subclassNames.iterator();
while (subclassIter.hasNext())
{
String subclassName = subclassIter.next();
DatastoreClass tbl = storeMgr.getDatastoreClass(subclassName, clr);
if (tbl != null)
{
candidateTables.add(tbl);
}
}
}
}
Iterator<DatastoreClass> iter = candidateTables.iterator();
int maxClassNameLength = cmd.getFullClassName().length();
while (iter.hasNext())
{
DatastoreClass cls = iter.next();
String className = cls.getType();
if (className.length() > maxClassNameLength)
{
maxClassNameLength = className.length();
}
}
iter = candidateTables.iterator();
while (iter.hasNext())
{
DatastoreClass cls = iter.next();
SQLStatement tblStmt =
new SQLStatement(parentStmt, storeMgr, cls, candidateAliasId, candidateTableGroupName);
tblStmt.setClassLoaderResolver(clr);
tblStmt.setCandidateClassName(cls.getType());
// Add SELECT of dummy column accessible as "NUCLEUS_TYPE" containing the classname
JavaTypeMapping m = storeMgr.getMappingManager().getMapping(String.class);
String nuctypeName = cls.getType();
if (maxClassNameLength > nuctypeName.length())
{
nuctypeName = StringUtils.leftAlignedPaddedString(nuctypeName, maxClassNameLength);
}
StringLiteral lit = new StringLiteral(tblStmt, m, nuctypeName, null);
tblStmt.select(lit, UnionStatementGenerator.NUC_TYPE_COLUMN);
if (stmt == null)
{
stmt = tblStmt;
}
else
{
stmt.union(tblStmt);
}
}
if (clsMapping != null)
{
clsMapping.setNucleusTypeColumnName(UnionStatementGenerator.NUC_TYPE_COLUMN);
}
}
else
{
// "new-table", "superclass-table", "subclass-table"
List<Class> candidateClasses = new ArrayList<Class>();
if (ClassUtils.isReferenceType(candidateCls))
{
// Persistent interface, so find all persistent implementations
String[] clsNames =
storeMgr.getNucleusContext().getMetaDataManager().getClassesImplementingInterface(candidateCls.getName(), clr);
for (int i=0;i<clsNames.length;i++)
{
Class cls = clr.classForName(clsNames[i]);
DatastoreClass table = storeMgr.getDatastoreClass(clsNames[i], clr);
candidateClasses.add(cls);
candidateTables.add(table);
AbstractClassMetaData implCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(cls, clr);
if (implCmd.getIdentityType() != cmd.getIdentityType())
{
throw new NucleusUserException("You are querying an interface (" + cmd.getFullClassName() + ") " +
"yet one of its implementations (" + implCmd.getFullClassName() + ") " +
" uses a different identity type!");
}
else if (cmd.getIdentityType() == IdentityType.APPLICATION)
{
if (cmd.getPKMemberPositions().length != implCmd.getPKMemberPositions().length)
{
throw new NucleusUserException("You are querying an interface (" + cmd.getFullClassName() + ") " +
"yet one of its implementations (" + implCmd.getFullClassName() + ") " +
" has a different number of PK members!");
}
}
}
}
else
{
DatastoreClass candidateTable = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
if (candidateTable != null)
{
// Candidate has own table
candidateClasses.add(candidateCls);
candidateTables.add(candidateTable);
}
else
{
// Candidate stored in subclass tables
AbstractClassMetaData[] cmds = storeMgr.getClassesManagingTableForClass(cmd, clr);
if (cmds != null && cmds.length > 0)
{
for (int i=0;i<cmds.length;i++)
{
DatastoreClass table = storeMgr.getDatastoreClass(cmds[i].getFullClassName(), clr);
Class cls = clr.classForName(cmds[i].getFullClassName());
candidateClasses.add(cls);
candidateTables.add(table);
}
}
else