public virtual bool IsTargetEntity(ObjectStateEntry item)
        {

            return item.State != EntityState.Detached &&
                   TargetType.IsInstanceOfType(item.Entity);

        }
 /// <summary>
 /// Gets the identifier value for the entity in the ObjectStateEntry.
 /// </summary>
 /// <param name="objectStateEntry">The ObjectStateEntry.</param>
 /// <returns>The identifier value.</returns>
 /// <remarks>
 /// When auditing Adds, this needs to be called AFTER SaveChanges, or else it will return null.
 /// </remarks>
 public static string GetIdentifierValue(ObjectStateEntry objectStateEntry)
 {
     if (objectStateEntry == null)
     {
         throw new ArgumentNullException("objectStateEntry");
     }
     string id = null;
     var entityKey = objectStateEntry.EntityKey;
     if (entityKey != null)
     {
         var keyValues = entityKey.EntityKeyValues;
         if (keyValues != null)
         {
             if (keyValues.Length == 1)
             {
                 id = keyValues[0].Value.ToString();
             }
             else if (keyValues.Length > 1)
             {
                 id = string.Join(_composikeKeySeparator, keyValues.OrderBy(k => k.Key).Select(k => k.Value.ToString()).ToArray());
             }
         }
     }
     return id;
 }
 /// <summary>
 /// Tracks the original value.
 /// </summary>
 /// <param name="objectStateEntry">The object state entry.</param>
 private void TrackOriginalValue(ObjectStateEntry objectStateEntry)
 {
     object propertyValue = objectStateEntry.OriginalValues[PropertyName];
     if (propertyValue != null)
     {
         OriginalValue = propertyValue.ToString();
     }
 }
 /// <summary>
 /// Tracks the new value.
 /// </summary>
 /// <param name="objectStateEntry">The object state entry.</param>
 private void TrackNewValue(ObjectStateEntry objectStateEntry)
 {
     object propertyValue = objectStateEntry.CurrentValues[PropertyName];
     if (propertyValue != null)
     {
         NewValue = propertyValue.ToString();
     }
 }
Beispiel #5
0
 private static IEnumerable<Tuple<string, object, object>> GetUpdatedEntityValues(ObjectStateEntry entry)
 {
     foreach (string propname in entry.GetModifiedProperties())
     {
         object newval = entry.CurrentValues[propname];
         object oldval = entry.OriginalValues[propname];
         yield return Tuple.Create(propname, newval, oldval);
     }
 }
 /// <summary>
 /// Tracks the delete.
 /// </summary>
 /// <param name="objectStateEntry">The object state entry.</param>
 public void TrackDelete(ObjectStateEntry objectStateEntry)
 {
     if (objectStateEntry == null)
     {
         throw new ArgumentNullException("objectStateEntry");
     }
     AuditEntities.Add(new EntityDeletedAudit(objectStateEntry));
     RemoveReads();
 }
 public TrackingAuditEntity(ObjectStateEntry objectStateEntry, AuditEntityAction action)
 {
     if (objectStateEntry == null)
     {
         throw new ArgumentNullException("objectStateEntry");
     }
     EntityType = EntityTypeResolver.ResolveActualEntityType(objectStateEntry.Entity).Name;
     EntityIdentifier = IdentifierUtility.GetIdentifierValue(objectStateEntry);
     AuditEntityAction = action;
     AuditEntityProperties = new Collection<AuditEntityProperty>();
 }
Beispiel #8
0
 private static IEnumerable<Tuple<string, object, object>> GetAddedEntityValues(ObjectStateEntry entry)
 {
     foreach (var propinfo in entry.CurrentValues.DataRecordInfo.FieldMetadata)
     {
         object newval = entry.CurrentValues[propinfo.FieldType.Name];
         if (newval.GetType() == typeof(EntityKey))
         {
             newval = ((EntityKey)newval).EntityKeyValues.Select(k => k.Value).FirstOrDefault();    
         }
         yield return Tuple.Create(propinfo.FieldType.Name, newval, (object)null);
     }
 }
 /// <summary>
 /// Tracks the properties.
 /// </summary>
 /// <param name="objectStateEntry">The object state entry.</param>
 protected void TrackProperties(ObjectStateEntry objectStateEntry)
 {
     var currentValues = objectStateEntry.CurrentValues;
     for (int ordinal = 0; ordinal < currentValues.FieldCount; ordinal++)
     {
         var propertyName = currentValues.GetName(ordinal);
         if (!IsStoreGenerated(propertyName, objectStateEntry))
         {
             AuditEntityProperties.Add(new TrackingAuditEntityProperty(propertyName, objectStateEntry));
         }
     }
 }
