bExpr = expr.eq(new NullLiteral(qs));
}
else
{
MappedStoreManager storeMgr = qs.getStoreManager();
DatastoreAdapter dba = storeMgr.getDatastoreAdapter();
ClassLoaderResolver clr = qs.getClassLoaderResolver();
if (value instanceof OID)
{
// Object is an OID
JavaTypeMapping m = dba.getMapping(((OID)value).getKeyValue().getClass(), storeMgr, clr);
ScalarExpression oidExpr = m.newLiteral(qs,((OID)value).getKeyValue());
bExpr = expr.expressionList.getExpression(0).eq(oidExpr);
}
else
{
ApiAdapter api = qs.getStoreManager().getApiAdapter();
AbstractClassMetaData cmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(value.getClass(), clr);
if (cmd == null)
{
// if there is no metadata, we either have an SingleFieldIdentity, application identity, or any object
if (storeMgr.getApiAdapter().isSingleFieldIdentityClass(value.getClass().getName()))
{
// Object is SingleFieldIdentity
JavaTypeMapping m = dba.getMapping(api.getTargetClassForSingleFieldIdentity(value), storeMgr, clr);
ScalarExpression oidExpr = m.newLiteral(qs, api.getTargetKeyForSingleFieldIdentity(value));
bExpr = expr.expressionList.getExpression(0).eq(oidExpr);
}
else
{
String pcClassName = storeMgr.getClassNameForObjectID(value, clr, null);
if (pcClassName != null)
{
// Object is an application identity
cmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(pcClassName, clr);
bExpr = eqApplicationIdentity(value, null, expr, null, storeMgr, clr);
}
else
{
// Value not PersistenceCapable nor an identity, so return nothing "(1 = 0)"
bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
}
}
}
else
{
// Value is a PersistenceCapable
if (cmd.getIdentityType() == IdentityType.APPLICATION)
{
// Application identity
if (api.getIdForObject(value) != null)
{
// Persistent PC object (FCO)
// Cater for composite PKs and parts of PK being PC mappings, and recursion
JavaTypeMapping[] pkMappingsApp = new JavaTypeMapping[expr.expressionList.size()];
Object[] pkFieldValues = new Object[expr.expressionList.size()];
int position = 0;
for (int i=0;i<cmd.getNoOfPrimaryKeyMembers();i++)
{
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[i]);
Object fieldValue = getFieldValue(mmd, value);
JavaTypeMapping mapping = dba.getMapping(fieldValue.getClass(), storeMgr, clr);
if (mapping instanceof PersistenceCapableMapping)
{
position = populatePrimaryKeyMappingsValuesForPCMapping(pkMappingsApp,
pkFieldValues, position, (PersistenceCapableMapping)mapping,
cmd, mmd, fieldValue, storeMgr, clr);
}
else
{
pkMappingsApp[position] = mapping;
pkFieldValues[position] = fieldValue;
position++;
}
}
for (int i=0; i<expr.expressionList.size(); i++)
{
ScalarExpression source = expr.expressionList.getExpression(i);
ScalarExpression target = pkMappingsApp[i].newLiteral(qs, pkFieldValues[i]);
if (bExpr == null)
{
bExpr = source.eq(target);
}
else
{
bExpr = bExpr.and(source.eq(target));
}
}
}
else
{
// PC object with no id (embedded, or transient maybe)
for (int i=0; i<expr.expressionList.size(); i++)
{
// Query should return nothing (so just do "(1 = 0)")
JPOXLogger.QUERY.warn(LOCALISER.msg("037003", value));
bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
// It is arguable that we should compare the id with null (as below)
/*bExpr = expr.eq(new NullLiteral(qs));*/
}
}
}
else
{
// Datastore identity
for (int i=0; i<expr.expressionList.size(); i++)
{
ScalarExpression source = expr.expressionList.getExpression(i);
OID objectId = (OID)api.getIdForObject(value);
if (objectId == null)
{
// PC object with no id (embedded, or transient maybe)
// Query should return nothing (so just do "(1 = 0)")
JPOXLogger.QUERY.warn(LOCALISER.msg("037003", value));
bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
// It is arguable that we should compare the id with null (as below)
/*bExpr = expr.eq(new NullLiteral(qs));*/
}
else
{
JavaTypeMapping m = dba.getMapping(objectId.getKeyValue().getClass(), storeMgr, clr);
ScalarExpression oidExpr = m.newLiteral(qs, objectId.getKeyValue());
bExpr = source.eq(oidExpr);
}
}
}