public static void SetSystemDefault(String AKey, String AValue) { Boolean NewTransaction = false; Boolean ShouldCommit = false; try { TDBTransaction Transaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.ReadCommitted, TEnforceIsolationLevel.eilMinimum, out NewTransaction); SSystemDefaultsTable tbl = SSystemDefaultsAccess.LoadByPrimaryKey(AKey, Transaction); if (tbl.Rows.Count > 0) // I already have this. (I expect this is the case usually!) { DataRow Row = tbl[0]; ((SSystemDefaultsRow)Row).DefaultValue = AValue; } else { DataRow Row = tbl.NewRowTyped(true); ((SSystemDefaultsRow)Row).DefaultCode = AKey; ((SSystemDefaultsRow)Row).DefaultDescription = "Created in OpenPetra"; ((SSystemDefaultsRow)Row).DefaultValue = AValue; tbl.Rows.Add(Row); } SSystemDefaultsAccess.SubmitChanges(tbl, Transaction); ShouldCommit = true; } catch (Exception Exc) { TLogging.Log("An Exception occured during the saving of a single System Default:" + Environment.NewLine + Exc.ToString()); ShouldCommit = false; throw; } finally { if (NewTransaction) { if (ShouldCommit) { DBAccess.GDBAccessObj.CommitTransaction(); } else { DBAccess.GDBAccessObj.RollbackTransaction(); } } } }
/// <summary> /// Determines the 'Last Reminder Date', that is the date when PartnerReminders last ran. /// <para> /// This is done by reading a certain SystemDefault. If PartnerReminders was never run before, /// this SystemDefault is created. /// </para> /// </summary> /// <param name="ALastReminderDate">Date when PartnerReminders last ran. Will be January 1st, 1980 /// if PartnerReminders never ran before.</param> /// <param name="ASystemDefaultsDR">SystemDefaults DataRow containing the date. This is used later for updating /// the date.</param> /// <param name="AReadWriteTransaction">Already instantiated DB Transaction.</param> /// <returns>True if the 'Last Reminder Date' could be read/created. False if PartnerReminders was never run before /// AND creation of the new SystemDefault record failed for some reason.</returns> private static bool GetLastReminderDate(out DateTime ALastReminderDate, out SSystemDefaultsRow ASystemDefaultsDR, TDBTransaction AReadWriteTransaction) { const string UNDEFINED_SYSTEMDEFAULT_LAST_REMINDER_DATE = "1980,1,1"; // Double check order! SSystemDefaultsTable SystemDefaultsDT = new SSystemDefaultsTable(); string LastReminderDateStr; string[] DateParts; bool ReturnValue = true; ASystemDefaultsDR = null; // Check if there is already a SystemDefault for the Last Reminder Date (most likely there is!) if (SSystemDefaultsAccess.Exists(SYSTEMDEFAULT_LAST_REMINDER_DATE, AReadWriteTransaction)) { if (TLogging.DebugLevel >= 6) { TLogging.Log("GetLastReminderDate: System Default for the Last Reminder Date exists: use it."); } // There is already a SystemDefault for the Last Reminder Date: read its value SystemDefaultsDT = SSystemDefaultsAccess.LoadByPrimaryKey(SYSTEMDEFAULT_LAST_REMINDER_DATE, AReadWriteTransaction); // Used later to update the row ASystemDefaultsDR = SystemDefaultsDT[0]; } else { // System Default for the Last Reminder Date doesn't exist: add a new SystemDefault for future use if (TLogging.DebugLevel >= 6) { TLogging.Log("GetLastReminderDate: System Default for the Last Reminder Date doesn't exist yet: creating it."); } ASystemDefaultsDR = SystemDefaultsDT.NewRowTyped(); ASystemDefaultsDR.DefaultCode = SYSTEMDEFAULT_LAST_REMINDER_DATE; ASystemDefaultsDR.DefaultDescription = SYSTEMDEFAULT_LAST_REMINDER_DATE_DESC; ASystemDefaultsDR.DefaultValue = UNDEFINED_SYSTEMDEFAULT_LAST_REMINDER_DATE; try { SSystemDefaultsAccess.SubmitChanges(SystemDefaultsDT, AReadWriteTransaction); } catch (Exception Exc) { TLogging.Log("TProcessPartnerReminders.GetLastReminderDate: An Exception occured:" + Environment.NewLine + Exc.ToString()); throw; } } LastReminderDateStr = ASystemDefaultsDR.DefaultValue; // Last Reminder Date is stored as YEAR,MONTH,DAY DateParts = LastReminderDateStr.Split(','); ALastReminderDate = new DateTime( Convert.ToInt32(DateParts[0]), Convert.ToInt32(DateParts[1]), Convert.ToInt32(DateParts[2]), 0, 0, 1); // One second past midnight if (TLogging.DebugLevel >= 6) { TLogging.Log(String.Format("GetLastReminderDate: DB Field value: {0}; Parsed date: {1}", LastReminderDateStr, ALastReminderDate)); } return(ReturnValue); }
/// <summary> /// Determines the 'Last Reminder Date', that is the date when PartnerReminders last ran. /// <para> /// This is done by reading a certain SystemDefault. If PartnerReminders was never run before, /// this SystemDefault is created. /// </para> /// </summary> /// <param name="ALastReminderDate">Date when PartnerReminders last ran. Will be January 1st, 1980 /// if PartnerReminders never ran before.</param> /// <param name="ASystemDefaultsDR">SystemDefaults DataRow containing the date. This is used later for updating /// the date.</param> /// <param name="AReadWriteTransaction">Already instantiated DB Transaction.</param> /// <returns>True if the 'Last Reminder Date' could be read/created. False if PartnerReminders was never run before /// AND creation of the new SystemDefault record failed for some reason.</returns> private static bool GetLastReminderDate(out DateTime ALastReminderDate, out SSystemDefaultsRow ASystemDefaultsDR, TDBTransaction AReadWriteTransaction) { const string UNDEFINED_SYSTEMDEFAULT_LAST_REMINDER_DATE = "1980,1,1"; // Double check order! SSystemDefaultsTable SystemDefaultsDT = new SSystemDefaultsTable(); string LastReminderDateStr; string[] DateParts; bool ReturnValue = true; ASystemDefaultsDR = null; // Check if there is already a SystemDefault for the Last Reminder Date (most likely there is!) if (SSystemDefaultsAccess.Exists(SYSTEMDEFAULT_LAST_REMINDER_DATE, AReadWriteTransaction)) { if (TLogging.DebugLevel >= 6) { TLogging.Log("GetLastReminderDate: System Default for the Last Reminder Date exists: use it."); } // There is already a SystemDefault for the Last Reminder Date: read its value SystemDefaultsDT = SSystemDefaultsAccess.LoadByPrimaryKey(SYSTEMDEFAULT_LAST_REMINDER_DATE, AReadWriteTransaction); // Used later to update the row ASystemDefaultsDR = SystemDefaultsDT[0]; } else { // System Default for the Last Reminder Date doesn't exist: add a new SystemDefault for future use if (TLogging.DebugLevel >= 6) { TLogging.Log("GetLastReminderDate: System Default for the Last Reminder Date doesn't exist yet: creating it."); } ASystemDefaultsDR = SystemDefaultsDT.NewRowTyped(); ASystemDefaultsDR.DefaultCode = SYSTEMDEFAULT_LAST_REMINDER_DATE; ASystemDefaultsDR.DefaultDescription = SYSTEMDEFAULT_LAST_REMINDER_DATE_DESC; ASystemDefaultsDR.DefaultValue = UNDEFINED_SYSTEMDEFAULT_LAST_REMINDER_DATE; try { SSystemDefaultsAccess.SubmitChanges(SystemDefaultsDT, AReadWriteTransaction); } catch (Exception Exc) { TLogging.Log("TProcessPartnerReminders.GetLastReminderDate: An Exception occured:" + Environment.NewLine + Exc.ToString()); throw; } } LastReminderDateStr = ASystemDefaultsDR.DefaultValue; // Last Reminder Date is stored as YEAR,MONTH,DAY DateParts = LastReminderDateStr.Split(','); ALastReminderDate = new DateTime( Convert.ToInt32(DateParts[0]), Convert.ToInt32(DateParts[1]), Convert.ToInt32(DateParts[2]), 0, 0, 1); // One second past midnight if (TLogging.DebugLevel >= 6) { TLogging.Log(String.Format("GetLastReminderDate: DB Field value: {0}; Parsed date: {1}", LastReminderDateStr, ALastReminderDate)); } return ReturnValue; }