Beispiel #10
0
 private static AuditAction GetAction(ObjectStateEntry entity)
 {
     switch (entity.State)
     {
         case EntityState.Added:
             return AuditAction.Added;
         case EntityState.Deleted:
             return AuditAction.Deleted;
         default:
             return AuditAction.Modified;
     }
 }
 /// <summary>
 /// Tracks the properties.
 /// </summary>
 /// <param name="objectStateEntry">The object state entry.</param>
 protected void TrackProperties(ObjectStateEntry objectStateEntry)
 {
     foreach (string propertyName in objectStateEntry.GetModifiedProperties())
     {
         if (!IsStoreGenerated(propertyName, objectStateEntry))
         {
             var auditProperty = new TrackingAuditEntityProperty(propertyName, objectStateEntry);
             if (auditProperty.HasChanges)
             {
                 AuditEntityProperties.Add(auditProperty);
             }
         }
     }
 }
 internal ObjectStateEntryDbDataRecord(RelationshipEntry cacheEntry)
 {
     EntityUtil.CheckArgumentNull(cacheEntry, "cacheEntry");
     Debug.Assert(!cacheEntry.IsKeyEntry, "Cannot create an ObjectStateEntryDbDataRecord for a key entry");
     switch (cacheEntry.State)
     {
         case EntityState.Unchanged:
         case EntityState.Modified:
         case EntityState.Deleted:
             _cacheEntry = cacheEntry;
             break;
         default:
             Debug.Assert(false, "A DbDataRecord cannot be created for an entity object that is in an added or detached state.");
             break;
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TrackingAuditEntityProperty"/> class.
 /// </summary>
 /// <param name="propertyName">Name of the property.</param>
 /// <param name="objectStateEntry">The object state entry.</param>
 public TrackingAuditEntityProperty(string propertyName, ObjectStateEntry objectStateEntry)
 {
     if (objectStateEntry == null)
     {
         throw new ArgumentNullException("objectStateEntry");
     }
     if (string.IsNullOrEmpty(propertyName))
     {
         throw new ArgumentNullException("propertyName");
     }
     PropertyName = propertyName;
     TrackNewValue(objectStateEntry);
     if (objectStateEntry.State == EntityState.Modified)
     {
         TrackOriginalValue(objectStateEntry);
     }
 }
		static void ValidateBelongsTo(this AssociationEndMember end, ObjectStateEntry entry)
		{
			if (!entry.IsRelationship)
			{
				throw new ArgumentException("is not a relationship entry", "entry");
			}

			var fieldMetadata =
					entry.UsableValues().DataRecordInfo.FieldMetadata;
			if (fieldMetadata[0].FieldType as AssociationEndMember != end &&
				fieldMetadata[1].FieldType as AssociationEndMember != end)
			{
				throw new InvalidOperationException(string.Format(
						"association end {0} does not participate in the " +
						"relationship {1}", end, entry));
			}
		}
 internal ObjectStateEntryDbDataRecord(EntityEntry cacheEntry, StateManagerTypeMetadata metadata, object userObject)
 {
     //Contract.Requires(cacheEntry != null);
     //Contract.Requires(userObject != null);
     //Contract.Requires(metadata != null);
     Debug.Assert(!cacheEntry.IsKeyEntry, "Cannot create an ObjectStateEntryDbDataRecord for a key entry");
     switch (cacheEntry.State)
     {
         case EntityState.Unchanged:
         case EntityState.Modified:
         case EntityState.Deleted:
             _cacheEntry = cacheEntry;
             _userObject = userObject;
             _metadata = metadata;
             break;
         default:
             Debug.Assert(false, "A DbDataRecord cannot be created for an entity object that is in an added or detached state.");
             break;
     }
 }
 internal ObjectStateEntryDbDataRecord(EntityEntry cacheEntry, StateManagerTypeMetadata metadata, object userObject)
 {
     EntityUtil.CheckArgumentNull(cacheEntry, "cacheEntry");
     EntityUtil.CheckArgumentNull(userObject, "userObject");
     EntityUtil.CheckArgumentNull(metadata, "metadata");
     Debug.Assert(!cacheEntry.IsKeyEntry, "Cannot create an ObjectStateEntryDbDataRecord for a key entry");
     switch (cacheEntry.State)
     {
         case EntityState.Unchanged:
         case EntityState.Modified:
         case EntityState.Deleted:
             _cacheEntry = cacheEntry;
             _userObject = userObject;
             _metadata = metadata;
             break;
         default:
             Debug.Assert(false, "A DbDataRecord cannot be created for an entity object that is in an added or detached state.");
             break;
     }
 }
 /// <summary>
 /// This determines if the value of the property is generated by the database (i.e. "Version"),
 /// in which case we don't want to store it as modified by the user.
 /// </summary>
 /// <param name="propertyName"></param>
 /// <param name="objectStateEntry"></param>
 /// <returns></returns>
 /// <remarks>
 /// This could potentially be a performance bottleneck.  If we need to speed things up,
 /// we can cache this information by type and propertyname in a static dictionary.
 /// </remarks>
 protected static bool IsStoreGenerated(string propertyName, ObjectStateEntry objectStateEntry)
 {
     bool returnValue = false;
     var metadataWorkspace = objectStateEntry.ObjectStateManager.MetadataWorkspace;
     ItemCollection items = null;
     if (metadataWorkspace.TryGetItemCollection(DataSpace.SSpace, out items))
     {
         var entityType = metadataWorkspace.GetItems<EntityType>(DataSpace.SSpace)
             .FirstOrDefault(t => t.Name == objectStateEntry.EntitySet.Name || t.Name + "s" == objectStateEntry.EntitySet.Name);
         if (entityType != null)
         {
             var edmProperty = entityType.Properties[propertyName];
             var facet = edmProperty.TypeUsage.Facets.Where(f => f.Name == "StoreGeneratedPattern").FirstOrDefault();
             if (facet != null)
             {
                 var facetValue = facet.Value.ToString().ToUpperInvariant();
                 returnValue = (facetValue != "NONE");
             }
         }
     }
     return returnValue;
 }
Beispiel #18
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),
            };
        }
