Beispiel #1
0
 public override async Task <object> DisassembleAsync(object value, ISessionImplementor session, object owner, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     return(value == null
                         ? null
                         : new ObjectTypeCacheEntry
     {
         EntityName = session.BestGuessEntityName(value),
         Id = await(ForeignKeys.GetEntityIdentifierIfNotUnsavedAsync(session.BestGuessEntityName(value), value, session, cancellationToken)).ConfigureAwait(false)
     });
 }
Beispiel #2
0
        private void addCollectionChangeWorkUnit(AuditProcess auditProcess, ISessionImplementor session, string fromEntityName, RelationDescription relDesc, object value)
        {
            // relDesc.getToEntityName() doesn't always return the entity name of the value - in case
            // of subclasses, this will be root class, no the actual class. So it can't be used here.
            string toEntityName;
            object id;

            if (value is INHibernateProxy newValueAsProxy)
            {
                toEntityName = session.BestGuessEntityName(value);
                id           = newValueAsProxy.HibernateLazyInitializer.Identifier;
                // We've got to initialize the object from the proxy to later read its state.
                value = Toolz.GetTargetFromProxy(session, newValueAsProxy);
            }
            else
            {
                toEntityName = session.GuessEntityName(value);

                var idMapper = VerCfg.EntCfg[toEntityName].IdMapper;
                id = idMapper.MapToIdFromEntity(value);
            }

            var toPropertyNames = VerCfg.EntCfg.ToPropertyNames(fromEntityName, relDesc.FromPropertyName, toEntityName);
            var toPropertyName  = toPropertyNames.First();

            auditProcess.AddWorkUnit(new CollectionChangeWorkUnit(session, toEntityName, toPropertyName, VerCfg, id, value));
        }
Beispiel #3
0
        public override async Task NullSafeSetAsync(DbCommand st, object value, int index, bool[] settable, ISessionImplementor session, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            object id;
            string entityName;

            if (value == null)
            {
                id         = null;
                entityName = null;
            }
            else
            {
                entityName = session.BestGuessEntityName(value);
                id         = await(ForeignKeys.GetEntityIdentifierIfNotUnsavedAsync(entityName, value, session, cancellationToken)).ConfigureAwait(false);
            }

            // metaType is assumed to be single-column type
            if (settable == null || settable[0])
            {
                await(metaType.NullSafeSetAsync(st, entityName, index, session, cancellationToken)).ConfigureAwait(false);
            }
            if (settable == null)
            {
                await(identifierType.NullSafeSetAsync(st, id, index + 1, session, cancellationToken)).ConfigureAwait(false);
            }
            else
            {
                bool[] idsettable = new bool[settable.Length - 1];
                Array.Copy(settable, 1, idsettable, 0, idsettable.Length);
                await(identifierType.NullSafeSetAsync(st, id, index + 1, idsettable, session, cancellationToken)).ConfigureAwait(false);
            }
        }
Beispiel #4
0
        public override void NullSafeSet(DbCommand st, object value, int index, bool[] settable, ISessionImplementor session)
        {
            object id;
            string entityName;

            if (value == null)
            {
                id         = null;
                entityName = null;
            }
            else
            {
                entityName = session.BestGuessEntityName(value);
                id         = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, value, session);
            }

            // metaType is assumed to be single-column type
            if (settable == null || settable[0])
            {
                metaType.NullSafeSet(st, entityName, index, session);
            }
            if (settable == null)
            {
                identifierType.NullSafeSet(st, id, index + 1, session);
            }
            else
            {
                bool[] idsettable = new bool[settable.Length - 1];
                Array.Copy(settable, 1, idsettable, 0, idsettable.Length);
                identifierType.NullSafeSet(st, id, index + 1, idsettable, session);
            }
        }
 private static object getIdentifier(ISessionImplementor session, object obj)
 {
     return(obj == null
                         ? null
                         : (obj is INHibernateProxy objAsProxy
                                 ? objAsProxy.HibernateLazyInitializer.Identifier
                                 : session.GetEntityPersister(session.BestGuessEntityName(obj), obj).GetIdentifier(obj)));
 }
Beispiel #6
0
 private static object Id(object component, ISessionImplementor session)
 {
     try
     {
         return(ForeignKeys.GetEntityIdentifierIfNotUnsaved(session.BestGuessEntityName(component), component, session));
     }
     catch (TransientObjectException)
     {
         return(null);
     }
 }
