public AuditEntryState(ObjectContext objectContext, ObjectStateEntry objectStateEntry)
        {
            if (objectStateEntry == null)
                throw new ArgumentNullException("objectStateEntry");

            if (objectStateEntry.Entity == null)
                throw new ArgumentException("The Entity property is null for the specified ObjectStateEntry.", "objectStateEntry");

            if (objectContext == null)
            {
                throw new ArgumentNullException("objectContext");
            }

            ObjectStateEntry = objectStateEntry;
            ObjectContext = objectContext;
            ObjectType = ObjectContext.GetObjectType(objectStateEntry.Entity.GetType());

            Entity = objectStateEntry.Entity;
            EntityType = objectContext.MetadataWorkspace.GetItems(DataSpace.OSpace).OfType<EntityType>().FirstOrDefault(x => x.Name == ObjectType.Name);
            EntityAccessor = TypeAccessor.GetAccessor(ObjectType);

            AuditEntity = new AuditEntity(objectStateEntry.Entity)
            {
                Action = GetAction(objectStateEntry),
            };
        }
        public void SelectRevisionTypeQuery()
        {
            var result = AuditReader().CreateQuery()
                         .ForRevisionsOfEntity(typeof(StrIntTestEntity), false, true)
                         .AddProjection(AuditEntity.RevisionType())
                         .Add(AuditEntity.Id().Eq(id1))
                         .GetResultList <RevisionType>();

            Assert.AreEqual(RevisionType.Added, result[0]);
            Assert.AreEqual(RevisionType.Modified, result[1]);
            Assert.AreEqual(RevisionType.Deleted, result[2]);
        }
        public void VerifyInsensitiveLikeWithMatchMode()
        {
            var site1 = new StrIntTestEntity {
                Id = id1, Number = 10, Str = "aBc"
            };

            AuditReader().CreateQuery()
            .ForRevisionsOf <StrIntTestEntity>(false)
            .Add(AuditEntity.Property("Str").InsensitiveLike("BC", MatchMode.Anywhere))
            .Single()
            .Should().Be.EqualTo(site1);
        }
 public void VerifyEntityIdOrdering()
 {
     AuditReader().CreateQuery().ForRevisionsOf <IntTestEntity>(true)
     .Add(AuditEntity.RevisionNumber().Lt(2))
     .AddOrder(AuditEntity.Id().Desc())
     .Results()
     .Should().Have.SameSequenceAs(new IntTestEntity {
         Id = 2, Number = 10
     }, new IntTestEntity {
         Id = 1, Number = 2
     });
 }
Пример #5
0
 public void VerifyHistoryOfOwning()
 {
     AuditReader().Find <OneToOneOwningEntity>(id, 1)
     .Owned.Should().Not.Be.Null();
     AuditReader().Find <OneToOneOwningEntity>(id, 2)
     .Owned.Should().Be.Null();
     AuditReader().CreateQuery().ForRevisionsOf <OneToOneOwningEntity>(true)
     .Add(AuditEntity.Id().Eq(id))
     .Add(AuditEntity.RevisionNumber().Eq(3))
     .Results().First()
     .Owned.Should().Be.Null();
 }
Пример #6
0
        public void CanQueryOnAccessNoneManyToOne()
        {
            var parent = new Parent {
                Id = parentId
            };

            AuditReader().CreateQuery()
            .ForHistoryOf <Child, DefaultRevisionEntity>()
            .Add(AuditEntity.Property("Parent2").Eq(parent))
            .Results()
            .Count().Should().Be.EqualTo(2);
        }
        public void Post([FromBody] AuditEntity auditEntity)
        {
            var existingEntity = GetEntity(auditEntity.Application, auditEntity.EntityId, auditEntity.EntityType);

            if (!string.IsNullOrWhiteSpace(existingEntity))
            {
                CalculatePatch(existingEntity, auditEntity);
            }

            _context.Add(auditEntity);
            _context.SaveChanges();
        }
        public void VerifyHistoryOfParent()
        {
            AuditReader().Find <StringSetEntity>(parentId, 1)
            .Strings.Should().Be.Empty();
            AuditReader().Find <StringSetEntity>(parentId, 2)
            .Strings.Single().Should().Be.EqualTo("2");
            var rev3 = AuditReader().CreateQuery().ForRevisionsOfEntity(typeof(StringSetEntity), true, true)
                       .Add(AuditEntity.Id().Eq(parentId))
                       .Add(AuditEntity.RevisionNumber().Eq(3))
                       .GetSingleResult <StringSetEntity>();

            rev3.Strings.Single().Should().Be.EqualTo("2");
        }
