public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     var requestForm = controllerContext.HttpContext.Request.Form;
     var model = new ProjectDetailsViewModel();
     model.ExtractForCodes<ProjectFieldOfResearch>(requestForm);
     model.ExtractSeoCodes<ProjectSocioEconomicObjective>(requestForm);
     if (controllerContext.Controller.GetType() == typeof(DataDepositController))
     {
         model.SourceProjectType = SourceProjectType.DEPOSIT;
     }
     bindingContext.ModelMetadata.Model = model;
     return base.BindModel(controllerContext, bindingContext); 
 }
        public ActionResult Project(int? id)
        {
            var vm = new ProjectDetailsViewModel();

            if (id > 0)
            {
                var savedProject = ProjectRepository.Get(id.Value);
                ActionResult result;
                if (!VerifyProjectRequest(savedProject, out result))
                {
                    return result;
                }
                vm.InjectFrom(savedProject);
                vm.StartDate = savedProject.StartDate != null ? savedProject.StartDate.Value.ToShortDateString() : string.Empty;
                vm.EndDate = savedProject.EndDate != null ? savedProject.EndDate.Value.ToShortDateString() : string.Empty;
                vm.Keywords = savedProject.Keywords;
                vm.MapFrom(savedProject);
            }

            return ReturnOnGetForProject(vm);
        }
        public ActionResult Project(ProjectDetailsViewModel vm)
        {
            if (vm.Id > 0)
            {
                var savedProject = this.ProjectRepository.Get(vm.Id);
                ActionResult result;
                if (!VerifyProjectRequest(savedProject, out result))
                {
                    return result;
                }
            }
            if (!ModelState.IsValid)
            {
                return View(vm);
            }
            var project = GetProjectFromViewModel(vm);
            project.MapFrom(vm);
            if (!project.Parties.Any(o => o.Relationship == ProjectRelationship.PrincipalInvestigator))
            {
                project.Parties.Add(new ProjectParty
                {
                    Party = new Party
                    {
                        UserId = CurrentUser.CurtinId,
                        Email = CurrentUser.EmailAddress,
                        FirstName = CurrentUser.FirstName,
                        FullName = CurrentUser.FullName,
                        LastName = CurrentUser.LastName,
						Organisation = ""	// TODO: Insert your organisation here
                    },
                    Project = project,
                    Role = AccessRole.Owners,
                    Relationship = ProjectRelationship.PrincipalInvestigator
                });
            }
            return ReturnOnPostForProject(project);
        }
        protected override ActionResult ReturnOnGetForProject(ProjectDetailsViewModel vm)
        {
            vm.SourceProjectType = SourceProjectType.DEPOSIT;
            vm.Status = ProjectStatus.Completed;

            return View("Project", vm);
        }
        public void Update_datadeposit_project()
        {
            var project = SetUpFullProjectWithAuthentication();
            
            var forCode = new ProjectFieldOfResearch
            {
                Code = Builder<FieldOfResearch>.CreateNew()
                    .With(o => o.Id = Pick<string>.RandomItemFrom(new[] { "ABC", "DEF", "GHI" }))
                    .And(o => o.Name = string.Format("{0} Name", o.Id))
                    .Build()
            };

            var seoCode = new ProjectSocioEconomicObjective
            {
                Code = Builder<SocioEconomicObjective>.CreateNew()
                    .With(o => o.Id = Pick<string>.RandomItemFrom(new[] { "XYZ", "RST", "UVW" }))
                    .And(o => o.Name = string.Format("{0} Name", o.Id))
                    .Build()
            };

            var vm = new ProjectDetailsViewModel
                         {
                             Id = project.Id,
                             Status = PickHelper.RandomEnumExcept(project.Status),
                             // deliberately change the source type to an invalid one
                             SourceProjectType = PickHelper.RandomEnumExcept(project.SourceProjectType, SourceProjectType.None),
                             EndDate = string.Format("{0:dd-MM-yyyy}", (project.EndDate ?? DateTime.Today).AddDays(10)),
                             StartDate = string.Format("{0:dd-MM-yyyy}", (project.StartDate ?? DateTime.Today).AddDays(-10)),
                             Description = "aut viam inveniam aut faciam",
                             FieldsOfResearch = project.FieldsOfResearch.Except(project.FieldsOfResearch.Take(1)).Union(new[] {forCode}).Cast<ClassificationBase>().ToList(),
                             SocioEconomicObjectives = project.SocioEconomicObjectives.Except(project.SocioEconomicObjectives.Take(1)).Union(new[] {seoCode}).Cast<ClassificationBase>().ToList(),
                             PrincipalInvestigator = project.Parties.Single(o => o.Relationship == ProjectRelationship.PrincipalInvestigator).Party,
                             ArcFunder = new ArcProjectFunderViewModel {GrantNumber = "BYC980", IsFunded = true},
                             NmhrcFunder = new NmhrcProjectFunderViewModel {IsFunded = false},
                             Keywords = "other keywords",
                             Title = "crede quod habes, et habes"
                         };

            _controller.WithCallTo(c => c.Project(vm)).ShouldRedirectTo<ProjectController>(typeof(ProjectController).GetMethod("Index"));

            _projectRepository.Received().Save(Arg.Is<Project>(p =>
                   p.Description == vm.Description &&
                   p.Title == vm.Title &&
                   p.StartDate == DateTime.Parse(vm.StartDate) &&
                   p.EndDate == DateTime.Parse(vm.EndDate) &&
                   p.Keywords == vm.Keywords &&
                   p.Status == ProjectStatus.Completed &&
                   p.SourceProjectType == SourceProjectType.DEPOSIT &&
                   p.Funders.Count() == 0 &&
                   p.SocioEconomicObjectives.All(o => vm.SocioEconomicObjectives.Any(q => q.Code.Id == o.Code.Id)) &&
                   p.FieldsOfResearch.All(o => vm.FieldsOfResearch.Any(q => q.Code.Id == o.Code.Id))));
        }
 protected override ActionResult ReturnOnGetForProject(ProjectDetailsViewModel vm)
 {
     return View("Project", vm);
 }
        public void Map_to_project_details_view_model()
        {
            var model = new ProjectDetailsViewModel().MapFrom(_project);

            Assert.That(model,Is.Not.Null,"View model is null");
            Assert.That(model.Id,Is.EqualTo(_project.Id),"View model has incorrect id");
            Assert.That(model.SocioEconomicObjectives, Is.Not.Null, "Socio economic objectives is null");
            Assert.That(model.SocioEconomicObjectives.Count, Is.EqualTo(_project.SocioEconomicObjectives.Count), "Socio economic objectives count is incorrect");
            Assert.That(model.SocioEconomicObjectives.All(o => _project.SocioEconomicObjectives.Any(q => q.SocioEconomicObjective.Id == o.Code.Id)), "Incorrect socio economic objectives");
            Assert.That(model.FieldsOfResearch, Is.Not.Null, "Fields of research is null");
            Assert.That(model.FieldsOfResearch.Count, Is.EqualTo(_project.FieldsOfResearch.Count), "Fields of research count is incorrect");
            Assert.That(model.FieldsOfResearch.All(o => _project.FieldsOfResearch.Any(q => q.FieldOfResearch.Id == o.Code.Id)), "Incorrect fields of research");
            Assert.That(model.Funders.IsNotEmpty(),"Funders are empty");
            Assert.That(model.Funders.Count(o => o.IsFunded),Is.EqualTo(1),"No project funder found");

            var projectFunder = model.Funders.First(o => o.IsFunded);
            Assert.That(projectFunder.Funder,Is.EqualTo(_funder.Funder),"Funder incorrect");
            Assert.That(projectFunder.GrantNumber,Is.EqualTo(_funder.GrantNumber),"Funder grant number incorrect");
        }
 protected abstract ActionResult ReturnOnGetForProject(ProjectDetailsViewModel vm);
 private static void ClearAllCodes(ProjectDetailsViewModel vm)
 {
     vm.FieldOfResearchCode = null;
     vm.SocioEconomicObjectiveCode = null;
 }
 public ActionResult DeleteSeoCodes(ProjectDetailsViewModel vm)
 {
     ModelState.Clear();
     try
     {
         vm.DeleteSeoCodes(ControllerContext.HttpContext.Request.Form);
     }
     catch (Exception e)
     {
         ModelState.AddModelError(e.Source, e.Message);
     }
     return View("Project", vm);
 }
        public ActionResult AddSeoCode(ProjectDetailsViewModel vm)
        {
            ModelState.Clear();

            try
            {
                SocioEconomicObjectiveRepository.PopulateSeoCodes<ProjectSocioEconomicObjective>(vm, ControllerContext.HttpContext.Request.Form);
                ClearAllCodes(vm);
            }
            catch (Exception e)
            {
                ModelState.AddModelError(e.Source, e.Message);
            }

            return View("Project", vm);
        }
        public ActionResult AddForCode(ProjectDetailsViewModel vm)
        {
            ModelState.Clear();

            try
            {
                FieldOfResearchRepository.PopulateForCodes<ProjectFieldOfResearch>(vm, ControllerContext.HttpContext.Request.Form);
                ClearAllCodes(vm);
            }
            catch (Exception e)
            {
                ModelState.AddModelError(e.Source, e.Message);
            }

            return View("Project", vm);
        }