using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using EnVisage.Code; namespace EnVisage.Models { public class CompanyModel : IBaseModel { public Guid Id { get; set; } [Required] [MaxLength(100)] public string Name { get; set; } [Display(Name = "Parent Company")] public Guid? ParentCompanyId { get; set; } [Display(Name = "Parent Company")] public string ParentCompanyName { get; set; } [Display(Name = "Number of Projects")] public int ProjectsCount { get; set; } [Display(Name = "Number of Companies")] public int CompaniesCount { get; set; } public List Clients { get { List result = new List(); EnVisageEntities DbContext = new EnVisageEntities(); var clients = (from c in DbContext.Clients select c).ToList(); foreach (var client in clients) result.Add((ClientModel)client); return result.OrderBy(x => x.Name).ToList(); } set { } } public IList ClientId { get; set; } public IList Client { get; set; } [Display(Name = "Views")] public List Views { get { List result = new List(); EnVisageEntities DbContext = new EnVisageEntities(); var views = (from c in DbContext.Views select c).ToList(); foreach (var view in views) result.Add((ViewModel)view); return result.OrderBy(x => x.Name).ToList(); } set { } } public IList ViewId { get; set; } public IList ViewName { get; set; } /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator CompanyModel(Company obj) { if (obj == null) return null; var model = new CompanyModel { Id = obj.Id, Name = obj.Name, ProjectsCount = obj.Projects.Count, CompaniesCount = obj.Company1.Count, ParentCompanyName = obj.Company2 == null ? string.Empty : obj.Company2.Name, ParentCompanyId = obj.ParentCompanyId }; model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(Company dbObj) { if (dbObj == null) throw new ArgumentNullException(); dbObj.Name = Name; dbObj.ParentCompanyId = ParentCompanyId; } } }