Пример #9
0
        public void CanQueryOnAccessNoopData()
        {
            var res = (IList)AuditReader().CreateQuery()
                      .ForRevisionsOfEntity(typeof(Entity), false, false)
                      .Add(AuditEntity.Property("Data3").Eq(2))
                      .GetSingleResult();

            var ent = (Entity)res[0];
            var rev = (DefaultRevisionEntity)res[1];

            rev.Id.Should().Be.EqualTo(2);
            ent.Data.Should().Be.EqualTo(2);
        }
Пример #10
0
 public void VerifyBetweenInsideDisjunction()
 {
     var result = AuditReader().CreateQuery()
         .ForRevisionsOf<StrIntTestEntity>()
         .Add(AuditEntity.Disjunction()
              	.Add(AuditEntity.Property("Number").Between(0, 5))
              	.Add(AuditEntity.Property("Number").Between(20, 100)))
         .Results();
     foreach (var number in result.Select(entity => entity.Number))
     {
         Assert.That(number, Is.InRange(0, 5).Or.InRange(20, 100));
     }
 }
Пример #11
0
        public IList <object[]> GetRawRevisions(Type type, RevisionType revisionType, DateTime?startDate, DateTime?endDate)
        {
            IAuditQuery q = AuditReaderFactory.Get(Session).CreateQuery()
                            .ForRevisionsOfEntity(type, false, true)
                            .Add(AuditEntity.RevisionType().Eq(revisionType))
                            .AddOrder(AuditEntity.RevisionProperty("REVTSTMP").Desc());

            if (startDate.HasValue && endDate.HasValue)
            {
                q = q.Add(AuditEntity.RevisionProperty("REVTSTMP").Between(startDate, endDate));
            }
            return(q.GetResultList <object[]>());
        }
Пример #12
0
        public void VerifyHistoryOfParent()
        {
            AuditReader().Find <BagParent>(parentId, 1)
            .Children.Single().Name.Should().Be.EqualTo("child1");
            AuditReader().Find <BagParent>(parentId, 2)
            .Children.Single().Name.Should().Be.EqualTo("child12");
            var rev3 = AuditReader().CreateQuery().ForRevisionsOfEntity(typeof(BagParent), true, true)
                       .Add(AuditEntity.Id().Eq(parentId))
                       .Add(AuditEntity.RevisionNumber().Eq(3))
                       .GetSingleResult <BagParent>();

            rev3.Children.Single().Name.Should().Be.EqualTo("child12");
        }
Пример #13
0
        public void VerifyOwnedManyToManyDifferentRevisions()
        {
            var res = AuditReader().CreateQuery().ForHistoryOf <SetOwningEntity, DefaultRevisionEntity>()
                      .Add(AuditEntity.Id().Eq(7))
                      .Add(AuditEntity.RevisionType().Eq(RevisionType.Deleted))
                      .Results().First();

            res.RevisionEntity.Id.Should().Be.EqualTo(9);
            res.Entity.Data.Should().Be.EqualTo("Demo Data 2");
            res.Entity.References.Should().Have.SameValuesAs(new SetOwnedEntity {
                Id = 8, Data = "Example Data 2"
            });
        }
Пример #14
0
        public void VerifyReferringOneToManySameRevision()
        {
            var res = AuditReader().CreateQuery().ForHistoryOf <SetRefEdEntity, DefaultRevisionEntity>()
                      .Add(AuditEntity.Id().Eq(1))
                      .Add(AuditEntity.RevisionType().Eq(RevisionType.Deleted))
                      .Results().First();

            res.RevisionEntity.Id.Should().Be.EqualTo(2);
            res.Entity.Data.Should().Be.EqualTo("Demo Data 1");
            res.Entity.Reffering.Should().Have.SameValuesAs(new SetRefIngEntity {
                Id = 2, Data = "Example Data 1"
            });
        }
