return;
}
Integer fieldKey = Integer.valueOf(fieldNumber);
AbstractClassMetaData cmd = ownerSM.getClassMetaData();
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
int relationType = mmd.getRelationType(ownerSM.getObjectManager().getClassLoaderResolver());
if (relationType == Relation.ONE_TO_ONE_BI || relationType == Relation.MANY_TO_ONE_BI)
{
if (!fieldChanges.containsKey(fieldKey))
{
// Only allow for set of PC field (ignore set of Collection fields)
fieldChanges.put(fieldKey, oldValue);
}
return;
}
List<RelationChange> changes = (List<RelationChange>)fieldChanges.get(fieldKey);
if (changes == null)
{
changes = new ArrayList<RelationChange>();
fieldChanges.put(fieldKey, changes);
}
if (relationType == Relation.ONE_TO_MANY_BI || relationType == Relation.MANY_TO_MANY_BI)
{
if (mmd.hasCollection())
{
if (oldValue == null)
{
if (newValue != null)
{
// Add all elements
Iterator iter = ((Collection)newValue).iterator();
while (iter.hasNext())
{
changes.add(new RelationChange(ChangeType.ADD_OBJECT, iter.next()));
}
}
}
else
{
if (newValue == null)
{
AbstractMemberMetaData relatedMmd = mmd.getRelatedMemberMetaData(ownerSM.getObjectManager().getClassLoaderResolver())[0];
Iterator iter = ((Collection)oldValue).iterator();
while (iter.hasNext())
{
Object element = iter.next();
if (ownerSM.getLifecycleState().isDeleted)
{
// Deleting the owner, so register the element to reset its owner
ownerSM.getObjectManager().removeObjectFromLevel2Cache(ownerSM.getObjectManager().getApiAdapter().getIdForObject(element));
ObjectProvider elementSM = ownerSM.getObjectManager().findObjectProvider(element);
if (relationType == Relation.ONE_TO_MANY_BI)
{
ec.getRelationshipManager(elementSM).relationChange(relatedMmd.getAbsoluteFieldNumber(), ownerSM.getObject(), null);
}
else if (relationType == Relation.MANY_TO_MANY_BI)
{
ec.getRelationshipManager(elementSM).relationRemove(relatedMmd.getAbsoluteFieldNumber(), ownerSM.getObject());
}
}
else
{
// Remove the element
changes.add(new RelationChange(ChangeType.REMOVE_OBJECT, element));
}
}
}
else
{
// Remove some and add some
Iterator newIter = ((Collection)newValue).iterator();
while (newIter.hasNext())
{
Object newElem = newIter.next();
Iterator oldIter = ((Collection)oldValue).iterator();
boolean alreadyExists = false;
while (oldIter.hasNext())
{
Object oldElem = oldIter.next();
if (newElem == oldElem)
{
alreadyExists = true;
break;
}
}
if (!alreadyExists)
{
ObjectProvider elemSM = ownerSM.getObjectManager().findObjectProvider(newElem);
if (elemSM != null)
{
AbstractMemberMetaData elemMmd = mmd.getRelatedMemberMetaData(ownerSM.getObjectManager().getClassLoaderResolver())[0];
Object oldOwner = elemSM.provideField(elemMmd.getAbsoluteFieldNumber());
if (!elemSM.isFieldLoaded(elemMmd.getAbsoluteFieldNumber()))
{
elemSM.loadField(elemMmd.getAbsoluteFieldNumber());
}
if (oldOwner != null)
{
// Remove from old owner collection
ObjectProvider oldOwnerSM = ownerSM.getObjectManager().findObjectProvider(oldOwner);