Beispiel #19
0
        internal static Dictionary<string, object> DetermineEntityKey(ObjectStateEntry entryState)
        {
            Dictionary<string, object> key = entryState.EntityKey.EntityKeyValues.ToDictionary(kv => kv.Key, kv => kv.Value);

            if (entryState.Entity is DeviceAttachment)
            {
                key["DeviceSerialNumber"] = ((DeviceAttachment)entryState.Entity).DeviceSerialNumber;
            }
            if (entryState.Entity is DeviceCertificate)
            {
                key["DeviceSerialNumber"] = ((DeviceCertificate)entryState.Entity).DeviceSerialNumber;
            }
            if (entryState.Entity is DeviceComponent)
            {
                key["DeviceModelId"] = ((DeviceComponent)entryState.Entity).DeviceModelId;
            }
            if (entryState.Entity is DeviceUserAssignment)
            {
                key["AssignedUserId"] = ((DeviceUserAssignment)entryState.Entity).AssignedUserId;
            }
            if (entryState.Entity is JobAttachment)
            {
                key["JobId"] = ((JobAttachment)entryState.Entity).JobId;
            }
            if (entryState.Entity is JobComponent)
            {
                key["JobId"] = ((JobComponent)entryState.Entity).JobId;
            }
            if (entryState.Entity is JobLog)
            {
                key["JobId"] = ((JobLog)entryState.Entity).JobId;
            }
            if (entryState.Entity is UserAttachment)
            {
                key["UserId"] = ((UserAttachment)entryState.Entity).UserId;
            }
            if (entryState.Entity is UserFlagAssignment)
            {
                key["UserFlagId"] = ((UserFlagAssignment)entryState.Entity).UserFlagId;
                key["UserId"] = ((UserFlagAssignment)entryState.Entity).UserId;
            }

            return key;
        }
Beispiel #20
0
 private static IEnumerable<Tuple<string, object, object>> GetDeletedEntityValues(ObjectStateEntry entry)
 {
     yield return Tuple.Create(string.Empty, (object)null, (object)null);
 }
