public static IList FetchHistory() { // initialize History item = new History(); PropertyInfo[] p = item.GetType().GetProperties(); List<History> list = new List<History>(); string qryString = "SELECT * FROM HISTORY_LOG"; User user = (User)System.Web.HttpContext.Current.Session[Constant.session.User]; IDBManager dbmgr = new DBManager(user.plantDBStr); dbmgr.ConnectionString = user.plantDBStr; try { dbmgr.Open(); dbmgr.ExecuteReader(CommandType.Text, qryString); while (dbmgr.DataReader.Read()) { item = new History(); item = (History)FetchObject(item, p, dbmgr); list.Add(item); } } catch (Exception ex) { throw (ex); } finally { dbmgr.Dispose(); } return list; }
// procedure saves object to history // definition of variables passed: // obj : current object // old_obj : old object prior to update // table_chg : name of table that was changed // data_chg : data that was changed // key_field : name of key_field (column) within table // (eg. changes in COMPLIST will have COMP as key_field, changes in CABLIST will have CABLE // dbmgr : database manager // fieldlist : list of fields that were involved // (this is needed because more fields may exisit for a given entity than was changed in webform) public void SaveHistory(Object obj, Object old_obj, string user_id, string table_chg, string data_chg, string key_field, IDBManager dbmgr, string[] fieldList) { History hist; // create new entity ArrayList listChgs = new ArrayList(); // array to hold list of changes // determine type of change: addition, deletion or modification // and set values accordingly // ADDITION: if old_obj is null then user entering a new record if (old_obj == null) { hist = new History(); // create new instance hist.field_chg = "RECORD ADDED"; // new record added hist.new_data = data_chg; hist.old_data = ""; // old data is null hist.user_id = user_id; hist.table_chg = table_chg; hist.key_field = key_field; listChgs.Add(hist); // add to arraylist } // DELETION: if obj is null then user deleting a record else if (obj == null) { hist = new History(); // create new instance hist.field_chg = "RECORD DELETED"; // record deleted hist.new_data = ""; // new data is null hist.old_data = data_chg; hist.user_id = user_id; hist.table_chg = table_chg; hist.key_field = key_field; listChgs.Add(hist); // add to arraylist } // MODIFICATION: otherwise user is modifying an existing record else { PropertyInfo[] properties = obj.GetType().GetProperties(); foreach (PropertyInfo info in properties) { // initialize by setting new and old data to empty string string new_data = ""; string old_data = ""; // properties that are *list or arg* are used base properties used only for application // these will not exist in table therefore screen for these if (!info.PropertyType.Name.Contains("list") && !info.Name.Contains("arg")) { // check to see if control for given name if (ControlExists(info.Name, fieldList)) { if (info.GetValue(old_obj, null) != null) old_data = info.GetValue(old_obj, null).ToString(); if (info.GetValue(obj, null) != null) new_data = info.GetValue(obj, null).ToString(); // if the new and old data are not equal to one another if (!new_data.Equals(old_data)) { hist = new History(); // create new instance hist.new_data = new_data; hist.old_data = old_data; hist.field_chg = info.Name.ToUpper(); // set field changed (convert to upper case) hist.user_id = user_id; hist.table_chg = table_chg; hist.key_field = key_field; listChgs.Add(hist); // add to arraylist } } } } } // update HISTORY_LOG table as long as there is changes if (listChgs.Count > 0) { foreach (History h in listChgs) { try { dbmgr.CreateParameters(6); dbmgr.AddParameters(0, "@user_id", h.user_id); dbmgr.AddParameters(1, "@field_chg", h.field_chg); dbmgr.AddParameters(2, "@new_data", h.new_data); dbmgr.AddParameters(3, "@old_data", h.old_data); dbmgr.AddParameters(4, "@table_chg", h.table_chg); dbmgr.AddParameters(5, "@key_field", h.key_field); dbmgr.ExecuteNonQuery(System.Data.CommandType.StoredProcedure, "HISTORY_LOG_i"); } catch (Exception ex) { throw (ex); } } } }