public ActionResult AllDataManagementPlansToCsv(CsvDumpViewModel model)
 {
     var projects = _projectRepository.GetAll().Where(p => p.DataManagementPlan != null).ToList();
     if (projects.Count != 0)
     {
         var dataManagementPlanTable = _csvHelper.DataManagementPlansToDataTable(projects);
         var reponse = this.ControllerContext.RequestContext.HttpContext.Response;
         reponse.AddHeader("Content-Disposition", "attachment;filename=DataManagementPlans_" + DateTime.Now.ToShortDateString() + ".csv");
         return File(Encoding.UTF8.GetBytes(_csvHelper.ExportToCsv(dataManagementPlanTable, _appSettingsService.CsvSeparator)), "text/csv");
     }
     return RedirectToAction("Index");
 }
 public ActionResult Index()
 {
     var projects = _projectRepository.GetAll();
     var projectModels = projects.Where(p => p.DataManagementPlan != null).Select(p =>
             new DmpListViewModel
                 {
                     Id = p.DataManagementPlan.Id,
                     Title = p.Title
                 }).ToList();
     var dataCollections = _dataCollectionRepository.GetAll();
     var dataCollectionModels =
         dataCollections.Select(d => new CollectionListViewModel { Id = d.Id, Title = d.Title }).ToList();
     var model = new CsvDumpViewModel { Projects = projectModels, DataCollections = dataCollectionModels };
     return View(model);
 }
        public void Render_DmpNotFound_view_for_post_to_Index_with_project_without_Dmp()
        {
            var vm = new CsvDumpViewModel { Projects = Builder<DmpListViewModel>.CreateListOfSize(3).Build() };
            _form["DataManagementPlanCsv"] = "Get Data Management Plan to CSV";
            _form["ProjectList"] = "5";
            _projectRepository.GetByDataManagementPlanId(5).Returns(x => null);
            var project = SetUpFullProject("GA37493");
            project.DataManagementPlan = null;
            _csvHelper.ExportToCsv(Arg.Any<DataTable>()).Returns("");
            _csvHelper.DataManagementPlanToDataTable(Arg.Any<DataManagementPlan>(), Arg.Any<IList<ProjectParty>>()).Returns(new DataTable());

            _controller.WithCallTo(c => c.DataManagementPlanToCsv(vm)).ShouldRenderView("DmpNotFound");

            _csvHelper.DidNotReceive().ExportToCsv(Arg.Any<DataTable>(), Arg.Any<string>());
            _csvHelper.DidNotReceive().DataManagementPlanToDataTable(Arg.Any<DataManagementPlan>(), Arg.Any<IList<ProjectParty>>());
        }
        public void Render_CSV_file_for_post_to_DataManagementPlanToCsv()
        {
            var vm = new CsvDumpViewModel {Projects = Builder<DmpListViewModel>.CreateListOfSize(3).Build()};
            _form["DataManagementPlanCsv"] = "Get Data Management Plan to CSV";
            _form["ProjectList"] = "1";
            var project = SetUpFullProject("GA37493");
            _projectRepository.GetByDataManagementPlanId(project.DataManagementPlan.Id).Returns(project);
            _csvHelper.ExportToCsv(Arg.Any<DataTable>()).Returns("");
            _csvHelper.DataManagementPlanToDataTable(Arg.Any<DataManagementPlan>(), Arg.Any<IList<ProjectParty>>()).Returns(new DataTable());
            _controller.WithCallTo(c => c.DataManagementPlanToCsv(vm)).ShouldRenderFile("text/csv");
            
            _csvHelper.Received().ExportToCsv(Arg.Any<DataTable>(), Arg.Any<string>());
            _csvHelper.Received().DataManagementPlanToDataTable(Arg.Any<DataManagementPlan>(), Arg.Any<IList<ProjectParty>>());
            const string headerValue = "attachment;filename=Feeding_habits_of_polarbears_2011_DataManagementPlan.csv";

            _context.HttpContext.Response.Received().AddHeader(Arg.Any<string>(), Arg.Is<string>(o => o == headerValue));
       }
 public ActionResult DataManagementPlanToCsv(CsvDumpViewModel model)
 {
     var formParams = ControllerContext.HttpContext.Request.Form;
     var dmpList = formParams["ProjectList"];
     int sourceProjectId;
     if (int.TryParse(dmpList, out sourceProjectId))
     {
         var project = _projectRepository.GetByDataManagementPlanId(sourceProjectId);
         if (project != null)
         {
             var dataManagementPlanTable = _csvHelper.DataManagementPlanToDataTable(project.DataManagementPlan, project.Parties);
             var reponse = this.ControllerContext.RequestContext.HttpContext.Response;
             var fileName = Regex.Replace(project.Title, @"\W+", "_").Trim('_');
             reponse.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "_DataManagementPlan.csv");
             return File(Encoding.UTF8.GetBytes(_csvHelper.ExportToCsv(dataManagementPlanTable, _appSettingsService.CsvSeparator)), "text/csv");
         }
         return View("DmpNotFound");
     }
     return RedirectToAction("Index");
 }
        public void Redirect_to_Index_for_post_to_DataManagementPlansToCsv_when_no_Projects()
        {
            var vm = new CsvDumpViewModel { Projects =new List<DmpListViewModel>() };
            _projectRepository.GetAll().Returns(new List<Project>());

            _controller.WithCallTo(c => c.AllDataManagementPlansToCsv(vm)).ShouldRedirectTo(_controller.GetType().GetMethod("Index"));
        }
        public void Render_CSV_file_for_post_to_DataCollectionsToCsv()
        {
            var vm = new CsvDumpViewModel { DataCollections = Builder<CollectionListViewModel>.CreateListOfSize(3).Build() };
            var dataCollections = Builder<DataCollection>.CreateListOfSize(5)
                .All()
                .Build();
            _dataCollectionRepository.GetAll().Returns(dataCollections);
            _csvHelper.ExportToCsv(Arg.Any<DataTable>()).Returns("");
            _csvHelper.DataCollectionsToDataTable(Arg.Any<IList<DataCollection>>()).Returns(new DataTable());
            _controller.WithCallTo(c => c.AllDataCollectionsToCsv(vm)).ShouldRenderFile("text/csv");

            _csvHelper.Received().ExportToCsv(Arg.Any<DataTable>(), Arg.Any<string>());
            _csvHelper.Received().DataCollectionsToDataTable(Arg.Any<IList<DataCollection>>());

            _context.HttpContext.Response.Received().AddHeader(Arg.Any<string>(), Arg.Any<string>());
        }
 public void Redirect_to_Index_for_post_to_DataCollectionsToCsv_when_no_DataCollections()
 {
     var vm = new CsvDumpViewModel { DataCollections = new List<CollectionListViewModel>() };
     _dataCollectionRepository.GetAll().Returns(new List<DataCollection>());
   
     _controller.WithCallTo(c => c.AllDataCollectionsToCsv(vm)).ShouldRedirectTo(_controller.GetType().GetMethod("Index"));
 }
        public void Render_CSV_file_for_post_to_DataManagementPlansToCsv()
        {
            var vm = new CsvDumpViewModel { Projects = Builder<DmpListViewModel>.CreateListOfSize(3).Build() };
            var projects = Builder<Project>.CreateListOfSize(5)
                .All()
                .With(p => p.DataManagementPlan = Builder<DataManagementPlan>.CreateNew().Build())
                .Build();
            _projectRepository.GetAll().Returns(projects);
            _csvHelper.ExportToCsv(Arg.Any<DataTable>()).Returns("");
            _csvHelper.DataManagementPlansToDataTable(Arg.Any<IList<Project>>()).Returns(new DataTable());
            _controller.WithCallTo(c => c.AllDataManagementPlansToCsv(vm)).ShouldRenderFile("text/csv");

            _csvHelper.Received().ExportToCsv(Arg.Any<DataTable>(), Arg.Any<string>());
            _csvHelper.Received().DataManagementPlansToDataTable(Arg.Any<IList<Project>>());
            
            _context.HttpContext.Response.Received().AddHeader(Arg.Any<string>(), Arg.Any<string>());
        }
 public ActionResult DataCollectionToCsv(CsvDumpViewModel model)
 {
     var formParams = ControllerContext.HttpContext.Request.Form;
     var collectionList = formParams["DataCollectionList"];
     int collectionId;
     if (int.TryParse(collectionList, out collectionId))
     {
         var dataCollection = _dataCollectionRepository.Get(collectionId);
         if (dataCollection != null)
         {
             var dataCollectionTable = _csvHelper.DataCollectionToDataTable(dataCollection);
             var reponse = this.ControllerContext.RequestContext.HttpContext.Response;
             var fileName = Regex.Replace(dataCollection.Title, @"\W+", "_").Trim('_');
             reponse.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "_DataCollection.csv");
             return File(Encoding.UTF8.GetBytes(_csvHelper.ExportToCsv(dataCollectionTable, _appSettingsService.CsvSeparator)), "text/csv");
         }
         return View("DataCollectionNotFound");
     }
     return RedirectToAction("Index");
 }