public ActionResult SaveAndNext(FireRiskAssessmentChecklistViewModel viewModel)
        {
            var request = viewModel.CreateSaveRequest(CurrentUser);

            _fireRiskAssessmentService.SaveFireRiskAssessmentChecklist(request);

            return Json(new { Success = true });
        }
        public ActionResult SaveChecklistOnlyForAuditing(FireRiskAssessmentChecklistViewModel viewModel)
        {
            var request = viewModel.CreateSaveRequest(CurrentUser);

            _fireRiskAssessmentChecklistService.Save(request);

            return Json(new { success = true });
        }
        public FireRiskAssessmentChecklistViewModel GetViewModel()
        {
            
            var fireRiskAssessment = _fireRiskAssessmentService.GetWithLatestFireRiskAssessmentChecklist(_riskAssessmentId, _companyId);

            var result = new FireRiskAssessmentChecklistViewModel
                         {
                             RiskAssessmentId = _riskAssessmentId,
                             CompanyId = _companyId,
                             Sections = new List<SectionViewModel>()
                         };

            var latestFireRiskAssessmentChecklist = fireRiskAssessment.LatestFireRiskAssessmentChecklist;
            if (latestFireRiskAssessmentChecklist != null)
            {
                result.FireRiskAssessmentChecklistId = latestFireRiskAssessmentChecklist.Id;

                foreach (var section in latestFireRiskAssessmentChecklist.Checklist.Sections.OrderBy(s => s.ListOrder))
                {
                    var sectionViewModel = new SectionViewModel
                                           {
                                               ControlId = "Section_" + section.Id.ToString(CultureInfo.InvariantCulture),
                                               Title = section.ShortTitle,
                                               Questions = new List<QuestionViewModel>()
                                           };

                    foreach (var question in section.Questions.OrderBy(q => q.ListOrder))
                    {
                        var questionViewModel = new QuestionViewModel
                                                {
                                                    Id = question.Id,
                                                    ListOrder = question.ListOrder,
                                                    Text = question.Text,
                                                    Information = question.Information,
                                                    Answer =
                                                        CreateAnswerViewModelForQuestion(question.Id,
                                                            latestFireRiskAssessmentChecklist.Answers)
                                                };
                        sectionViewModel.Questions.Add(questionViewModel);
                    }

                    result.Sections.Add(sectionViewModel);
                }

                if (result.Sections.Any())
                {
                    result.Sections.First().Active = true;
                }

            }

            if (HasLatestFireChecklistGotCompleteFailureAttempt(latestFireRiskAssessmentChecklist))
            {
                return AddCompleteValidationErrorsToViewModel(result);
            }

            return result;
        }
        public ActionResult Save(FireRiskAssessmentChecklistViewModel viewModel)
        {
            var request = viewModel.CreateSaveRequest(CurrentUser);

            _fireRiskAssessmentService.SaveFireRiskAssessmentChecklist(request);

            TempData["Notice"] = "Checklist has been saved.";
            
            return RedirectToAction("Index", new { companyId = viewModel.CompanyId, riskAssessmentId = viewModel.RiskAssessmentId });
        }
        public void Setup()
        {
            _viewModel = new FireRiskAssessmentChecklistViewModel
            {
                CompanyId = 41212L,
                FireRiskAssessmentChecklistId = 121L,
                RiskAssessmentId = 142L,
                Sections = new List<SectionViewModel>
                {
                    new SectionViewModel
                    {
                         Questions = new List<QuestionViewModel>
                         {
                             new QuestionViewModel
                             {
                                 Id = 80L,
                                 Answer = new FireAnswerViewModel
                                 {
                                     YesNoNotApplicableResponse = YesNoNotApplicableEnum.Yes,
                                     AdditionalInfo = "Test Additional Info 1"
                                 }
                             },
                             new QuestionViewModel
                             {
                                 Id = 81L,
                                 Answer = new FireAnswerViewModel
                                 {
                                     YesNoNotApplicableResponse = YesNoNotApplicableEnum.No
                                 }
                             },
                             new QuestionViewModel
                             {
                                 Id = 82L,
                                 Answer = new FireAnswerViewModel
                                 {
                                     YesNoNotApplicableResponse = YesNoNotApplicableEnum.NotApplicable
                                 }
                             },
                             new QuestionViewModel
                             {
                                 Id = 83L,
                                 Answer = new FireAnswerViewModel
                                 {
                                     YesNoNotApplicableResponse = null
                                 }
                             },
                         }
                    }
                }
            };

            _fireRiskAssessmentService = new Mock<IFireRiskAssessmentService>();
            _fireRiskAssessmentChecklistService = new Mock<IFireRiskAssessmentChecklistService>();

            _fireRiskAssessmentChecklistViewModelFactory = new FireRiskAssessmentChecklistViewModelFactory(_fireRiskAssessmentService.Object, null);
            _controller = new ChecklistController(
                _fireRiskAssessmentChecklistViewModelFactory,
                _fireRiskAssessmentChecklistService.Object,
                null,
                null
                );

            _controller = TestControllerHelpers.AddUserToController(_controller);
            _controller.SaveChecklistOnlyForAuditing(_viewModel);
        }
        public FireRiskAssessmentChecklistViewModel GetViewModel(FireRiskAssessmentChecklistViewModel viewModel, IList<ValidationFailure> errors)
        {
            if (errors.Any())
            {
                viewModel.MarkAsInvalid();

                foreach (var error in errors)
                {
                    var questionId = error.GetQuestionId();
                    var sectionViewModel = viewModel.GetSectionViewModel(questionId);
                    sectionViewModel.MarkAsInvalid();

                    var questionViewModel = viewModel.GetQuestionViewModel(questionId);
                    questionViewModel.MarkAsInvalid();

                }
            }
            return viewModel;
        }
 private FireRiskAssessmentChecklistViewModel AddCompleteValidationErrorsToViewModel(FireRiskAssessmentChecklistViewModel viewModel)
 {
     var nonNotApplicableAnswerQuestionIds = viewModel.GetAllNonNotApplicableFireAnswerQuestionIds();
     var validationResult = _fireRiskAssessmentChecklistService.ValidateFireRiskAssessmentChecklist(new ValidateCompleteFireRiskAssessmentChecklistRequest
                                                  {
                                                      ChecklistId = viewModel.FireRiskAssessmentChecklistId,
                                                      QuestionIds = nonNotApplicableAnswerQuestionIds
                                                  });
     if (!validationResult.IsValid)
     {
         viewModel = GetViewModel(viewModel, validationResult.Errors);
     }
     return viewModel;
 }
        public ActionResult Complete(FireRiskAssessmentChecklistViewModel viewModel)
        {
            // Validate 
            var allNonNotApplicableFireAnswerQuestionIds = viewModel.GetAllNonNotApplicableFireAnswerQuestionIds();
            var validationResult = GetValidationResult(viewModel.FireRiskAssessmentChecklistId, allNonNotApplicableFireAnswerQuestionIds);
            if (!validationResult.IsValid)
            {
                _fireRiskAssessmentChecklistService.MarkChecklistWithCompleteFailureAttempt(GetMarkCompleteAsFailureRequest(viewModel.FireRiskAssessmentChecklistId));

                var viewModelWithValidationErrors = _viewModelFactory.GetViewModel(viewModel, validationResult.Errors);
                return View("Index", viewModelWithValidationErrors);
            }

            // Complete
            var request = viewModel.CreateCompleteRequest(CurrentUser);

            _fireRiskAssessmentService.CompleteFireRiskAssessmentChecklist(request);

            TempData["Notice"] = "Checklist has been completed.";
            return RedirectToAction("Index", new { viewModel.RiskAssessmentId, viewModel.CompanyId });
        }