using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EgtBEAMWALL.DataLayer.DatabaseModels;
namespace EgtBEAMWALL.DataLayer.Controllers
{
public class ProjController : IDisposable
{
#region Private Fields
private DatabaseContext dbCtx;
#endregion Private Fields
#region Public Constructors
public ProjController()
{
// Initialize database context
dbCtx = new DatabaseContext();
}
#endregion Public Constructors
#region Protected Methods
///
/// Get LAST paginated data from DB (DESC ordered)
///
///
///
protected List GetLastDbModelDesc(int numRecord)
{
// se numRecord = 0 --> passo tutti
if (numRecord == 0)
{
numRecord = dbCtx.ProjList.Count();
}
// retrieve
return dbCtx
.ProjList
.OrderByDescending(x => x.ProjId)
.Take(numRecord)
.ToList();
}
#endregion Protected Methods
#region Public Methods
public bool DeleteProj(int ProjId)
{
bool done = false;
var currProj = FindByProjId(ProjId);
// sel delle BTLParts da proj
var parts2del = dbCtx
.BTLPartList
.Where(x => x.ProjDbId == currProj.ProjDbId);
try
{
// remove from database
dbCtx.BTLPartList.RemoveRange(parts2del);
dbCtx.ProjList.Remove(currProj);
// Commit changes
dbCtx.SaveChanges();
done = true;
}
catch
{ }
return done;
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
///
/// Get record by ProjDbId
///
///
///
public ProjModel FindByProjDbId(int ProjDbId)
{
return dbCtx
.ProjList
.Where(x => x.ProjDbId == ProjDbId)
.SingleOrDefault();
}
///
/// Get record by ProjId
///
///
///
public ProjModel FindByProjId(int ProjId)
{
return dbCtx
.ProjList
.Where(x => x.ProjId == ProjId)
.SingleOrDefault();
}
#if false
///
/// Get paginated data from DB (ASC ordered)
///
///
///
///
public List GetPaginatedAsc(int ProjDbIdStart, int numRecord)
{
int numEnd = ProjDbIdStart - numRecord;
// check numEnd
if (numEnd < 0)
numEnd = 0;
// retrieve
return dbCtx
.ProjList
.Where(x => x.ProjDbId <= ProjDbIdStart)
.OrderBy(x => x.ProjDbId)
.Take(numRecord)
.ToList();
}
///
/// Get paginated data from DB (DESC ordered)
///
///
///
///
public List GetPaginatedDesc(int PartDbIdStart, int numRecord)
{
int numEnd = PartDbIdStart - numRecord;
// check numEnd
if (numEnd < 0)
numEnd = 0;
// retrieve
return dbCtx
.ProjList
.Where(x => x.ProjDbId <= PartDbIdStart)
.OrderByDescending(x => x.ProjDbId)
.Take(numRecord)
.ToList();
}
#endif
///
/// Get filtered data by ProjectId (ASC ordered)
///
///
///
public List GetByProdAsc(int ProdDbId)
{
// retrieve
return dbCtx
.ProjList
.Where(x => x.ProdDbId == ProdDbId)
.OrderBy(x => x.ProdDbId)
.ToList();
}
///
/// Get filtered data by ProjectId (DESC ordered)
///
///
///
public List GetByProdDesc(int ProdDbId)
{
// retrieve
return dbCtx
.ProjList
.Where(x => x.ProdDbId == ProdDbId)
.OrderByDescending(x => x.ProdDbId)
.ToList();
}
///
/// Elenco progetti
///
///
///
public List GetLastDesc(int numRecord)
{
List result = new List();
var dbResult = GetLastDbModelDesc(numRecord);
// conversione
result = dbResult.Select(x => new Core.ProjectFile(Core.ConstBeam.ProjectType.PROJ, x.ProjId, x.ProdDbId == null ? 0 : (int)x.ProdDbId, x.BTLFileName)).ToList();
return result;
}
///
/// Fornisce nuovo indice VUOTO da usare (allocando sul DB)
///
///
public int GetNextIndex()
{
int nextId = 0;
// cerco se ne ho ALMENO 1....
var numRec = dbCtx.ProjList.Count();
if (numRec == 0)
{
nextId = 1;
}
else
{
// retrieve ultimo...
var maxRecord = dbCtx
.ProjList
.OrderByDescending(x => x.ProjId)
.Take(1)
.FirstOrDefault();
// incremento
nextId = maxRecord.ProjId + 1;
}
// creo nuovo...
var newRec = dbCtx
.ProjList
.Add(new ProjModel() { ProjId = nextId, BTLFileName = "", Locked = true });
// Commit changes
dbCtx.SaveChanges();
return nextId;
}
///
/// Manage Lock by ProjId (proj & prod)
///
/// ProjID
/// Stato Lock da impostare
///
public ProjModel LockByProjId(int ProjId, bool Locked)
{
var currProj = dbCtx
.ProjList
.Where(x => x.ProjId == ProjId)
.SingleOrDefault();
// aggiorno stato
currProj.Locked = Locked;
// seleziono il prod...
var currProd = dbCtx
.ProdList
.Where(x => x.ProdDbId == currProj.ProdDbId)
.SingleOrDefault();
currProd.Locked = Locked;
dbCtx.SaveChanges();
return currProj;
}
///
/// Update record su DB x elenco BTLParts
///
///
///
///
public ProjModel UpdateBtlParts(int ProjId, List BtlPartList)
{
var currData = FindByProjId(ProjId);
// sel delle BTLParts da proj
var items2del = dbCtx
.BTLPartList
.Where(x => x.ProjDbId == currData.ProjDbId);
// converto le BtlParts
var items2add = BtlPartList.Select(x => BTLPartController.ConvertFromCore(x, currData.ProjDbId)).ToList();
try
{
// elimino dal DB
dbCtx.BTLPartList.RemoveRange(items2del);
// aggiungo le nuove
dbCtx.BTLPartList.AddRange(items2add);
}
catch
{ }
// aggiorno valore isNew a false
currData.IsNew = false;
// Commit changes
dbCtx.SaveChanges();
return currData;
}
///
/// Update record su DB x nome
///
///
///
///
public ProjModel UpdateName(int ProjId, string newBTLFileName)
{
var currData = FindByProjId(ProjId);
// aggiorno valore BTL
currData.BTLFileName = newBTLFileName;
// Commit changes
dbCtx.SaveChanges();
return currData;
}
#endregion Public Methods
}
}