/// <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(ImportModels.ServiceArea[]), new XmlRootAttribute(rootAttr)); ser.UnknownAttribute += ImportUtility.UnknownAttribute; ser.UnknownElement += ImportUtility.UnknownElement; MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ImportModels.ServiceArea[] legacyItems = (ImportModels.ServiceArea[])ser.Deserialize(memoryStream); Debug.WriteLine("Importing ServiceArea Data. Total Records: " + legacyItems.Length); foreach (ImportModels.ServiceArea item in legacyItems.WithProgress(progress)) { // see if we have this one already HetImportMap importMap = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Service_Area_Id.ToString()); // new entry if (importMap == null && item.Service_Area_Cd != "000") { HetServiceArea serviceArea = null; CopyToInstance(dbContext, item, ref serviceArea, systemId); ImportUtility.AddImportMap(dbContext, OldTable, item.Service_Area_Id.ToString(), NewTable, serviceArea.ServiceAreaId); } } 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 Attachments /// </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 maxEquipAttachIndex = 0; if (dbContext.HetEquipmentAttachment.Any()) { maxEquipAttachIndex = dbContext.HetEquipmentAttachment.Max(x => x.EquipmentAttachmentId); } try { string rootAttr = "ArrayOf" + OldTable; // create 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.EquipAttach[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ImportModels.EquipAttach[] legacyItems = (ImportModels.EquipAttach[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } Debug.WriteLine("Importing Equipment Attachment Data. Total Records: " + legacyItems.Length); foreach (ImportModels.EquipAttach item in legacyItems.WithProgress(progress)) { // see if we have this one already. We used old combine because item.Equip_Id is not unique string oldKeyCombined = (item.Equip_Id ?? 0 * 100 + item.Attach_Seq_Num ?? 0).ToString(); HetImportMap importMap = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == oldKeyCombined); // new entry if (importMap == null && item.Equip_Id > 0) { HetEquipmentAttachment instance = null; CopyToInstance(dbContext, item, ref instance, systemId, ref maxEquipAttachIndex); ImportUtility.AddImportMap(dbContext, OldTable, oldKeyCombined, NewTable, instance.EquipmentAttachmentId); } // 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, 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 (EquipmentAttachmentIndex: {0}): {1}", maxEquipAttachIndex, e.Message); performContext.WriteLine(temp); throw new DataException(temp); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); throw; } }
/// <summary> /// Create Last Called /// </summary> /// <param name="performContext"></param> /// <param name="dbContext"></param> /// <param name="systemId"></param> public static void ProcessLastCalled(PerformContext performContext, DbAppContext dbContext, string systemId) { try { performContext.WriteLine("*** Recreating Last Called ***"); Debug.WriteLine("Recreating Last Called"); int ii = 0; string _oldTableProgress = "LastCalled_Progress"; string _newTable = "LastCalled"; // check if the last called 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("*** Recreating Last Called is complete from the former process ***"); return; } // ************************************************************ // get all last called records // ************************************************************ List <HetLocalAreaRotationList> rotationList = dbContext.HetLocalAreaRotationList.AsNoTracking() .Distinct() .ToList(); // ************************************************************************ // iterate the data and create rotation requests // ************************************************************************ Debug.WriteLine("Recreating Last Called - Rotation List Record Count: " + rotationList.Count); // get status int?statusIdComplete = StatusHelper.GetStatusId(HetRentalRequest.StatusComplete, "rentalRequestStatus", dbContext); if (statusIdComplete == null) { throw new DataException("Status Id cannot be null"); } foreach (HetLocalAreaRotationList listItem in rotationList) { HetRentalRequest request = new HetRentalRequest { LocalAreaId = listItem.LocalAreaId, DistrictEquipmentTypeId = listItem.DistrictEquipmentTypeId, RentalRequestStatusTypeId = (int)statusIdComplete, ExpectedStartDate = DateTime.Now, ExpectedEndDate = DateTime.Now, EquipmentCount = 1, ExpectedHours = 0, AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; dbContext.HetRentalRequest.Add(request); // save change to database if (ii++ % 100 == 0) { Debug.WriteLine("Recreating Last Called - Index: " + ii); ImportUtility.AddImportMapForProgress(dbContext, _oldTableProgress, ii.ToString(), BcBidImport.SigId, _newTable); dbContext.SaveChangesForImport(); } } // save remaining requests dbContext.SaveChangesForImport(); // ************************************************************************ // iterate the data and create "last called" records // ************************************************************************ foreach (HetLocalAreaRotationList listItem in rotationList) { // get request HetRentalRequest request = dbContext.HetRentalRequest.AsNoTracking() .FirstOrDefault(x => x.LocalAreaId == listItem.LocalAreaId && x.DistrictEquipmentTypeId == listItem.DistrictEquipmentTypeId); if (request == null) { throw new DataException("Rental request cannot be null"); } // block 1 if (listItem.AskNextBlock1Id != null) { // create last call record HetRentalRequestRotationList rotation = new HetRentalRequestRotationList { RentalRequestId = request.RentalRequestId, EquipmentId = listItem.AskNextBlock1Id, BlockNumber = 1, RotationListSortOrder = 1, AskedDateTime = DateTime.Now, WasAsked = true, OfferResponse = "Yes", OfferResponseDatetime = DateTime.Now, IsForceHire = false, Note = "CONVERSION", AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; dbContext.HetRentalRequestRotationList.Add(rotation); } // block 2 if (listItem.AskNextBlock2Id != null) { // create last call record HetRentalRequestRotationList rotation = new HetRentalRequestRotationList { RentalRequestId = request.RentalRequestId, EquipmentId = listItem.AskNextBlock2Id, BlockNumber = 2, RotationListSortOrder = 2, AskedDateTime = DateTime.Now, WasAsked = true, OfferResponse = "Yes", OfferResponseDatetime = DateTime.Now, IsForceHire = false, Note = "CONVERSION", AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; dbContext.HetRentalRequestRotationList.Add(rotation); } // open block if (listItem.AskNextBlockOpenId != null) { // get equipment record HetEquipment equipment = dbContext.HetEquipment.AsNoTracking() .FirstOrDefault(x => x.EquipmentId == listItem.AskNextBlockOpenId); if (equipment == null) { throw new DataException("Equipment cannot be null"); } // create last call record HetRentalRequestRotationList rotation = new HetRentalRequestRotationList { RentalRequestId = request.RentalRequestId, EquipmentId = listItem.AskNextBlockOpenId, BlockNumber = equipment.BlockNumber, RotationListSortOrder = 3, AskedDateTime = DateTime.Now, WasAsked = true, OfferResponse = "Yes", OfferResponseDatetime = DateTime.Now, IsForceHire = false, Note = "CONVERSION", AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; dbContext.HetRentalRequestRotationList.Add(rotation); } // save change to database if (ii++ % 100 == 0) { Debug.WriteLine("Recreating Last Called - Index: " + ii); ImportUtility.AddImportMapForProgress(dbContext, _oldTableProgress, ii.ToString(), BcBidImport.SigId, _newTable); dbContext.SaveChangesForImport(); } } // save remaining requests dbContext.SaveChangesForImport(); // ************************************************************ // save final set of updates // ************************************************************ try { performContext.WriteLine("*** Recreating Last Called is Done ***"); Debug.WriteLine("Recreating Last Called 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 Rotation Doc Records 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, NewTable); 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; } int maxIndex = startPoint; try { string rootAttr = "ArrayOf" + OldTable; // create 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.RotationDoc[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ImportModels.RotationDoc[] legacyItems = (ImportModels.RotationDoc[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } Debug.WriteLine("Importing Rotation Doc Data. Total Records: " + legacyItems.Length); foreach (ImportModels.RotationDoc item in legacyItems.WithProgress(progress)) { // see if we have this one already. HetImportMap importMap = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Note_Id.ToString()); // new entry if (importMap == null) { BcbidRotationDoc rotationDoc = null; CopyToInstance(dbContext, item, ref rotationDoc, systemId, ref maxIndex); ImportUtility.AddImportMap(dbContext, OldTable, item.Note_Id.ToString(), NewTable, rotationDoc.NoteId); } // save change to database if (++ii % 2000 == 0) { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChangesForImport(); } } 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 (Index: {0}): {1}", maxIndex, 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.HetUser.Any()) { maxUserIndex = dbContext.HetUser.Max(x => x.UserId); maxUserIndex = maxUserIndex + 1; } try { string rootAttr = "ArrayOf" + OldTable; // create 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.UserHets[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ImportModels.UserHets[] legacyItems = (ImportModels.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 (ImportModels.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 (ImportModels.UserHets item in legacyItems.WithProgress(progress)) { string tempId = item.Popt_Id + "-" + item.Service_Area_Id; HetImportMap importMap = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == tempId); if (importMap == null) { string username = NormalizeUserCode(item.User_Cd).ToUpper(); string firstName = GetNamePart(username, firstNames); string lastName = GetNamePart(username, lastNames); HetUser user = null; username = username.ToLower(); CopyToInstance(dbContext, item, ref user, systemId, username, firstName, lastName, ref maxUserIndex); if (user != null) { ImportUtility.AddImportMap(dbContext, OldTable, tempId, NewTable, user.UserId); dbContext.SaveChangesForImport(); } } } 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> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="rotationDoc"></param> /// <param name="systemId"></param> /// <param name="maxIndex"></param> private static void CopyToInstance(DbAppContext dbContext, ImportModels.RotationDoc oldObject, ref BcbidRotationDoc rotationDoc, string systemId, ref int maxIndex) { try { if (rotationDoc != null) { return; } rotationDoc = new BcbidRotationDoc { NoteId = oldObject.Note_Id }; ++maxIndex; // *********************************************** // we only need records from the current fiscal // so ignore all others // *********************************************** DateTime fiscalStart; 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); } // *********************************************** // set rotation data // *********************************************** string noteType = oldObject.Note_Type; if (string.IsNullOrEmpty(noteType)) { return; } rotationDoc.NoteType = noteType.Trim(); // reason string reason = oldObject.Reason; if (!string.IsNullOrEmpty(reason)) { rotationDoc.Reason = reason; } // asked date DateTime?createdDate = ImportUtility.CleanDate(oldObject.Created_Dt); if (createdDate == null || createdDate < fiscalStart) { return; // move to next } rotationDoc.AskedDate = (DateTime)createdDate; // was asked -- ForceHire if (noteType.ToUpper() == "FORCEHIRE") { rotationDoc.WasAsked = false; rotationDoc.IsForceHire = true; } else { rotationDoc.WasAsked = true; rotationDoc.IsForceHire = false; } // setup the reason string tempResponse = ""; if (noteType.ToUpper() == "FORCEHIRE") { tempResponse = "Force Hire"; } else if (noteType.ToUpper() == "NOHIRE") { tempResponse = "No Hire"; } else { switch (noteType.ToUpper()) { case "0": tempResponse = "Owner didn't call back/no answer"; break; case "1": tempResponse = "Equipment not suitable"; break; case "2": tempResponse = "Working elsewhere"; break; case "3": tempResponse = "No agreement on rates"; break; case "4": tempResponse = "Equipment under repairs"; break; case "5": tempResponse = "Work limit reached"; break; case "6": tempResponse = "No WCB/WCB in arrears"; break; case "7": tempResponse = "No insurance/inadequate insurance"; break; case "8": tempResponse = "Not interested/turned job down"; break; case "9": tempResponse = "Equipment not available"; break; case "10": tempResponse = "Other"; break; } } if (string.IsNullOrEmpty(tempResponse)) { tempResponse = noteType; } rotationDoc.OfferRefusalReason = tempResponse; // ************************************************ // get the imported equipment record map // ************************************************ string tempId = oldObject.Equip_Id.ToString(); HetImportMap mapEquip = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempId && x.OldTable == ImportEquip.OldTable && x.NewTable == ImportEquip.NewTable); if (mapEquip == null) { return; // ignore and move to the next record } // *********************************************** // find the equipment record // *********************************************** HetEquipment equipment = dbContext.HetEquipment.AsNoTracking() .Include(x => x.LocalArea) .ThenInclude(y => y.ServiceArea) .ThenInclude(z => z.District) .FirstOrDefault(x => x.EquipmentId == mapEquip.NewKey); if (equipment == null) { return; // ignore and move to the next record } int tempNewEquipmentId = equipment.EquipmentId; rotationDoc.EquipmentId = tempNewEquipmentId; // ************************************************ // get the imported project record map // ************************************************ string tempProjectId = oldObject.Project_Id.ToString(); HetImportMap mapProject = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempProjectId && x.OldTable == ImportProject.OldTable && x.NewTable == ImportProject.NewTable); // *********************************************** // find the project record // *********************************************** HetProject project; if (mapProject != null) { project = dbContext.HetProject.AsNoTracking() .FirstOrDefault(x => x.ProjectId == mapProject.NewKey); if (project == null) { throw new ArgumentException(string.Format("Cannot locate Project record (Rotation Doc Id: {0}", tempId)); } int tempNewProjectId = project.ProjectId; rotationDoc.ProjectId = tempNewProjectId; } else { int districtId = equipment.LocalArea.ServiceArea.District.DistrictId; int?statusId = StatusHelper.GetStatusId(HetProject.StatusComplete, "projectStatus", dbContext); if (statusId == null) { throw new DataException(string.Format("Status Id cannot be null (Time Sheet Equip Id: {0}", tempId)); } // create new project project = new HetProject { DistrictId = districtId, Information = "Created to support Rotation Doc import from BCBid", ProjectStatusTypeId = (int)statusId, Name = "Legacy BCBid Project", AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; // save now so we can access it for other time records dbContext.HetProject.Add(project); dbContext.SaveChangesForImport(); // add mapping record ImportUtility.AddImportMapForProgress(dbContext, ImportProject.OldTable, tempProjectId, project.ProjectId, ImportProject.NewTable); dbContext.SaveChangesForImport(); } // *********************************************** // create rotationDoc // *********************************************** rotationDoc.AppCreateUserid = systemId; rotationDoc.AppCreateTimestamp = DateTime.UtcNow; rotationDoc.AppLastUpdateUserid = systemId; rotationDoc.AppLastUpdateTimestamp = DateTime.UtcNow; dbContext.BcbidRotationDoc.Add(rotationDoc); } catch (Exception ex) { Debug.WriteLine("***Error*** - Master Rotation Doc Index: " + maxIndex); Debug.WriteLine(ex.Message); throw; } }
/// <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, ImportModels.EquipUsage oldObject, ref HetTimeRecord 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; 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.CleanDate(tempRecordDate); if (recordDate == null || recordDate < fiscalStart) { return; // ignore this record - it is outside of the fiscal years } } // ************************************************ // get the imported equipment record map // ************************************************ string tempId = oldObject.Equip_Id.ToString(); HetImportMap mapEquip = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempId && x.OldTable == ImportEquip.OldTable && x.NewTable == ImportEquip.NewTable); if (mapEquip == null) { return; // ignore and move to the next record } // *********************************************** // find the equipment record // *********************************************** HetEquipment equipment = dbContext.HetEquipment.AsNoTracking() .Include(x => x.LocalArea) .ThenInclude(y => y.ServiceArea) .ThenInclude(z => z.District) .FirstOrDefault(x => x.EquipmentId == mapEquip.NewKey); if (equipment == null) { return; // ignore and move to the next record } // ************************************************ // get the imported project record map // ************************************************ string tempProjectId = oldObject.Project_Id.ToString(); HetImportMap mapProject = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempProjectId && x.OldTable == ImportProject.OldTable && x.NewTable == ImportProject.NewTable); // *********************************************** // find the project record // (or create a project (inactive)) // *********************************************** HetProject project; if (mapProject != null) { project = dbContext.HetProject.AsNoTracking() .FirstOrDefault(x => x.ProjectId == mapProject.NewKey); if (project == null) { throw new ArgumentException(string.Format("Cannot locate Project record (Time Sheet Equip Id: {0}", tempId)); } } else { int districtId = equipment.LocalArea.ServiceArea.District.DistrictId; int?statusId = StatusHelper.GetStatusId(HetProject.StatusComplete, "projectStatus", dbContext); if (statusId == null) { throw new DataException(string.Format("Status Id cannot be null (Time Sheet Equip Id: {0}", tempId)); } // create new project project = new HetProject { DistrictId = districtId, Information = "Created to support Time Record import from BCBid", ProjectStatusTypeId = (int)statusId, Name = "Legacy BCBid Project", AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; // save now so we can access it for other time records dbContext.HetProject.Add(project); dbContext.SaveChangesForImport(); // add mapping record ImportUtility.AddImportMapForProgress(dbContext, ImportProject.OldTable, tempProjectId, project.ProjectId, ImportProject.NewTable); dbContext.SaveChangesForImport(); } // *********************************************** // find or create the rental agreement // *********************************************** DateTime?enteredDate = ImportUtility.CleanDate(oldObject.Entered_Dt); // use for the agreement HetRentalAgreement agreement = dbContext.HetRentalAgreement.AsNoTracking() .FirstOrDefault(x => x.EquipmentId == equipment.EquipmentId && x.ProjectId == project.ProjectId && x.DistrictId == equipment.LocalArea.ServiceArea.District.DistrictId); if (agreement == null) { int equipmentId = equipment.EquipmentId; int projectId = project.ProjectId; int districtId = equipment.LocalArea.ServiceArea.District.DistrictId; int?statusId = StatusHelper.GetStatusId(HetRentalAgreement.StatusComplete, "rentalAgreementStatus", dbContext); if (statusId == null) { throw new DataException(string.Format("Status Id cannot be null (Time Sheet Equip Id: {0}", tempId)); } int?agrRateTypeId = StatusHelper.GetRatePeriodId(HetRatePeriodType.PeriodDaily, dbContext); if (agrRateTypeId == null) { throw new DataException("Rate Period Id cannot be null"); } int?year = (ImportUtility.CleanDate(oldObject.Worked_Dt))?.Year; // create a new agreement record agreement = new HetRentalAgreement { EquipmentId = equipmentId, ProjectId = projectId, DistrictId = districtId, RentalAgreementStatusTypeId = (int)statusId, RatePeriodTypeId = (int)agrRateTypeId, Note = "Created to support Time Record import from BCBid", Number = string.Format("BCBid{0}-{1}-{2}", projectId, equipmentId, year), DatedOn = enteredDate, AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; // save now so we can access it for other time records dbContext.HetRentalAgreement.Add(agreement); dbContext.SaveChangesForImport(); } // *********************************************** // create time record // *********************************************** timeRecord = new HetTimeRecord { TimeRecordId = ++maxTimeSheetIndex }; // *********************************************** // set time period type // *********************************************** int?timePeriodTypeId = StatusHelper.GetTimePeriodId(HetTimePeriodType.PeriodDay, dbContext); if (timePeriodTypeId == null) { throw new DataException("Time Period Id cannot be null"); } timeRecord.TimePeriodTypeId = (int)timePeriodTypeId; // *********************************************** // set time record attributes // *********************************************** DateTime?workedDate = ImportUtility.CleanDate(oldObject.Worked_Dt); if (workedDate != null) { timeRecord.WorkedDate = (DateTime)workedDate; } else { throw new DataException(string.Format("Worked Date cannot be null (TimeSheet Index: {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 (TimeSheet Index: {0}", maxTimeSheetIndex)); } if (enteredDate != null) { timeRecord.EnteredDate = (DateTime)enteredDate; } else { throw new DataException(string.Format("Entered Date cannot be null (TimeSheet Index: {0}", maxTimeSheetIndex)); } // *********************************************** // create time record // *********************************************** int raId = agreement.RentalAgreementId; timeRecord.RentalAgreementId = raId; timeRecord.AppCreateUserid = systemId; timeRecord.AppCreateTimestamp = DateTime.UtcNow; timeRecord.AppLastUpdateUserid = systemId; timeRecord.AppLastUpdateTimestamp = DateTime.UtcNow; dbContext.HetTimeRecord.Add(timeRecord); } catch (Exception ex) { Debug.WriteLine("***Error*** - Worked Date: " + oldObject.Worked_Dt); Debug.WriteLine("***Error*** - Master Time Record Index: " + maxTimeSheetIndex); Debug.WriteLine(ex.Message); throw; } }
/// <summary> /// Import Equipment Usage /// </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 completed for all the records in this file { performContext.WriteLine("*** Importing " + XmlFileName + " is complete from the former process ***"); return; } int maxTimeSheetIndex = 0; if (dbContext.HetTimeRecord.Any()) { maxTimeSheetIndex = dbContext.HetTimeRecord.Max(x => x.TimeRecordId); } try { string rootAttr = "ArrayOf" + OldTable; // create 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.EquipUsage[]), new XmlRootAttribute(rootAttr)); MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); ImportModels.EquipUsage[] legacyItems = (ImportModels.EquipUsage[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } Debug.WriteLine("Importing TimeSheet Data. Total Records: " + legacyItems.Length); foreach (ImportModels.EquipUsage item in legacyItems.WithProgress(progress)) { // see if we have this one already string oldProjectKey = item.Project_Id.ToString(); string oldEquipKey = item.Equip_Id.ToString(); string oldCreatedDate = item.Created_Dt; string oldKey = string.Format("{0}-{1}-{2}", oldProjectKey, oldEquipKey, oldCreatedDate); HetImportMap importMap = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == oldKey); // new entry if (importMap == null && item.Equip_Id > 0 && item.Project_Id > 0) { HetTimeRecord instance = null; CopyToTimeRecorded(dbContext, item, ref instance, systemId, ref maxTimeSheetIndex); if (instance != null) { ImportUtility.AddImportMap(dbContext, OldTable, oldKey, NewTable, instance.TimeRecordId); } // save change to database if (++ii % 2000 == 0) { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChanges(); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BcBidImport.SigId.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChanges(); } catch (Exception e) { string temp = string.Format("Error saving data (TimeRecordIndex: {0}): {1}", maxTimeSheetIndex, e.Message); performContext.WriteLine(temp); throw new DataException(temp); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); throw; } }
/// <summary> /// Generate secret keys /// </summary> /// <param name="performContext"></param> /// <param name="dbContext"></param> public static void GenerateSecretKeys(PerformContext performContext, DbAppContext dbContext) { try { performContext.WriteLine("*** Generating New Secret Keys ***"); Debug.WriteLine("Generating New Secret Keys"); int ii = 0; string _oldTableProgress = "SecretKeys_Progress"; string _newTable = "SecretKeys"; // check if the secret keys have 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("*** Generating New Secret Key is complete from the former process ***"); return; } // get records List <HetOwner> owners = dbContext.HetOwner.AsNoTracking() .Where(x => x.BusinessId == null) .ToList(); int i = 0; foreach (HetOwner owner in owners) { i++; string key = SecretKeyHelper.RandomString(8, owner.OwnerId); string temp = owner.OwnerCode; if (string.IsNullOrEmpty(temp)) { temp = SecretKeyHelper.RandomString(4, owner.OwnerId); } key = temp + "-" + DateTime.UtcNow.Year + "-" + key; // get owner and update HetOwner ownerRecord = dbContext.HetOwner.First(x => x.OwnerId == owner.OwnerId); ownerRecord.SharedKey = key; if (i % 500 == 0) { dbContext.SaveChangesForImport(); } // save change to database if (ii++ % 100 == 0) { try { Debug.WriteLine("Generating New Secret Keys - 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("*** Done generating New Secret Keys ***"); Debug.WriteLine("Generating New Secret Keys 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) { Console.WriteLine(e); throw; } }
/// <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 <HetImportMap> importMapList = dbContext.HetImportMap .Where(x => x.OldTable == _oldTableProgress && x.NewTable == _newTable) .ToList(); foreach (HetImportMap importMap in importMapList) { dbContext.HetImportMap.Remove(importMap); } dbContext.SaveChangesForImport(); // ************************************************************ // get processing rules // ************************************************************ SeniorityScoringRules scoringRules = new SeniorityScoringRules(seniorityScoringRules); // ************************************************************ // get all local areas // (using active equipment to minimize the results) // ************************************************************ List <HetLocalArea> localAreas = dbContext.HetEquipment.AsNoTracking() .Include(x => x.EquipmentStatusType) .Include(x => x.LocalArea) .Where(x => x.EquipmentStatusType.EquipmentStatusTypeCode == HetEquipment.StatusApproved && x.ArchiveCode == "N") .Select(x => x.LocalArea) .Distinct() .ToList(); // ************************************************************************ // iterate the data and update the assignment blocks // (seniority is already calculated) // ************************************************************************ Debug.WriteLine("Recalculating Equipment Block Assignment - Local Area Record Count: " + localAreas.Count); foreach (HetLocalArea localArea in localAreas) { IQueryable <HetDistrictEquipmentType> equipmentTypes = dbContext.HetEquipment.AsNoTracking() .Include(x => x.EquipmentStatusType) .Include(x => x.DistrictEquipmentType) .Where(x => x.EquipmentStatusType.EquipmentStatusTypeCode == HetEquipment.StatusApproved && x.ArchiveCode == "N" && x.LocalArea.LocalAreaId == localArea.LocalAreaId) .Select(x => x.DistrictEquipmentType) .Distinct(); foreach (HetDistrictEquipmentType districtEquipmentType in equipmentTypes) { // get the associated equipment type HetEquipmentType equipmentTypeRecord = dbContext.HetEquipmentType.AsNoTracking() .FirstOrDefault(x => x.EquipmentTypeId == districtEquipmentType.EquipmentTypeId); if (equipmentTypeRecord == null) { throw new DataException(string.Format("Invalid District Equipment Type. No associated Equipment Type record (District Equipment Id: {0})", districtEquipmentType.DistrictEquipmentTypeId)); } // get rules int blockSize = equipmentTypeRecord.IsDumpTruck ? scoringRules.GetBlockSize("DumpTruck") : scoringRules.GetBlockSize(); int totalBlocks = equipmentTypeRecord.IsDumpTruck ? scoringRules.GetTotalBlocks("DumpTruck") : scoringRules.GetTotalBlocks(); // assign blocks SeniorityListHelper.AssignBlocks(localArea.LocalAreaId, districtEquipmentType.DistrictEquipmentTypeId, blockSize, totalBlocks, dbContext, false); // save change to database if (ii++ % 100 == 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; } }