public virtual void save(bool LogMe = true) { String ConnectionName = "Default"; Type objectType = typeof(T); object[] connections = objectType.GetCustomAttributes(typeof(DBConnectionAttribute), true); DBLoggingAttribute logging = objectType.GetCustomAttributes(typeof(DBLoggingAttribute), true).Cast <DBLoggingAttribute>().SingleOrDefault(); if (connections.Length > 0) { ConnectionName = ((DBConnectionAttribute)connections[0]).ConnectionName; } FieldInfo tableName = objectType.GetField("_tableName", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); FieldInfo IDproperty = objectType.GetField("_IDproperty", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); FieldInfo EcludeForSave = objectType.GetField("_EcludeForSave", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); object SavedID = DBFunction.SaveEntityToDB(this, tableName.GetValue(null).ToString(), IDproperty.GetValue(null).ToString(), EcludeForSave.GetValue(null).ToString() + "||IsDirty||LogEntryKey||LogEntryValue||InitialValue||", ConnectionName); PropertyInfo IDField = objectType.GetProperty(IDproperty.GetValue(null).ToString()); if (Convert.ToInt64(IDField.GetValue(this, null)) != Convert.ToInt64(SavedID)) { IDField.SetValue(this, SavedID, null); } if (logging != null && LogMe) { LogModifications(logging, LogCommand.Save); } SynapseForm.SynapseLogger.Debug(string.Format("Saving object<{0}> with id {1}", objectType.Name, SavedID.ToString())); this.ComputeHash(); }
public virtual void delete(bool LogMe = true) { String ConnectionName = "Default"; Type objectType = typeof(T); object[] connections = objectType.GetCustomAttributes(typeof(DBConnectionAttribute), true); DBLoggingAttribute logging = objectType.GetCustomAttributes(typeof(DBLoggingAttribute), true).Cast <DBLoggingAttribute>().SingleOrDefault(); if (connections.Length > 0) { ConnectionName = ((DBConnectionAttribute)connections[0]).ConnectionName; } FieldInfo tableName = objectType.GetField("_tableName", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); FieldInfo IDproperty = objectType.GetField("_IDproperty", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); DBFunction.DeleteEntityFromDB(tableName.GetValue(null).ToString(), IDproperty.GetValue(null).ToString(), this, ConnectionName); if (logging != null && LogMe) { LogModifications(logging, LogCommand.Delete); } SynapseForm.SynapseLogger.Debug(string.Format("Deleting object<{0}>", objectType.Name)); }
private void LogModifications(DBLoggingAttribute LogAttribute, LogCommand Command) { Type objectType = typeof(T); FieldInfo IDproperty = objectType.GetField("_IDproperty", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); PropertyInfo IDField = objectType.GetProperty(IDproperty.GetValue(null).ToString()); List <LogValue> _LogValues = new List <LogValue>(); if (Command == LogCommand.Save) { if (_initial == null) { Command = LogCommand.Insert; } else if (Convert.ToInt64(IDField.GetValue(_initial, null)) == 0) { Command = LogCommand.Insert; } else { Command = LogCommand.Modify; } } LogEntry.TableName = "LogEntry_" + Assembly.GetEntryAssembly().GetName().Name; LogValue.TableName = "LogValue_" + Assembly.GetEntryAssembly().GetName().Name; foreach (PropertyInfo p in objectType.GetProperties()) { bool _LogProperty = false; IList <BaseLogAttribute> CustomLogAttributes = p.GetCustomAttributes(typeof(BaseLogAttribute), true).Cast <BaseLogAttribute>().ToList(); string OldValue = string.Empty; string NewValue = string.Empty; if (Command == LogCommand.Modify || Command == LogCommand.Delete) { if (p.GetValue(_initial, null) != null) { switch (p.PropertyType.ToString()) { case "System.DateTime": if ((DateTime)p.GetValue(_initial, null) != DateTime.MinValue) { OldValue = ((DateTime)p.GetValue(_initial, null)).ToString("dd/MM/yyyy"); } break; case "System.TimeSpan": OldValue = ((TimeSpan)p.GetValue(_initial, null)).Hours.ToString("00") + "h" + ((TimeSpan)p.GetValue(_initial, null)).Minutes.ToString("00"); break; case "System.Decimal": OldValue = ((decimal)p.GetValue(_initial, null)).ToString("0.00"); break; case "System.Boolean": if ((bool)p.GetValue(_initial, null)) { OldValue = "X"; } break; default: OldValue = p.GetValue(_initial, null).ToString(); break; } } } if (Command == LogCommand.Modify || Command == LogCommand.Insert) { if (p.GetValue(this, null) != null) { switch (p.PropertyType.ToString()) { case "System.DateTime": if ((DateTime)p.GetValue(this, null) != DateTime.MinValue) { NewValue = ((DateTime)p.GetValue(this, null)).ToString("dd/MM/yyyy"); } break; case "System.TimeSpan": NewValue = ((TimeSpan)p.GetValue(this, null)).Hours.ToString("00") + "h" + ((TimeSpan)p.GetValue(this, null)).Minutes.ToString("00"); break; case "System.Decimal": NewValue = ((decimal)p.GetValue(this, null)).ToString("0.00"); break; case "System.Boolean": if ((bool)p.GetValue(this, null)) { NewValue = "X"; } break; default: NewValue = p.GetValue(this, null).ToString(); break; } } } if (CustomLogAttributes.Count != 0) { foreach (BaseLogAttribute CLA in CustomLogAttributes) { if (CLA is LogAttributes) { if (((LogAttributes)CLA).LogMe) { if (Command == LogCommand.Delete && ((LogAttributes)CLA).OnDelete) { _LogProperty = true; } if (Command == LogCommand.Insert && ((LogAttributes)CLA).OnNew) { _LogProperty = true; } if (Command == LogCommand.Modify && ((LogAttributes)CLA).OnEdit) { _LogProperty = true; } if (((LogAttributes)CLA).QueryValue != "") { if (OldValue != null && OldValue != "") { OldValue = (String)SynapseCore.Database.DBFunction.ExecuteScalarQuery(((LogAttributes)CLA).QueryValue + OldValue); } if (NewValue != null && NewValue != "") { NewValue = (String)SynapseCore.Database.DBFunction.ExecuteScalarQuery(((LogAttributes)CLA).QueryValue + NewValue); } if (OldValue == null) { OldValue = ""; } if (NewValue == null) { NewValue = ""; } } } } if (CLA is LogEntryAttributeValue) { if (this.LogEntryValue == null) { if (Command == LogCommand.Delete) { this.LogEntryValue = OldValue; } else { this.LogEntryValue = NewValue; } } //_LogProperty = true; } if (CLA is LogEntryAttributeKey) { if (this.LogEntryKey == null) { this.LogEntryKey = NewValue; } //_LogProperty = true; } } } else { if (p.Name != "LogEntryKey" && p.Name != "LogEntryValue" && p.Name != "IsDirty" && p.Name != "InitialValue") { _LogProperty = true; } } if ((OldValue != NewValue && _LogProperty) || (Command == LogCommand.Delete && _LogProperty)) { LogValue _LogValue = new LogValue(); _LogValue.FIELD = LogAttribute.EntityName + "." + p.Name; _LogValue.OLD_VALUE = OldValue; _LogValue.NEW_VALUE = NewValue; _LogValues.Add(_LogValue); } } if (_LogValues.Count > 0) { try { LogEntry _LogEntry = new LogEntry(); switch (Command) { case LogCommand.Delete: _LogEntry.ACTION_CODE = "DELETE"; break; case LogCommand.Insert: _LogEntry.ACTION_CODE = "INSERT"; break; case LogCommand.Modify: _LogEntry.ACTION_CODE = "MODIFY"; break; } _LogEntry.USERID = SynapseForm.FormUser.UserID; _LogEntry.TIMESTAMP = DateTime.Now; _LogEntry.OBJECT_NAME = LogAttribute.EntityName; _LogEntry.OBJECT_VALUE = this.LogEntryValue; _LogEntry.LOGKEY = this.LogEntryKey; _LogEntry.save(); try { foreach (LogValue lv in _LogValues) { lv.FK_LOGENTRY = _LogEntry.ID; } _LogValues.SaveCollection(); } // TODO: Catch more specific exception catch (Exception) { // TODO: Handle exception (e.g. log) } } // TODO: Catch more specific exception catch (Exception) { // TODO: Handle exception (e.g. log) } } }