using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; using EnVisage.Code; //using Excel; namespace EnVisage.Models { public class ProjectModel : IBaseModel, IValidatableObject { public Guid Id { get; set; } [Required] [MaxLength(100, ErrorMessage = "Name should not exceed 100 characters")] [Display(Name = "Name")] public string Name { get; set; } [Display(Name = "Project Number")] public string Number { get; set; } //[Required] [Display(Name = "Status")] public Guid StatusId { get; set; } [Display(Name = "Status")] public string StatusName { get; set; } //[Required] [Display(Name = "Type")] public Guid TypeId { get; set; } [Display(Name = "Type")] public string TypeName { get; set; } //[Required] [Display(Name = "Client")] public Guid? ClientId { get; set; } [Display(Name = "Client")] public string ClientName { get; set; } [Required] [Display(Name = "Company")] public Guid? CompanyId { get; set; } [Display(Name = "Company")] public string CompanyName { get; set; } [MaxLength(8)] public string Color { get; set; } [Display(Name = "Details")] [MaxLength(2000)] public string Details { get; set; } [Display(Name = "Number of Scenarios")] public int ScenariosCount { get; set; } [Display(Name = "Priority")] [Range(0, 25)] public int Priority { get; set; } [Display(Name = "Probability")] [Range(0, 100)] [UIHint("Slider")] public short Probability { get; set; } [Display(Name = "Revenue Generating")] public bool IsRevenueGenerating { get; set; } [Display(Name = "Deadline")] [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] public DateTime? Deadline { get; set; } [Display(Name = "Teams")] public List AssignedTeams { get; set; } [Display(Name = "Internal Contacts")] public List InternalContacts { get; set; } [Display(Name = "External Contacts")] public List ExternalContacts { get; set; } public int PartsCount { get; set; } public bool IsProbability100 { get; set; } public Guid? ParentProjectId { get; set; } public bool DeletedPart { get; set; } [Display(Name = "Project has multiple parts")] public bool HasChildren { get; set; } public Guid? TemplateId { get; set; } public bool SaveAsCopy { get; set; } public bool ContinueToScenarios { get; set; } public virtual List Scenarios { get; set; } public virtual List InactiveScenarios { get; set; } public List Notes { get { var result = new List(); using (var dbContext = new EnVisageEntities()) { var notes = (from c in dbContext.Notes where c.ParentId == Id select c).ToList(); result.AddRange(notes.Select(note => (NoteModel) note)); } return result; } } public IList Parts { get; set; } /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator ProjectModel(Project obj) { if (obj == null) return null; var model = new ProjectModel { Id = obj.Id, Name = obj.Name, Number = obj.ProjectNumber, StatusId = obj.StatusId, StatusName = obj.Status != null ? obj.Status.Name : string.Empty, TypeId = obj.TypeId, TypeName = obj.Type != null ? obj.Type.Name : string.Empty, ClientId = obj.ClientId, ClientName = obj.Client != null ? obj.Client.Name : string.Empty, CompanyId = obj.CompanyId, CompanyName = obj.Company != null ? obj.Company.Name : string.Empty, Color = obj.Color, Details = obj.Details, ScenariosCount = 0, Priority = obj.Priority, IsRevenueGenerating = obj.IsRevenueGenerating, Deadline = obj.Deadline, Probability = Convert.ToInt16(obj.Probability * 100), IsProbability100 = obj.Status != null && obj.Status.Probability100, Scenarios = new List(), InactiveScenarios = new List(), Parts = new List(), HasChildren = obj.HasChildren, ParentProjectId = obj.ParentProjectId }; if (model.HasChildren) { foreach (var childProject in obj.ChildProjects) { var part = (ProjectPartModel) childProject; model.Parts.Add(part); foreach (var scenario in childProject.Scenarios) { if (scenario.Type != ScenarioType.Actuals.GetHashCode()) model.ScenariosCount++; model.Scenarios.Add((ScenarioTabModel)scenario); } part.AssignedTeams = childProject.Team2Project.Select(t => t.TeamId).ToList(); var partContacts = childProject.Contact2Project.Select(c => c.Contact); part.InternalContacts = partContacts.Where(c => c.Type == ContactType.CompanyContact.GetHashCode()).Select(c => c.Id).ToList(); part.ExternalContacts = partContacts.Where(c => c.Type == ContactType.ClientContact.GetHashCode()).Select(c => c.Id).ToList(); part.CompanyId = model.CompanyId; part.TrimStringProperties(); } } else if (!obj.ParentProjectId.HasValue) { // if this is a main project then add one part to display it as main project properties model.Parts.Add(new ProjectPartModel { StatusId = obj.StatusId, StatusName = obj.Status != null ? obj.Status.Name : string.Empty, TypeId = obj.TypeId, TypeName = obj.Type != null ? obj.Type.Name : string.Empty, ClientId = obj.ClientId, ClientName = obj.Client != null ? obj.Client.Name : string.Empty, Details = obj.Details, Priority = obj.Priority, IsRevenueGenerating = obj.IsRevenueGenerating, Deadline = obj.Deadline, Probability = Convert.ToInt16(obj.Probability * 100), IsProbability100 = obj.Status != null && obj.Status.Probability100, ParentProjectId = obj.Id, AssignedTeams = obj.Team2Project.Select(t2p => t2p.TeamId).ToList(), CompanyId = obj.CompanyId }); foreach (var scenario in obj.Scenarios) { if (scenario.Type != ScenarioType.Actuals.GetHashCode()) model.ScenariosCount++; model.Scenarios.Add((ScenarioTabModel)scenario); } model.Parts[0].AssignedTeams = obj.Team2Project.Select(t2p => t2p.TeamId).ToList(); var contacts = obj.Contact2Project.Select(c2p => c2p.Contact); model.Parts[0].InternalContacts = contacts.Where(c => c.Type == ContactType.CompanyContact.GetHashCode()).Select(c => c.Id).ToList(); model.Parts[0].ExternalContacts = contacts.Where(c => c.Type == ContactType.ClientContact.GetHashCode()).Select(c => c.Id).ToList(); } model.PartsCount = model.Parts.Count; model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(Project dbObj) { if (dbObj == null) throw new ArgumentNullException(); dbObj.Name = Name; dbObj.ProjectNumber = Number; dbObj.StatusId = StatusId; dbObj.TypeId = TypeId; dbObj.ClientId = ClientId; dbObj.CompanyId = CompanyId; dbObj.Details = Details; dbObj.Color = Color != null ? Color.Replace("#", "") : Color; dbObj.Priority = Priority; dbObj.Probability = Probability / 100.0M; dbObj.IsRevenueGenerating = IsRevenueGenerating; dbObj.Deadline = Deadline; dbObj.HasChildren = HasChildren; } public ProjectModel Clone() { return new ProjectModel { Id = this.Id, TemplateId = this.TemplateId, ClientId = this.ClientId, ClientName = this.ClientName, Color = this.Color, CompanyId = this.CompanyId, CompanyName = this.CompanyName, Details = this.Details, InactiveScenarios = this.InactiveScenarios, IsRevenueGenerating = this.IsRevenueGenerating, Deadline = this.Deadline, Name = this.Name, Number = this.Number, Priority = this.Priority, Probability = this.Probability, SaveAsCopy = this.SaveAsCopy, ContinueToScenarios = this.ContinueToScenarios, Scenarios = this.Scenarios, ScenariosCount = this.ScenariosCount, StatusId = this.StatusId, StatusName = this.StatusName, TypeId = this.TypeId, TypeName = this.TypeName, AssignedTeams = this.AssignedTeams, InternalContacts = this.InternalContacts, ExternalContacts = this.ExternalContacts, DeletedPart = this.DeletedPart, HasChildren = this.HasChildren, IsProbability100 = this.IsProbability100, PartsCount = this.PartsCount, Parts = this.Parts }; } public ProjectModel() { Parts = new List(); } public IEnumerable Validate(ValidationContext validationContext) { if (HasChildren && Parts != null && Parts.Count > 0) { for (var i = 0; i < Parts.Count;i++) { if (string.IsNullOrEmpty(Parts[i].Name)) yield return new ValidationResult(string.Format(Constants.ERROR_TEMPLATE_REQUIRED, "Part " + (i+1) + " Name"), null); } } } } public class ProjectPartModel : IBaseModel { public Guid Id { get; set; } //[Required] [MaxLength(100, ErrorMessage = "Name should not exceed 100 characters")] [Display(Name = "Name")] public string Name { get; set; } [Required] [Display(Name = "Status")] public Guid StatusId { get; set; } [Display(Name = "Status")] public string StatusName { get; set; } [Required] [Display(Name = "Type")] public Guid TypeId { get; set; } [Display(Name = "Type")] public string TypeName { get; set; } [Required] [Display(Name = "Client")] public Guid? ClientId { get; set; } [Display(Name = "Client")] public string ClientName { get; set; } [Display(Name = "Details")] [MaxLength(2000)] public string Details { get; set; } [Display(Name = "Priority")] [Range(1, 25)] public int Priority { get; set; } [Display(Name = "Probability")] [Range(0, 100)] [UIHint("Slider")] public short Probability { get; set; } [Display(Name = "Revenue Generating")] public bool IsRevenueGenerating { get; set; } [Display(Name = "Deadline")] [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] public DateTime? Deadline { get; set; } [Display(Name = "Teams")] public List AssignedTeams { get; set; } [Display(Name = "Internal Contacts")] public List InternalContacts { get; set; } [Display(Name = "External Contacts")] public List ExternalContacts { get; set; } public bool IsProbability100 { get; set; } public Guid? ParentProjectId { get; set; } public bool DeletedPart { get; set; } public bool SaveAsCopy { get; set; } public Guid? CompanyId { get; set; } public Guid? OldId { get; set; } /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator ProjectPartModel(Project obj) { if (obj == null) return null; var model = new ProjectPartModel { Id = obj.Id, Name = obj.Name, StatusId = obj.StatusId, StatusName = obj.Status != null ? obj.Status.Name : string.Empty, TypeId = obj.TypeId, TypeName = obj.Type != null ? obj.Type.Name : string.Empty, ClientId = obj.ClientId, ClientName = obj.Client != null ? obj.Client.Name : string.Empty, Details = obj.Details, Priority = obj.Priority, IsRevenueGenerating = obj.IsRevenueGenerating, Deadline = obj.Deadline, Probability = Convert.ToInt16(obj.Probability * 100), IsProbability100 = obj.Status != null && obj.Status.Probability100, ParentProjectId = obj.ParentProjectId }; model.AssignedTeams = obj.Team2Project.Select(t2p => t2p.TeamId).ToList(); var contacts = obj.Contact2Project.Select(c2p => c2p.Contact); model.InternalContacts = contacts.Where(c => c.Type == ContactType.CompanyContact.GetHashCode()).Select(c => c.Id).ToList(); model.ExternalContacts = contacts.Where(c => c.Type == ContactType.ClientContact.GetHashCode()).Select(c => c.Id).ToList(); model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(Project dbObj) { if (dbObj == null) throw new ArgumentNullException(); dbObj.Name = Name; //dbObj.ProjectNumber = Number; dbObj.StatusId = StatusId; dbObj.TypeId = TypeId; dbObj.ClientId = ClientId; //dbObj.CompanyId = CompanyId; dbObj.Details = Details; //dbObj.Color = Color != null ? Color.Replace("#", "") : Color; dbObj.Priority = Priority; dbObj.Probability = Probability / 100.0M; dbObj.IsRevenueGenerating = IsRevenueGenerating; dbObj.Deadline = Deadline; dbObj.ParentProjectId = ParentProjectId; //dbObj.HasChildren = HasChildren; } public ProjectPartModel Clone() { return new ProjectPartModel { Id = this.Id, ClientId = this.ClientId, ClientName = this.ClientName, Details = this.Details, IsRevenueGenerating = this.IsRevenueGenerating, Deadline = this.Deadline, Name = this.Name, Priority = this.Priority, Probability = this.Probability, StatusId = this.StatusId, StatusName = this.StatusName, TypeId = this.TypeId, TypeName = this.TypeName, AssignedTeams = this.AssignedTeams, InternalContacts = this.InternalContacts, ExternalContacts = this.ExternalContacts, DeletedPart = this.DeletedPart, IsProbability100 = this.IsProbability100, OldId = this.Id , ParentProjectId = this.ParentProjectId }; } } /// /// An UI representation of project to be displayed as list items /// public class ProjectListModel { public Guid Id { get; set; } public string ProjectName { get; set; } public string ProjectNumber { get; set; } public string Status { get; set; } public int Priority { get; set; } public short Probability { get; set; } public string Classification { get; set; } public string Client { get; set; } public string Company { get; set; } public IEnumerable Teams { get; set; } public int ScenariosCount { get; set; } public bool WritePermissionEnabledForCurrentUser { get; set; } public string ActiveScenario { get; set; } public IEnumerable InactiveScenarios { get; set; } public bool HasChildren { get; set; } public List ProjectParts { get; set; } } public class ProjectPartListModel : ProjectListModel { } }