public virtual bool SoftDelete(DbConnect con, TKey id) { Is_Child_Records_Exists = false; //Default child record off //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); } } TableDetailAttribute tableDatail = oldData.GetTableDetail(); //09 feb 2016 Foreign key refrence table data exists or not checke.. #region todo:shivahwor StringBuilder Sql = new StringBuilder(); Sql.AppendFormat(@" SELECT --tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name --,ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name WHERE constraint_type = 'FOREIGN KEY' AND ccu.table_name='{0}' ", tableDatail.Name); IEnumerable <DynamicDictionary> foreignkey_Table = DbServiceUtility.ExecuteList(con, Sql.ToString()); Sql.Length = 0; DynamicDictionary rec = null; if (foreignkey_Table != null) { foreach (DynamicDictionary item in foreignkey_Table) { object tbl_Name = item.GetValue("table_name"); object col_name = item.GetValue("column_name"); if (Sql.Length > 0) { Sql.AppendLine("\n\t UNION ALL "); } Sql.AppendFormat(" SELECT 1 from {0} Where {1}={2} AND is_deleted ='F' ", tbl_Name.ToString(), col_name.ToString(), id); } rec = DbServiceUtility.ExecuteItem(con, Sql.ToString()); } if (rec != null) { if (rec.KeyList.Count > 0) { Is_Child_Records_Exists = true; //if child records exists than do not remove... return(false); } } #endregion //data = new TModel(); DynamicDictionary data_param = new DynamicDictionary(); ChangeHistoryHelper <TModel> chngHlpr = null; chngHlpr = new ChangeHistoryHelper <TModel>(AuditActivityTypes.SOFTDELETE); //if CREATED_BY, CREATED_on field exists then update those fields PropertyInfo by = oldData.GetType().GetProperty("deleted_by"); if (by != null) { data_param.SetValue("deleted_by", SessionData.user_id); //by.SetValue(data, Bango.GetCurrentUserId()); } PropertyInfo on = oldData.GetType().GetProperty("deleted_on"); if (on != null) { //on.SetValue(data, DateTime.Now); data_param.SetValue("deleted_on", DateTime.Now); } PropertyInfo uq_code = oldData.GetType().GetProperty("deleted_uq_code"); if (on != null) { //on.SetValue(data, DateTime.Now); data_param.SetValue("deleted_uq_code", DateTime.Now.ToString("MMddHHmmss")); } PropertyInfo is_deleted = oldData.GetType().GetProperty(tableDatail.DeleteFlagField); if (is_deleted != null) { //is_deleted.SetValue(data, true); data_param.SetValue(tableDatail.DeleteFlagField, true); } chngHlpr.CheckChangeChanges(oldData, data_param); //chngHlpr.Diff.Add("id", id, DbType.Int32, ParameterDirection.Input); DynamicParameters where = new DynamicParameters(); where.Add("id", id); if (CheckClientID) { PropertyInfo client_id = oldData.GetType().GetProperty("client_id"); if (client_id != null) { where.Add("client_id", SessionData.client_id); } } try { int?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; } return(true); }
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); } }
public virtual bool Insert(DbConnect con, DynamicDictionary data) { //create empty object TModel empty = new TModel(); ChangeHistoryHelper <TModel> chngHlpr = null; chngHlpr = new ChangeHistoryHelper <TModel>(AuditActivityTypes.INSERT); PropertyInfo key = Models.ModelService.GetKeyPropertyInfo(empty); if (data.ContainsKey(key.Name)) { data.Remove(key.Name); } //if CREATED_BY, CREATED_on field exists then update those fields PropertyInfo by = empty.GetType().GetProperty("created_by"); if (by != null) { data.SetValue("created_by", SessionData.user_id); } PropertyInfo on = empty.GetType().GetProperty("created_on"); if (on != null) { data.SetValue("created_on", DateTime.Now); } if (CheckClientID) { PropertyInfo client_id = empty.GetType().GetProperty("client_id"); if (client_id != null) { if (!data.ContainsKey("client_id")) { data.SetValue("client_id", SessionData.client_id); } } } chngHlpr.CheckChangeChanges(new TModel(), data); ITable <TModel, int?> tbl = con.GetModelTable <TModel>(); int?id = null; try { id = tbl.Insert(chngHlpr.Diff); //set the primary key in the object if (key != null) { data.SetValue(key.Name, id); } if (TrackChanges) { // chngHlpr.Changes.pkey_value = Convert.ToString(id); //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 (id > 0) { return(true); } return(false); }