Beispiel #7
0
 private static async Task <object> IdAsync(object component, ISessionImplementor session, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     try
     {
         return(await(ForeignKeys.GetEntityIdentifierIfNotUnsavedAsync(session.BestGuessEntityName(component), component, session, cancellationToken)).ConfigureAwait(false));
     }
     catch (TransientObjectException)
     {
         return(null);
     }
 }
Beispiel #8
0
 public override object Replace(object original, object current, ISessionImplementor session, object owner,
                                IDictionary copiedAlready)
 {
     if (original == null)
     {
         return(null);
     }
     else
     {
         string entityName = session.BestGuessEntityName(original);
         object id         = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, original, session);
         return(session.InternalLoad(entityName, id, false, false));
     }
 }
Beispiel #9
0
 public override async Task <object> ReplaceAsync(object original, object current, ISessionImplementor session, object owner,
                                                  IDictionary copiedAlready, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (original == null)
     {
         return(null);
     }
     else
     {
         string entityName = session.BestGuessEntityName(original);
         object id         = await(ForeignKeys.GetEntityIdentifierIfNotUnsavedAsync(entityName, original, session, cancellationToken)).ConfigureAwait(false);
         return(await(session.InternalLoadAsync(entityName, id, false, false, cancellationToken)).ConfigureAwait(false));
     }
 }
Beispiel #10
0
 public Task <object> GetPropertyValueAsync(Object component, int i, ISessionImplementor session, CancellationToken cancellationToken)
 {
     if (cancellationToken.IsCancellationRequested)
     {
         return(Task.FromCanceled <object>(cancellationToken));
     }
     try
     {
         return(i == 0 ? Task.FromResult <object>(session.BestGuessEntityName(component)): IdAsync(component, session, cancellationToken));
     }
     catch (Exception ex)
     {
         return(Task.FromException <object>(ex));
     }
 }
Beispiel #11
0
        public override bool IsModified(object old, object current, bool[] checkable, ISessionImplementor session)
        {
            if (current == null)
            {
                return(old != null);
            }
            if (old == null)
            {
                return(current != null);
            }
            ObjectTypeCacheEntry holder = (ObjectTypeCacheEntry)old;

            bool[] idcheckable = new bool[checkable.Length - 1];
            Array.Copy(checkable, 1, idcheckable, 0, idcheckable.Length);
            return((checkable[0] && !holder.entityName.Equals(session.BestGuessEntityName(current))) ||
                   identifierType.IsModified(holder.id, Id(current, session), idcheckable, session));
        }
Beispiel #12
0
        public override async Task <bool> IsModifiedAsync(object old, object current, bool[] checkable, ISessionImplementor session, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (current == null)
            {
                return(old != null);
            }
            if (old == null)
            {
                return(current != null);
            }
            ObjectTypeCacheEntry holder = (ObjectTypeCacheEntry)old;

            bool[] idcheckable = new bool[checkable.Length - 1];
            Array.Copy(checkable, 1, idcheckable, 0, idcheckable.Length);
            return((checkable[0] && !holder.entityName.Equals(session.BestGuessEntityName(current))) ||
                   await(identifierType.IsModifiedAsync(holder.id, await(IdAsync(current, session, cancellationToken)).ConfigureAwait(false), idcheckable, session, cancellationToken)).ConfigureAwait(false));
        }
        public virtual async Task OnLoadAsync(LoadEvent @event, LoadType loadType, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ISessionImplementor source = @event.Session;

            IEntityPersister persister;

            if (@event.InstanceToLoad != null)
            {
                @event.EntityClassName = source.BestGuessEntityName(@event.InstanceToLoad);                 //the load() which takes an entity does not pass an entityName
                persister = source.GetEntityPersister(@event.EntityClassName, @event.InstanceToLoad);
            }
            else
            {
                persister = GetEntityPersister(source.Factory, @event.EntityClassName);
            }

            if (persister == null)
            {
                var message = new StringBuilder(512);
                message.AppendLine(string.Format("Unable to locate persister for the entity named '{0}'.", @event.EntityClassName));
                message.AppendLine("The persister define the persistence strategy for an entity.");
                message.AppendLine("Possible causes:");
                message.AppendLine(string.Format(" - The mapping for '{0}' was not added to the NHibernate configuration.", @event.EntityClassName));
                throw new HibernateException(message.ToString());
            }

            if (persister.IdentifierType.IsComponentType)
            {
                // skip this check for composite-ids relating to dom4j entity-mode;
                // alternatively, we could add a check to make sure the incoming id value is
                // an instance of Element...
            }
            else
            {
                System.Type idClass = persister.IdentifierType.ReturnedClass;
                if (idClass != null && !idClass.IsInstanceOfType(@event.EntityId) &&
                    !(@event.EntityId is DelayedPostInsertIdentifier))
                {
                    throw new TypeMismatchException("Provided id of the wrong type. Expected: " + idClass + ", got " + @event.EntityId.GetType());
                }
            }

            EntityKey keyToLoad = source.GenerateEntityKey(@event.EntityId, persister);

            try
            {
                if (loadType.IsNakedEntityReturned)
                {
                    //do not return a proxy!
                    //(this option indicates we are initializing a proxy)
                    @event.Result = await(LoadAsync(@event, persister, keyToLoad, loadType, cancellationToken)).ConfigureAwait(false);
                }
                else
                {
                    //return a proxy if appropriate
                    if (@event.LockMode == LockMode.None)
                    {
                        @event.Result = await(ProxyOrLoadAsync(@event, persister, keyToLoad, loadType, cancellationToken)).ConfigureAwait(false);
                    }
                    else
                    {
                        @event.Result = await(LockAndLoadAsync(@event, persister, keyToLoad, loadType, source, cancellationToken)).ConfigureAwait(false);
                    }
                }
            }
            catch (HibernateException e)
            {
                log.Info(e, "Error performing load command");
                throw;
            }
        }
