public ScalarExpression newFieldExpression(String fieldName)
{
if (mainTable instanceof DatastoreClass)
{
// Field of class in its primary/secondary table
DatastoreClass ct = (DatastoreClass) mainTable;
JavaTypeMapping m = null;
if (fieldName.equals(qs.getCandidateAlias()))
{
// Candidate table so return id mapping
m = ct.getIDMapping();
return m.newScalarExpression(qs, this);
}
else
{
if (fieldName.indexOf(".") > 0)
{
String baseField = fieldName.substring(0, fieldName.indexOf("."));
try
{
m = ct.getFieldMapping(baseField);
}
catch (NoSuchPersistentFieldException npfe)
{
// Check if this field is a previously utilised embedded field
if (embeddedFieldMappings != null)
{
m = (JavaTypeMapping)embeddedFieldMappings.get(baseField);
}
if (m == null)
{
// Field is not valid for this class, and we have no known embedded mapping for it so its a user error
throw npfe;
}
}
if (m == null)
{
throw new JPOXUserException(LOCALISER.msg("037001", fieldName, ct.toString()));
}
if (m instanceof EmbeddedPCMapping)
{
// Embedded PC field
String subField = fieldName.substring(fieldName.indexOf(".") + 1);
m = getMappingForEmbeddedField((EmbeddedPCMapping)m, subField);
if (m == null)
{
throw new JPOXUserException(LOCALISER.msg("037002", fieldName, subField, baseField));
}
// Save this embedded mapping in case the user has nested subobjects within it
// TODO This doesnt allow for embedded "subfields" having the same name as other embedded subfields
// Currently we just keep on saving these against the subfield name, but maybe we only to keep the
// most recent since the field will be processed straight away if it's part of a JDOQL query
if (embeddedFieldMappings == null)
{
embeddedFieldMappings = new HashMap();
}
embeddedFieldMappings.put(subField, m);
}
ScalarExpression expr = m.newScalarExpression(qs, this);
if (expr instanceof ObjectExpression)
{
((ObjectExpression)expr).setFieldDefinition(m.getFieldMetaData().getName(), m.getFieldMetaData().getTypeName());
}
return expr;
}
else
{
// Field of main table
m = ct.getFieldMapping(fieldName);
if (m == null)
{
throw new JPOXUserException(LOCALISER.msg("037001", fieldName, ct.toString()));
}
ScalarExpression expr = m.newScalarExpression(qs, this);
if (expr instanceof ObjectExpression)
{