Пример #15
0
        public IList <AuditTrailDTO> GetEventRelationshipRevisions(Event e)
        {
            IList <object[]> subjects = AuditReaderFactory.Get(Session).CreateQuery()
                                        .ForRevisionsOfEntity(typeof(EventRelationship), false, true)
                                        .Add(AuditEntity.Property("SubjectEvent").Eq(e))
                                        .GetResultList <object[]>();

            IList <object[]> objects = AuditReaderFactory.Get(Session).CreateQuery()
                                       .ForRevisionsOfEntity(typeof(EventRelationship), false, true)
                                       .Add(AuditEntity.Property("ObjectEvent").Eq(e))
                                       .GetResultList <object[]>();

            return(this.AddDifferences <EventRelationship>(this.TransformToDto(subjects.Concat(objects).ToList())));
        }
        private long ValidateACExpression(AuditEntity auditEntity, string expression)
        {
            long result = 0;
            // get key
            long       id      = (long)auditEntity.Keys.SingleOrDefault(k => k.Name == "Id").Value;
            BaseEntity entity  = this.GetByEntityIdAndType(id, auditEntity.EntityType.Name) as BaseEntity;
            bool       isMatch = FleeExpression.Evaluate <bool>(entity, expression);

            if (isMatch == true)
            {
                result = id;
            }
            return(result);
        }
Пример #17
0
        public IList <object[]> GetRawRevisions(Person person)
        {
            IList <object[]> asSubject = AuditReaderFactory.Get(Session).CreateQuery()
                                         .ForRevisionsOfEntity(typeof(ActionTaken), false, true)
                                         .Add(AuditEntity.Property("SubjectPerson").Eq(person))
                                         .GetResultList <object[]>();

            IList <object[]> asObject = AuditReaderFactory.Get(Session).CreateQuery()
                                        .ForRevisionsOfEntity(typeof(ActionTaken), false, true)
                                        .Add(AuditEntity.Property("ObjectPerson").Eq(person))
                                        .GetResultList <object[]>();

            return(asSubject.Concat(asObject).ToList <object[]>());
        }
Пример #18
0
        public override SaaS.Common.SaveResult Save(SaaS.Common.IEntityBase data)
        {
            AuditEntity auditEntity = data as AuditEntity;
            SubmitData  submitData  = auditEntity.Data as SubmitData;
            var         dataResult  = Submit(submitData);

            return(new SaaS.Common.SaveResult(new AuditEntity()
            {
                Data = dataResult
            })
            {
                State = SaaS.Common.SaveResultState.Success
            });
        }
Пример #19
0
        private static AuditEntity GetAuditEntity(EntityEntry entry, IEntityInfo entityInfo)
        {
            AuditEntity audit = new AuditEntity()
            {
                Name = entityInfo.Name, TypeName = entityInfo.TypeName, OperateType = OperateType.Insert
            };

            EntityProperty[] entityProperties = entityInfo.Properties;
            foreach (IProperty property in entry.CurrentValues.Properties)
            {
                if (property.IsConcurrencyToken)
                {
                    continue;
                }
                string name = property.Name;
                if (property.IsPrimaryKey())
                {
                    audit.EntityKey = entry.State == EntityState.Deleted
                        ? entry.Property(property.Name).OriginalValue?.ToString()
                        : entry.Property(property.Name).CurrentValue?.ToString();
                }
                AuditEntityProperty auditProperty = new AuditEntityProperty()
                {
                    FieldName   = name,
                    DisplayName = entityProperties.First(m => m.Name == name).Display,
                    DataType    = property.ClrType.ToString()
                };
                if (entry.State == EntityState.Added)
                {
                    auditProperty.NewValue = entry.Property(property.Name).CurrentValue?.ToString();
                }
                else if (entry.State == EntityState.Deleted)
                {
                    auditProperty.OriginalValue = entry.Property(property.Name).OriginalValue?.ToString();
                }
                else if (entry.State == EntityState.Modified)
                {
                    string currentValue  = entry.Property(property.Name).CurrentValue?.ToString();
                    string originalValue = entry.Property(property.Name).OriginalValue?.ToString();
                    if (currentValue != originalValue)
                    {
                        auditProperty.NewValue      = currentValue;
                        auditProperty.OriginalValue = originalValue;
                    }
                }
                audit.Properties.Add(auditProperty);
            }
            return(audit);
        }
        public void VerifyRevisionsPropertyEqQuery()
        {
            var revsId1 = AuditReader().CreateQuery()
                          .ForRevisionsOfEntity(typeof(StrIntTestEntity), false, true)
                          .Add(AuditEntity.Id().Eq(id1))
                          .GetResultList <IList>();

            Assert.AreEqual(2, revsId1.Count);
            Assert.AreEqual(new StrIntTestEntity {
                Str = "a", Number = 10, Id = id1
            }, revsId1[0][0]);
            Assert.AreEqual(new StrIntTestEntity {
                Str = "a", Number = 10, Id = id1
            }, revsId1[1][0]);
        }
