using System.Globalization; using System.Linq; using EnVisage.Code; using System; using System.Collections.Generic; using MongoDB.Bson; using EnVisage.Code.DAL.Mongo; using MongoDB.Bson.Serialization; namespace EnVisage.Models { public class MixSaveModel : MixModelBase { public MixCalendarModel Calendar { get; set; } public MixFilterModel Filter { get; set; } public MixSaveModel() : base() { Calendar = new MixCalendarModel(); Filter = new MixFilterModel(); } /// /// Converts an instance of entity to the new instance of entity. /// /// An object of type. /// An object of prefilled with values from . public static explicit operator MixSaveModel(EnVisage.Code.DAL.Mongo.Mix entity) { if (entity == null) return null; var model = new MixSaveModel() { Id = entity.Id.ToString(), Name = entity.Name, StartDate = entity.StartDate, EndDate = entity.EndDate, CreatedBy = entity.CreatedBy, Users = entity.Users, Calendar = new MixCalendarModel() { Teams = entity.Teams.Select(t => new MixTeamModel { Id = t.Id, Name = t.Name, CompanyId = t.CompanyId, CostCenterId = t.CostCenterId, UserId = t.UserId, IsNew = t.IsNew, PlannedCapacity = (t.PlannedCapacity != null) ? t.PlannedCapacity.ToList() : new List(), ExpCategories = (t.ExpCategories != null) ? t.ExpCategories.Select(ec => new ActivityExpCategoryFooterModel() { Id = ec.Value.Id, Name = ec.Value.Name, UomValue = ec.Value.UomValue, ActualCapacityValues = ec.Value.ActualCapacityValues, AllocatedCapacity = ec.Value.AllocatedCapacity, NeedCapacity = ec.Value.NeedCapacity, PlannedCapacityValues = ec.Value.PlannedCapacityValues, Resources = (ec.Value.Resources != null) ? ec.Value.Resources.Select(r => new ActivityResourceFooterModel() { Id = r.Value.Id, Name = r.Value.Name, AllocatedCapacity = r.Value.AllocatedCapacity, TotalCapacity = r.Value.TotalCapacity }).ToDictionary(k => k.Id.ToString(), v => v) : null }).ToDictionary(k => k.Id.ToString(), v => v) : new Dictionary() }).ToList() }, Filter = new MixFilterModel() { Selection = new MixFilterSelectionModel() { TeamsViews = entity.Teams.Select(t => new MixTeamViewModel { Id = t.Id, TVName = t.Name, IsNew = t.IsNew, Group = new System.Web.Mvc.SelectListGroup() { Name = t.Group, Disabled = false } }).ToList(), StartDate = entity.StartDate, EndDate = entity.EndDate } } }; return model; } /// /// Copies property values from current object of type to object of type entity. /// /// An object of type. public void CopyTo(EnVisage.Code.DAL.Mongo.Mix obj) { if (obj == null) throw new ArgumentNullException("obj"); obj.Name = Name; obj.StartDate = StartDate; obj.EndDate = EndDate; obj.CreatedBy = CreatedBy; obj.Teams = Calendar.Teams.Select(t => new Code.DAL.Mongo.MixTeam { Id = t.Id, Name = t.Name, CompanyId = t.CompanyId, CostCenterId = t.CostCenterId, UserId = t.UserId, IsNew = t.IsNew, PlannedCapacity = (t.PlannedCapacity != null) ? t.PlannedCapacity.ToArray() : null, ExpCategories = (t.ExpCategories != null) ? t.ExpCategories.Select(e => new ExpCategoryCapacity() { Id = e.Value.Id, Name = e.Value.Name, NeedCapacity = e.Value.NeedCapacity, PlannedCapacityValues = e.Value.PlannedCapacityValues, AllocatedCapacity = e.Value.AllocatedCapacity, ActualCapacityValues = e.Value.ActualCapacityValues, UomValue = e.Value.UomValue, Resources = (e.Value.Resources != null) ? e.Value.Resources.Select(r => new ActivityResource() { Id = r.Value.Id, Name = r.Value.Name, AllocatedCapacity = r.Value.AllocatedCapacity, TotalCapacity = r.Value.TotalCapacity }).ToDictionary(kk => kk.Id.ToString(), vv => vv) : null }).ToDictionary(k => k.Id.ToString(), v => v) : null }).ToList(); obj.Users = Users; obj.ManagedProjects = new List(); obj.UnscheduledProjects = new List(); obj.QueuedProjects = new List(); obj.UnassignedExpProjects = new List(); // SA. ENV-1210 obj.Layout = new List(); obj.TeamsViews = Filter.Selection.TeamsViews.Select(x => new MixTeamView() { Id = x.Id, IsNew = x.IsNew, Name = x.TVName, Group = x.Group.Name, Data = x.Data, CapacityTeamId = x.CapacityTeamId, CompanyId = x.CompanyId, CopyPlanned = x.CopyPlanned, CostCenterId = x.CostCenterId, UserId = x.UserId }).ToList(); if (this.Calendar.ManagedProjects != null) obj.ManagedProjects.AddRange(this.Calendar.ManagedProjects); if (this.Calendar.UnscheduledProjects != null) obj.UnscheduledProjects.AddRange(this.Calendar.UnscheduledProjects); if (this.Calendar.QueuedProjects != null) obj.QueuedProjects.AddRange(this.Calendar.QueuedProjects); // SA. ENV-1210 if (this.Calendar.UnassignedExpendituresProjects != null) obj.UnassignedExpProjects = this.Calendar.UnassignedExpendituresProjects.Select(t => new CategoryExpenditureInMongoProject() { ProjectId = t.ProjectId, ExpCatId = t.ExpCatId, TargetTeamId = t.TargetTeamId, Name = t.Name }).ToList(); if (Calendar.Projects != null) { obj.Projects = Calendar.Projects.Select(p => (MixProject)p.Value).ToDictionary(p => p.Id.ToString(), i => i); } if (Calendar.Layout != null) { obj.Layout = this.Calendar.Layout.Select(l => new MixTeamLayoutRowItem() { TeamId = l.TeamId, Row = l.Row, Index = l.Index, ProjectId = l.ProjectId }).ToList(); } } } }