Beispiel #21
0
        internal static RepositoryMonitorEvent EventFromEntryState(DiscoDataContext Database, DbEntityEntry entityEntry, ObjectStateEntry entryState)
        {
            RepositoryMonitorEventType eventType;
            string[] modifiedProperties = null;
            Dictionary<string, object> entityKey = null;
            Type entityType;

            switch (entryState.State)
            {
                case System.Data.EntityState.Added:
                    eventType = RepositoryMonitorEventType.Added;
                    break;
                case System.Data.EntityState.Deleted:
                    eventType = RepositoryMonitorEventType.Deleted;
                    break;
                case System.Data.EntityState.Detached:
                    eventType = RepositoryMonitorEventType.Detached;
                    break;
                case System.Data.EntityState.Modified:
                    eventType = RepositoryMonitorEventType.Modified;
                    break;
                case System.Data.EntityState.Unchanged:
                    eventType = RepositoryMonitorEventType.Unchanged;
                    break;
                default:
                    throw new NotSupportedException(string.Format("Database Entry State not supported: {0}", entryState.State.ToString()));
            }

            entityType = EntityTypeFromProxy(entryState.Entity.GetType());

            // Only pass modified properties on Modified Event
            if (eventType == RepositoryMonitorEventType.Modified)
                modifiedProperties = entryState.GetModifiedProperties().ToArray();
            else
                modifiedProperties = new string[] { }; // Empty array for Added/Deleted.

            // Don't pass entity key when entity newly added
            if (eventType != RepositoryMonitorEventType.Added)
                entityKey = DetermineEntityKey(entryState);

            return new RepositoryMonitorEvent()
            {
                EventType = eventType,
                Entity = entryState.Entity,
                EntityKey = entityKey,
                EntityType = entityType,
                ModifiedProperties = modifiedProperties,
                Database = Database,
                dbEntityState = entityEntry,
                objectEntryState = entryState
            };
        }
Beispiel #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EntityAddedAudit"/> class.
 /// </summary>
 /// <param name="entry">The entry.</param>
 public EntityAddedAudit(ObjectStateEntry entry)
     : base(entry, AuditEntityAction.Add)
 {
     _objectStateEntry = entry;
 }
 object IGenericBusinessObj.ReadProperty(int index, ObjectStateEntry currentEntry)
 {
     return Adage.EF.BusObj.BusinessObjectHelper.ReadObjectValue(this, index, currentEntry);
 }
        /// <summary>
        /// Convert an ObjectStateEntry object to a string representation
        /// </summary>
        /// <param name="entry">The given ObjectStateEntry</param>
        /// <returns>The string representation</returns>
        private static string ObjectStateEntryToString(ObjectStateEntry entry)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat("\n- <b>{0} ", entry.State.ToString());

            if (entry.EntityKey == null)
            {
                if (entry.EntitySet == null)
                    builder.Append("Entity : null </b>[null]");
                else
                    builder.AppendFormat("EntitySet : {0}</b>", entry.EntitySet.Name);
            }
            else
            {
                builder.AppendFormat("Entity : {0} </b>", entry.EntityKey.EntitySetName);

                if (entry.EntityKey.IsTemporary)
                {
                    builder.Append("[Temporary]");
                }
                else
                {
                    foreach (var key in entry.EntityKey.EntityKeyValues)
                    {
                        builder.AppendFormat("[{0} = {1}]", key.Key, ObjectToString(key.Value));
                    }
                }
            }
            return (builder.ToString());
        }
 internal DbUpdatableDataRecord(ObjectStateEntry cacheEntry, StateManagerTypeMetadata metadata, object userObject)
 {
     _cacheEntry = cacheEntry;
     _userObject = userObject;
     _metadata = metadata;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="EntityDeletedAudit"/> class.
 /// </summary>
 /// <param name="entry">The entry.</param>
 public EntityDeletedAudit(ObjectStateEntry entry)
     : base(entry, AuditEntityAction.Delete)
 {
 }
 internal CurrentValueRecord(ObjectStateEntry cacheEntry) :
     base(cacheEntry)
 {
 }
 public EntityUpdatedAudit(ObjectStateEntry entry)
     : base(entry, AuditEntityAction.Update)
 {
     TrackProperties(entry);
 }
 internal DbUpdatableDataRecord(ObjectStateEntry cacheEntry)
     :
         this(cacheEntry, null, null)
 {
 }
 internal OriginalValueRecord(ObjectStateEntry cacheEntry, StateManagerTypeMetadata metadata, object userObject) :
     base(cacheEntry, metadata, userObject)
 {
 }