public static CorrectiveAction MapToUIModelOnCreate(CorrectiveActionFormViewModel vm, string[] selectedErrorTypes) { var logSnippet = "[CorrectiveActionUtil][MapToUIModelOnCreate] => "; CorrectiveAction newCorrectiveAction = new CorrectiveAction { Id = vm.CorrectiveActionId, ActionRequestTypeId = Int32.Parse(vm.ActionRequestTypeId), EmplId = CorrectiveActionUtil.ExtractEmplId(vm.EmployeeSearchResult), Employee = null, // CorrectiveActionService will hydrate this object NOACode = vm.NatureOfAction, NatureOfAction = new ReferenceService().RetrieveNatureOfAction(vm.NatureOfAction), EffectiveDateOfPar = (DateTime)vm.EffectiveDateOfPar, IsPaymentMismatch = vm.IsPaymentMismatch, Details = vm.Details, ActionId = Int32.Parse(vm.StatusTypeId) }; Console.WriteLine(logSnippet + $"(vm.CorrectiveActionId): '{vm.CorrectiveActionId}' / (vm.NatureOfAction): '{vm.NatureOfAction}'"); Console.WriteLine(logSnippet + "(newCorrectiveAction.NatureOfAction == null): " + (newCorrectiveAction.NatureOfAction == null)); if (newCorrectiveAction.NatureOfAction != null) { Console.WriteLine(logSnippet + $"(newCorrectiveAction.NatureOfAction.NoaCode): '{newCorrectiveAction.NatureOfAction.NoaCode}' / (newCorrectiveAction.NatureOfAction.RoutesToBr): '{newCorrectiveAction.NatureOfAction.RoutesToBr}'"); } ErrorTypeDictionary errorTypeDict = new ErrorTypeDictionary(); foreach (var errorTypeId in selectedErrorTypes) { newCorrectiveAction.ErrorTypes.Add(errorTypeDict.GetErrorType(Int32.Parse(errorTypeId))); } return(newCorrectiveAction); }
public static CorrectiveActionFormViewModel MapToViewModel(CorrectiveAction entity, int userId, string useCase, List <ModuleMenuItem> moduleMenuItems, bool userCanAssign = false) { string logSnippet = "[CorrectiveActionUtil][MapToViewModel] => "; Console.WriteLine(logSnippet + $"(userId).................................: '{userId}'"); Console.WriteLine(logSnippet + $"(useCase)................................: '{useCase}'"); Console.WriteLine(logSnippet + $"(entity.IsReadOnly)......................: {entity.IsReadOnly}"); Console.WriteLine(logSnippet + $"(userCanAssign)..........................: {userCanAssign}"); Console.WriteLine(logSnippet + $"(CorrectiveAction.CreatedByUser == null).: {entity.CreatedByUser == null}"); Console.WriteLine(logSnippet + $"(CorrectiveAction.CreatedByOrg == null)..: {entity.CreatedByOrg == null}"); Console.WriteLine(logSnippet + $"(CorrectiveAction.AssignedToUser == null): {entity.AssignedToUser == null}"); Console.WriteLine(logSnippet + $"(CorrectiveAction.AssignedToOrg == null).: {entity.AssignedToOrg == null}"); CorrectiveActionFormViewModel viewModel = new CorrectiveActionFormViewModel { UserId = userId, CorrectiveActionId = entity.Id, CorrectiveActionIdForAddComment = entity.Id, EmployeeSearchResult = entity.Employee.SearchResultValue, NatureOfAction = entity.NOACode, EffectiveDateOfPar = entity.EffectiveDateOfPar, IsPaymentMismatch = entity.IsPaymentMismatch, ActionRequestTypeId = entity.ActionRequestTypeId.ToString(), Details = entity.Details, CanAssign = userCanAssign && entity.IsAssignable, Comments = entity.Comments, Histories = entity.Histories, StatusLabel = entity.Status.StatusLabel, DateSubmitted = entity.CreatedAt.ToString("MMMM dd, yyyy"), CreatedByUserName = entity.CreatedByUser.DisplayName, CreatedByOrgLabel = entity.CreatedByOrg.OrgLabel, AssignedToUserName = (entity.AssignedToUser == null) ? "Unassigned" : entity.AssignedToUser.DisplayName, AssignedToOrgLabel = (entity.AssignedToOrg == null) ? "Unassigned" : entity.AssignedToOrg.OrgLabel, RowVersion = entity.RowVersion, PersonnelOfficeIDDesc = entity.Employee.PersonnelOfficeIdentifierDescription, IsReadOnly = entity.IsReadOnly, UseCase = useCase, Controller = MenuUtil.FindControllerForUseCase(useCase, moduleMenuItems) }; if (String.IsNullOrEmpty(viewModel.Details) || String.IsNullOrWhiteSpace(viewModel.Details)) { viewModel.Details = "None provided"; } if (entity.StatusId != null && entity.StatusId.HasValue) { viewModel.CurrentStatusId = (int)entity.StatusId; } Console.WriteLine(logSnippet + "BEGIN: CorrectiveActionFormViewModel.ToString()"); Console.WriteLine(viewModel); Console.WriteLine(logSnippet + "END: CorrectiveActionFormViewModel.ToString()"); return(viewModel); }
public static CorrectiveAction MapToUIModelOnUpdate(CorrectiveActionFormViewModel vm, string[] selectedErrorTypes, CorrectiveAction existingCA) { var logSnippet = "[CorrectiveActionUtil][MapToUIModelOnUpdate] => "; Console.WriteLine(logSnippet + $"(vm.Details): '{vm.Details}'"); existingCA.EmplId = CorrectiveActionUtil.ExtractEmplId(vm.EmployeeSearchResult); existingCA.Employee = null; // CorrectiveActionService will hydrate this object existingCA.NOACode = vm.NatureOfAction; existingCA.NatureOfAction = new ReferenceService().RetrieveNatureOfAction(vm.NatureOfAction); existingCA.EffectiveDateOfPar = (DateTime)vm.EffectiveDateOfPar; existingCA.IsPaymentMismatch = vm.IsPaymentMismatch; existingCA.ActionId = Int32.Parse(vm.StatusTypeId); existingCA.RowVersion = vm.RowVersion; existingCA.ActionRequestTypeId = int.Parse(vm.ActionRequestTypeId); existingCA.Details = vm.Details; if (vm.AssignedToUserId.HasValue) { existingCA.AssignedToUserId = vm.AssignedToUserId; } Console.WriteLine(logSnippet + $"(existingCA.Id): '{existingCA.Id}' / (existingCA.NatureOfAction): '{existingCA.NatureOfAction}'"); Console.WriteLine(logSnippet + "(existingCA.NatureOfAction == null): " + (existingCA.NatureOfAction == null)); if (existingCA.NatureOfAction != null) { Console.WriteLine(logSnippet + $"(existingCA.NatureOfAction.NoaCode): '{existingCA.NatureOfAction.NoaCode}' / (existingCA.NatureOfAction.RoutesToBr): '{existingCA.NatureOfAction.RoutesToBr}'"); } ErrorTypeDictionary errorTypeDict = new ErrorTypeDictionary(); existingCA.ErrorTypes = new List <ErrorType>(); foreach (var errorTypeId in selectedErrorTypes) { existingCA.ErrorTypes.Add(errorTypeDict.GetErrorType(Int32.Parse(errorTypeId))); } return(existingCA); }
public IActionResult Comment ( [Bind("CorrectiveActionIdForAddComment,Comment")] CorrectiveActionFormViewModel correctiveActionVM ) { if (HttpContext == null || HttpContext.Session == null || HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY) == null) { return(RedirectToAction("Warning", "Home")); } string logSnippet = new StringBuilder("[") .Append(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")) .Append("][CorrectiveActionsController][HttpPost][Comment] => ") .ToString(); UserViewModel qmsUserVM = HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY); Console.WriteLine(logSnippet + $"(qmsUserVM): {qmsUserVM}"); if (qmsUserVM.CanCommentOnTask == false) { return(RedirectToAction("UnauthorizedAccess", "Home")); } Console.WriteLine(logSnippet + $"(CorrectiveAction.Id): '{correctiveActionVM.CorrectiveActionIdForAddComment}'"); _correctiveActionService.SaveComment(correctiveActionVM.Comment, correctiveActionVM.CorrectiveActionIdForAddComment, qmsUserVM.UserId); HttpContext.Session.SetObject(CorrectiveActionsConstants.NEWLY_CREATED_COMMENT_KEY, true); Console.WriteLine(logSnippet + "Redirecting to [CorrectiveActionsController][HttpGet][Edit]"); return(RedirectToAction(nameof(Edit), new{ @id = correctiveActionVM.CorrectiveActionIdForAddComment })); }
// [Authorize(Roles = "SC_SPECIALIST,SC_REVIEWER,PPRB_SPECIALIST,PPRB_REVIEWER,PPRM_SPECIALIST,PPRM_REVIEWER")] public IActionResult Edit ( [Bind("EmployeeSearchResult, NatureOfAction,EffectiveDateOfPar,IsPaymentMismatch,ErrorTypeIds,ActionRequestTypeId,StatusTypeId,CurrentStatusId,CorrectiveActionId,AssignedToUserId,RowVersion,Details")] CorrectiveActionFormViewModel correctiveActionVM, string[] selectedErrorTypes ) { if (HttpContext == null || HttpContext.Session == null || HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY) == null) { return(RedirectToAction("Warning", "Home")); } string logSnippet = new StringBuilder("[") .Append(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")) .Append("][CorrectiveActionsController][HttpPost][Edit] => ") .ToString(); UserViewModel qmsUserVM = HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY); Console.WriteLine(logSnippet + $"(qmsUserVM): {qmsUserVM}"); if (qmsUserVM.CanEditCorrectiveAction == false) { return(RedirectToAction("UnauthorizedAccess", "Home")); } Console.WriteLine(logSnippet + $"(correctiveActionVM.AssignedToUserId): '{correctiveActionVM.AssignedToUserId}'"); Console.WriteLine(logSnippet + $"(correctiveActionVM.CurrentStatusId).: '{correctiveActionVM.CurrentStatusId}'"); /////////////////////////////////////////////////////////////////////////////////// // Error Categories Validation /////////////////////////////////////////////////////////////////////////////////// if (selectedErrorTypes == null || selectedErrorTypes.Count() == 0) { Console.WriteLine(logSnippet + "No error categories/types selected. Adding a model error."); ModelState.AddModelError(string.Empty, "Please select at least one error category."); } /////////////////////////////////////////////////////////////////////////////////// // Employee Search /////////////////////////////////////////////////////////////////////////////////// string emplIdStr = CorrectiveActionUtil.ExtractEmplId(correctiveActionVM.EmployeeSearchResult); long emplIdLong; if (String.IsNullOrEmpty(emplIdStr) == false && long.TryParse(emplIdStr, out emplIdLong) == true) { try { Employee employee = _employeeService.RetrieveById(emplIdStr); } catch (EmployeeNotFoundException enfe) { Console.WriteLine(logSnippet + "EmployeeNotFoundException encountered:"); Console.WriteLine(enfe.Message); ModelState.AddModelError(string.Empty, $"Employee not found with Employee ID of '{emplIdStr}'"); } } User qmsUser = _userService.RetrieveByEmailAddress(qmsUserVM.EmailAddress); CorrectiveAction oldCorrectiveAction = _correctiveActionService.RetrieveById(correctiveActionVM.CorrectiveActionId, qmsUser); if (correctiveActionVM.CorrectiveActionId != oldCorrectiveAction.Id) { StringBuilder sb = new StringBuilder(logSnippet); sb.Append("Corrective Action ID discrepancy detected: "); sb.Append("UIModel ID: '"); sb.Append(oldCorrectiveAction.Id); sb.Append("'; ViewModel ID: '"); sb.Append(correctiveActionVM.CorrectiveActionId); sb.Append("'."); Console.WriteLine(sb.ToString()); } if (correctiveActionVM.RowVersion != oldCorrectiveAction.RowVersion) { StringBuilder sb = new StringBuilder(logSnippet); sb.Append("Row version discrepancy detected for Corrective Action (ID: '"); sb.Append(oldCorrectiveAction.Id); sb.Append("'). UIModel Row Version: '"); sb.Append(oldCorrectiveAction.RowVersion); sb.Append("'; ViewModel Row Version: '"); sb.Append(correctiveActionVM.RowVersion); sb.Append("'."); Console.WriteLine(sb.ToString()); } Console.WriteLine(logSnippet + "(ModelState.IsValid): " + (ModelState.IsValid)); if (ModelState.IsValid) { CorrectiveAction updatedCorrectiveAction = CorrectiveActionUtil.MapToUIModelOnUpdate(correctiveActionVM, selectedErrorTypes, oldCorrectiveAction); Console.WriteLine(logSnippet + "Updated CorrectiveAction (BEFORE SAVE) => BEGIN"); Console.WriteLine(updatedCorrectiveAction.ToJson()); Console.WriteLine("END <= Updated CorrectiveAction (BEFORE SAVE)"); Console.WriteLine(logSnippet + $"(updatedCorrectiveAction.AssignedToUserId)(BEFORE SAVE): '{updatedCorrectiveAction.AssignedToUserId}'"); int entityId = 0; try { entityId = _correctiveActionService.Save(updatedCorrectiveAction, qmsUser); } catch (LockingException lockExc) { Console.WriteLine(logSnippet + "(BEGIN CorrectiveActionLockingException:"); Console.WriteLine(lockExc.Message); Console.WriteLine(logSnippet + "(:END CorrectiveActionLockingException"); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // WRITE SUCCESS MESSAGE TO LOG /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Console.WriteLine(logSnippet + "Record saved to DB, redirecting in CorrectiveAction Index page"); Console.WriteLine(logSnippet + $"Updated CorrectiveAction #{updatedCorrectiveAction.Id} has been saved to the database, redirecting in Home Index page"); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // PREPARE SUCCESS MESSAGE FOR USER /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// string msgKey = Guid.NewGuid().ToString(); string msgVal = $"Corrective Action with an ID of {entityId} has been updated for {correctiveActionVM.EmployeeSearchResult}"; HttpContext.Session.SetObject(msgKey, msgVal); return(RedirectToAction("Index", "Home", new { @mk = msgKey })); } else { Console.WriteLine(logSnippet + "ModelState IS NOT VALID, returning User to Edit Corrective Actions page"); if (selectedErrorTypes != null || selectedErrorTypes.Count() > 0) { ///////////////////////////////////////////////////////////////////////////////////////////// // The HttpContext.Items collection is used to store data while processing a single request. // The collection's contents are discarded after a request is processed. ///////////////////////////////////////////////////////////////////////////////////////////// HttpContext.Items[CorrectiveActionsConstants.SELECTED_ERROR_TYPES_KEY] = selectedErrorTypes; HttpContext.Items[CorrectiveActionsConstants.CURRENT_STATUS_ID_KEY] = correctiveActionVM.CurrentStatusId; } if (qmsUserVM.CanAssignTasks && oldCorrectiveAction.IsAssignable) { HttpContext.Items[CorrectiveActionsConstants.IS_ASSIGNABLE_KEY] = true; if (oldCorrectiveAction.AssignedByUserId.HasValue) { HttpContext.Items[CorrectiveActionsConstants.CURRENT_ASSIGNED_TO_USER_ID_KEY] = oldCorrectiveAction.AssignedToUserId.ToString(); } else { HttpContext.Items[CorrectiveActionsConstants.CURRENT_ASSIGNED_TO_USER_ID_KEY] = null; } } correctiveActionVM.CorrectiveActionId = oldCorrectiveAction.Id; correctiveActionVM.CorrectiveActionIdForAddComment = oldCorrectiveAction.Id; correctiveActionVM.Details = oldCorrectiveAction.Details; correctiveActionVM.Comments = oldCorrectiveAction.Comments; correctiveActionVM.Histories = oldCorrectiveAction.Histories; correctiveActionVM.StatusLabel = oldCorrectiveAction.Status.StatusLabel; correctiveActionVM.DateSubmitted = oldCorrectiveAction.CreatedAt.ToString("MMMM dd, yyyy"); correctiveActionVM.CreatedByUserName = oldCorrectiveAction.CreatedByUser.DisplayName; correctiveActionVM.CreatedByOrgLabel = oldCorrectiveAction.CreatedByOrg.OrgLabel; correctiveActionVM.IsReadOnly = oldCorrectiveAction.IsReadOnly; return(View(correctiveActionVM)); } }
// [Authorize(Roles = "SC_SPECIALIST,SC_REVIEWER,PPRB_SPECIALIST,PPRB_REVIEWER")] public IActionResult Create ( [Bind("EmployeeSearchResult, NatureOfAction,EffectiveDateOfPar,IsPaymentMismatch,ErrorTypeIds,ActionRequestTypeId,StatusTypeId,Details")] CorrectiveActionFormViewModel correctiveActionVM, string[] selectedErrorTypes ) { if (HttpContext == null || HttpContext.Session == null || HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY) == null) { return(RedirectToAction("Warning", "Home")); } string logSnippet = new StringBuilder("[") .Append(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")) .Append("][CorrectiveActionsController][HttpPost][Create] => ") .ToString(); UserViewModel qmsUserVM = HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY); Console.WriteLine(logSnippet + $"(qmsUserVM): {qmsUserVM}"); if (qmsUserVM.CanCreateCorrectiveAction == false) { return(RedirectToAction("UnauthorizedAccess", "Home")); } /////////////////////////////////////////////////////////////////////////////////// // Error Categories Validation /////////////////////////////////////////////////////////////////////////////////// if (selectedErrorTypes == null || selectedErrorTypes.Count() == 0) { Console.WriteLine(logSnippet + "No error categories/types selected. Adding a model error."); ModelState.AddModelError(string.Empty, "Please select at least one error category."); } /////////////////////////////////////////////////////////////////////////////////// // Employee Search /////////////////////////////////////////////////////////////////////////////////// string emplIdStr = CorrectiveActionUtil.ExtractEmplId(correctiveActionVM.EmployeeSearchResult); long emplIdLong; if (String.IsNullOrEmpty(emplIdStr) == false && long.TryParse(emplIdStr, out emplIdLong) == true) { try { Employee employee = _employeeService.RetrieveById(emplIdStr); } catch (EmployeeNotFoundException enfe) { Console.WriteLine(logSnippet + "EmployeeNotFoundException encountered:"); Console.WriteLine(enfe.Message); ModelState.AddModelError(string.Empty, $"Employee not found with Employee ID of '{emplIdStr}'"); } } Console.WriteLine(logSnippet + $"(ModelState.IsValid): {ModelState.IsValid}"); if (ModelState.IsValid) { CorrectiveAction correctiveActionModel = CorrectiveActionUtil.MapToUIModelOnCreate(correctiveActionVM, selectedErrorTypes); User qmsUser = _userService.RetrieveByEmailAddress(qmsUserVM.EmailAddress); Console.WriteLine(logSnippet + "CorrectiveAction (BEFORE SAVE) => BEGIN"); Console.WriteLine(correctiveActionModel.ToJson()); Console.WriteLine(logSnippet + "END <= CorrectiveAction (BEFORE SAVE)"); int entityId = _correctiveActionService.Save(correctiveActionModel, qmsUser); Console.WriteLine(logSnippet + $"Newly created CorrectiveAction #{entityId} has been saved to the database, redirecting in Home Index page"); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // PREPARE SUCCESS MESSAGE /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// string msgKey = Guid.NewGuid().ToString(); string msgVal = $"Corrective Action with an ID of {entityId} has been created for {correctiveActionVM.EmployeeSearchResult}"; HttpContext.Session.SetObject(msgKey, msgVal); return(RedirectToAction("Index", "Home", new { @mk = msgKey })); } else { Console.WriteLine(logSnippet + "ModelState IS NOT VALID, returning User to Create Corrective Actions page"); if (selectedErrorTypes != null || selectedErrorTypes.Count() > 0) { ///////////////////////////////////////////////////////////////////////////////////////////// // The HttpContext.Items collection is used to store data while processing a single request. // The collection's contents are discarded after a request is processed. ///////////////////////////////////////////////////////////////////////////////////////////// HttpContext.Items[CorrectiveActionsConstants.SELECTED_ERROR_TYPES_KEY] = selectedErrorTypes; } return(View()); } }