{
return null;
}
DBObject embeddedValue = (DBObject)dbObject.get(fieldName);
ObjectProvider embSM = ec.newObjectProviderForEmbedded(mmd, embcmd, sm, fieldNumber);
FetchFieldManager ffm = new FetchFieldManager(embSM, embeddedValue, embcmd);
ffm.ownerMmd = mmd;
ffm.embedded = true;
embSM.replaceFields(embcmd.getAllMemberPositions(), ffm);
return embSM.getObject();
}
else
{
// Flat embedding as fields of the owning document
// TODO Cater for null (use embmd.getNullIndicatorColumn/Value)
EmbeddedMetaData embmd = mmd.getEmbeddedMetaData();
AbstractMemberMetaData[] embmmds = embmd.getMemberMetaData();
boolean isNull = true;
for (int i=0;i<embmmds.length;i++)
{
String embFieldName = MongoDBUtils.getFieldName(mmd, i);
if (dbObject.containsField(embFieldName))
{
isNull = false;
break;
}
}
if (isNull)
{
return null;
}
ObjectProvider embSM = ec.newObjectProviderForEmbedded(mmd, embcmd, sm, fieldNumber);
FieldManager ffm = new FetchEmbeddedFieldManager(embSM, dbObject, mmd);
embSM.replaceFields(embcmd.getAllMemberPositions(), ffm);
return embSM.getObject();
}
}
else if (Relation.isRelationMultiValued(relationType))
{
if (mmd.hasCollection())
{
String fieldName = MongoDBUtils.getFieldName(mmd);
if (!dbObject.containsField(fieldName))
{
return null;
}
Object value = dbObject.get(fieldName);
Collection<Object> coll;
AbstractClassMetaData elemCmd = mmd.getCollection().getElementClassMetaData(clr, ec.getMetaDataManager());
try
{
Class instanceType = SCOUtils.getContainerInstanceType(mmd.getType(), mmd.getOrderMetaData() != null);
coll = (Collection<Object>) instanceType.newInstance();
}
catch (Exception e)
{
throw new NucleusDataStoreException(e.getMessage(), e);
}
Collection collValue = (Collection)value;
Iterator collIter = collValue.iterator();
while (collIter.hasNext())
{
DBObject elementObj = (DBObject)collIter.next();
ObjectProvider embSM = ec.newObjectProviderForEmbedded(mmd, elemCmd, sm, fieldNumber);
embSM.setPcObjectType(ObjectProvider.EMBEDDED_COLLECTION_ELEMENT_PC);
FetchFieldManager ffm = new FetchFieldManager(embSM, elementObj, elemCmd);
ffm.ownerMmd = mmd;
ffm.embedded = true;
embSM.replaceFields(elemCmd.getAllMemberPositions(), ffm);
coll.add(embSM.getObject());
}
if (sm != null)
{
return sm.wrapSCOField(fieldNumber, coll, false, false, true);
}
return coll;
}
else if (mmd.hasArray())
{
String fieldName = MongoDBUtils.getFieldName(mmd);
if (!dbObject.containsField(fieldName))
{
return null;
}
AbstractClassMetaData elemCmd = mmd.getArray().getElementClassMetaData(clr, ec.getMetaDataManager());
Object value = dbObject.get(fieldName);
Object[] array = new Object[Array.getLength(value)];
for (int i=0;i<array.length;i++)
{
DBObject elementObj = (DBObject)Array.get(value, i);
ObjectProvider embSM = ec.newObjectProviderForEmbedded(mmd, elemCmd, sm, fieldNumber);
embSM.setPcObjectType(ObjectProvider.EMBEDDED_COLLECTION_ELEMENT_PC);
FetchFieldManager ffm = new FetchFieldManager(embSM, elementObj, elemCmd);
ffm.ownerMmd = mmd;
ffm.embedded = true;
embSM.replaceFields(elemCmd.getAllMemberPositions(), ffm);
array[i] = embSM.getObject();
}
return array;
}
else
{
String fieldName = MongoDBUtils.getFieldName(mmd);
if (!dbObject.containsField(fieldName))
{
return null;
}
Object value = dbObject.get(fieldName);
Map map = null;
try
{
Class instanceType = SCOUtils.getContainerInstanceType(mmd.getType(), null);
map = (Map) instanceType.newInstance();
}
catch (Exception e)
{
throw new NucleusDataStoreException(e.getMessage(), e);
}
AbstractClassMetaData keyCmd = mmd.getMap().getKeyClassMetaData(clr, ec.getMetaDataManager());
AbstractClassMetaData valCmd = mmd.getMap().getValueClassMetaData(clr, ec.getMetaDataManager());
Collection<DBObject> entryColl = (Collection)value;
Iterator<DBObject> entryIter = entryColl.iterator();
while (entryIter.hasNext())
{
DBObject entryObj = entryIter.next();
Object keyObj = entryObj.get("key");
Object valObj = entryObj.get("value");
Object mapKey = null;
if (keyCmd != null)
{
// Key is embedded object
DBObject keyDbObj = (DBObject)keyObj;
ObjectProvider embSM = ec.newObjectProviderForEmbedded(mmd, keyCmd, sm, fieldNumber);
embSM.setPcObjectType(ObjectProvider.EMBEDDED_MAP_KEY_PC);
FetchFieldManager ffm = new FetchFieldManager(embSM, keyDbObj, keyCmd);
ffm.ownerMmd = mmd;
ffm.embedded = true;
embSM.replaceFields(keyCmd.getAllMemberPositions(), ffm);
mapKey = embSM.getObject();
}
else
{
mapKey = getMapKeyForReturnValue(mmd, keyObj);
}
Object mapVal = null;
if (valCmd != null)
{
// Value is embedded object
DBObject valDbObj = (DBObject)valObj;
ObjectProvider embSM = ec.newObjectProviderForEmbedded(mmd, valCmd, sm, fieldNumber);
embSM.setPcObjectType(ObjectProvider.EMBEDDED_MAP_VALUE_PC);
FetchFieldManager ffm = new FetchFieldManager(embSM, valDbObj, valCmd);
ffm.ownerMmd = mmd;
ffm.embedded = true;
embSM.replaceFields(valCmd.getAllMemberPositions(), ffm);
mapVal = embSM.getObject();
}
else
{
mapVal = getMapValueForReturnValue(mmd, valObj);
}