Exemplo n.º 1
0
        static bool EnumCompare(ref FieldChange change, PropertyInfo info, object valueOld, object valueNew)
        {
            if (valueOld != null && valueNew != null && Enum.Equals(valueOld, valueNew))
            {
                return(false);
            }

            change          = new FieldChange();
            change.OldValue = valueOld?.ToString();
            change.NewValue = valueNew?.ToString();
            return(true);
        }
Exemplo n.º 2
0
        static bool StringCompare(ref FieldChange change, string valueOld, string valueNew)
        {
            if (String.IsNullOrWhiteSpace(valueNew) && String.IsNullOrWhiteSpace(valueOld))
            {
                return(false);
            }

            if (String.Equals(valueOld, valueNew))
            {
                return(false);
            }

            change          = new FieldChange();
            change.OldValue = valueOld;
            change.NewValue = valueNew;
            return(true);
        }
Exemplo n.º 3
0
        static bool EntityCompare(ref FieldChange change, object valueOld, object valueNew)
        {
            if (DomainHelper.EqualDomainObjects(valueOld, valueNew))
            {
                return(false);
            }

            change = new FieldChange();
            if (valueOld != null)
            {
                change.OldValue = HistoryMain.GetObjectTilte(valueOld);
                change.OldId    = DomainHelper.GetId(valueOld);
            }
            if (valueNew != null)
            {
                change.NewValue = HistoryMain.GetObjectTilte(valueNew);
                change.NewId    = DomainHelper.GetId(valueNew);
            }
            return(true);
        }
Exemplo n.º 4
0
        static bool BooleanCompare(ref FieldChange change, PropertyInfo info, object valueOld, object valueNew)
        {
            var boolOld = valueOld as bool?;
            var boolNew = valueNew as bool?;

            if (boolOld != null && boolNew != null && bool.Equals(boolOld.Value, boolNew.Value))
            {
                return(false);
            }

            change = new FieldChange();
            if (boolOld != null)
            {
                change.OldValue = boolOld.Value.ToString();
            }
            if (boolNew != null)
            {
                change.NewValue = boolNew.Value.ToString();
            }
            return(true);
        }
Exemplo n.º 5
0
        static bool IntCompare <TNumber>(ref FieldChange change, PropertyInfo info, object valueOld, object valueNew) where TNumber : struct
        {
            var numberOld = valueOld as TNumber?;
            var numberNew = valueNew as TNumber?;

            if (numberOld != null && numberNew != null && Equals(numberOld.Value, numberNew.Value))
            {
                return(false);
            }

            change = new FieldChange();
            if (numberOld != null)
            {
                change.OldValue = String.Format("{0:D}", numberOld);
            }
            if (numberNew != null)
            {
                change.NewValue = String.Format("{0:D}", numberNew);
            }
            return(true);
        }
Exemplo n.º 6
0
        static bool DecimalCompare(ref FieldChange change, PropertyInfo info, object valueOld, object valueNew)
        {
            var numberOld = valueOld as Decimal?;
            var numberNew = valueNew as Decimal?;

            if (numberOld != null && numberNew != null && Decimal.Equals(numberOld.Value, numberNew.Value))
            {
                return(false);
            }

            change = new FieldChange();
            if (numberOld != null)
            {
                change.OldValue = numberOld.Value.ToString("G20");
            }
            if (numberNew != null)
            {
                change.NewValue = numberNew.Value.ToString("G20");
            }
            return(true);
        }
Exemplo n.º 7
0
        static bool DateTimeCompare(ref FieldChange change, PropertyInfo info, object valueOld, object valueNew)
        {
            var dateOld = valueOld as DateTime?;
            var dateNew = valueNew as DateTime?;

            if (dateOld != null && dateNew != null && DateTime.Equals(dateOld.Value, dateNew.Value))
            {
                return(false);
            }

            var dateOnly = info.GetCustomAttributes(typeof(HistoryDateOnlyAttribute), true).Length > 0;

            change = new FieldChange();
            if (dateOld != null)
            {
                change.OldValue = dateOnly ? dateOld.Value.ToShortDateString() : dateOld.Value.ToString();
            }
            if (dateNew != null)
            {
                change.NewValue = dateOnly ? dateNew.Value.ToShortDateString() : dateNew.Value.ToString();
            }
            return(true);
        }
Exemplo n.º 8
0
 public virtual void AddFieldChange(FieldChange change)
 {
     change.Entity = this;
     Changes.Add(change);
 }
Exemplo n.º 9
0
        private static FieldChange CreateChange(object valueNew, object valueOld, NHibernate.Persister.Entity.IEntityPersister persister, int i)
        {
            if (valueOld == null && valueNew == null)
            {
                return(null);
            }

            NHibernate.Type.IType propType = persister.PropertyTypes[i];
            string propName = persister.PropertyNames[i];

            var propInfo = persister.MappedClass.GetProperty(propName);

            if (propInfo.GetCustomAttributes(typeof(IgnoreHistoryTraceAttribute), true).Length > 0)
            {
                return(null);
            }

            FieldChange change = null;

            #region Обработка в зависимости от типа данных

            if (propType is NHibernate.Type.StringType && !StringCompare(ref change, (string)valueOld, (string)valueNew))
            {
                return(null);
            }

            var link = propType as NHibernate.Type.ManyToOneType;
            if (link != null)
            {
                if (!EntityCompare(ref change, valueOld, valueNew))
                {
                    return(null);
                }
            }

            if ((propType is NHibernate.Type.DateTimeType || propType is NHibernate.Type.TimestampType) && !DateTimeCompare(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.DecimalType && !DecimalCompare(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.BooleanType && !BooleanCompare(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.Int16Type && !IntCompare <Int16>(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.Int32Type && !IntCompare <Int32>(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.Int64Type && !IntCompare <Int64>(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.UInt16Type && !IntCompare <UInt16>(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.UInt32Type && !IntCompare <UInt32>(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.UInt64Type && !IntCompare <UInt64>(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            if (propType is NHibernate.Type.EnumStringType && !EnumCompare(ref change, propInfo, valueOld, valueNew))
            {
                return(null);
            }

            #endregion

            if (change != null)
            {
                change.Path = propName;
                change.UpdateType();
                return(change);
            }

            logger.Warn("Трекер не умеет сравнивать изменения в полях типа {0}. Поле {1} пропущено.", propType, propName);
            return(null);
        }