public override ReviewAuditDocumentResult CreateReviewAuditDocument(RiskAssessmentType riskAssessmentType, RiskAssessmentDto riskAssessment)
        {
            if (IsSqlReportAvailable)
                return base.CreateReviewAuditDocument(riskAssessmentType, riskAssessment);

            return new ReviewAuditDocumentResult();
        }
        public void When_CreateReviewAuditDocument_Then_should_call_correct_methods(long riskAssessmentId, long reviewId, RiskAssessmentType raType, SqlReportHelper.ReportType sqlReportType)
        {
            // Given
            var riskAssessmentDto = new RiskAssessmentDto()
                                        {
                                            Id = riskAssessmentId,
                                            Reference = "Test Reference",
                                            Title = "Test Tile"
                                        };

            var documentViewModel = new DocumentViewModel()
                                        {
                                            FileName = "Test File Name.pdf",
                                            FileStream = new MemoryStream(100)
                                        };

            var expectedStartOfFileName = string.Format("{0}_{1}_", riskAssessmentDto.Title, riskAssessmentDto.Reference);
            var expectedEndOfFileName = ".pdf";

            _sqlReportsService
                .Setup(x => x.GetReport(sqlReportType, It.IsAny<object[]>(), SqlReportHelper.ReportFormatType.PDF))
                .Returns(documentViewModel);

            //When
            target.CreateReviewAuditDocument(raType, riskAssessmentDto);



            //Then
            _sqlReportsService.VerifyAll();
            _documentLibraryUploader.Verify(x => x.Upload(It.Is<string>(y => y.StartsWith(expectedStartOfFileName) && y.EndsWith(expectedEndOfFileName) && y.Contains(DateTime.Now.ToString("dd_MM_yyyy"))), It.IsAny<Stream>()));

        }
        public virtual ReviewAuditDocumentResult CreateReviewAuditDocument(RiskAssessmentType riskAssessmentType, RiskAssessmentDto riskAssessment)
        {
            
            DocumentTypeEnum documentType = documentTypes[riskAssessmentType];
            SqlReportHelper.ReportType reportType = SqlReportHelper.GetSqlReportType(riskAssessmentType);
            
            var documentViewModel = GetSqlReport(riskAssessment.Id, reportType);
            var newFileName = GetNewFileName(riskAssessment, documentViewModel.FileName);
            var documentLibraryId = _documentLibraryUploader.Upload(newFileName, documentViewModel.FileStream);

            return new ReviewAuditDocumentResult()
                       {
                           DocumentLibraryId = documentLibraryId,
                           NewFileName = newFileName,
                           DocumentType = documentType
                       };
        }
        public static AddEditRiskAssessmentReviewViewModel CreateFrom(RiskAssessmentDto riskAssessment,
                                                                      IEnumerable<EmployeeDto> employees,
                                                                      IRiskAssessmentService riskAssessmentService)
        {
            var reviewDate = riskAssessmentService.GetDefaultDateOfNextReviewById(riskAssessment.Id);

            var viewModel = new AddEditRiskAssessmentReviewViewModel
                                {
                                    CompanyId = riskAssessment.CompanyId,
                                    RiskAssessmentId = riskAssessment.Id,
                                    ReviewingEmployeeId =
                                        riskAssessment.RiskAssessor != null
                                            ? riskAssessment.RiskAssessor.Employee.Id
                                            : Guid.Empty,
                                    ReviewDate = reviewDate.HasValue ? reviewDate.Value.ToString("dd/MM/yyyy") : null,
                                    RiskAssessmentReviewers = GetReviewersSelectList(employees)
                                };

            return viewModel;
        }
        public virtual ReviewAuditDocumentResult CreateReviewAuditDocument(RiskAssessmentType riskAssessmentType, RiskAssessmentDto riskAssessment)
        {
            FeatureSwitches featureSwitch = SqlReportHelper.GetSqlReportFeatureSwitch(riskAssessmentType);
            var isReportAvailable = FeatureSwitchEnabled(featureSwitch, User.GetCustomPrinciple());
            var doesReportDefinitionExist = SqlReportDefinitionFactory.GetSqlReportDefinitions().Exists(r => r.Report == SqlReportHelper.GetSqlReportType(riskAssessmentType));

            if (!isReportAvailable || !doesReportDefinitionExist)
            {
                return new ReviewAuditDocumentResult();
            }

            return _reviewAuditDocumentHelper.CreateReviewAuditDocument(riskAssessmentType, riskAssessment);

        }
        private CompleteRiskAssessmentReviewRequest CreateCompleteRiskAssessmentReviewRequest(CompleteReviewViewModel viewModel,
            RiskAssessmentDto riskAssessment, string newFileName, long documentLibraryId, DocumentTypeEnum documentTypeEnum)
        {
            var completeRiskAssessmentReviewRequest = new CompleteRiskAssessmentReviewRequestBuilder()
                .WithClientId(CurrentUser.CompanyId)
                .WithUserId(CurrentUser.UserId)
                .WithCompleteReview_completeReviewViewModel(viewModel);

            if (documentLibraryId != default(long))
            {
                var requestBuilder = new CreateDocumentRequestBuilder()
                    .WithCompanyId(CurrentUser.CompanyId)
                    .WithDocumentOriginTypeId(DocumentOriginType.TaskCompleted)
                    .WithDocumentType(documentTypeEnum)
                    .WithFilename(newFileName)
                    .WithDocumentLibraryId(documentLibraryId);

                if (riskAssessment.RiskAssessmentSite != null)
                    requestBuilder.WithSiteId(riskAssessment.RiskAssessmentSite.Id);

                var createDocumentRequest = requestBuilder.Build();

                completeRiskAssessmentReviewRequest.WithCreateDocumentRequest(createDocumentRequest);
            }

            return completeRiskAssessmentReviewRequest.Build();
        }
 private static string GetNewFileName(RiskAssessmentDto riskAssessment, string existingReportFileName)
 {
     var extension = existingReportFileName.Substring(existingReportFileName.LastIndexOf('.'));
     return string.Format("{0}_{1}_{2}{3}", riskAssessment.Title, riskAssessment.Reference, DateTime.Now.ToString("dd_MM_yyyy"), extension);
 }
        public RiskAssessmentDto MapWithSiteAndRiskAssessor(RiskAssessment entity)
        {
            RiskAssessmentDto dto = null;

            if (entity.Self as MultiHazardRiskAssessment != null) 
            {
                if(entity.Self as PersonalRiskAssessment != null)
                {
                    dto = new PersonalRiskAssessmentDto();
                    var personalRiskAssessment = entity.Self as PersonalRiskAssessment;
                    var personalRiskAssessmentDto = dto as PersonalRiskAssessmentDto;
                    personalRiskAssessmentDto.Sensitive = personalRiskAssessment.Sensitive;
                    personalRiskAssessmentDto.ChecklistGeneratorMessage = personalRiskAssessment.ChecklistGeneratorMessage;
                    personalRiskAssessmentDto.PersonalRiskAssessementEmployeeChecklistStatus = personalRiskAssessment.PersonalRiskAssessementEmployeeChecklistStatus;
                    personalRiskAssessmentDto.SendCompletedChecklistNotificationEmail = personalRiskAssessment.SendCompletedChecklistNotificationEmail;
                    personalRiskAssessmentDto.CompletionDueDateForChecklists = personalRiskAssessment.CompletionDueDateForChecklists;
                    personalRiskAssessmentDto.CompletionNotificationEmailAddress = personalRiskAssessment.CompletionNotificationEmailAddress;
                    personalRiskAssessmentDto.Hazards = personalRiskAssessment.Hazards.ToList().Select(h => HazardDtoMapper.Map(h)).ToList();
                }

                if(entity.Self as GeneralRiskAssessment != null)
                {
                    dto = new GeneralRiskAssessmentDto();
                }

                var multiHazardRiskAssessmentDto = dto as MultiHazardRiskAssessmentDto;
                var multiHazardRiskAssessment = entity.Self as MultiHazardRiskAssessment;
                if (multiHazardRiskAssessment != null)
                {
                    multiHazardRiskAssessmentDto.Location = multiHazardRiskAssessment.Location;
                    multiHazardRiskAssessmentDto.TaskProcessDescription = multiHazardRiskAssessment.TaskProcessDescription;
                    multiHazardRiskAssessmentDto.CompletionDueDate = multiHazardRiskAssessment.CompletionDueDate;
                }
            }
            else if(entity.Self as FireRiskAssessment != null)
            {
                dto = new FireRiskAssessmentDto();
                var fireRiskAssessment = entity.Self as FireRiskAssessment;
                var fireRiskAssessmentDto = dto as FireRiskAssessmentDto;
                fireRiskAssessmentDto.PersonAppointed = fireRiskAssessment.PersonAppointed;
                fireRiskAssessmentDto.PremisesProvidesSleepingAccommodation = fireRiskAssessment.PremisesProvidesSleepingAccommodation;
                fireRiskAssessmentDto.PremisesProvidesSleepingAccommodationConfirmed = fireRiskAssessment.PremisesProvidesSleepingAccommodationConfirmed;
                fireRiskAssessmentDto.Location = fireRiskAssessment.Location;
                fireRiskAssessmentDto.BuildingUse = fireRiskAssessment.BuildingUse;
                fireRiskAssessmentDto.NumberOfFloors = fireRiskAssessment.NumberOfFloors;
                fireRiskAssessmentDto.NumberOfPeople = fireRiskAssessment.NumberOfPeople;
                fireRiskAssessmentDto.ElectricityEmergencyShutOff = fireRiskAssessment.ElectricityEmergencyShutOff;
                fireRiskAssessmentDto.WaterEmergencyShutOff = fireRiskAssessment.WaterEmergencyShutOff;
                fireRiskAssessmentDto.GasEmergencyShutOff = fireRiskAssessment.GasEmergencyShutOff;
                fireRiskAssessmentDto.OtherEmergencyShutOff = fireRiskAssessment.OtherEmergencyShutOff;
                fireRiskAssessmentDto.CompletionDueDate = fireRiskAssessment.CompletionDueDate;
                
            }
            else
            {
                dto = new RiskAssessmentDto();
            }

            dto.Id = entity.Id;
            dto.AssessmentDate = entity.AssessmentDate;
            dto.CreatedOn = entity.CreatedOn;
            dto.CompanyId = entity.CompanyId;
            dto.Reference = entity.Reference;
            dto.Status = entity.Status;
            dto.Title = entity.Title;
            dto.Deleted = entity.Deleted;
            dto.CreatedBy = entity.CreatedBy != null ?  new AuditedUserDtoMapper().Map(entity.CreatedBy) : null;

            dto.RiskAssessmentSite = entity.RiskAssessmentSite != null
                ? new SiteStructureElementDtoMapper().Map(entity.RiskAssessmentSite)
                : null;

            dto.RiskAssessor = entity.RiskAssessor != null
                                   ? new RiskAssessorDtoMapper().MapWithEmployee(entity.RiskAssessor)
                                   : null;

            dto.NextReviewDate = entity.NextReviewDate;
            //Required for the API
            dto.Reviews = entity.Reviews != null ? entity.Reviews.Select(review => new RiskAssessmentReviewDto()
                                                                                       {
                                                                                           Comments = review.Comments
                                                                                           ,CompletedBy = review.CompletedBy != null ? new EmployeeDtoMapper().Map(review.CompletedBy) : null
                                                                                           ,CompletedDate = review.CompletedDate
                                                                                           ,CompletionDueDate = review.CompletionDueDate,
                                                                                           Id = review.Id,
                                                                                           IsReviewOutstanding = review.IsReviewOutstanding
                                                                                           ,ReviewAssignedTo = review.ReviewAssignedTo != null ? new EmployeeDtoMapper().Map(review.ReviewAssignedTo) : null
                                                                                       }).ToList() : new List<RiskAssessmentReviewDto>();
            return dto;
        }
        public RiskAssessmentDto MapWithTitleReference(RiskAssessment entity)
        {
            var dto = new RiskAssessmentDto();

            dto.Id = entity.Id;
            dto.Reference = entity.Reference;
            dto.Title = entity.Title;
            
            return dto;
        }