public virtual NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo BuildNnoi
     (object o, NeoDatis.Odb.Core.Layers.Layer2.Meta.ClassInfo classInfo, NeoDatis.Odb.Core.Layers.Layer2.Meta.AbstractObjectInfo
     [] values, long[] attributesIdentification, int[] attributeIds, System.Collections.Generic.IDictionary
     <object, NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo> alreadyReadObjects
     )
 {
     NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo nnoi = new NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo
                                                                         (o, classInfo, values, attributesIdentification, attributeIds);
     if (storageEngine != null)
     {
         // for unit test purpose
         NeoDatis.Odb.Core.Transaction.ICache cache = storageEngine.GetSession(true).GetCache
                                                          ();
         // Check if object is in the cache, if so sets its oid
         NeoDatis.Odb.OID oid = cache.GetOid(o, false);
         if (oid != null)
         {
             nnoi.SetOid(oid);
             // Sets some values to the new header to keep track of the infos
             // when
             // executing NeoDatis without closing it, just committing.
             // Bug reported by Andy
             NeoDatis.Odb.Core.Layers.Layer2.Meta.ObjectInfoHeader oih = cache.GetObjectInfoHeaderFromOid
                                                                             (oid, true);
             nnoi.GetHeader().SetObjectVersion(oih.GetObjectVersion());
             nnoi.GetHeader().SetUpdateDate(oih.GetUpdateDate());
             nnoi.GetHeader().SetCreationDate(oih.GetCreationDate());
         }
     }
     return(nnoi);
 }
