public JsonResult Save(StatusReportViewModel report)
        {
            // the following causes a problem - by reloading the status report from the database, we can't overwrite with
            // objects being posted back by the client.  Either client provides all details of statusreport, or we go
            // more manual on mapping back to actual objects.
            StatusReport sr = this.StatusReportRepository.Get(report.Id);

            using (var txn = this.StatusReportRepository.BeginTransaction())
            {
                if (report.Items != null)
                {
                    report.Items.ToList().ForEach(r =>
                                                      {
                                                          // need to map this back to status report
                                                          StatusItem sri = null;

                                                          // using Id won't work with multiple new entries
                                                          if (r.Id > 0)
                                                          { // is new
                                                              var srSource =
                                                                  (from srSourceItem in sr.Items
                                                                   where srSourceItem.Id == r.Id
                                                                   select srSourceItem).FirstOrDefault();
                                                              // map over the new item
                                                              sri = Mapper.Map<StatusReportItemViewModel, StatusItem>(r, srSource);
                                                          }
                                                          else
                                                          {
                                                              sri = Mapper.Map<StatusReportItemViewModel, StatusItem>(r);
                                                          }
                                                          sri.Milestone.Type = r.MilestoneType;

                                                          sri.AuditInfo =
                                                              GetAuditInfo();
                                                          // if topic doesn't exist yet, we should create
                                                          if (string.IsNullOrEmpty(sri.Caption))
                                                              throw new ArgumentNullException("Caption cannot be null!");
                                                          Topic topic = null;
                                                          if (r.TopicId != 0)
                                                              topic = this.TopicRepository.Get(r.TopicId);
                                                          else
                                                              topic =
                                                                  this.TopicRepository.GetOrAddTopicByCaption(
                                                                      sri.Caption);
                                                          Project project = this.ProjectRepository.Get(r.ProjectId);
                                                          sri.Topic = topic;
                                                          sri.Project = project;
                                                          sri.Milestone.Date = r.MilestoneDate;

                                                          sr.AddStatusItem(sri);
                                                          sri.StatusReport = sr;

                                                          // update the tags
                                                          if (String.IsNullOrEmpty(r.TagsString))
                                                          {
                                                              sri.Tags.Clear();
                                                          }
                                                          else
                                                          {
                                                              var tagsList = r.TagsString.Split(',');
                                                              var tagsL = tagsList.ToList();
                                                              tagsL.ForEach(tag =>
                                                              {
                                                                  if (sri.Tags.FirstOrDefault(tagS => { return tagS.Name == tag; }) == null)
                                                                  {
                                                                      var t = this.TagRepository.GetOrAddTagByName(tag);
                                                                      sri.Tags.Add(t);
                                                                  }
                                                              });
                                                              // delete tags not found
                                                              sri.Tags.ToList().ForEach(tag =>
                                                              {
                                                                  if (tagsL.FirstOrDefault(tagName =>
                                                                  {
                                                                      return tagName == tag.Name;
                                                                  }) == null)
                                                                      sri.Tags.Remove(tag);
                                                              });
                                                          }
                                                          //this.StatusReportRepository.Update(sr);// each item any better

                                                      });
                }
                if (report.ItemsToRemove != null)
                {
                    report.ItemsToRemove.ToList().ForEach(r =>
                                                              {
                                                                  var sri =
                                                                      Mapper.Map<StatusReportItemViewModel, StatusItem>(
                                                                          r);
                                                                  var sriDeleteItem = (from sriD in sr.Items
                                                                                       where sriD.Id == sri.Id
                                                                                       select sriD).First();
                                                                  sr.Items.Remove(sriDeleteItem);
                                                              });
                }
                this.StatusReportRepository.Update(sr); //.UpsertStatusReportItem(sri);
                txn.Commit();
            }
            return Json(report, JsonRequestBehavior.AllowGet);
        }
 public JsonResult Create(StatusReportViewModel vm)
 {
     StatusReport sr = null;
     Mapper.Map(vm, sr);
     if (sr == null)
         throw new NullReferenceException(
             "Unable to convert StatusReportViewModel to StatusReport - invalid data?");
     // ReSharper disable HeuristicUnreachableCode
     // this is reachable as Mapper.Map will fill in sr
     if (sr.Id == 0)
         // create
         this.StatusReportRepository.Add(sr);
     else
         this.StatusReportRepository.Update(sr);
     //do the persistence logic here
     var message = "Status Report: " + vm.PeriodStart + " Saved";
     return Json(message);
     // ReSharper restore HeuristicUnreachableCode
 }
 public JsonResult RollStatus(StatusReportViewModel report)
 {
     // the following causes a problem - by reloading the status report from the database, we can't overwrite with
     // objects being posted back by the client.  Either client provides all details of statusreport, or we go
     // more manual on mapping back to actual objects.
     StatusReport sr = this.StatusReportRepository.Get(report.Id);
     this.StatusReportManager.StatusReportRepository = this.StatusReportRepository;
     var rolledReport = this.StatusReportManager.RollStatusReport(sr, GetAuditInfo());
     var vm = GetStatusReportViewModel(rolledReport);
     return Json(vm, JsonRequestBehavior.AllowGet);
 }
        public void StatusReportControllerConstructorTest()
        {
            IStatusReportRepository repository = new StatusReportRepository(ConfigurationManager.ConnectionStrings["StatusAgain"].ConnectionString); // TODO: Initialize to an appropriate value
            StatusReportController target = _kernel.Get<StatusReportController>();// new StatusReportController(repository);
            StatusReportViewModel vm = new StatusReportViewModel {
                Caption = "SR 1",
                Items = new List<StatusReportItemViewModel>()
            };
            vm.Items.Add(new StatusReportItemViewModel { ProjectId=1,Caption="TEST CAPTION 1" });
            target.Save(vm);

            Assert.Inconclusive("TODO: Implement code to verify target");
        }