/// <summary> /// Import Local 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; // create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(Area[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); Area[] legacyItems = (Area[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } Debug.WriteLine("Importing LocalArea Data. Total Records: " + legacyItems.Length); foreach (Area item in legacyItems.WithProgress(progress)) { // see if we have this one already ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Area_Id.ToString()); // new entry if (importMap == null && item.Area_Id > 0) { LocalArea localArea = null; CopyToInstance(dbContext, item, ref localArea, systemId); ImportUtility.AddImportMap(dbContext, OldTable, item.Area_Id.ToString(), NewTable, localArea.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 existing Cities /// </summary> /// <param name="performContext"></param> /// <param name="dbContext"></param> /// <param name="fileLocation"></param> /// <param name="systemId"></param> private static void ImportCities(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; } int maxCityIndex = 0; if (dbContext.Cities.Any()) { maxCityIndex = dbContext.Cities.Max(x => x.Id); } try { string rootAttr = "ArrayOf" + OldTable; performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(HetsCity[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); HetsCity[] legacyItems = (HetsCity[])ser.Deserialize(memoryStream); foreach (HetsCity item in legacyItems.WithProgress(progress)) { // see if we have this one already ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.City_Id.ToString()); // new entry if (importMap == null) { City city = null; CopyToInstance(dbContext, item, ref city, systemId, ref maxCityIndex); ImportUtility.AddImportMap(dbContext, OldTable, item.City_Id.ToString(), NewTable, city.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 Equipment Types /// </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; } int maxEquipTypeIndex = 0; if (dbContext.EquipmentTypes.Any()) { maxEquipTypeIndex = dbContext.EquipmentTypes.Max(x => x.Id); } try { string rootAttr = "ArrayOf" + OldTable; // create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(EquipType[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); EquipType[] legacyItems = (EquipType[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } Debug.WriteLine("Importing EquipmentType Data. Total Records: " + legacyItems.Length); foreach (EquipType item in legacyItems.WithProgress(progress)) { // see if we have this one already ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Equip_Type_Id.ToString()); // new entry (only import new records of Equipment Type) if (importMap == null && item.Equip_Type_Id > 0) { EquipmentType equipType = null; CopyToInstance(dbContext, item, ref equipType, systemId, ref maxEquipTypeIndex); if (equipType != null) { ImportUtility.AddImportMap(dbContext, OldTable, item.Equip_Type_Id.ToString(), NewTable, equipType.Id); } } // save change to database periodically to avoid frequent writing to the database if (ii++ % 500 == 0) { try { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BcBidImport.SigId.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChangesForImport(); } catch (Exception e) { string temp = string.Format("Error saving data (EquipmentTypeIndex: {0}): {1}", maxEquipTypeIndex, e.Message); performContext.WriteLine(temp); throw new DataException(temp); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); throw; } }
/// <summary> /// Import Users /// </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; } // manage the id value for new user records int maxUserIndex = 0; if (dbContext.Users.Any()) { maxUserIndex = dbContext.Users.Max(x => x.Id); } try { string rootAttr = "ArrayOf" + OldTable; // create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(UserHets[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); UserHets[] legacyItems = (UserHets[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } // create an array of names using the created by and modified by values in the data performContext.WriteLine("Extracting first and last names"); progress.SetValue(0); Dictionary <string, string> firstNames = new Dictionary <string, string>(); Dictionary <string, string> lastNames = new Dictionary <string, string>(); foreach (UserHets item in legacyItems.WithProgress(progress)) { string name = item.Created_By; GetNameParts(name, ref firstNames, ref lastNames); name = item.Modified_By; GetNameParts(name, ref firstNames, ref lastNames); } // import the data performContext.WriteLine("Importing User Data"); progress.SetValue(0); foreach (UserHets item in legacyItems.WithProgress(progress)) { // see if we have this one already ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Popt_Id.ToString()); // new entry if (importMap == null && item.Popt_Id != null) { string username = NormalizeUserCode(item.User_Cd); string firstName = GetNamePart(username, firstNames); string lastName = GetNamePart(username, lastNames); username = username.ToLower(); User instance = dbContext.Users.FirstOrDefault(x => x.SmUserId == username); // if the user exists - move to the next record if (instance != null) { continue; } CopyToInstance(dbContext, item, ref instance, systemId, username, firstName, lastName, ref maxUserIndex); if (instance != null) { ImportUtility.AddImportMap(dbContext, OldTable, item.Popt_Id, NewTable, instance.Id); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BcBidImport.SigId.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChangesForImport(); } catch (Exception e) { string temp = string.Format("Error saving data (UserIndex: {0}): {1}", maxUserIndex, e.Message); performContext.WriteLine(temp); throw new DataException(temp); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); throw; } }
/// <summary> /// Import Local 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); 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; // create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(ImportModels.Area[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ImportModels.Area[] legacyItems = (ImportModels.Area[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } foreach (Area item in legacyItems.WithProgress(progress)) { LocalArea localArea = null; // see if we have this one already ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Area_Id.ToString()); if (dbContext.LocalAreas.Count(x => String.Equals(x.Name, item.Area_Desc.Trim(), StringComparison.CurrentCultureIgnoreCase)) > 0) { localArea = dbContext.LocalAreas.FirstOrDefault(x => x.Name.ToUpper() == item.Area_Desc.Trim().ToUpper()); } // new entry if (importMap == null || dbContext.LocalAreas.Count(x => String.Equals(x.Name, item.Area_Desc.Trim(), StringComparison.CurrentCultureIgnoreCase)) == 0) { if (item.Area_Id > 0) { CopyToInstance(dbContext, item, ref localArea, systemId); ImportUtility.AddImportMap(dbContext, OldTable, item.Area_Id.ToString(), NewTable, localArea.Id); } } else // update { localArea = dbContext.LocalAreas.FirstOrDefault(x => x.Id == importMap.NewKey); // record was deleted if (localArea == null) { CopyToInstance(dbContext, item, ref localArea, systemId); // update the import map importMap.NewKey = localArea.Id; dbContext.ImportMaps.Update(importMap); } else // ordinary update { CopyToInstance(dbContext, item, ref localArea, systemId); // touch the import map importMap.LastUpdateTimestamp = DateTime.UtcNow; dbContext.ImportMaps.Update(importMap); } } // save change to database periodically to avoid frequent writing to the database if (++ii % 250 == 0) { try { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BCBidImport.SigId.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); } }
/// <summary> /// Import Rotaion List /// </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); 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; //Create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(Block[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); Block[] legacyItems = (Block[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } foreach (Block item in legacyItems.WithProgress(progress)) { int areaId = item.Area_Id ?? 0; int equipmentTypeId = item.Equip_Type_Id ?? 0; int blockNum = Convert.ToInt32(float.Parse(item.Block_Num ?? "0.0")); // this is for conversion record hope this is unique string oldUniqueId = ((areaId * 10000 + equipmentTypeId) * 100 + blockNum).ToString(); // see if we have this one already ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == oldUniqueId); // new entry if (importMap == null) { if (areaId > 0) { LocalAreaRotationList instance = null; CopyToInstance(dbContext, item, ref instance, systemId); ImportUtility.AddImportMap(dbContext, OldTable, oldUniqueId, NewTable, instance.Id); } } else // update { LocalAreaRotationList instance = dbContext.LocalAreaRotationLists.FirstOrDefault(x => x.Id == importMap.NewKey); // record was deleted if (instance == null) { CopyToInstance(dbContext, item, ref instance, systemId); // update the import map importMap.NewKey = instance.Id; dbContext.ImportMaps.Update(importMap); } else // ordinary update { CopyToInstance(dbContext, item, ref instance, systemId); // touch the import map importMap.LastUpdateTimestamp = DateTime.UtcNow; dbContext.ImportMaps.Update(importMap); } } // save change to database periodically to avoid frequent writing to the database if (++ii % 500 == 0) { try { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BCBidImport.SigId.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); } }
/// <summary> /// Import Owner Records /// </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); if (startPoint == BCBidImport.SigId) // this means the import job it has done today is complete for all the records in the xml file. // 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; } List <Owner> data = new List <Owner>(); int maxOwnerIndex = dbContext.Owners.Max(x => x.Id); int maxContactIndex = dbContext.Contacts.Max(x => x.Id); try { string rootAttr = "ArrayOf" + OldTable; // create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(ImportModels.Owner[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ImportModels.Owner[] legacyItems = (ImportModels.Owner[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } foreach (ImportModels.Owner item in legacyItems.WithProgress(progress)) { // see if we have this one already. ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Popt_Id.ToString()); // new entry if (importMap == null) { Owner owner = null; CopyToInstance(dbContext, item, ref owner, systemId, ref maxOwnerIndex, ref maxContactIndex); data.Add(owner); ImportUtility.AddImportMap(dbContext, OldTable, item.Popt_Id.ToString(), NewTable, owner.Id); } else // update { Owner owner = dbContext.Owners.FirstOrDefault(x => x.Id == importMap.NewKey); if (owner == null) // record was deleted { CopyToInstance(dbContext, item, ref owner, systemId, ref maxOwnerIndex, ref maxContactIndex); // update the import map importMap.NewKey = owner.Id; dbContext.ImportMaps.Update(importMap); } else // ordinary update. { CopyToInstance(dbContext, item, ref owner, systemId, ref maxOwnerIndex, ref maxContactIndex); // touch the import map importMap.LastUpdateTimestamp = DateTime.UtcNow; dbContext.ImportMaps.Update(importMap); } } // save change to database periodically to avoid frequent writing to the database if (++ii % 500 == 0) { try { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BCBidImport.SigId.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); } }
/// <summary> /// Import Rotations /// </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); 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; //Create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(RotationDoc[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); RotationDoc[] legacyItems = (RotationDoc[])ser.Deserialize(memoryStream); //Use this list to save a trip to query database in each iteration List <Equipment> equips = dbContext.Equipments .Include(x => x.DumpTruck) .Include(x => x.DistrictEquipmentType) .ToList(); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } foreach (RotationDoc item in legacyItems.WithProgress(progress)) { // see if we have this one already string oldKey = item.Equip_Id + item.Note_Dt + item.Created_Dt; ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == oldKey); // new entry if (importMap == null) { Note instance = null; CopyToInstance(dbContext, item, ref instance, equips); ImportUtility.AddImportMap(dbContext, OldTable, oldKey, NewTable, instance.Id); } else // update { Note instance = dbContext.Notes.FirstOrDefault(x => x.Id == importMap.NewKey); // record was deleted if (instance == null) { CopyToInstance(dbContext, item, ref instance, equips); // update the import map importMap.NewKey = instance.Id; dbContext.ImportMaps.Update(importMap); } else // ordinary update. { CopyToInstance(dbContext, item, ref instance, equips); // touch the import map importMap.LastUpdateTimestamp = DateTime.UtcNow; dbContext.ImportMaps.Update(importMap); } } // save change to database periodically to avoid frequent writing to the database if (++ii % 1000 == 0) { try { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BCBidImport.SigId.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); } }
/// <summary> /// Recalculate the block assignment for each piece of equipment /// </summary> /// <param name="performContext"></param> /// <param name="seniorityScoringRules"></param> /// <param name="dbContext"></param> /// <param name="systemId"></param> public static void ProcessBlocks(PerformContext performContext, string seniorityScoringRules, DbAppContext dbContext, string systemId) { try { performContext.WriteLine("*** Recalculating Equipment Block Assignment ***"); Debug.WriteLine("Recalculating Equipment Block Assignment"); int ii = 0; string _oldTableProgress = "BlockAssignment_Progress"; string _newTable = "BlockAssignment"; // check if the block assignment has already been completed int startPoint = ImportUtility.CheckInterMapForStartPoint(dbContext, _oldTableProgress, BcBidImport.SigId, _newTable); if (startPoint == BcBidImport.SigId) // this means the assignment job is complete { performContext.WriteLine("*** Recalculating Equipment Block Assignment is complete from the former process ***"); return; } // ************************************************************ // cleanup old block assignment status records // ************************************************************ List <ImportMap> importMapList = dbContext.ImportMaps .Where(x => x.OldTable == _oldTableProgress && x.NewTable == _newTable) .ToList(); foreach (ImportMap importMap in importMapList) { dbContext.ImportMaps.Remove(importMap); } dbContext.SaveChanges(); // ************************************************************ // get processing rules // ************************************************************ SeniorityScoringRules scoringRules = new SeniorityScoringRules(seniorityScoringRules); // ************************************************************ // get all local areas // (using active equipment to minimize the results) // ************************************************************ List <LocalArea> localAreas = dbContext.Equipments .Include(x => x.LocalArea) .Where(x => x.Status == Equipment.StatusApproved && x.ArchiveCode == "N") .Select(x => x.LocalArea) .Distinct() .ToList(); // ************************************************************ // get all district equipment types // (using active equipment to minimize the results) // ************************************************************ List <DistrictEquipmentType> equipmentTypes = dbContext.Equipments .Include(x => x.DistrictEquipmentType) .Where(x => x.Status == Equipment.StatusApproved && x.ArchiveCode == "N") .Select(x => x.DistrictEquipmentType) .Distinct() .ToList(); // ************************************************************************ // iterate the data and update the assugnment blocks // (seniority is already calculated) // ************************************************************************ Debug.WriteLine("Recalculating Equipment Block Assignment - Local Area Record Count: " + localAreas.Count); foreach (LocalArea localArea in localAreas) { foreach (DistrictEquipmentType districtEquipmentType in equipmentTypes) { // get the associated equipment type EquipmentType equipmentTypeRecord = dbContext.EquipmentTypes.FirstOrDefault(x => x.Id == districtEquipmentType.EquipmentTypeId); if (equipmentTypeRecord == null) { throw new DataException(string.Format("Invalid District Equipment Type. No associated Equipment Type record (District Equipment Id: {0})", districtEquipmentType.Id)); } // get rules int blockSize = equipmentTypeRecord.IsDumpTruck ? scoringRules.GetBlockSize("DumpTruck") : scoringRules.GetBlockSize(); int totalBlocks = equipmentTypeRecord.IsDumpTruck ? scoringRules.GetTotalBlocks("DumpTruck") : scoringRules.GetTotalBlocks(); // assign blocks SeniorityListExtensions.AssignBlocks(dbContext, localArea.Id, districtEquipmentType.Id, blockSize, totalBlocks, false); // save change to database periodically to avoid frequent writing to the database if (ii++ % 1000 == 0) { try { Debug.WriteLine("Recalculating Equipment Block Assignment - Index: " + ii); ImportUtility.AddImportMapForProgress(dbContext, _oldTableProgress, ii.ToString(), BcBidImport.SigId, _newTable); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } } // ************************************************************ // save final set of updates // ************************************************************ try { performContext.WriteLine("*** Recalculating Equipment Block Assignment is Done ***"); Debug.WriteLine("Recalculating Equipment Block Assignment is Done"); ImportUtility.AddImportMapForProgress(dbContext, _oldTableProgress, BcBidImport.SigId.ToString(), BcBidImport.SigId, _newTable); dbContext.SaveChangesForImport(); } catch (Exception e) { string temp = string.Format("Error saving data (Record: {0}): {1}", ii, e.Message); performContext.WriteLine(temp); throw new DataException(temp); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); throw; } }
/// <summary> /// Import Dis(trict) Equip(ment) Type /// </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); 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; // create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(EquipType[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); EquipType[] legacyItems = (EquipType[])ser.Deserialize(memoryStream); int ii = startPoint; if (startPoint > 0) // skip the portion already processed { legacyItems = legacyItems.Skip(ii).ToArray(); } foreach (EquipType item in legacyItems.WithProgress(progress)) { string serviceAreaName; // see if we have this one already. ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Equip_Type_Id.ToString()); float equipRentalRateNo; try { equipRentalRateNo = (float)Decimal.Parse(item.Equip_Rental_Rate_No, System.Globalization.NumberStyles.Any); } catch { equipRentalRateNo = (float)0.1; } string description; try { description = item.Equip_Type_Desc.Length >= 225 ? item.Equip_Type_Desc.Substring(0, 225) : item.Equip_Type_Desc; } catch { description = ""; } // new entry if (importMap == null) { if (item.Equip_Type_Id > 0) { DistrictEquipmentType instance = null; serviceAreaName = CopyToInstance(dbContext, item, ref instance, systemId, equipRentalRateNo); if (serviceAreaName != "ERROR") { AddingDistrictEquipmentTypeInstance(dbContext, item, instance, equipRentalRateNo, description, serviceAreaName, true); } } } else // update { DistrictEquipmentType instance = dbContext.DistrictEquipmentTypes.FirstOrDefault(x => x.Id == importMap.NewKey); if (instance == null) // record was deleted { serviceAreaName = CopyToInstance(dbContext, item, ref instance, systemId, equipRentalRateNo); if (serviceAreaName != "ERROR") { AddingDistrictEquipmentTypeInstance(dbContext, item, instance, equipRentalRateNo, description, serviceAreaName, false); // update the import map importMap.NewKey = instance.Id; dbContext.ImportMaps.Update(importMap); } } else // ordinary update. { serviceAreaName = CopyToInstance(dbContext, item, ref instance, systemId, equipRentalRateNo); if (serviceAreaName != "ERROR") { AddingDistrictEquipmentTypeInstance(dbContext, item, instance, equipRentalRateNo, description, serviceAreaName, false); // touch the import map importMap.LastUpdateTimestamp = DateTime.UtcNow; dbContext.ImportMaps.Update(importMap); } } } // save change to database periodically to avoid frequent writing to the database if (++ii % 250 == 0) { try { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BCBidImport.SigId.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); } }
/// <summary> /// Import Users /// </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); 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; // create Processer progress indicator performContext.WriteLine("Processing " + OldTable); IProgressBar progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(UserHets[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); UserHets[] legacyItems = (UserHets[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } foreach (UserHets item in legacyItems.WithProgress(progress)) { // see if we have this one already ImportMap importMap = dbContext.ImportMaps.FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Popt_Id.ToString()); User instance = dbContext.Users.FirstOrDefault(x => item.User_Cd.ToUpper().IndexOf(x.SmUserId.ToUpper(), StringComparison.Ordinal) >= 0); if (instance == null) { CopyToInstance(dbContext, item, ref instance, systemId); // new entry if (importMap == null && instance != null) { ImportUtility.AddImportMap(dbContext, OldTable, item.Popt_Id.ToString(), NewTable, instance.Id); } ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, (++ii).ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BCBidImport.SigId.ToString(), BCBidImport.SigId); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); } }
/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="timeRecord"></param> /// <param name="systemId"></param> /// <param name="maxTimesheetIndex"></param> private static void CopyToTimeRecorded(DbAppContext dbContext, EquipUsage oldObject, ref TimeRecord timeRecord, string systemId, ref int maxTimesheetIndex) { try { if (oldObject.Equip_Id <= 0) { return; } if (oldObject.Project_Id <= 0) { return; } // *********************************************** // we only need records from the current fiscal // so ignore all others // *********************************************** DateTime fiscalStart; DateTime fiscalEnd; if (DateTime.UtcNow.Month == 1 || DateTime.UtcNow.Month == 2 || DateTime.UtcNow.Month == 3) { fiscalEnd = new DateTime(DateTime.UtcNow.Year, 3, 31); } else { fiscalEnd = new DateTime(DateTime.UtcNow.AddYears(1).Year, 3, 31); } if (DateTime.UtcNow.Month == 1 || DateTime.UtcNow.Month == 2 || DateTime.UtcNow.Month == 3) { fiscalStart = new DateTime(DateTime.UtcNow.AddYears(-1).Year, 4, 1); } else { fiscalStart = new DateTime(DateTime.UtcNow.Year, 4, 1); } string tempRecordDate = oldObject.Worked_Dt; if (string.IsNullOrEmpty(tempRecordDate)) { return; // ignore if we don't have a created date } if (!string.IsNullOrEmpty(tempRecordDate)) { DateTime?recordDate = ImportUtility.CleanDateTime(tempRecordDate); if (recordDate == null || recordDate < fiscalStart || recordDate > fiscalEnd) { return; // ignore this record - it is outside of the current fiscal year } } // ************************************************ // get the imported equipment record map // ************************************************ string tempId = oldObject.Equip_Id.ToString(); ImportMap mapEquip = dbContext.ImportMaps.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempId && x.OldTable == ImportEquip.OldTable && x.NewTable == ImportEquip.NewTable); if (mapEquip == null) { throw new DataException(string.Format("Cannot locate Equipment record (Timesheet Equip Id: {0}", tempId)); } // *********************************************** // find the equipment record // *********************************************** Equipment equipment = dbContext.Equipments.AsNoTracking().FirstOrDefault(x => x.Id == mapEquip.NewKey); if (equipment == null) { throw new ArgumentException(string.Format("Cannot locate Equipment record (Timesheet Equip Id: {0}", tempId)); } // ************************************************ // get the imported project record map // ************************************************ string tempProjectId = oldObject.Project_Id.ToString(); ImportMap mapProject = dbContext.ImportMaps.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempProjectId && x.OldTable == ImportProject.OldTable && x.NewTable == ImportProject.NewTable); // *********************************************** // find the project record // (or create a project (inactive)) // *********************************************** Models.Project project; if (mapProject != null) { project = dbContext.Projects.AsNoTracking().FirstOrDefault(x => x.Id == mapProject.NewKey); if (project == null) { throw new ArgumentException(string.Format("Cannot locate Project record (Timesheet Equip Id: {0}", tempId)); } } else { // create new project project = new Models.Project { Information = "Created to support Time Record import from BCBid", Status = "Complete", Name = "Legacy BCBid Project", AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; dbContext.Projects.Add(project); // save now so we can access it for other time records dbContext.SaveChanges(); // add mapping record ImportUtility.AddImportMapForProgress(dbContext, ImportProject.OldTable, tempProjectId, project.Id, ImportProject.NewTable); dbContext.SaveChanges(); } // *********************************************** // find or create the rental agreement // *********************************************** DateTime?enteredDate = ImportUtility.CleanDateTime(oldObject.Entered_Dt); // use for the agreement RentalAgreement agreement = dbContext.RentalAgreements.AsNoTracking() .FirstOrDefault(x => x.EquipmentId == equipment.Id && x.ProjectId == project.Id); if (agreement == null) { // create a new agreement record agreement = new RentalAgreement { EquipmentId = equipment.Id, ProjectId = project.Id, Note = "Created to support Time Record import from BCBid", Number = "Legacy BCBid Agreement", DatedOn = enteredDate, AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; if (project.RentalAgreements == null) { project.RentalAgreements = new List <RentalAgreement>(); } project.RentalAgreements.Add(agreement); // save now so we can access it for other time records dbContext.SaveChangesForImport(); } // *********************************************** // create time record // *********************************************** timeRecord = new TimeRecord { Id = ++maxTimesheetIndex }; // *********************************************** // set time record attributes // *********************************************** DateTime?workedDate = ImportUtility.CleanDateTime(oldObject.Worked_Dt); if (workedDate != null) { timeRecord.WorkedDate = (DateTime)workedDate; } else { throw new DataException(string.Format("Worked Date cannot be null (TimesheetIndex: {0}", maxTimesheetIndex)); } // get hours worked float?tempHoursWorked = ImportUtility.GetFloatValue(oldObject.Hours); if (tempHoursWorked != null) { timeRecord.Hours = tempHoursWorked; } else { throw new DataException(string.Format("Hours cannot be null (TimesheetIndex: {0}", maxTimesheetIndex)); } if (enteredDate != null) { timeRecord.EnteredDate = (DateTime)enteredDate; } else { throw new DataException(string.Format("Entered Date cannot be null (TimesheetIndex: {0}", maxTimesheetIndex)); } // *********************************************** // create time record // *********************************************** timeRecord.AppCreateUserid = systemId; timeRecord.AppCreateTimestamp = DateTime.UtcNow; timeRecord.AppLastUpdateUserid = systemId; timeRecord.AppLastUpdateTimestamp = DateTime.UtcNow; if (agreement.TimeRecords == null) { agreement.TimeRecords = new List <TimeRecord>(); } agreement.TimeRecords.Add(timeRecord); } catch (Exception ex) { Debug.WriteLine("***Error*** - Worked Date: " + oldObject.Worked_Dt); Debug.WriteLine("***Error*** - Master TimeRecord Index: " + maxTimesheetIndex); Debug.WriteLine(ex.Message); throw; } }