Beispiel #14
0
		private static object Id(object component, ISessionImplementor session)
		{
			try
			{
				return ForeignKeys.GetEntityIdentifierIfNotUnsaved(session.BestGuessEntityName(component), component, session);
			}
			catch (TransientObjectException)
			{
				return null;
			}
		}
Beispiel #15
0
 public string BestGuessEntityName(object entity)
 {
     return(_session.BestGuessEntityName(entity));
 }
Beispiel #16
0
 public async Task <object[]> GetPropertyValuesAsync(object component, ISessionImplementor session, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     return(new object[] { session.BestGuessEntityName(component), await(IdAsync(component, session, cancellationToken)).ConfigureAwait(false) });
 }
Beispiel #17
0
        private void addCollectionChangeWorkUnitToAuditProcess(ISessionImplementor session, AuditProcess auditProcess, object value)
        {
            // relDesc.getToEntityName() doesn't always return the entity name of the value - in case
            // of subclasses, this will be root class, no the actual class. So it can't be used here.
            string toEntityName;
            object id;

            var newValueAsProxy = value as INHibernateProxy;
            if (newValueAsProxy != null)
            {
                toEntityName = session.BestGuessEntityName(value);
                id = newValueAsProxy.HibernateLazyInitializer.Identifier;
                // We've got to initialize the object from the proxy to later read its state.
                value = Toolz.GetTargetFromProxy((ISession) session, newValueAsProxy);
            }
            else
            {
                toEntityName = session.GuessEntityName(value);

                var idMapper = VerCfg.EntCfg[toEntityName].IdMapper;
                id = idMapper.MapToIdFromEntity(value);
            }

            auditProcess.AddWorkUnit(new CollectionChangeWorkUnit(session, toEntityName, VerCfg, id, value));
        }
