/// <summary> /// Import Service Areas /// </summary> /// <param name="performContext"></param> /// <param name="dbContext"></param> /// <param name="fileLocation"></param> /// <param name="systemId"></param> public static void Import(PerformContext performContext, DbAppContext dbContext, string fileLocation, string systemId) { // check the start point. If startPoint == sigId then it is already completed int startPoint = ImportUtility.CheckInterMapForStartPoint(dbContext, OldTableProgress, BcBidImport.SigId, NewTable); if (startPoint == BcBidImport.SigId) // this means the import job it has done today is complete for all the records in the xml file. { performContext.WriteLine("*** Importing " + XmlFileName + " is complete from the former process ***"); return; } try { string rootAttr = "ArrayOf" + OldTable; performContext.WriteLine("Processing Service Areas"); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(ServiceArea[]), new XmlRootAttribute(rootAttr)); ser.UnknownAttribute += ImportUtility.UnknownAttribute; ser.UnknownElement += ImportUtility.UnknownElement; MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ServiceArea[] legacyItems = (ServiceArea[])ser.Deserialize(memoryStream); Debug.WriteLine("Importing ServiceArea Data. Total Records: " + legacyItems.Length); foreach (ServiceArea item in legacyItems.WithProgress(progress)) { // see if we have this one already ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Service_Area_Id.ToString()); // new entry if (importMap == null && item.Service_Area_Cd != "000") { Models.ServiceArea serviceArea = null; CopyToInstance(dbContext, item, ref serviceArea, systemId); ImportUtility.AddImportMap(dbContext, OldTable, item.Service_Area_Id.ToString(), NewTable, serviceArea.Id); } } performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BcBidImport.SigId.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); throw; } }
/// <summary> /// Import Service Areas /// </summary> /// <param name="performContext"></param> /// <param name="dbContext"></param> /// <param name="fileLocation"></param> /// <param name="systemId"></param> public static void Import(PerformContext performContext, DbAppContext dbContext, string fileLocation, string systemId) { string completed = DateTime.Now.ToString("d") + "-" + "Completed"; ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == completed && x.NewKey == SigId); if (importMap != null) { return; } try { string rootAttr = "ArrayOf" + OldTable; performContext.WriteLine("Processing Service Areas"); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(ServiceArea[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ServiceArea[] legacyItems = (ServiceArea[])ser.Deserialize(memoryStream); foreach (ServiceArea item in legacyItems.WithProgress(progress)) { // see if we have this one already importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Service_Area_Id.ToString()); Models.ServiceArea serviceArea = dbContext.ServiceAreas.FirstOrDefault(x => x.Name == item.Service_Area_Desc.Trim()); if (serviceArea == null) { serviceArea = new Models.ServiceArea(); } // new entry if (importMap == null) { if (item.Service_Area_Cd > 0) { CopyToInstance(performContext, dbContext, item, ref serviceArea, systemId); ImportUtility.AddImportMap(dbContext, OldTable, item.Service_Area_Id.ToString(), NewTable, serviceArea.Id); } } else // update { // record was deleted if (serviceArea.Name == null) { CopyToInstance(performContext, dbContext, item, ref serviceArea, systemId); // update the import map importMap.NewKey = serviceArea.Id; dbContext.ImportMaps.Update(importMap); dbContext.SaveChangesForImport(); } else // ordinary update { CopyToInstance(performContext, dbContext, item, ref serviceArea, systemId); // touch the import map importMap.LastUpdateTimestamp = DateTime.UtcNow; dbContext.ImportMaps.Update(importMap); dbContext.SaveChangesForImport(); } } } performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMap(dbContext, OldTable, completed, NewTable, SigId); } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); } }
/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="serviceArea"></param> /// <param name="systemId"></param> private static void CopyToInstance(DbAppContext dbContext, ServiceArea oldObject, ref Models.ServiceArea serviceArea, string systemId) { try { if (serviceArea == null) { serviceArea = new Models.ServiceArea(); } if (oldObject.Service_Area_Id <= 0) { return; } serviceArea.Id = oldObject.Service_Area_Id; serviceArea.MinistryServiceAreaID = oldObject.Service_Area_Id; serviceArea.Name = oldObject.Service_Area_Desc.Trim(); // remove " CA" from Service Area Names if (serviceArea.Name.EndsWith(" CA")) { serviceArea.Name = serviceArea.Name.Replace(" CA", ""); } // service area number if (oldObject.Service_Area_Cd != null) { serviceArea.AreaNumber = int.Parse(oldObject.Service_Area_Cd); } // get the district for this service area int tempServiceAreaId = GetServiceAreaId(serviceArea.Name); if (tempServiceAreaId > 0) { District district = dbContext.Districts.AsNoTracking() .FirstOrDefault(x => x.MinistryDistrictID == tempServiceAreaId); if (district != null) { serviceArea.DistrictId = district.Id; } } if (oldObject.FiscalStart != null) { serviceArea.StartDate = DateTime.ParseExact(oldObject.FiscalStart.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); } serviceArea.AppCreateUserid = systemId; serviceArea.AppCreateTimestamp = DateTime.UtcNow; serviceArea.AppLastUpdateUserid = systemId; serviceArea.AppLastUpdateTimestamp = DateTime.UtcNow; dbContext.ServiceAreas.Add(serviceArea); } catch (Exception ex) { Debug.WriteLine("***Error*** - Service Area Id: " + oldObject.Service_Area_Id); Debug.WriteLine(ex.Message); throw; } }
/// <summary> /// Map data /// </summary> /// <param name="performContext"></param> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="serviceArea"></param> /// <param name="systemId"></param> private static void CopyToInstance(PerformContext performContext, DbAppContext dbContext, ServiceArea oldObject, ref Models.ServiceArea serviceArea, string systemId) { bool isNew = false; if (serviceArea == null) { isNew = true; serviceArea = new Models.ServiceArea(); } if (oldObject.Service_Area_Id <= 0) { return; } serviceArea.Id = oldObject.Service_Area_Id; serviceArea.MinistryServiceAreaID = oldObject.Service_Area_Id; serviceArea.DistrictId = oldObject.District_Area_Id; serviceArea.Name = oldObject.Service_Area_Desc.Trim(); serviceArea.AreaNumber = oldObject.Service_Area_Cd; District district = dbContext.Districts.FirstOrDefault(x => x.MinistryDistrictID == oldObject.District_Area_Id); if (district == null) { // this means that the District is not in the database // (this happens when the production data does not include district Other than "Lower Mainland" or all the districts) return; } serviceArea.District = district; try { serviceArea.StartDate = DateTime.ParseExact(oldObject.FiscalStart.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); } catch { // do nothing } if (isNew) { serviceArea.CreateUserid = systemId; serviceArea.CreateTimestamp = DateTime.UtcNow; dbContext.ServiceAreas.Add(serviceArea); } else { serviceArea.LastUpdateUserid = systemId; serviceArea.LastUpdateTimestamp = DateTime.UtcNow; dbContext.ServiceAreas.Update(serviceArea); } try { dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("*** ERROR With add or update Service Area ***"); performContext.WriteLine(e.ToString()); } }