{
if (value != null)
{
AbstractMemberMetaData fmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
boolean persistCascade = fmd.isCascadePersist();
ApiAdapter api = sm.getObjectManager().getApiAdapter();
if (replaceSCOsWithWrappers)
{
// Replace any SCO field that isnt already a wrapper, with its wrapper object
boolean[] secondClassMutableFieldFlags = sm.getClassMetaData().getSCOMutableMemberFlags();
if (secondClassMutableFieldFlags[fieldNumber] && !(value instanceof SCO))
{
// Replace the field with a SCO wrapper
sm.wrapSCOField(fieldNumber, value, false, true, true);
}
}
if (persistCascade)
{
if (api.isPersistable(value))
{
// Process PC fields
if (fmd.isEmbedded() || fmd.isSerialized())
{
processPersistable(value, fieldNumber, StateManager.EMBEDDED_PC);
}
else
{
processPersistable(value, -1, StateManager.PC);
}
}
else if (value instanceof Collection)
{
// Process all elements of the Collection that are PC
Collection coll = (Collection)value;
Iterator iter = coll.iterator();
while (iter.hasNext())
{
Object element = iter.next();
if (api.isPersistable(element))
{
if (fmd.getCollection().isEmbeddedElement() || fmd.getCollection().isSerializedElement())
{
processPersistable(element, fieldNumber, StateManager.EMBEDDED_COLLECTION_ELEMENT_PC);
}
else
{
processPersistable(element, -1, StateManager.PC);
}
}
}
}
else if (value instanceof Map)
{
// Process all keys, values of the Map that are PC
Map map = (Map)value;
// Process any keys that are PersistenceCapable
Set keys = map.keySet();
Iterator iter = keys.iterator();
while (iter.hasNext())
{
Object mapKey = iter.next();
if (api.isPersistable(mapKey))
{
if (fmd.getMap().isEmbeddedKey() || fmd.getMap().isSerializedKey())
{
processPersistable(mapKey, fieldNumber, StateManager.EMBEDDED_MAP_KEY_PC);
}
else
{
processPersistable(mapKey, -1, StateManager.PC);
}
}
}
// Process any values that are PersistenceCapable
Collection values = map.values();
iter = values.iterator();
while (iter.hasNext())
{
Object mapValue = iter.next();
if (api.isPersistable(mapValue))
{
if (fmd.getMap().isEmbeddedValue() || fmd.getMap().isSerializedValue())
{
processPersistable(mapValue, fieldNumber, StateManager.EMBEDDED_MAP_VALUE_PC);
}
else
{
processPersistable(mapValue, -1, StateManager.PC);
}
}
}
}
else if (value instanceof Object[])
{
Object[] array = (Object[]) value;
for (int i=0;i<array.length;i++)
{
Object element = array[i];
if (api.isPersistable(element))
{
if (fmd.getArray().isEmbeddedElement() || fmd.getArray().isSerializedElement())
{
// TODO This should be ARRAY_ELEMENT_PC but we havent got that yet
processPersistable(element, fieldNumber, StateManager.EMBEDDED_COLLECTION_ELEMENT_PC);