Example #2
0
        public override object GetObjectFromOid(NeoDatis.Odb.OID oid)
        {
            if (oid == null)
            {
                throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Odb.Core.NeoDatisError.CanNotGetObjectFromNullOid
                                                           );
            }
            NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo nnoi = GetObjectReader()
                                                                            .ReadNonNativeObjectInfoFromOid(null, oid, true, true);
            if (nnoi.IsDeletedObject())
            {
                throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Odb.Core.NeoDatisError.ObjectIsMarkedAsDeletedForOid
                                                           .AddParameter(oid));
            }
            object o = nnoi.GetObject();

            if (o == null)
            {
                o = GetObjectReader().GetInstanceBuilder().BuildOneInstance(nnoi);
            }
            NeoDatis.Odb.Core.Transaction.ISession lsession = GetSession(true);
            // Here oid can be different from nnoi.getOid(). This is the case when
            // the oid is an external oid. That`s why we use
            // nnoi.getOid() to put in the cache
            lsession.GetCache().AddObject(nnoi.GetOid(), o, nnoi.GetHeader());
            lsession.GetTmpCache().ClearObjectInfos();
            return(o);
        }
		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;
		}
		public virtual object BuildOneInstance(NonNativeObjectInfo objectInfo)
		{
			ICache cache = GetSession().GetCache();
			
			// verify if the object is check to delete
			if (objectInfo.IsDeletedObject())
			{
				throw new ODBRuntimeException(NeoDatisError.ObjectIsMarkedAsDeletedForOid.AddParameter(objectInfo.GetOid()));
			}
			// Then check if object is in cache
			object o = cache.GetObjectWithOid(objectInfo.GetOid());
			if (o != null)
			{
				return o;
			}
			Type instanceClazz = null;
			instanceClazz = classPool.GetClass(objectInfo.GetClassInfo().GetFullClassName());
			try
			{
				o = classIntrospector.NewInstanceOf(instanceClazz);
			}
			catch (System.Exception e)
			{
				throw new ODBRuntimeException(NeoDatisError.InstanciationError.AddParameter(objectInfo.GetClassInfo().GetFullClassName()), e);
			}
			// This can happen if ODB can not create the instance
			// TODO Check if returning null is correct
			if (o == null)
			{
				return null;
			}
			// Keep the initial hash code. In some cases, when the class redefines
			// the hash code method
			// Hash code can return wrong values when attributes are not set (when
			// hash code depends on attribute values)
			// Hash codes are used as the key of the map,
			// So at the end of this method, if hash codes are different, object
			// will be removed from the cache and inserted back
			bool hashCodeIsOk = true;
			int initialHashCode = 0;
			try
			{
				initialHashCode = o.GetHashCode();
			}
			catch (System.Exception)
			{
				hashCodeIsOk = false;
			}
			// Adds this incomplete instance in the cache to manage cyclic reference
			if (hashCodeIsOk)
			{
				cache.AddObject(objectInfo.GetOid(), o, objectInfo.GetHeader());
			}
			ClassInfo ci = objectInfo.GetClassInfo();
			IOdbList<FieldInfo> fields = classIntrospector.GetAllFields(ci.GetFullClassName());
			
			FieldInfo field = null;
			AbstractObjectInfo aoi = null;
			object value = null;
			
			for (int i = 0; i < fields.Count; i++)
			{
				field = fields[i];
				// Gets the id of this field
				int attributeId = ci.GetAttributeId(field.Name);
				if (OdbConfiguration.IsDebugEnabled(LogIdDebug))
				{
					DLogger.Debug("getting field with name " + field.Name + ", attribute id is "+ attributeId);
				}
				aoi = objectInfo.GetAttributeValueFromId(attributeId);
				// Check consistency
				// ensureClassCompatibily(field,
				// instanceInfo.getClassInfo().getAttributeinfo(i).getFullClassname());
				if (aoi != null && (!aoi.IsNull()))
				{
					if (aoi.IsNative())
					{
						if (aoi.IsAtomicNativeObject())
						{
							if (aoi.IsNull())
							{
								value = null;
							}
							else
							{
								value = aoi.GetObject();
							}
						}
						if (aoi.IsCollectionObject())
						{
							value = BuildCollectionInstance((CollectionObjectInfo)aoi);
							// Manage a specific case of Set
							/*
							if (typeof(Java.Util.Set).IsAssignableFrom(field.GetType()) && typeof(ICollection).IsAssignableFrom(value.GetType()))
							{
								Java.Util.Set s = new Java.Util.HashSet();
								s.AddAll((System.Collections.ICollection)value);
								value = s;
							}*/
						}
						if (aoi.IsArrayObject())
						{
							value = BuildArrayInstance((ArrayObjectInfo)aoi);
						}
						if (aoi.IsMapObject())
						{
							value = BuildMapInstance((MapObjectInfo)aoi);
						}
						if (aoi.IsEnumObject())
						{
							value = BuildEnumInstance((EnumNativeObjectInfo)aoi, field.FieldType);
						}
					}
					else
					{
						if (aoi.IsNonNativeObject())
						{
							if (aoi.IsDeletedObject())
							{
								if (NeoDatis.Odb.OdbConfiguration.DisplayWarnings())
								{
									IError warning = NeoDatisError.AttributeReferencesADeletedObject
										.AddParameter(objectInfo.GetClassInfo().GetFullClassName())
										.AddParameter(objectInfo.GetOid()).AddParameter(field.Name);
									DLogger.Info(warning.ToString());
								}
								value = null;
							}
							else
							{
								value = BuildOneInstance((NonNativeObjectInfo)aoi);
							}
						}
					}
					if (value != null)
					{
						if (OdbConfiguration.IsDebugEnabled(LogIdDebug))
						{
							DLogger.Debug("Setting field " + field.Name + "(" + field.GetType().FullName + ") to " + value + " / " + value.GetType().FullName);
						}
						try
						{
							field.SetValue(o, value);
						}
						catch (System.Exception e)
						{
							throw new ODBRuntimeException(NeoDatisError.InstanceBuilderWrongObjectContainerType
								.AddParameter(objectInfo.GetClassInfo().GetFullClassName())
								.AddParameter(value.GetType().FullName).AddParameter(field.GetType().FullName), e);
						}
					}
				}
			}
			if (o != null && !OdbClassUtil.GetFullName(o.GetType()).Equals(objectInfo.GetClassInfo().GetFullClassName()))
			{
				new ODBRuntimeException(NeoDatisError.InstanceBuilderWrongObjectType
						.AddParameter(objectInfo.GetClassInfo().GetFullClassName())
						.AddParameter(o.GetType().FullName));
			}
			if (hashCodeIsOk || initialHashCode != o.GetHashCode())
			{
				// Bug (sf bug id=1875544 )detected by glsender
				// This can happen when an object has redefined its own hashcode
				// method and depends on the field values
				// Then, we have to remove object from the cache and re-insert to
				// correct map hash code
				cache.RemoveObjectWithOid(objectInfo.GetOid());
				// re-Adds instance in the cache
				cache.AddObject(objectInfo.GetOid(), o, objectInfo.GetHeader());
			}
			if (triggerManager != null)
			{
				triggerManager.ManageSelectTriggerAfter(objectInfo.GetClassInfo().GetFullClassName
					(), objectInfo, objectInfo.GetOid());
			}
			if (OdbConfiguration.ReconnectObjectsToSession()) {

			   ICrossSessionCache crossSessionCache = CacheFactory.GetCrossSessionCache(engine.GetBaseIdentification().GetIdentification());
			   crossSessionCache.AddObject(o, objectInfo.GetOid());

		   }
			
			return o;
		}
		public virtual NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo BuildNnoi
			(object o, NeoDatis.Odb.Core.Layers.Layer2.Meta.ClassInfo classInfo, NeoDatis.Odb.Core.Layers.Layer2.Meta.AbstractObjectInfo
			[] values, long[] attributesIdentification, int[] attributeIds, System.Collections.Generic.IDictionary
			<object, NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo> alreadyReadObjects
			)
		{
			NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo nnoi = new NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo
				(o, classInfo, values, attributesIdentification, attributeIds);
			if (storageEngine != null)
			{
				// for unit test purpose
				NeoDatis.Odb.Core.Transaction.ICache cache = storageEngine.GetSession(true).GetCache
					();
				// Check if object is in the cache, if so sets its oid
				NeoDatis.Odb.OID oid = cache.GetOid(o, false);
				if (oid != null)
				{
					nnoi.SetOid(oid);
					// Sets some values to the new header to keep track of the infos
					// when
					// executing NeoDatis without closing it, just committing.
					// Bug reported by Andy
					NeoDatis.Odb.Core.Layers.Layer2.Meta.ObjectInfoHeader oih = cache.GetObjectInfoHeaderFromOid
						(oid, true);
					nnoi.GetHeader().SetObjectVersion(oih.GetObjectVersion());
					nnoi.GetHeader().SetUpdateDate(oih.GetUpdateDate());
					nnoi.GetHeader().SetCreationDate(oih.GetCreationDate());
				}
			}
			return nnoi;
		}