Пример #21
0
        public void VerifyRevisionsOfId2Query()
        {
            var result = AuditReader().CreateQuery()
                         .ForRevisionsOfEntity(typeof(StrIntTestEntity), false, true)
                         .Add(AuditEntity.Id().Eq(id2))
                         .GetResultList();

            Assert.AreEqual(new StrIntTestEntity {
                Str = "b", Number = 15, Id = id2
            }, ((IList)result[0])[0]);
            var customRevEntity1 = ((IList)result[0])[1];

            Assert.IsInstanceOf <CustomRevEntity>(customRevEntity1);
            Assert.AreEqual(1, ((CustomRevEntity)customRevEntity1).CustomId);
        }
Пример #22
0
 public void VerifyEntityIdRestriction()
 {
     AuditReader().CreateQuery().ForRevisionsOf <IntTestEntity>(true)
     .Add(AuditEntity.Id().Between(2, 3))
     .Results()
     .Should().Have.SameValuesAs(new IntTestEntity {
         Id = 2, Number = 10
     },
                                 new IntTestEntity {
         Id = 3, Number = 8
     },
                                 new IntTestEntity {
         Id = 2, Number = 52
     });
 }
Пример #23
0
        public void ShouldFindEntitiesRemovedAtRevision()
        {
            var result = AuditReader().CreateQuery()
                .ForEntitiesModifiedAtRevision(typeof(StrIntTestEntity).FullName, 4)
                .GetResultList<StrIntTestEntity>();
            var revisionType = AuditReader().CreateQuery()
                .ForEntitiesModifiedAtRevision(typeof(StrIntTestEntity), 4)
                .AddProjection(AuditEntity.RevisionType())
                .Add(AuditEntity.Id().Eq(id1))
                .GetSingleResult<RevisionType>();

            result.Should().Have.SameValuesAs(new StrIntTestEntity { Id = id1 });

            revisionType.Should().Be.EqualTo(RevisionType.Deleted);
        }
 public void VerifyHasChanged()
 {
     QueryForPropertyHasChangedWithDeleted(typeof(BasicTestEntity1), id, "Str1")
     .ExtractRevisionNumbersFromRevision()
     .Should().Have.SameSequenceAs(1);
     QueryForPropertyHasChangedWithDeleted(typeof(BasicTestEntity1), id, "Long1")
     .ExtractRevisionNumbersFromRevision()
     .Should().Have.SameSequenceAs(1);
     AuditReader().CreateQuery().ForRevisionsOfEntity(typeof(BasicTestEntity1), false, true)
     .Add(AuditEntity.Property("Str1").HasChanged())
     .Add(AuditEntity.Property("Long1").HasChanged())
     .GetResultList()
     .ExtractRevisionNumbersFromRevision()
     .Should().Have.SameSequenceAs(1);
 }
Пример #25
0
        private void CalculatePatch(string originalEntity, AuditEntity auditEntity)
        {
            var jdp = new JsonDiffPatch();

            foreach (var patch in GetPatches(auditEntity.Application, auditEntity.EntityId, auditEntity.EntityType))
            {
                originalEntity = jdp.Patch(originalEntity, patch);
            }

            var left     = JToken.Parse(originalEntity);
            var right    = JToken.Parse(auditEntity.Entity);
            var newPatch = jdp.Diff(left, right);

            auditEntity.Entity = null;
            auditEntity.Patch  = newPatch.ToString(Formatting.None);
        }
        public void VerifyHasChangedPerson2()
        {
            AuditReader().CreateQuery().ForRevisionsOfEntity("Personaje", false, false)
            .Add(AuditEntity.Id().Eq(idPers2))
            .Add(AuditEntity.Property("Cars").HasChanged())
            .GetResultList()
            .ExtractRevisionNumbersFromRevision()
            .Should().Have.SameSequenceAs(1, 2);

            AuditReader().CreateQuery().ForRevisionsOfEntity("Personaje", false, false)
            .Add(AuditEntity.Id().Eq(idPers2))
            .Add(AuditEntity.Property("Cars").HasNotChanged())
            .GetResultList()
            .ExtractRevisionNumbersFromRevision()
            .Should().Be.Empty();
        }
