Beispiel #1
0
        public static string WorkoutDeltaStringForUpdate(FinanceAccountExtDPViewModel oldAcnt,
                                                         FinanceAccountExtDPViewModel newAcnt)
        {
            var diffs = WorkoutDeltaForUpdate(oldAcnt, newAcnt);

            List <String> listHeaderSqls = new List <string>();

            foreach (var diff in diffs)
            {
                var dbfield = newAcnt.GetDBFieldName(diff.Key);

                if (diff.Value == null)
                {
                    listHeaderSqls.Add("[" + dbfield + "] = NULL");
                }
                else
                {
                    if (diff.Value is DateTime)
                    {
                        listHeaderSqls.Add("[" + dbfield + "] = '" + ((DateTime)diff.Value).ToString("yyyy-MM-dd") + "'");
                    }
                    else if (diff.Value is Boolean)
                    {
                        listHeaderSqls.Add("[" + dbfield + "] = " + (((Boolean)diff.Value) ? "1" : "NULL"));
                    }
                    else if (diff.Value is String)
                    {
                        if (String.IsNullOrEmpty((string)diff.Value))
                        {
                            listHeaderSqls.Add("[" + dbfield + "] = NULL");
                        }
                        else
                        {
                            listHeaderSqls.Add("[" + dbfield + "] = N'" + diff.Value + "'");
                        }
                    }
                    else if (diff.Value is Decimal)
                    {
                        if (Decimal.Compare((Decimal)diff.Value, 0) == 0)
                        {
                            listHeaderSqls.Add("[" + dbfield + "] = NULL");
                        }
                        else
                        {
                            listHeaderSqls.Add("[" + dbfield + "] = " + diff.Value.ToString());
                        }
                    }
                    else
                    {
                        listHeaderSqls.Add("[" + dbfield + "] = " + diff.Value.ToString());
                    }
                }
            }

            return(listHeaderSqls.Count == 0 ?
                   String.Empty :
                   (@"UPDATE [dbo].[t_fin_account_ext_dp] SET " + string.Join(",", listHeaderSqls) + " WHERE [ACCOUNTID] = " + oldAcnt.AccountID.ToString()));
        }
Beispiel #2
0
        public static Dictionary <String, Object> WorkoutDeltaForUpdate(FinanceAccountExtDPViewModel oldAcnt,
                                                                        FinanceAccountExtDPViewModel newAcnt)
        {
            Dictionary <String, Object> dictDelta = new Dictionary <string, object>();

            if (oldAcnt == null || newAcnt == null || Object.ReferenceEquals(oldAcnt, newAcnt) ||
                oldAcnt.AccountID != newAcnt.AccountID)
            {
                throw new ArgumentException("Invalid inputted parameter Or AccountID change is not allowed");
            }
            if (!oldAcnt.IsValid() || !newAcnt.IsValid())
            {
                throw new Exception("Account info is invalid");
            }

            Type t = typeof(FinanceAccountExtDPViewModel);

            PropertyInfo[] listProperties       = t.GetProperties();
            var            listSortedProperties = listProperties.OrderBy(o => o.Name);

            foreach (PropertyInfo item in listSortedProperties)
            {
                if (item.Name == "Direct" || item.Name == "DefrrDays" || item.Name == "DPTmpDocs")
                {
                    continue;
                }

                object oldValue = item.GetValue(oldAcnt, null);
                object newValue = item.GetValue(newAcnt, null);
                if (item.PropertyType == typeof(Decimal))
                {
                    if (Decimal.Compare((Decimal)oldValue, (Decimal)newValue) != 0)
                    {
                        dictDelta.Add(item.Name, newValue);
                    }
                }
                else if (item.PropertyType == typeof(String))
                {
                    if (String.CompareOrdinal((string)oldValue, (string)newValue) != 0)
                    {
                        dictDelta.Add(item.Name, newValue);
                    }
                }
                else if (item.PropertyType == typeof(DateTime))
                {
                    if (DateTime.Compare(((DateTime)oldValue).Date, ((DateTime)newValue).Date) != 0)
                    {
                        dictDelta.Add(item.Name, newValue);
                    }
                }
                else if (item.PropertyType == typeof(RepeatFrequency))
                {
                    if ((RepeatFrequency)oldValue != (RepeatFrequency)newValue)
                    {
                        dictDelta.Add(item.Name, (Byte)newValue);
                    }
                }
                else
                {
                    if (!Object.Equals(oldValue, newValue))
                    {
                        dictDelta.Add(item.Name, newValue);
                    }
                }
            }

            return(dictDelta);
        }