Пример #1
0
        public bool MapToMapFromEntity(ISessionImplementor session, IDictionary <String, Object> data, Object newObj, Object oldObj)
        {
            //Simon 27/06/2010 - era new LinkedHashMap
            IDictionary <String, Object> newData = new Dictionary <String, Object>();

            data.Add(propertyData.Name, newData);

            // If this property is originally non-insertable, but made insertable because it is in a many-to-one "fake"
            // bi-directional relation, we always store the "old", unchaged data, to prevent storing changes made
            // to this field. It is the responsibility of the collection to properly update it if it really changed.
            delegat.MapToMapFromEntity(newData, nonInsertableFake ? oldObj : newObj);

            //noinspection SimplifiableConditionalExpression
            return(nonInsertableFake ? false : !Toolz.EntitiesEqual(session, newObj, oldObj));
        }
Пример #2
0
        private void generateBidirectionalCollectionChangeWorkUnits(AuditProcess auditProcess,
                                                                    IEntityPersister entityPersister,
                                                                    string entityName,
                                                                    IList <object> newState,
                                                                    IList <object> oldState,
                                                                    ISessionImplementor session)
        {
            // Checking if this is enabled in configuration ...
            if (!VerCfg.GlobalCfg.GenerateRevisionsForCollections)
            {
                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.
            var propertyNames = entityPersister.PropertyNames;

            for (var i = 0; i < propertyNames.GetLength(0); i++)
            {
                var propertyName = propertyNames[i];
                var relDesc      = VerCfg.EntCfg.GetRelationDescription(entityName, propertyName);
                if (relDesc != null &&
                    relDesc.Bidirectional &&
                    relDesc.RelationType == RelationType.ToOne &&
                    relDesc.Insertable)
                {
                    // Checking for changes
                    var oldValue = oldState?[i];
                    var newValue = 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).
                        if (newValue != null)
                        {
                            addCollectionChangeWorkUnit(auditProcess, session, entityName, relDesc, newValue);
                        }
                        if (oldValue != null)
                        {
                            addCollectionChangeWorkUnit(auditProcess, session, entityName, relDesc, oldValue);
                        }
                    }
                }
            }
        }
Пример #3
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));
                        }
                    }
                }
            }
        }
Пример #4
0
 private bool checkModified(ISessionImplementor session, object newObj, object oldObj)
 {
     return(!_nonInsertableFake && !Toolz.EntitiesEqual(session, newObj, oldObj));
 }