public static void SaveTagBalance(String file, SuncorProductionFile pf, List <TagBalance> tb) { using (DBContextWithConnectionString context = new DBContextWithConnectionString()) { Batch batch = new Batch(); batch.Id = pf.BatchId.ToString(); batch.Created = DateTime.Now; batch.CreatedBy = "System"; batch.Filename = file; context.Batches.Add(batch); foreach (var item in tb) { TagBalance found = context.TagBalances.Find(new object[] { item.Tag, item.BalanceDate }); if (found == null) { batch.TagBalances.Add(item); } else { UpdateTagBalance(found, item); } } context.SaveChanges(); pf.SavedRecords = tb; } }
public static void RecordFailure(String plantName, string fileName, int successfulRecordCount, int failedRecordCount, string msg) { using (DBContextWithConnectionString context = new DBContextWithConnectionString()) { TransactionEvent te = new TransactionEvent() { Plant = plantName, Filename = fileName, SuccessfulRecordCount = 0, FailedRecordCount = failedRecordCount, ErrorMessage = msg }; context.TransactionEvents.Add(te); context.SaveChanges(); } }
public static string UpdateTagMappings(string plant, DataTable dt) { string output = ""; List <TagMap> existingTM = null; List <TagMap> toAdd = new List <TagMap>(); List <TagMap> toChange = new List <TagMap>(); using (DBContextWithConnectionString context = new DBContextWithConnectionString()) { existingTM = context.TagMaps.Where(t => t.Plant == plant).ToList(); } foreach (DataRow r in dt.AsEnumerable()) { TagMap current = TagMapFromRow(r); if (current.Plant != plant) { continue; // ignore other plant tags } TagMap found = existingTM.SingleOrDefault(t => t.Plant == current.Plant && t.Tag == current.Tag); if (found == null) { toAdd.Add(current); output += "adding " + current.Plant + "," + current.Tag + "\r\n"; } else { existingTM.Remove(found); if (UpdateValues(found, current)) { toChange.Add(current); output += "updated " + current.Plant + "," + current.Tag + "\r\n"; } } } existingTM.ForEach(t => { output += "deleting " + t.Plant + "," + t.Tag + "\r\n"; }); using (DBContextWithConnectionString context = new DBContextWithConnectionString()) { context.TagMaps.RemoveRange(existingTM); context.TagMaps.AddRange(toAdd); toChange.ForEach(chg => { UpdateValues(context.TagMaps.Single(t => t.Plant == chg.Plant && t.Tag == chg.Tag), chg); }); context.SaveChanges(); } return(output); }
public static void RecordStats(SuncorProductionFile pf, string filename, List <WarningMessage> warnings) { using (DBContextWithConnectionString context = new DBContextWithConnectionString()) { TransactionEvent te = new TransactionEvent() { Plant = pf.Plant, Filename = filename, SuccessfulRecordCount = pf.SavedRecords.Count, FailedRecordCount = pf.FailedRecords.Count }; foreach (var item in warnings.Select(y => new { Tag = y.Tag, Message = y.Message }).Distinct()) { te.TransactionEventDetails.Add(new TransactionEventDetail() { Tag = item.Tag, ErrorMessage = item.Message }); } List <WarningMessage> noMappings = warnings.Where(t => t.Message == "No TagMapping").ToList(); List <WarningMessage> otherErrors = warnings.Where(t => t.Message != "No TagMapping").ToList(); te.ErrorMessage = ""; if (otherErrors.Count > 0) { te.ErrorMessage = String.Join(",", otherErrors.Select(y => y.Message).Distinct().ToArray()); } if (noMappings.Count > 0) { te.ErrorMessage += (te.ErrorMessage.Length > 0 ? " and " : "") + noMappings.Count + " records with no tag mappings"; } if (pf.SavedRecords.Count == 0 && te.ErrorMessage == "") { te.ErrorMessage = "File rejected due to no successfuly records"; } if (te.ErrorMessage == "") { te.ErrorMessage = "File completed successfully"; } context.TransactionEvents.Add(te); context.SaveChanges(); } }