/// <summary> /// Begin a new Service Request /// </summary> /// <param name="id">selected option Id</param> /// <param name="serviceRequestAction"></param> /// <returns></returns> public ActionResult Begin(int id, ServiceRequestAction serviceRequestAction = ServiceRequestAction.New) { ServiceRequestModel model = new ServiceRequestModel { ServiceRequest = new ServiceRequestDto { ServiceOptionId = id }, SelectedAction = serviceRequestAction }; //start new SR model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, id, ServiceRequestAction.New); model.ChangePackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, id, ServiceRequestAction.Change); model.RemovePackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, id, ServiceRequestAction.Remove); //default if no package found if (model.NewPackage == null && model.ChangePackage == null && model.RemovePackage == null) { model.SelectedAction = ServiceRequestAction.New; model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, id); } //add only package found else if (model.NewPackage != null && model.ChangePackage == null && model.RemovePackage == null) { model.SelectedAction = ServiceRequestAction.New; } //change only package found else if (model.NewPackage == null && model.ChangePackage != null && model.RemovePackage == null) { model.SelectedAction = ServiceRequestAction.Change; } //remove package else if (model.NewPackage == null && model.ChangePackage == null && model.RemovePackage != null) { model.SelectedAction = ServiceRequestAction.Remove; } model.CurrentIndex = -1; /* index for info tab */ return(View("ServiceRequest", model)); }
/// <summary> /// View a tab in the SR form /// </summary> /// <param name="id">service request id</param> /// <param name="index">package index</param> /// <param name="mode">display mode</param> /// <returns></returns> public ActionResult Form(int id, int index, ServiceRequestMode mode = ServiceRequestMode.Selection) { ServiceRequestModel model = new ServiceRequestModel { CurrentIndex = index, ServiceRequestId = id, Mode = mode }; /* STEP ONE - get SR, get package */ try { model.ServiceRequest = _serviceRequestController.GetServiceRequest(UserId, id); //get db info model.SelectedAction = model.ServiceRequest.Action; if (!_requestManager.UserCanEditRequest(UserId, model.ServiceRequest.Id)) //business logic { throw new Exception("Service Request cannot be edited"); } //add package if (model.ServiceRequest.Action == ServiceRequestAction.New) { model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId, ServiceRequestAction.New); } //change package else if (model.SelectedAction == ServiceRequestAction.Change) { model.ChangePackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId, ServiceRequestAction.Change); } //remove package else if (model.SelectedAction == ServiceRequestAction.Remove) { model.RemovePackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId, ServiceRequestAction.Remove); } if (model.InUsePackage == null) { model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId); } //deal with no package by making one } catch (Exception exception) { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = $"Failed to retrieve Service Request, error: {exception.Message}"; return(View("ServiceRequest", model)); } /* STEP TWO - get any user inputs & associate with the option */ List <ServiceOptionTag> optionInputList = new List <ServiceOptionTag>(); if (index < 0) /*not much to do here, eh... */ } {
public ActionResult SaveFormSelection(ServiceRequestFormReturnModel form, int submit) { /* STEP ONE - Get the Service Package and SR */ ServiceRequestModel model = new ServiceRequestModel //used to hold all the data until redirecting { CurrentIndex = submit, ServiceRequestId = form.Id, Mode = ServiceRequestMode.Selection }; try { model.ServiceRequest = _serviceRequestController.GetServiceRequest(UserId, form.Id); //SR model.SelectedAction = model.ServiceRequest.Action; if (model.SelectedAction == ServiceRequestAction.New) //Package { model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId, ServiceRequestAction.New); //package model.SelectedAction = ServiceRequestAction.New; //new package if (model.NewPackage == null) { model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId); model.SelectedAction = ServiceRequestAction.New; } } else if (model.SelectedAction == ServiceRequestAction.Change) { model.ChangePackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId, ServiceRequestAction.Change); } else if (model.ServiceRequest.Action == ServiceRequestAction.Remove) { model.RemovePackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId, ServiceRequestAction.Remove); model.SelectedAction = ServiceRequestAction.Remove; //remove package if (model.NewPackage == null && model.RemovePackage == null) { model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId); } } } catch (Exception exception) { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = $"Failed to retrieve service request information, error: {exception.Message}"; model.CurrentIndex = -1; //either the SR does not exist or the option does not exist return(View("ServiceRequest", model)); } /* STEP TWO - figure out what category or service to work with */ //tag identified IServicePackageTag currentTag = null; if (model.ServiceRequest.Action == ServiceRequestAction.New) { if (model.NewPackage != null) { currentTag = model.GetPackageTags(ServiceRequestAction.New).ToArray()[form.CurrentIndex]; } } else if (model.ServiceRequest.Action == ServiceRequestAction.Change) { if (model.ChangePackage != null) { currentTag = model.GetPackageTags(ServiceRequestAction.Change).ToArray()[form.CurrentIndex]; } } else if (model.ServiceRequest.Action == ServiceRequestAction.Remove) { if (model.RemovePackage != null) { currentTag = model.GetPackageTags(ServiceRequestAction.Remove).ToArray()[form.CurrentIndex]; } } /* STEP THREE - add/remove options in the SR */ { ICollection <IServiceRequestOptionDto> formOptions; if (form.Options == null) { formOptions = new List <IServiceRequestOptionDto>(); } //need to avoid a null pointer exception here else { formOptions = form.GetServiceRequestOptions().ToList(); //get metadata for form options foreach (var option in formOptions) { option.ServiceOptionName = _portfolioService.GetServiceOption(UserId, option.ServiceOptionId).Name; option.Code = _portfolioService.GetServiceOptionCategory(UserId, _portfolioService.GetServiceOption(UserId, option.ServiceOptionId).ServiceOptionCategoryId).Code; } } if (model.ServiceRequest.ServiceRequestOptions == null) { model.ServiceRequest.ServiceRequestOptions = new List <IServiceRequestOptionDto>(); } List <IServiceOptionCategoryDto> currentCategories = new List <IServiceOptionCategoryDto>(); if (currentTag is IServiceOptionCategoryTagDto) //deal with a category { currentCategories.Add(_portfolioService.GetServiceOptionCategory(UserId, ((IServiceOptionCategoryTagDto)model.GetPackageTags(model.SelectedAction).ToArray()[form.CurrentIndex]).ServiceOptionCategoryId)); } else if (currentTag is IServiceTagDto) { currentCategories.AddRange(_portfolioService.GetService(((IServiceTagDto)model.GetPackageTags(model.SelectedAction).ToArray()[form.CurrentIndex]).ServiceId).ServiceOptionCategories); } try { foreach (var category in currentCategories) { foreach (var option in category.ServiceOptions) /* sighhhhh */ { var formDto = (from o in formOptions where o.ServiceOptionId == option.Id select o).FirstOrDefault(); var srDto = (from o in model.ServiceRequest.ServiceRequestOptions where o.ServiceOptionId == option.Id select o) .FirstOrDefault(); if (formDto != null && srDto == null) //add condition { _serviceRequestOptionController.ModifyServiceRequestOption(UserId, (from o in formOptions where o.ServiceOptionId == option.Id select o).First(), EntityModification.Create); } else if (formDto == null && srDto != null) //remove condition { _serviceRequestOptionController.ModifyServiceRequestOption(UserId, (from o in model.ServiceRequest.ServiceRequestOptions where o.ServiceOptionId == option.Id select o).First(), EntityModification.Delete); } else if (formDto != null /* && srDto != null */) //update condition { _serviceRequestOptionController.ModifyServiceRequestOption(UserId, srDto, EntityModification.Delete); _serviceRequestOptionController.ModifyServiceRequestOption(UserId, formDto, EntityModification.Create); } /* done \*/ } } model.ServiceRequest = _serviceRequestController.GetServiceRequest(UserId, form.Id); // refresh the data in the model now } catch (Exception exception) //what, a problem? { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = $"Failed to retrieve service request information, error: {exception.Message}"; model.CurrentIndex = -1; //either the SR does not exist or the option does not exist return(View("ServiceRequest", model)); } } /* STEP FOUR - add/remove user input data */ //update and add if (form.UserInput != null) { List <ServiceRequestUserInputDto> userDataList = (from u in form.UserInput where u.Value != null select new ServiceRequestUserInputDto { Id = u.Id, Name = u.Name, UserInputType = u.UserInputType, ServiceRequestId = form.Id, InputId = u.InputId, Value = u.Value }).ToList(); foreach (var userData in userDataList) { try { _serviceRequestUserInputController.ModifyServiceRequestUserInput(UserId, userData, userData.Id > 0 ? EntityModification.Update : EntityModification.Create); } catch (Exception exception) { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = $"Failed to save user input data, error: {exception.Message}"; return(View("ServiceRequest", model)); } } } ICollection <UserInputTag> userInputs; userInputs = form.UserInput == null ? new List <UserInputTag>() : (from u in form.UserInput select new UserInputTag { UserInput = u, Required = false }).ToList(); var options = from o in model.ServiceRequest.ServiceRequestOptions select new ServiceOptionDto { Id = o.ServiceOptionId }; var requiredInputs = _portfolioService.GetInputsForServiceOptions(UserId, options); //clean up - figure out what can be removed foreach (var userData in userInputs) { switch (userData.UserInput.UserInputType) { case UserInputType.ScriptedSelection: foreach (var input in (from s in requiredInputs.UserInputs where s is IScriptedSelectionInputDto select s)) { if (userData.UserInput.UserInputType == UserInputType.ScriptedSelection & userData.UserInput.InputId == input.Id) { userData.Required = true; } } break; case UserInputType.Selection: foreach (var input in (from s in requiredInputs.UserInputs where s is ISelectionInputDto select s)) { if (userData.UserInput.UserInputType == UserInputType.Selection & userData.UserInput.InputId == input.Id) { userData.Required = true; } } break; default: foreach (var input in (from s in requiredInputs.UserInputs where s is ITextInputDto select s)) { if (userData.UserInput.UserInputType == UserInputType.Text & userData.UserInput.InputId == input.Id) { userData.Required = true; } } break; } } //do the removals foreach (var userData in userInputs) { if (!userData.Required && userData.UserInput.Id > 0) //avoid the new { _serviceRequestUserInputController.ModifyServiceRequestUserInput(UserId, new ServiceRequestUserInputDto { Id = userData.UserInput.Id }, EntityModification.Delete); } } //you've made it this far, all saving is complete (if saves were done) TempData["MessageType"] = WebMessageType.Success; TempData["Message"] = "Service Request saved successfully"; /* STEP FIVE - navigation */ model.CurrentIndex = submit; if (submit >= 99999) //submission { return(RedirectToAction("ShowServiceRequest", "ServiceRequestApproval", new { id = form.Id })); } else if (submit >= 88888) { return(RedirectToAction("Index", "ServiceRequestApproval")); } model.Mode = ServiceRequestMode.Selection; return(RedirectToAction("Form", new { id = model.ServiceRequestId, index = model.CurrentIndex, mode = model.Mode })); }