Exemplo n.º 1
0
        public ISet <Tuple <string, System.Type> > FindEntityTypes(long revision)
        {
            ArgumentsTools.CheckPositive(revision, "revision");
            if (!_verCfg.GlobalCfg.IsTrackEntitiesChangedInRevisionEnabled)
            {
                throw new AuditException(@"This query is designed for Envers default mechanism of tracking entities modified in a given revision." +
                                         " Extend DefaultTrackingModifiedEntitiesRevisionEntity, utilize ModifiedEntityNamesAttribute or set " +
                                         "'nhibernate.envers.track_entities_changed_in_revision' parameter to true.");
            }
            var session            = _auditReaderImplementor.Session;
            var sessionImplementor = _auditReaderImplementor.SessionImplementor;
            var revisions          = new HashSet <long> {
                revision
            };
            var query        = _verCfg.RevisionInfoQueryCreator.RevisionsQuery(session, revisions);
            var revisionInfo = query.UniqueResult();

            if (revisionInfo != null)
            {
                // If revision exists
                var entityNames = _verCfg.ModifiedEntityNamesReader.ModifiedEntityTypes(revisionInfo);
                if (entityNames != null)
                {
                    // Generate result that contains entity names and corresponding CLR classes.
                    var result = new HashSet <Tuple <string, System.Type> >();
                    foreach (var entityName in entityNames)
                    {
                        result.Add(new Tuple <string, System.Type>(entityName, Toolz.ResolveEntityClass(sessionImplementor, entityName)));
                    }
                    return(result);
                }
            }
            return(new HashSet <Tuple <string, System.Type> >());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Notifies <see cref="IRevisionInfoGenerator"/> about changes made in the current revision. Provides information
        /// about modified entity class, entity name and its id, as well as <see cref="RevisionType"/> and revision log entity.
        /// </summary>
        /// <param name="currentRevisionData">Revision log entity.</param>
        /// <param name="vwu">Performed work unit.</param>
        public void EntityChanged(object currentRevisionData, IAuditWorkUnit vwu)
        {
            var entityId = vwu.EntityId;

            if (entityId is PersistentCollectionChangeWorkUnit.PersistentCollectionChangeWorkUnitId idAsPersistentColl)
            {
                entityId = idAsPersistentColl.OwnerId;
            }
            var entClass = Toolz.ResolveEntityClass(_sessionImplementor, vwu.EntityName);

            _revisionInfoGenerator.EntityChanged(entClass, vwu.EntityName, entityId, vwu.RevisionType, currentRevisionData);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates an entity instance based on an entry from the versions table.
        /// </summary>
        /// <param name="entityName">Name of the entity, which instances should be read.</param>
        /// <param name="versionsEntity">An entry in the versions table, from which data should be mapped.</param>
        /// <param name="revision">Revision at which this entity was read.</param>
        /// <returns>An entity instance, with versioned properties set as in the versionsEntity map, and proxies created for collections.</returns>
        public object CreateInstanceFromVersionsEntity(string entityName, IDictionary versionsEntity, long revision)
        {
            const string typeKey = "$type$";

            if (versionsEntity == null)
            {
                return(null);
            }

            if (versionsEntity.Contains(typeKey))
            {
                entityName = verCfg.EntCfg.GetEntityNameForVersionsEntityName((string)versionsEntity[typeKey]);
            }

            // First mapping the primary key
            var idMapper   = verCfg.EntCfg[entityName].IdMapper;
            var originalId = (IDictionary)versionsEntity[verCfg.AuditEntCfg.OriginalIdPropName];

            var primaryKey = idMapper.MapToIdFromMap(originalId);

            object ret;

            // Checking if the entity is in cache
            if (versionsReader.FirstLevelCache.TryGetValue(entityName, revision, primaryKey, out ret))
            {
                return(ret);
            }

            // If it is not in the cache, creating a new entity instance
            try
            {
                var cls = Toolz.ResolveEntityClass(versionsReader.SessionImplementor, entityName);
                ret = verCfg.EntCfg[entityName].Factory(cls);
            }
            catch (Exception e)
            {
                throw new AuditException("Cannot create instance of type " + entityName, e);
            }

            // Putting the newly created entity instance into the first level cache, in case a one-to-one bidirectional
            // relation is present (which is eagerly loaded).
            versionsReader.FirstLevelCache.Add(entityName, revision, primaryKey, ret);

            verCfg.EntCfg[entityName].PropertyMapper.MapToEntityFromMap(verCfg, ret, versionsEntity, primaryKey, versionsReader, revision);
            idMapper.MapToEntityFromMap(ret, originalId);

            verCfg.GlobalCfg.PostInstantiationListener.PostInstantiate(ret);

            // Put entity on entityName cache after mapping it from the map representation
            versionsReader.FirstLevelCache.AddEntityName(primaryKey, revision, ret, entityName);

            return(ret);
        }