public void RunFix() { using (TransactionScope transaction = new TransactionScope()) { using (CmsEntities cee = new CmsEntities(120,mConnectCMS)) { var issues = (from x in cee.Issues.Include("IssueAssignedCategories") orderby x.Id select x).ToList(); foreach (var issue in issues) { Console.Out.WriteLine("Processing issue {0}", issue.Id); foreach (var issueAssignedCategory in issue.IssueAssignedCategories) { var closeoutExist = (from x in cee.IssueCloseouts where x.IssueId == issue.Id && x.IssueCategoryId == issueAssignedCategory.IssueCategoryId select x).FirstOrDefault(); if (closeoutExist == null) { //CloseOut Doesnt exist add one var newCloseout = new IssueCloseout { IssueId = issue.Id, IssueCategoryId = issueAssignedCategory.IssueCategoryId }; cee.IssueCloseouts.Add(newCloseout); Console.Out.WriteLine("Added new category '{0}' closeout to issue {1}", issueAssignedCategory.IssueCategoryId,issue.Id); } } } cee.SaveChanges(); transaction.Complete(); Console.Out.WriteLine("Transaction Complete"); Console.ReadLine(); } } }
private void UpdateCloseouts() { foreach (IssueAssignedCategory issueAssignedCategory in mIssue.IssueAssignedCategories) { IssueCloseout existingCategoryCloseout = (from x in mIssue.IssueCloseouts where x.IssueId == issueAssignedCategory.IssueId && x.IssueCategoryId == issueAssignedCategory.IssueCategoryId select x).FirstOrDefault(); if (existingCategoryCloseout == null) { //If this category doesn't exists int IssueCloseouts add it var issueCloseout = new IssueCloseout { IssueId = issueAssignedCategory.IssueId, IssueCategoryId = issueAssignedCategory.IssueCategoryId, //IssueCategory = issueAssignedCategory.IssueCategory, VerifierId = null }; mIssue.IssueCloseouts.Add(issueCloseout); } } //Remove not needed Closeouts (Approvals that are not verified and category was removed for them) var closeoutsToRemove = new List<IssueCloseout>(); foreach (IssueCloseout issueCloseout in mIssue.IssueCloseouts) { if (issueCloseout.Verified == false) { IssueAssignedCategory existInIssueAssignedCategories = (from x in mIssue.IssueAssignedCategories where x.IssueCategoryId == issueCloseout.IssueCategoryId select x).FirstOrDefault(); if (existInIssueAssignedCategories == null) { closeoutsToRemove.Add(issueCloseout); } } } closeoutsToRemove.ForEach(x => mIssue.IssueCloseouts.Remove(x)); }
private void UpdateData(IList<IssueDataAdapter> importData) { if (importData.Count == 0) { RaiseMessage(CommonUtils.MessageType.Warning, NoDataFoundForWorkSheetMessage()); return; } for (int i = 0; i < importData.Count; i++) { IssueDataAdapter adapter = importData[i]; IList<string> ids = (from x in importData select x.ID).ToList(); if (DuplicateItemNameFoundInExcell(adapter.ID, ids, adapter.RowNumber)) { continue; } //NUMBER int issueIdInteger; if (!int.TryParse(adapter.ID, out issueIdInteger)) { RaiseMessage(CommonUtils.MessageType.Error, string.Format("ID '{0}' is not in a valid numeric format for Issue ID Type '{1}' at Row {2}", adapter.ID, adapter.Name, i + 1)); continue; } Issue dbIssue = (from x in Cee.Issues where x.Id == issueIdInteger select x).FirstOrDefault(); if (dbIssue == null) { RaiseMessage(CommonUtils.MessageType.Error, BuildItemNameDoesNotExistInDbMessage(adapter.Name, adapter.RowNumber)); continue; } var issueTracking = (from x in Cee.IssueTrackings where x.Id == dbIssue.IssueTrackingId select x).FirstOrDefault(); if (issueTracking == null) { dbIssue.IssueTracking = new IssueTracking(); } //NAME if (!string.IsNullOrEmpty(adapter.Name)) { if (adapter.Name.Length > 500) { RaiseMessage(CommonUtils.MessageType.Error, BuildPropertyTooLong(adapter.Name, 500, adapter.RowNumber)); continue; } dbIssue.Name = adapter.Name.Trim(); } //DESCRIPTION if (!string.IsNullOrEmpty(adapter.Description)) { if (adapter.Description.Length > 4000) { RaiseMessage(CommonUtils.MessageType.Error, BuildPropertyTooLong(adapter.Description.Substring(0, 50) + "...", 4000, adapter.RowNumber)); continue; } dbIssue.Description = adapter.Description.Trim(); } //REASON if (!string.IsNullOrEmpty(adapter.Reason)) { if (adapter.Reason.Length > 4000) { RaiseMessage(CommonUtils.MessageType.Error, BuildPropertyTooLong(adapter.Reason.Substring(0, 50) + "...", 4000, adapter.RowNumber)); continue; } dbIssue.Reason = adapter.Reason.Trim(); } //SUGGESTED SOLUTION if (!string.IsNullOrEmpty(adapter.SuggestedSolution)) { if (adapter.SuggestedSolution.Length > 4000) { RaiseMessage(CommonUtils.MessageType.Error,BuildPropertyTooLong(adapter.SuggestedSolution.Substring(0, 50) + "...", 4000, adapter.RowNumber)); continue; } dbIssue.SuggestedSolution = adapter.SuggestedSolution.Trim(); } //REQUESTED BY if (!string.IsNullOrEmpty(adapter.RequestedBy)) { if (adapter.RequestedBy.Length > 50) { RaiseMessage(CommonUtils.MessageType.Error, BuildPropertyTooLong(adapter.RequestedBy, 50, adapter.RowNumber)); continue; } dbIssue.RequestedBy = adapter.RequestedBy.Trim(); } //ProjectSupervisor BY if (!string.IsNullOrEmpty(adapter.ProjectSupervisor)) { User user = GetUserFromText(adapter.ProjectSupervisor); if (user != null) { dbIssue.ProjectSupervisorId = user.Id; } else { RaiseMessage(CommonUtils.MessageType.Error, BuildItemNotFoundInDatabaseMessage("ProjectSupervisor", adapter.ProjectSupervisor, adapter.RowNumber)); continue; } } //INITIATED BY if (!string.IsNullOrEmpty(adapter.InitiatedBy)) { User user = GetUserFromText(adapter.InitiatedBy); if (user != null) { dbIssue.InitiatedById = user.Id; } else { RaiseMessage(CommonUtils.MessageType.Error,BuildItemNotFoundInDatabaseMessage("InitiatedBy", adapter.InitiatedBy, adapter.RowNumber)); continue; } } //ASSIGNED TO if (!string.IsNullOrEmpty(adapter.AssignedTo)) { User user = GetUserFromText(adapter.AssignedTo); if (user != null) { dbIssue.CurrentlyAssignedToId = user.Id; } else { RaiseMessage(CommonUtils.MessageType.Error, BuildItemNotFoundInDatabaseMessage("AssignedTo", adapter.AssignedTo, adapter.RowNumber)); continue; } } //INITIATIONDATE if (!string.IsNullOrEmpty(adapter.InitiationDate)) { DateTime dt; if (DateTime.TryParse(adapter.InitiationDate, out dt)) { if (dt.Date > DateTime.Now.Date) { RaiseMessage(CommonUtils.MessageType.Error, BuildDateCannotBeInFutureInExcelMessage("InitiatedDate", adapter.InitiationDate, adapter.RowNumber)); continue; } } else { RaiseMessage(CommonUtils.MessageType.Error, BuildItemNotInValidFormatInExcelMessage("InitiatedDate", adapter.InitiationDate, adapter.RowNumber)); continue; } dbIssue.InitiatedDate = dt; } //HAZARD if (!string.IsNullOrEmpty(adapter.Hazards)) { if (adapter.Hazards.Length > 4000) { RaiseMessage(CommonUtils.MessageType.Error, BuildPropertyTooLong(adapter.Hazards.Substring(0, 50) + "...", 4000, adapter.RowNumber)); continue; } dbIssue.Hazard = adapter.Hazards.Trim(); } //CONTROL METHOD if (!string.IsNullOrEmpty(adapter.ControlMethod)) { if (adapter.ControlMethod.Length > 4000) { RaiseMessage(CommonUtils.MessageType.Error, BuildPropertyTooLong(adapter.ControlMethod.Substring(0, 50) + "...", 4000, adapter.RowNumber)); continue; } dbIssue.ControlMethod = adapter.ControlMethod.Trim(); } //TRAINING DETAILS if (!string.IsNullOrEmpty(adapter.TrainingDetails)) { if (adapter.TrainingDetails.Length > 4000) { RaiseMessage(CommonUtils.MessageType.Error,BuildPropertyTooLong(adapter.TrainingDetails.Substring(0, 50) + "...", 4000, adapter.RowNumber)); continue; } dbIssue.TrainingDetails = adapter.TrainingDetails.Trim(); } //TESTING DETAILS if (!string.IsNullOrEmpty(adapter.TestingDetails)) { if (adapter.TestingDetails.Length > 4000) { RaiseMessage(CommonUtils.MessageType.Error, BuildPropertyTooLong(adapter.TestingDetails.Substring(0, 50) + "...", 4000, adapter.RowNumber)); continue; } dbIssue.TestingDetails = adapter.TestingDetails.Trim(); } //PRIORITY if (!string.IsNullOrEmpty(adapter.Priority)) { IssuePriority priorityMatch = (from x in mExistingPriorities where String.Compare(x.Name, adapter.Priority, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (priorityMatch != null) { dbIssue.IssuePriorityId = priorityMatch.Id; } else { RaiseMessage(CommonUtils.MessageType.Error, BuildItemNotFoundInDatabaseMessage("Priority", adapter.Priority, adapter.RowNumber)); continue; } } //STATUS if (!string.IsNullOrEmpty(adapter.Status)) { IssueStatus statusMatch = (from x in mExistingStatuses where String.Compare(x.Name, adapter.Status, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (statusMatch != null) { dbIssue.IssueStatusId = statusMatch.Id; } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotFoundInDatabaseMessage("Status", adapter.Status, adapter.RowNumber))); continue; } } //AREAS if (!string.IsNullOrEmpty(adapter.AreaNumbers)) { string[] areas = adapter.AreaNumbers.Split(new[] { ',' }); foreach (string number in areas) { Area areaMatch = (from x in Cee.Areas where x.AreaNumber.ToString() == number.Trim() select x).FirstOrDefault(); if (areaMatch != null) { dbIssue.IssueAreas.Add(new IssueArea { Issue = dbIssue, AreaId = areaMatch.Id }); } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotFoundInDatabaseMessage("Areas", adapter.AreaNumbers, adapter.RowNumber))); break; } } } //CLASSIFICATION if (!string.IsNullOrEmpty(adapter.Classification)) { IssueClassification classificationMatch = (from x in mExistingClassifications where string.Compare(x.Name, adapter.Classification, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (classificationMatch != null) { dbIssue.IssueClassificationId = classificationMatch.Id; } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotFoundInDatabaseMessage("Classification", adapter.Classification, adapter.RowNumber))); continue; } } //CATEGORY if (!string.IsNullOrEmpty(adapter.Categories)) { string[] categories = adapter.Categories.Split(new[] { ',' }); foreach (string adapterCategoryName in categories) { IssueCategory issueCategoryMatch = (from x in mExistingCategories where String.Compare(x.Name, adapterCategoryName, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (issueCategoryMatch != null) { var issueAssignedCategoryMatch = mExistingIssueAssignedCategories.FirstOrDefault(x => x.IssueId == dbIssue.Id && x.IssueCategoryId == issueCategoryMatch.Id); if (issueAssignedCategoryMatch == null) { var newIssueAssignedCategory = new IssueAssignedCategory {Issue = dbIssue, IssueCategoryId = issueCategoryMatch.Id}; dbIssue.IssueAssignedCategories.Add(newIssueAssignedCategory); mExistingIssueAssignedCategories.Add(newIssueAssignedCategory); } var issueApprovalMatch = mExistingIssueApprovals.FirstOrDefault(x => x.IssueId == dbIssue.Id && x.IssueCategoryId == issueCategoryMatch.Id); if (issueApprovalMatch == null) { var newApproval = new IssueApproval {Issue = dbIssue, IssueCategoryId = issueCategoryMatch.Id}; dbIssue.IssueApprovals.Add(newApproval); mExistingIssueApprovals.Add(newApproval); } var issueCloseoutMatch = mExistingIssueCloseouts.FirstOrDefault(x => x.IssueId == dbIssue.Id && x.IssueCategoryId == issueCategoryMatch.Id); if (issueCloseoutMatch == null) { var newCloseout = new IssueCloseout { Issue = dbIssue, IssueCategoryId = issueCategoryMatch.Id }; dbIssue.IssueCloseouts.Add(newCloseout); mExistingIssueCloseouts.Add(newCloseout); } } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotFoundInDatabaseMessage("Category", adapterCategoryName, adapter.RowNumber))); break; } } } //FUNDING if (!string.IsNullOrEmpty(adapter.Funding)) { if (!adapter.Funding.Equals("OPEX", StringComparison.CurrentCultureIgnoreCase) && !adapter.Funding.Equals("CAPEX", StringComparison.CurrentCultureIgnoreCase)) { RaiseMessage(CommonUtils.MessageType.Error, string.Format("Funding Type must be either 'OPEX' or 'CAPEX' - row number {0}.", adapter.RowNumber)); continue; } dbIssue.IssueTracking.FundingType = adapter.Funding.ToUpper(); } //BUDGETED if (!string.IsNullOrEmpty(adapter.Budgeted)) { bool result; if (Boolean.TryParse(adapter.Budgeted, out result)) { dbIssue.IssueTracking.Budgeted = result; } else { if (String.Compare(adapter.Budgeted, "Yes", true, CultureInfo.CurrentCulture) == 0) { dbIssue.IssueTracking.Budgeted = true; } else { dbIssue.IssueTracking.Budgeted = false; } } } //ESTIMATEDCOST if (!string.IsNullOrEmpty(adapter.EstimatedCost)) { double dr; if (Double.TryParse(adapter.EstimatedCost, out dr)) { dbIssue.IssueTracking.EstimatedCost = dr; } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotInValidFormatInExcelMessage("EstimatedCost", adapter.EstimatedCost, adapter.RowNumber))); continue; } } //EFFORT if (!string.IsNullOrEmpty(adapter.Effort)) { int effort; if (Int32.TryParse(adapter.Effort, out effort)) { dbIssue.IssueTracking.Effort = effort; } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotInValidFormatInExcelMessage("Effort", adapter.Effort, adapter.RowNumber))); continue; } } //REWARD if (!string.IsNullOrEmpty(adapter.Reward)) { int reward; if (Int32.TryParse(adapter.Reward, out reward)) { dbIssue.IssueTracking.Return = reward; } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotInValidFormatInExcelMessage("Reward", adapter.Reward, adapter.RowNumber))); continue; } } //KEYSTAKEHOLDER if (!string.IsNullOrEmpty(adapter.KeyStakeholder)) { if (adapter.KeyStakeholder.ToUpper() == "NULL") { dbIssue.KeyStakeholder = ""; } else { dbIssue.KeyStakeholder = adapter.KeyStakeholder; } } //ISSUETYPE if (!string.IsNullOrEmpty(adapter.IssueType)) { IssueType issueType = (from x in Cee.IssueTypes where String.Compare(x.Name, adapter.IssueType, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (issueType != null) { dbIssue.IssueTypeId = issueType.Id; } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotFoundInDatabaseMessage("IssueType", adapter.IssueType, adapter.RowNumber))); continue; } } //SUB TYPE if (!string.IsNullOrEmpty(adapter.IssueSubType)) { IssueSubType issueSubType = (from x in mExistingIssueSubTypes where x.Name.Equals(adapter.IssueSubType, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); if (issueSubType != null) { dbIssue.IssueSubTypeId = issueSubType.Id; } else { RaiseMessage(CommonUtils.MessageType.Error, String.Format(BuildItemNotFoundInDatabaseMessage("IssueSubType", adapter.IssueType, adapter.RowNumber))); continue; } } mSavedResults.Add(dbIssue); } Cee.SaveChanges(); }