using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using EnVisage.Models; namespace EnVisage.Code.BLL { public class CompanyManager : ManagerBase { public CompanyManager(EnVisageEntities dbContext) : base(dbContext) { } protected override Company InitInstance() { return new Company { Id = Guid.NewGuid() }; } protected override Company RetrieveReadOnlyById(Guid key) { return DataTable.AsNoTracking().FirstOrDefault(t => t.Id == key); } public override DbSet DataTable { get { return DbContext.Companies; } } #region Public Methods /// /// Loads a first Company with parentId == null. /// /// Indicates that object will not be saved later in the code. Use false if you need to save an updated object. /// A object. public Company LoadParent(bool isReadOnly = true) { var parent = DbContext.Companies.AsNoTracking().FirstOrDefault(t => t.ParentCompanyId == null); if (parent == null) throw new BLLException("There is no Parent Company in the database. It should be created on application start.", false); return parent; } public CompanyModel LoadWithChildCollections(Guid? value, bool isReadOnly = true) { CompanyModel result = (CompanyModel)base.Load(value, isReadOnly); if (value.HasValue) { var clients = DbContext.Company2Client.Where(x => x.CompanyId == value.Value).ToList(); result.ClientId = clients.Select(x => x.ClientId).ToList(); result.Client = clients.Select(x => x.Client.Name).ToList(); var company2Views = DbContext.Company2View.Where(x => x.CompanyId == value.Value).ToList(); result.ViewId = company2Views.Select(x => x.ViewId.Value).ToList(); result.ViewName = company2Views.Select(x => x.View.Name).ToList(); } return result; } public override Company Save(CompanyModel model) { Company company = null; if (Guid.Empty.Equals(model.Id)) { company = base.Save(model); if (model.ClientId != null) { foreach (var clientId in model.ClientId) { var client = DbContext.Company2Client.Create(); client.Id = Guid.NewGuid(); client.ClientId = clientId; client.CompanyId = company.Id; DbContext.Company2Client.Add(client); } } if (model.ViewId != null) { foreach (var viewId in model.ViewId) { var company2View = DbContext.Company2View.Create(); company2View.Id = Guid.NewGuid(); company2View.ViewId = viewId; company2View.CompanyId = company.Id; DbContext.Company2View.Add(company2View); } } } else { company = base.Save(model); var clients = DbContext.Company2Client.Where(x => x.CompanyId == model.Id).ToList(); clients.Where(x => model.ClientId == null || !model.ClientId.Contains(x.ClientId)).ToList(). ForEach(x => DbContext.Entry(x).State = EntityState.Deleted); var company2Views = DbContext.Company2View.Where(x => x.CompanyId == model.Id).ToList(); company2Views.Where(x => model.ViewId == null || !model.ViewId.Contains(x.ViewId.Value)).ToList(). ForEach(x => DbContext.Entry(x).State = EntityState.Deleted); if (model.ClientId != null) { foreach (var clientId in model.ClientId.Where(c => !clients.Select(x => x.ClientId).Contains(c))) { var client = DbContext.Company2Client.Create(); client.Id = Guid.NewGuid(); client.ClientId = clientId; client.CompanyId = company.Id; DbContext.Company2Client.Add(client); } } if (model.ViewId != null) { foreach (var viewId in model.ViewId.Where(c => !company2Views.Select(x => x.ViewId).Contains(c))) { var company2View = DbContext.Company2View.Create(); company2View.Id = Guid.NewGuid(); company2View.ViewId = viewId; company2View.CompanyId = model.Id; DbContext.Company2View.Add(company2View); } } } return company; } #endregion } }