Пример #27
0
        public void VerifyRevisionPropertyRestriction()
        {
            var result = AuditReader().CreateQuery()
                         .ForRevisionsOfEntity(typeof(StrIntTestEntity), false, true)
                         .Add(AuditEntity.Id().Eq(id1))
                         .Add(AuditEntity.RevisionProperty("CustomTimestamp").Ge(timestamp))
                         .GetResultList();

            Assert.AreEqual(new StrIntTestEntity {
                Str = "c", Number = 10, Id = id1
            }, ((IList)result[0])[0]);
            var customRevEntity1 = ((IList)result[0])[1];

            Assert.IsInstanceOf <CustomRevEntity>(customRevEntity1);
            Assert.IsTrue(((CustomRevEntity)customRevEntity1).CustomTimestamp >= timestamp);
        }
Пример #28
0
        public void ShouldFindEntitiesModifiedAtRevision()
        {
            var result = AuditReader().CreateQuery()
                .ForEntitiesModifiedAtRevision(typeof(StrIntTestEntity), 2)
                .GetResultList<StrIntTestEntity>();
            var revisionType = AuditReader().CreateQuery()
                .ForEntitiesModifiedAtRevision(typeof(StrIntTestEntity), 2)
                .AddProjection(AuditEntity.RevisionType())
                .Add(AuditEntity.Id().Eq(id1))
                .GetSingleResult<RevisionType>();

            result.Should().Have.SameValuesAs(new StrIntTestEntity { Id = id1, Str = "aBc", Number = 10 },
                                                            new StrIntTestEntity { Id = id2, Str = "a", Number = 20 });

            revisionType.Should().Be.EqualTo(RevisionType.Modified);
        }
        public void VerifyRevisionOfEntityWithoutDelete()
        {
            var result = AuditReader().CreateQuery()
                         .ForRevisionsOfEntity(typeof(StrIntTestEntity), false, false)
                         .Add(AuditEntity.Id().Eq(id2))
                         .GetResultList();

            Assert.AreEqual(1, result.Count);
            var res = (object[])result[0];

            Assert.AreEqual(new StrIntTestEntity {
                Str = "b", Number = 11, Id = id2
            }, res[0]);
            Assert.AreEqual(1, ((DefaultRevisionEntity)res[1]).Id);
            Assert.AreEqual(RevisionType.Added, res[2]);
        }
Пример #30
0
        public IEnumerable <long> GetRevisions(string entityName, object primaryKey)
        {
            ArgumentsTools.CheckNotNull(primaryKey, "Primary key");

            if (!verCfg.EntCfg.IsVersioned(entityName))
            {
                throw new NotAuditedException(entityName, entityName + " is not versioned!");
            }

            var resultList = CreateQuery().ForRevisionsOfEntity(entityName, false, true)
                             .AddProjection(AuditEntity.RevisionNumber())
                             .Add(AuditEntity.Id().Eq(primaryKey))
                             .GetResultList();

            return(from object revision in resultList select Convert.ToInt64(revision));
        }
        public void VerifyRevisionProjectionQuery()
        {
            var result = AuditReader().CreateQuery()
                         .ForRevisionsOfEntity(typeof(StrIntTestEntity), false, true)
                         .AddProjection(AuditEntity.RevisionNumber().Max())
                         .AddProjection(AuditEntity.RevisionNumber().Count())
                         .AddProjection(AuditEntity.RevisionNumber().CountDistinct())
                         .AddProjection(AuditEntity.RevisionNumber().Min())
                         .Add(AuditEntity.Id().Eq(id1))
                         .GetSingleResult <object[]>();

            Assert.AreEqual(4, result[0]);
            Assert.AreEqual(4, result[1]);
            Assert.AreEqual(4, result[2]);
            Assert.AreEqual(1, result[3]);
        }
Пример #32
0
        public AuditEntryState(ObjectStateEntry objectStateEntry)
        {
            if (objectStateEntry == null)
                throw new ArgumentNullException("objectStateEntry");

            if (objectStateEntry.Entity == null)
                throw new ArgumentException("The Entity property is null for the specified ObjectStateEntry.", "objectStateEntry");

            ObjectStateEntry = objectStateEntry;
            Entity = objectStateEntry.Entity;

            EntityType = objectStateEntry.EntitySet.ElementType as EntityType;

            Type entityType = objectStateEntry.Entity.GetType();
            entityType = ObjectContext.GetObjectType(entityType);

            ObjectType = entityType;
            EntityAccessor = TypeAccessor.GetAccessor(entityType);

            AuditEntity = new AuditEntity(objectStateEntry.Entity)
            {
                Action = GetAction(objectStateEntry),
            };
        }