/// <summary> /// Run the history import, one chunk at a time if necessary /// </summary> private void RunHistoryImport() { using (ETCMDataContext context = new ETCMDataContext()) { DateTime? lastImport = null; DateTime now, historyStart, historyEnd; do { now = DateTime.Now; // Get the last import time ETCMProperty lastImportProp = (from e in context.ETCMProperties where e.PropertyName == LAST_IMPORT_PROPERTY select e).SingleOrDefault(); if (lastImportProp != null) lastImport = DateTime.Parse(lastImportProp.PropertyValue); ; // Determine the start time of the history query historyStart = lastImport ?? PTExporter.PT_CREATED_DATE; // Determine the end time of the history query historyEnd = now; if (now - historyStart > PTExporter.MAX_HISTORY) { logger.Debug("The history from " + historyStart.ToString() + " to " + now + " is too long, limiting."); historyEnd = historyStart.Add(PTExporter.MAX_HISTORY); } logger.Info("Importing history from timeframe (" + historyStart.ToString() + " - " + historyEnd.ToString() + ")."); // Run the export, and if there is history to export, import it if (exporter.Run(historyStart, historyEnd)) importer.Run(); // Update the last import time if (lastImportProp == null) { lastImportProp = new ETCMProperty(); context.ETCMProperties.InsertOnSubmit(lastImportProp); } lastImportProp.PropertyValue = historyEnd.ToString(); context.SubmitChanges(); } while (historyEnd < now); } }
/// <summary> /// Import the given history changes into SQL. /// </summary> /// <param name="history"></param> private void ImportHistory(DataTable history) { try { // Get a comma-delimited list of RPNs for log statement List<string> prns = new List<string>(); foreach (DataRow row in history.Rows) { prns.Add(row["PRN"].ToString()); } logger.Debug("Importing the following " + history.Rows.Count + " changed issues: " + string.Join(",", prns.ToArray())); // Update and add issues using (ETCMDataContext etcm = new ETCMDataContext()) { Dictionary<int, Issue> newIssues = new Dictionary<int, Issue>(); // Loop through the changes foreach (DataRow change in history.Rows) { int changedPRN = change.Field<int>("PRN"); Issue existingIssue = (from i in etcm.Issues where i.PRN == changedPRN select i).SingleOrDefault(); Issue issue = existingIssue; if (existingIssue == null) { // This is a new issue, handle case of update(s) to new issue if (!newIssues.TryGetValue(changedPRN, out issue)) { issue = new Issue(); newIssues.Add(changedPRN, issue); } } UpdateIssue(issue, change); } etcm.Issues.InsertAllOnSubmit(newIssues.Values); etcm.SubmitChanges(); } logger.Debug("Done importing changed issues"); } catch (Exception e) { logger.Error("Error imported changes into ETCM database", e); throw e; } }