EnVisageOnline/Main-RMO/Source/EnVisage/Models/CompanyModel.cs

94 lines
3.2 KiB
C#

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<Company>
{
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<ClientModel> Clients
{
get
{
List<ClientModel> result = new List<ClientModel>();
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<Guid> ClientId { get; set; }
public IList<string> Client { get; set; }
[Display(Name = "Views")]
public List<ViewModel> Views
{
get
{
List<ViewModel> result = new List<ViewModel>();
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<Guid> ViewId { get; set; }
public IList<string> ViewName { get; set; }
/// <summary>
/// Casts a <see cref="Company"/> obect to the object of type <see cref="CompanyModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Company"/> object.</param>
/// <returns>A <see cref="CompanyModel"/> object filled with data from db.</returns>
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;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(Company dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Name = Name;
dbObj.ParentCompanyId = ParentCompanyId;
}
}
}