Ejemplo n.º 1
0
        public virtual bool Update(DbConnect con, TKey id, DynamicDictionary data)
        {
            //pull old data
            TModel oldData            = new TModel();
            ITable <TModel, TKey> tbl = con.GetModelTable <TModel, TKey>();

            oldData = tbl.Get(id);
            //checking if the data is editable by current login or not
            if (CheckClientID)
            {
                if (ValidateForClientData(oldData) == false)
                {
                    return(false);
                }
            }


            ChangeHistoryHelper <TModel> chngHlpr = null;

            chngHlpr = new ChangeHistoryHelper <TModel>(AuditActivityTypes.UPDATE);
            //if CREATED_BY, CREATED_on field exists then update those fields
            PropertyInfo by = oldData.GetType().GetProperty("updated_by");

            if (by != null)
            {
                data.SetValue("updated_by", SessionData.user_id);
            }
            PropertyInfo on = oldData.GetType().GetProperty("updated_on");

            if (on != null)
            {
                data.SetValue("updated_on", DateTime.Now);
            }



            dynamic cloned = data.Clone();

            chngHlpr.CheckChangeChanges(oldData, data);
            //if no changes then return true
            if (chngHlpr.Diff.ParameterNames.Count() == 0)
            {
                return(true);
            }

            int?savedId = null;

            //chngHlpr.Diff.Add("id", id, DbType.Int32, ParameterDirection.Input);
            DynamicParameters where = new DynamicParameters();
            where.Add("id", id, DbServiceUtility.GetDbType(typeof(TKey)));
            if (CheckClientID)
            {
                PropertyInfo client_id = oldData.GetType().GetProperty("client_id");
                if (client_id != null)
                {
                    where.Add("client_id", SessionData.client_id, DbServiceUtility.GetDbType(client_id.PropertyType));
                }
            }
            try
            {
                savedId = tbl.Update(where, chngHlpr.Diff);

                if (TrackChanges)
                {
                    //save the changes
                    chngHlpr.LogChanges(con);
                }
            }
            catch (Npgsql.NpgsqlException ex)
            {
                LogTrace.WriteErrorLog(ex.ToString());
                LogTrace.WriteDebugLog(string.Format("SQL which gave exception:\r{0}", ex.Routine));
                throw ex;
            }
            if (savedId > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }