public virtual object BuildGenericCollectionInstance(CollectionObjectInfo coi, Type t) { Type genericType = t.GetGenericTypeDefinition(); object newCollection = null; try { newCollection = System.Activator.CreateInstance(t); } catch (System.Exception e) { Console.WriteLine(e); throw new ODBRuntimeException(NeoDatisError.CollectionInstanciationError.AddParameter(coi.GetRealCollectionClassName())); } System.Collections.IEnumerator iterator = coi.GetCollection().GetEnumerator(); AbstractObjectInfo aoi = null; MethodInfo method = t.GetMethod("Add"); while (iterator.MoveNext()) { aoi = (AbstractObjectInfo)iterator.Current; if (!aoi.IsDeletedObject()) { method.Invoke(newCollection, new object[] { BuildOneInstance(aoi) }); } } return(newCollection); }
public virtual object BuildOneInstance(NativeObjectInfo objectInfo) { if (objectInfo.IsAtomicNativeObject()) { return(BuildOneInstance((AtomicNativeObjectInfo)objectInfo)); } if (objectInfo.IsCollectionObject()) { CollectionObjectInfo coi = (CollectionObjectInfo)objectInfo; object value = BuildCollectionInstance(coi); /* Manage a specific case of Set * if (typeof(Java.Util.Set).IsAssignableFrom(value.GetType()) && typeof(System.Collections.ICollection).IsAssignableFrom(value.GetType())) * { * Java.Util.Set s = new Java.Util.HashSet(); * s.AddAll((System.Collections.ICollection)value); * value = s; * } */ return(value); } if (objectInfo.IsArrayObject()) { return(BuildArrayInstance((ArrayObjectInfo)objectInfo)); } if (objectInfo.IsMapObject()) { return(BuildMapInstance((MapObjectInfo)objectInfo)); } if (objectInfo.IsNull()) { return(null); } throw new ODBRuntimeException(NeoDatisError.InstanceBuilderNativeType.AddParameter(ODBType.GetNameFromId(objectInfo.GetOdbTypeId()))); }
public virtual object BuildCollectionInstance(CollectionObjectInfo coi) { Type t = classPool.GetClass(coi.GetRealCollectionClassName()); if (t.IsGenericType) { return(BuildGenericCollectionInstance(coi, t)); } else { return(BuildNonGenericCollectionInstance(coi, t)); } }
public virtual object BuildNonGenericCollectionInstance(CollectionObjectInfo coi, Type t) { System.Collections.IList newCollection = (System.Collections.IList)System.Activator.CreateInstance(classPool.GetClass(coi.GetRealCollectionClassName())); System.Collections.IEnumerator iterator = coi.GetCollection().GetEnumerator(); AbstractObjectInfo aoi = null; while (iterator.MoveNext()) { aoi = (AbstractObjectInfo)iterator.Current; if (!aoi.IsDeletedObject()) { newCollection.Add(BuildOneInstance(aoi)); } } return(newCollection); }
private bool HasChanged(NonNativeObjectInfo nnoi1, NonNativeObjectInfo nnoi2, int objectRecursionLevel) { AbstractObjectInfo value1 = null; AbstractObjectInfo value2 = null; bool hasChanged = false; // If the object is already being checked, return false, this second // check will not affect the check int n = 0; alreadyCheckingObjects.TryGetValue(nnoi2, out n); if (n != 0) { return(false); } // Put the object in the temporary cache alreadyCheckingObjects[nnoi1] = 1; alreadyCheckingObjects[nnoi2] = 1; // Warning ID Start with 1 and not 0 for (int id = 1; id <= nnoi1.GetMaxNbattributes(); id++) { value1 = nnoi1.GetAttributeValueFromId(id); // Gets the value by the attribute id to be sure // Problem because a new object info may not have the right ids ? // Check if // the new oiD is ok. value2 = nnoi2.GetAttributeValueFromId(id); if (value2 == null) { // this means the object to have attribute id StoreChangedObject(nnoi1, nnoi2, id, objectRecursionLevel); hasChanged = true; continue; } if (value1 == null) { //throw new ODBRuntimeException("ObjectInfoComparator.hasChanged:attribute with id "+id+" does not exist on "+nnoi2); // This happens when this object was created with an version of ClassInfo (which has been refactored). // In this case,we simply tell that in place update is not supported so that the object will be rewritten with // new metamodel supportInPlaceUpdate = false; continue; } // If both are null, no effect if (value1.IsNull() && value2.IsNull()) { continue; } if (value1.IsNull() || value2.IsNull()) { supportInPlaceUpdate = false; hasChanged = true; StoreActionSetAttributetoNull(nnoi1, id, objectRecursionLevel); continue; } if (!ClassAreCompatible(value1, value2)) { if (value2 is NativeObjectInfo) { StoreChangedObject(nnoi1, nnoi2, id, objectRecursionLevel); StoreChangedAttributeAction(new ChangedNativeAttributeAction (nnoi1, nnoi2, nnoi1.GetHeader().GetAttributeIdentificationFromId(id), (NativeObjectInfo )value2, objectRecursionLevel, false, nnoi1.GetClassInfo().GetAttributeInfoFromId (id).GetName())); } if (value2 is ObjectReference) { NonNativeObjectInfo nnoi = (NonNativeObjectInfo )value1; ObjectReference oref = (ObjectReference )value2; if (!nnoi.GetOid().Equals(oref.GetOid())) { StoreChangedObject(nnoi1, nnoi2, id, objectRecursionLevel); int attributeIdThatHasChanged = id; // this is the exact position where the object reference // definition is stored long attributeDefinitionPosition = nnoi2.GetAttributeDefinitionPosition(attributeIdThatHasChanged ); StoreChangedAttributeAction(new ChangedObjectReferenceAttributeAction (attributeDefinitionPosition, (ObjectReference )value2, objectRecursionLevel)); } else { continue; } } hasChanged = true; continue; } if (value1.IsAtomicNativeObject()) { if (!value1.Equals(value2)) { // storeChangedObject(nnoi1, nnoi2, id, // objectRecursionLevel); StoreChangedAttributeAction(new ChangedNativeAttributeAction (nnoi1, nnoi2, nnoi1.GetHeader().GetAttributeIdentificationFromId(id), (NativeObjectInfo )value2, objectRecursionLevel, false, nnoi1.GetClassInfo().GetAttributeInfoFromId (id).GetName())); hasChanged = true; continue; } continue; } if (value1.IsCollectionObject()) { CollectionObjectInfo coi1 = (CollectionObjectInfo)value1; CollectionObjectInfo coi2 = (CollectionObjectInfo)value2; bool collectionHasChanged = ManageCollectionChanges(nnoi1, nnoi2, id, coi1, coi2, objectRecursionLevel); hasChanged = hasChanged || collectionHasChanged; continue; } if (value1.IsArrayObject()) { ArrayObjectInfo aoi1 = (ArrayObjectInfo)value1; ArrayObjectInfo aoi2 = (ArrayObjectInfo)value2; bool arrayHasChanged = ManageArrayChanges(nnoi1, nnoi2, id, aoi1, aoi2, objectRecursionLevel ); hasChanged = hasChanged || arrayHasChanged; continue; } if (value1.IsMapObject()) { MapObjectInfo moi1 = (MapObjectInfo)value1; MapObjectInfo moi2 = (MapObjectInfo)value2; bool mapHasChanged = ManageMapChanges(nnoi1, nnoi2, id, moi1, moi2, objectRecursionLevel ); hasChanged = hasChanged || mapHasChanged; continue; } if (value1.IsEnumObject()) { EnumNativeObjectInfo enoi1 = (EnumNativeObjectInfo)value1; EnumNativeObjectInfo enoi2 = (EnumNativeObjectInfo)value2; bool enumHasChanged = !enoi1.GetEnumClassInfo().GetId().Equals(enoi2.GetEnumClassInfo ().GetId()) || !enoi1.GetEnumName().Equals(enoi2.GetEnumName()); hasChanged = hasChanged || enumHasChanged; continue; } if (value1.IsNonNativeObject()) { NonNativeObjectInfo oi1 = (NonNativeObjectInfo)value1; NonNativeObjectInfo oi2 = (NonNativeObjectInfo)value2; // If oids are equal, they are the same objects if (oi1.GetOid() != null && oi1.GetOid().Equals(oi2.GetOid())) { hasChanged = HasChanged(value1, value2, objectRecursionLevel + 1) || hasChanged; } else { // This means that an object reference has changed. hasChanged = true; // keep track of the position where the reference must be // updated long positionToUpdateReference = nnoi1.GetAttributeDefinitionPosition(id); StoreNewObjectReference(positionToUpdateReference, oi2, objectRecursionLevel, nnoi1 .GetClassInfo().GetAttributeInfoFromId(id).GetName()); objectRecursionLevel++; // Value2 may have change too AddPendingVerification(value2); } continue; } } int i1 = (int)alreadyCheckingObjects[nnoi1]; int i2 = (int)alreadyCheckingObjects[nnoi2]; if (i1 != null) { i1 = i1 - 1; } if (i2 != null) { i2 = i2 - 1; } if (i1 == 0) { alreadyCheckingObjects.Remove(nnoi1); } else { alreadyCheckingObjects.Add(nnoi1, i1); } if (i2 == 0) { alreadyCheckingObjects.Remove(nnoi2); } else { alreadyCheckingObjects.Add(nnoi2, i2); } return(hasChanged); }
/// <summary> /// Checks if something in the Collection has changed, if yes, stores the /// change /// </summary> /// <param name="nnoi1"> /// The first Object meta representation (nnoi = /// NonNativeObjectInfo) /// </param> /// <param name="nnoi2">The second object meta representation</param> /// <param name="fieldIndex">The field index that this collection represents</param> /// <param name="coi1"> /// The Meta representation of the collection 1 (coi = /// CollectionObjectInfo) /// </param> /// <param name="coi2">The Meta representation of the collection 2</param> /// <param name="objectRecursionLevel"></param> /// <returns>true if 2 collection representation are different</returns> private bool ManageCollectionChanges(NonNativeObjectInfo nnoi1, NonNativeObjectInfo nnoi2, int fieldId , CollectionObjectInfo coi1, CollectionObjectInfo coi2, int objectRecursionLevel) { ICollection <AbstractObjectInfo > collection1 = coi1.GetCollection(); ICollection <AbstractObjectInfo > collection2 = coi2.GetCollection(); if (collection1.Count != collection2.Count) { System.Text.StringBuilder buffer = new System.Text.StringBuilder(); buffer.Append("Collection size has changed oldsize=").Append(collection1.Count).Append ("/newsize=").Append(collection2.Count); StoreChangedObject(nnoi1, nnoi2, fieldId, coi1, coi2, buffer.ToString(), objectRecursionLevel ); return(true); } System.Collections.IEnumerator iterator1 = collection1.GetEnumerator(); System.Collections.IEnumerator iterator2 = collection2.GetEnumerator(); AbstractObjectInfo value1 = null; AbstractObjectInfo value2 = null; int index = 0; while (iterator1.MoveNext()) { iterator2.MoveNext(); value1 = (AbstractObjectInfo)iterator1.Current; value2 = (AbstractObjectInfo)iterator2.Current; bool hasChanged = this.HasChanged(value1, value2, objectRecursionLevel); if (hasChanged) { // We consider collection has changed only if object are // different, If objects are the same instance, but something in // the object has changed, then the collection has not // changed,only the object if (value1.IsNonNativeObject() && value2.IsNonNativeObject()) { NonNativeObjectInfo nnoia = (NonNativeObjectInfo )value1; NonNativeObjectInfo nnoib = (NonNativeObjectInfo )value2; if (nnoia.GetOid() != null && !nnoia.GetOid().Equals(nnoi2.GetOid())) { // Objects are not the same instance -> the collection // has changed StoreChangedObject(nnoi1, nnoi2, fieldId, value1, value2, "List element index " + index + " has changed", objectRecursionLevel); } } else { supportInPlaceUpdate = false; nbChanges++; } //storeChangedObject(nnoi1, nnoi2, fieldId, value1, value2, "List element index " + index + " has changed", objectRecursionLevel); return(true); } index++; } return(false); }