135 lines
5.3 KiB
C#
135 lines
5.3 KiB
C#
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<Company, CompanyModel>
|
|
{
|
|
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<Company> DataTable
|
|
{
|
|
get
|
|
{
|
|
return DbContext.Companies;
|
|
}
|
|
}
|
|
|
|
#region Public Methods
|
|
/// <summary>
|
|
/// Loads a first Company with parentId == null.
|
|
/// </summary>
|
|
/// <param name="isReadOnly">Indicates that object will not be saved later in the code. Use <b>false</b> if you need to save an updated object.</param>
|
|
/// <returns>A <see cref="Company"/> object.</returns>
|
|
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
|
|
}
|
|
} |