Beispiel #18
0
        private void GenerateBidirectionalCollectionChangeWorkUnits(AuditSync verSync, IEntityPersister entityPersister,
                                                                    String entityName, Object[] newState, Object[] oldState,
                                                                    ISessionImplementor session)
        {
            // Checking if this is enabled in configuration ...
            if (!verCfg.GlobalCfg.isGenerateRevisionsForCollections())
            {
                return;
            }

            // Checks every property of the entity, if it is an "owned" to-one relation to another entity.
            // If the value of that property changed, and the relation is bi-directional, a new revision
            // for the related entity is generated.
            String[] propertyNames = entityPersister.PropertyNames;

            for (int i = 0; i < propertyNames.GetLength(0); i++)
            {
                String propertyName         = propertyNames[i];
                RelationDescription relDesc = verCfg.EntCfg.GetRelationDescription(entityName, propertyName);
                if (relDesc != null && relDesc.Bidirectional && relDesc.RelationType == RelationType.TO_ONE &&
                    relDesc.Insertable)
                {
                    // Checking for changes
                    Object oldValue = oldState == null ? null : oldState[i];
                    Object newValue = newState == null ? null : newState[i];

                    if (!Toolz.EntitiesEqual(session, oldValue, newValue))
                    {
                        // We have to generate changes both in the old collection (size decreses) and new collection
                        // (size increases).

                        //<TODO Simon: doua if-uri cu cod duplicat, refact.
                        if (newValue != null)
                        {
                            // relDesc.getToEntityName() doesn't always return the entity name of the value - in case
                            // of subclasses, this will be root class, no the actual class. So it can't be used here.
                            String toEntityName;

                            // Java: Serializable id
                            object id;

                            if (newValue is INHibernateProxy)
                            {
                                INHibernateProxy hibernateProxy = (INHibernateProxy)newValue;
                                toEntityName = session.BestGuessEntityName(newValue);
                                id           = hibernateProxy.HibernateLazyInitializer.Identifier;
                                // We've got to initialize the object from the proxy to later read its state.
                                newValue = NHibernate.Envers.Tools.Toolz.GetTargetFromProxy(session.Factory, hibernateProxy);
                            }
                            else
                            {
                                toEntityName = session.GuessEntityName(newValue);

                                IIdMapper idMapper = verCfg.EntCfg[toEntityName].GetIdMapper();
                                id = idMapper.MapToIdFromEntity(newValue);
                            }

                            verSync.AddWorkUnit(new CollectionChangeWorkUnit(session, toEntityName, verCfg, id, newValue));
                        }

                        if (oldValue != null)
                        {
                            String toEntityName;
                            object id;

                            if (oldValue is INHibernateProxy)
                            {
                                INHibernateProxy hibernateProxy = (INHibernateProxy)oldValue;
                                toEntityName = session.BestGuessEntityName(oldValue);
                                id           = hibernateProxy.HibernateLazyInitializer.Identifier;
                                // We've got to initialize the object as we'll read it's state anyway.
                                oldValue = Toolz.GetTargetFromProxy(session.Factory, hibernateProxy);
                            }
                            else
                            {
                                toEntityName = session.GuessEntityName(oldValue);

                                IIdMapper idMapper = verCfg.EntCfg[toEntityName].GetIdMapper();
                                id = idMapper.MapToIdFromEntity(oldValue);
                            }

                            verSync.AddWorkUnit(new CollectionChangeWorkUnit(session, toEntityName, verCfg, id, oldValue));
                        }
                    }
                }
            }
        }
Beispiel #19
0
		public override object Disassemble(object value, ISessionImplementor session, object owner)
		{
			return value == null ? null : 
				new ObjectTypeCacheEntry(session.BestGuessEntityName(value), 
				ForeignKeys.GetEntityIdentifierIfNotUnsaved(session.BestGuessEntityName(value), value, session));
		}
Beispiel #20
0
		public override void NullSafeSet(IDbCommand st, object value, int index, bool[] settable, ISessionImplementor session)
		{
			object id;
			string entityName;
			if (value == null)
			{
				id = null;
				entityName = null;
			}
			else
			{
				entityName = session.BestGuessEntityName(value);
				id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, value, session);
			}

			// metaType is assumed to be single-column type
			if (settable == null || settable[0])
			{
				metaType.NullSafeSet(st, entityName, index, session);
			}
			if (settable == null)
			{
				identifierType.NullSafeSet(st, id, index + 1, session);
			}
			else
			{
				bool[] idsettable = new bool[settable.Length - 1];
				Array.Copy(settable, 1, idsettable, 0, idsettable.Length);
				identifierType.NullSafeSet(st, id, index + 1, idsettable, session);
			}
		}
Beispiel #21
0
 public object GetPropertyValue(Object component, int i, ISessionImplementor session)
 {
     return(i == 0 ? session.BestGuessEntityName(component) : Id(component, session));
 }
Beispiel #22
0
 public object[] GetPropertyValues(object component, ISessionImplementor session)
 {
     return(new object[] { session.BestGuessEntityName(component), Id(component, session) });
 }
Beispiel #23
0
		public override object Replace(object original, object current, ISessionImplementor session, object owner,
									   IDictionary copiedAlready)
		{
			if (original == null)
			{
				return null;
			}
			else
			{
				string entityName = session.BestGuessEntityName(original);
				object id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, original, session);
				return session.InternalLoad(entityName, id, false, false);
			}
		}
Beispiel #24
0
 public override object Disassemble(object value, ISessionImplementor session, object owner)
 {
     return(value == null ? null :
            new ObjectTypeCacheEntry(session.BestGuessEntityName(value),
                                     ForeignKeys.GetEntityIdentifierIfNotUnsaved(session.BestGuessEntityName(value), value, session)));
 }
 public String bestGuessEntityName(Object obj)
 {
     return(delegat.BestGuessEntityName(obj));
 }
