public void updateSourceObject(Object anEO) { String masterKey = _localRelationshipKey(); Object aSourceObject = _localSourceObject(); boolean isDictionary = (aSourceObject instanceof NSMutableDictionary); NSMutableDictionary _dictionary = (isDictionary) ? (NSMutableDictionary) aSourceObject : null; EOEnterpriseObject _eo = !(isDictionary) ? (EOEnterpriseObject) aSourceObject : null; if (anEO != null) { if (isDictionary) { _dictionary.setObjectForKey(anEO, masterKey); } else if (_eo.valueForKey(masterKey) != anEO) { _eo.addObjectToBothSidesOfRelationshipWithKey((EOEnterpriseObject) anEO, masterKey); } } else { // remove if (isDictionary) { _dictionary.removeObjectForKey(masterKey); } else if (_eo.valueForKey(masterKey) != null) { _eo.removeObjectFromBothSidesOfRelationshipWithKey( (EOEnterpriseObject) _eo.valueForKey(masterKey), masterKey); } } }
public void updateSourceObject(NSArray newValues) { Object realSourceObject = realSourceObject(); String realRelationshipKey = realRelationshipKey(); newValues = newValues != null ? newValues : NSArray.EmptyArray; if (realSourceObject instanceof EOEnterpriseObject && // only add/remove if we have an EO and handle a relationship ((EOEnterpriseObject) realSourceObject) .classDescriptionForDestinationKey(realRelationshipKey) != null) { EOEnterpriseObject eo = (EOEnterpriseObject) realSourceObject; NSArray currentValues = (NSArray) eo.valueForKey(realRelationshipKey); currentValues = currentValues != null ? currentValues : NSArray.EmptyArray; for (int i = currentValues.count() - 1; i >= 0; i--) { EOEnterpriseObject o = (EOEnterpriseObject) currentValues.objectAtIndex(i); if (newValues.indexOfIdenticalObject(o) == NSArray.NotFound) { // not found eo.removeObjectFromBothSidesOfRelationshipWithKey(o, realRelationshipKey); } } for (int i = newValues.count() - 1; i >= 0; i--) { EOEnterpriseObject o = (EOEnterpriseObject) newValues.objectAtIndex(i); if (currentValues.indexOfIdenticalObject(o) == NSArray.NotFound) { // not found eo.addObjectToBothSidesOfRelationshipWithKey(o, realRelationshipKey); } } } else { // NOTE ak: this implementation is different from what JavaWOExtensions do. // There, the existing array is fetched and added to/removed from. Here, we simply set the // new array. I changed this because it looked like a bad idea to change the array without // the sourceObjects's knowledge. NSKeyValueCoding.Utility.takeValueForKey( realSourceObject, (newValues instanceof NSMutableArray) ? newValues : newValues.mutableClone(), realRelationshipKey); } }