Beispiel #1
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 #2
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));
                        }
                    }
                }
            }
        }