return (String) sm.provideField(fieldNumber);
}
public Object fetchObjectField(int fieldNumber)
{
ExecutionContext ec = sm.getExecutionContext();
ClassLoaderResolver clr = ec.getClassLoaderResolver();
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
int relationType = mmd.getRelationType(clr);
if (relationType == Relation.NONE)
{
// Object-based field (non-relation)
sm.copyFieldsFromObject(value, new int[]{fieldNumber});
return sm.provideField(fieldNumber);
}
else if (mmd.getEmbeddedMetaData() != null)
{
// TODO Implement embedded relation objects
}
else
{
switch (relationType)
{
case Relation.ONE_TO_ONE_UNI :
case Relation.ONE_TO_ONE_BI :
case Relation.MANY_TO_ONE_BI :
final AbstractClassMetaData cmd = sm.getExecutionContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
final NodeList nList = ((Element) node).getElementsByTagName(mmd.getName());
if (nList.getLength() == 1)
{
Object id = ec.getApiAdapter().getNewApplicationIdentityObjectId(clr.classForName(cmd.getFullClassName()), nList.item(0).getFirstChild().getNodeValue());
Object related = ec.findObject(id, true, true, null);
if (relationType == Relation.ONE_TO_ONE_BI)
{
// Set other side of relation to avoid reloading
ObjectProvider relatedSM = ec.findObjectProvider(related);
AbstractMemberMetaData relatedMmd = mmd.getRelatedMemberMetaDataForObject(clr, sm.getObject(), related);
relatedSM.replaceField(relatedMmd.getAbsoluteFieldNumber(), sm.getObject());
}
return related;
}
return null;
case Relation.ONE_TO_MANY_UNI :
case Relation.ONE_TO_MANY_BI :
// TODO Cater for Map/array
if (mmd.hasCollection())
{
AbstractClassMetaData cmd2 = sm.getExecutionContext().getMetaDataManager().getMetaDataForClass(mmd.getCollection().getElementType(), clr);
// Get value being unmarshalled
sm.copyFieldsFromObject(value, new int[]{fieldNumber});
Collection collection = (Collection) sm.provideField(fieldNumber);
// Make sure we get the right type of element (allow for inheritance)
NodeList nLists = ((Element) node).getElementsByTagName(
XMLUtils.getElementNameForMember(mmd, FieldRole.ROLE_COLLECTION_ELEMENT));
for (int i = 0; i < nLists.getLength(); i++)
{
final String nodeValue = nLists.item(i).getFirstChild().getNodeValue();
if (nodeValue != null && nodeValue.trim().length() > 0)
{
final AbstractClassMetaData elementCmd = XMLUtils.findMetaDataForNode(doc, cmd2,
ec.getMetaDataManager(), nodeValue, clr);
if (elementCmd == null && cmd2 != null)
{
throw new NucleusException("Unable to find object of type " + cmd2.getFullClassName() + " with id=" + nodeValue);
}
Object id = ec.getApiAdapter().getNewApplicationIdentityObjectId(
clr.classForName(elementCmd.getFullClassName()), nodeValue);
Object related = ec.findObject(id, true, true, null);
if (relationType == Relation.ONE_TO_MANY_BI)
{
// Set other side of relation to avoid reloading
ObjectProvider relatedSM = ec.findObjectProvider(related);
AbstractMemberMetaData relatedMmd = relatedSM.getClassMetaData().getMetaDataForMember(mmd.getMappedBy());
relatedSM.replaceField(relatedMmd.getAbsoluteFieldNumber(), sm.getObject());
}
collection.add(related);
}