Beispiel #26
0
		public object GetPropertyValue(Object component, int i, ISessionImplementor session)
		{
			return i == 0 ? session.BestGuessEntityName(component) : Id(component, session);
		}
Beispiel #27
0
		public object[] GetPropertyValues(object component, ISessionImplementor session)
		{
			return new object[] { session.BestGuessEntityName(component), Id(component, session) };
		}
Beispiel #28
0
		public override bool IsModified(object old, object current, bool[] checkable, ISessionImplementor session)
		{
			if (current == null)
				return old != null;
			if (old == null)
				return current != null;
			ObjectTypeCacheEntry holder = (ObjectTypeCacheEntry)old;
			bool[] idcheckable = new bool[checkable.Length - 1];
			Array.Copy(checkable, 1, idcheckable, 0, idcheckable.Length);
			return (checkable[0] && !holder.entityName.Equals(session.BestGuessEntityName(current))) || 
				identifierType.IsModified(holder.id, Id(current, session), idcheckable, session);
		}
        private void GenerateBidirectionalCollectionChangeWorkUnits(AuditSync verSync, IEntityPersister entityPersister,
                                                                    String entityName, Object[] newState, Object[] oldState,
                                                                    ISessionImplementor session) {
            // Checking if this is enabled in configuration ...
            if (!verCfg.GlobalCfg.isGenerateRevisionsForCollections()) {
                return;
            }

            // Checks every property of the entity, if it is an "owned" to-one relation to another entity.
            // If the value of that property changed, and the relation is bi-directional, a new revision
            // for the related entity is generated.
            String[] propertyNames = entityPersister.PropertyNames;

            for (int i=0; i<propertyNames.GetLength(0); i++) {
                String propertyName = propertyNames[i];
                RelationDescription relDesc = verCfg.EntCfg.GetRelationDescription(entityName, propertyName);
                if (relDesc != null && relDesc.Bidirectional && relDesc.RelationType == RelationType.TO_ONE &&
                        relDesc.Insertable) {
                    // Checking for changes
                    Object oldValue = oldState == null ? null : oldState[i];
                    Object newValue = newState == null ? null : newState[i];

                    if (!Toolz.EntitiesEqual(session, oldValue, newValue)) {
                        // We have to generate changes both in the old collection (size decreses) and new collection
                        // (size increases).

                        //<TODO Simon: doua if-uri cu cod duplicat, refact.
                        if (newValue != null) {
                            // relDesc.getToEntityName() doesn't always return the entity name of the value - in case
                            // of subclasses, this will be root class, no the actual class. So it can't be used here.
                            String toEntityName;
                            
                            // Java: Serializable id
						    object id;

                            if (newValue is INHibernateProxy) {
                    	        INHibernateProxy hibernateProxy = (INHibernateProxy) newValue;
                    	        toEntityName = session.BestGuessEntityName(newValue);
                    	        id = hibernateProxy.HibernateLazyInitializer.Identifier;
							    // We've got to initialize the object from the proxy to later read its state.   
							    newValue = NHibernate.Envers.Tools.Toolz.GetTargetFromProxy(session.Factory, hibernateProxy);
                    	    } else {
                    		    toEntityName =  session.GuessEntityName(newValue);

							    IIdMapper idMapper = verCfg.EntCfg[toEntityName].GetIdMapper();
                         	    id = idMapper.MapToIdFromEntity(newValue);
                    	    }

                            verSync.AddWorkUnit(new CollectionChangeWorkUnit(session, toEntityName, verCfg, id, newValue));
                        }

                        if (oldValue != null) {
                    	    String toEntityName;
						    object id;

                    	    if(oldValue is INHibernateProxy) {
                    	        INHibernateProxy hibernateProxy = (INHibernateProxy) oldValue;
                    	        toEntityName = session.BestGuessEntityName(oldValue);
                    	        id = hibernateProxy.HibernateLazyInitializer.Identifier;
							    // We've got to initialize the object as we'll read it's state anyway.
							    oldValue = Toolz.GetTargetFromProxy(session.Factory, hibernateProxy);
                    	    } else {
                    		    toEntityName =  session.GuessEntityName(oldValue);

							    IIdMapper idMapper = verCfg.EntCfg[toEntityName].GetIdMapper();
							    id = idMapper.MapToIdFromEntity(oldValue);
                    	    }
    						
                            verSync.AddWorkUnit(new CollectionChangeWorkUnit(session, toEntityName, verCfg, id, oldValue));
                        }
                    }
                }
            }
        }