EnVisageOnline/Main/Source/EnVisage/Code/BLL/NoteManager.cs

504 lines
12 KiB
C#

using EnVisage.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using Prevu.Core.Audit.Model;
using static EnVisage.Code.AuditProxy;
namespace EnVisage.Code.BLL
{
public class NoteManager : ManagerBase<Note, NoteModel>
{
public NoteManager() : base(null)
{
}
public NoteManager(EnVisageEntities dbContext) : base(dbContext)
{
}
protected override Note InitInstance()
{
return new Note { Id = Guid.NewGuid() };
}
protected override Note RetrieveReadOnlyById(Guid key)
{
return DataTable.AsNoTracking().FirstOrDefault(t => t.Id == key);
}
public override DbSet<Note> DataTable => DbContext.Notes;
public override Note Save(NoteModel model)
{
var isNew = false;
Note oldNote = null;
if (model == null)
throw new ArgumentNullException(nameof(model));
#region Save Rate data
Note dbObj = null;
if (model.Id != Guid.Empty)
{
dbObj = DataTable.Find(model.Id);
}
if (dbObj == null)
{
isNew = true;
dbObj = InitInstance();
}
if (isNew)
{
model.CopyTo(dbObj);
DataTable.Add(dbObj);
}
else
{
oldNote = Load(dbObj.Id);
model.CopyTo(dbObj);
DbContext.Entry(dbObj).State = EntityState.Modified;
}
#endregion
if (IsContextLocal)
{
DbContext.SaveChanges();
LogNoteEvents(dbObj, oldNote, null, null);
}
return dbObj;
}
public void MoveNotesDomain(Guid oldDomainId, Guid newDomainId)
{
var notes = DataTable.Where(x => x.DomainId == oldDomainId).ToList();
foreach (var note in notes)
{
note.DomainId = newDomainId;
DbContext.Entry(note).State = EntityState.Modified;
}
if (IsContextLocal)
DbContext.SaveChanges();
}
public void CloneNotesDomain(Guid oldDomainId, Guid newDomainId)
{
var notes = DbContext.Notes.Where(x => x.DomainId == oldDomainId).ToList();
var notesForSave = new List<Note>();
foreach (var n in notes)
{
var note = new Note
{
Id = Guid.NewGuid(),
ParentId = n.ParentId,
DomainId = newDomainId,
DateAdded = DateTime.Now,
NoteDetail = n.NoteDetail,
NoteType = n.NoteType,
Title = n.Title,
SystemAttributeId = n.SystemAttributeId,
UserId = n.UserId
};
notesForSave.Add(note);
}
DataTable.AddRange(notesForSave);
if (IsContextLocal)
{
DbContext.SaveChanges();
LogNotesCreateEvents(notesForSave, null, null);
}
}
public void RemoveNotesForDomain(Guid domainId)
{
var notes = DataTable.Where(x => x.DomainId == domainId).ToList();
foreach (var n in notes)
{
DbContext.Notes.Remove(n);
}
if (IsContextLocal)
{
DbContext.SaveChanges();
LogNotesDeleteEvent(notes, null, null);
}
}
public void RemoveNotes(Guid domainId, Guid parentId)
{
var notes = DataTable.Where(x => x.DomainId == domainId && x.ParentId == parentId).ToList();
foreach (var n in notes)
{
DbContext.Notes.Remove(n);
}
if (IsContextLocal)
{
DbContext.SaveChanges();
LogNotesDeleteEvent(notes, null, null);
}
}
public void RemoveNotesForParent(Guid parentId)
{
var notes = DbContext.Notes.Where(x => x.ParentId == parentId).ToList();
foreach (var n in notes)
{
DbContext.Notes.Remove(n);
}
if (IsContextLocal)
{
DbContext.SaveChanges();
LogNotesDeleteEvent(notes, null, null);
}
}
public void CloneNotesParent(Guid oldParentId, Guid newParentId)
{
var notes = DbContext.Notes.Where(x => x.ParentId == oldParentId).ToList();
var notesForSave = new List<Note>();
foreach (var n in notes)
{
var note = new Note
{
Id = Guid.NewGuid(),
ParentId = newParentId,
DomainId = n.DomainId,
DateAdded = DateTime.Now,
NoteDetail = n.NoteDetail,
NoteType = n.NoteType,
Title = n.Title,
SystemAttributeId = n.SystemAttributeId,
UserId = n.UserId
};
notesForSave.Add(note);
}
if (IsContextLocal)
{
DbContext.SaveChanges();
LogNotesCreateEvents(notesForSave, null, null);
}
}
public void MoveNotesParent(Guid oldParentId, Guid newParent)
{
var notes = DataTable.Where(x => x.ParentId == oldParentId).ToList();
foreach (var note in notes)
{
note.ParentId = newParent;
DbContext.Entry(note).State = EntityState.Modified;
}
if (IsContextLocal)
DbContext.SaveChanges();
//todo: add log event ?
}
public void Delete(Guid id)
{
Note dbObj = Load(id, false);
if (dbObj != null)
DbContext.Notes.Remove(dbObj);
if (IsContextLocal)
{
DbContext.SaveChanges();
LogNoteEvents(null, dbObj, null, null);
}
}
public List<NoteModel> GetNotes(Guid? domainId, Guid parentId, int t, bool withChildren)
{
List<NoteModel> noteModelList = new List<NoteModel>();
var notes = DataTable.Where(x => x.NoteType == t && x.ParentId == parentId &&
(domainId.HasValue && domainId.Value != Guid.Empty
? x.DomainId == domainId
: !x.DomainId.HasValue));
var authorIds = notes.Where(p => p.UserId.HasValue).Select(p => p.UserId.ToString()).Distinct().ToList();
var authors = DbContext.AspNetUsers.Where(p => authorIds.Contains(p.Id)).ToDictionary(p => p.Id);
foreach (var n in notes)
{
noteModelList.Add(new NoteModel
{
Id = n.Id,
Author = n.UserId.HasValue && authors.ContainsKey(n.UserId.Value.ToString()) ? authors[n.UserId.Value.ToString()] : null,
//child notes will tie directly to the note id of the parent and will not need domain id filled in
ChildNotes = withChildren ? GetNotes(null, n.Id, t, true) : new List<NoteModel>(),
DateAdded = n.DateAdded ?? DateTime.Now,
Title = n.Title,
ParentId = n.ParentId,
Details = n.NoteDetail,
DomainId = n.DomainId,
NoteType = n.NoteType ?? -1
});
}
return noteModelList;
}
#region Log Methods
private readonly Func<Note, string> _noteValue = r => $"{r.Title} <br /> {r.NoteDetail}";
public void LogNotesDeleteEvent(IEnumerable<Note> notes, string userId, string transactionId)
{
transactionId = Utils.GetOrCreateTransactionId(DbContext, transactionId);
foreach (var rate in notes)
{
LogNoteEvents(null, rate, userId, transactionId);
}
}
public void LogNotesCreateEvents(IEnumerable<Note> notes, string userId, string transactionId)
{
transactionId = Utils.GetOrCreateTransactionId(DbContext, transactionId);
foreach (var rate in notes)
{
LogNoteEvents(rate, null, userId, transactionId);
}
}
public void LogNoteEvents(Note note, Note oldNote, string userId, string transactionId)
{
EventRequestBaseModel eventTosave = null;
transactionId = Utils.GetOrCreateTransactionId(DbContext, transactionId);
if (string.IsNullOrEmpty(userId))
userId = SecurityManager.GetUserPrincipal().ToString();
// Note for Scenario
if ((note != null && note.NoteType == NoteType.Scenario.GetHashCode()) ||
(oldNote != null && oldNote.NoteType == NoteType.Scenario.GetHashCode()))
{
eventTosave = GetEventForScenarioNote(note, oldNote, userId);
}
// Note for Project
if ((note != null && note.NoteType == NoteType.Project.GetHashCode()) ||
(oldNote != null && oldNote.NoteType == NoteType.Project.GetHashCode()))
{
eventTosave = GetEventForProjectNote(note, oldNote, userId);
}
// Note for row in Scenario Details Grid
if ((note != null && note.NoteType == NoteType.ExpenditureCategory.GetHashCode()) ||
(oldNote != null && oldNote.NoteType == NoteType.ExpenditureCategory.GetHashCode()))
{
eventTosave = GetEventForExpenditureCategoryNote(note, oldNote, userId);
}
if (eventTosave != null)
{
LogEvent(transactionId, eventTosave);
Task.Run(() => CommitEventChanges(transactionId));
}
}
protected EventRequestBaseModel GetEventForScenarioNote(Note note, Note oldNote, string userId)
{
EventRequestBaseModel result = null;
Guid? scenarioId = null;
Guid? projectId = null;
var parentid = note?.ParentId ?? oldNote?.ParentId;
if (parentid != Guid.Empty)
{
Scenario scenario;
using (var scenarioManager = new ScenarioManager(DbContext))
{
scenario = scenarioManager.Load(parentid);
}
if (scenario != null)
{
scenarioId = scenario.Id;
projectId = scenario.ParentId;
}
}
if (note != null && oldNote == null)
{
#region Note Created
result = new EventRequestBaseModel
{
EntityId = note.Id,
ProjectId = projectId,
ScenarioId = scenarioId,
ClassificationKey = "ScenarioNoteAdd",
NewValue = _noteValue(note),
UserId = userId
};
#endregion
}
if (oldNote != null)
{
if (note != null)
result = new EventRequestBaseModel
{
EntityId = note.Id,
ProjectId = projectId,
ScenarioId = scenarioId,
ClassificationKey = "ScenarioNoteEdit",
UserId = userId,
NewValue = _noteValue(note),
OldValue = _noteValue(oldNote)
};
else
result = new EventRequestBaseModel
{
EntityId = oldNote.Id,
ProjectId = projectId,
ScenarioId = scenarioId,
ClassificationKey = "ScenarioNoteDelete",
UserId = userId,
OldValue = _noteValue(oldNote)
};
}
return result;
}
protected EventRequestBaseModel GetEventForProjectNote(Note note, Note oldNote, string userId)
{
EventRequestBaseModel result = null;
Guid? projectId = null;
var parentid = note?.ParentId ?? oldNote?.ParentId;
if (parentid != Guid.Empty)
projectId = parentid;
if (note != null && oldNote == null)
{
#region Note Created
result = new EventRequestBaseModel
{
EntityId = note.Id,
ProjectId = projectId,
ClassificationKey = "ProjectNotesAdd",
NewValue = _noteValue(note),
UserId = userId
};
#endregion
}
if (oldNote != null)
{
if (note != null)
result = new EventRequestBaseModel
{
EntityId = note.Id,
ProjectId = projectId,
ClassificationKey = "ProjectNotesChange",
UserId = userId,
NewValue = _noteValue(note),
OldValue = _noteValue(oldNote)
};
else
result = new EventRequestBaseModel
{
EntityId = oldNote.Id,
ProjectId = projectId,
ClassificationKey = "ProjectNotesDelete",
UserId = userId,
OldValue = _noteValue(oldNote)
};
}
return result;
}
protected EventRequestBaseModel GetEventForExpenditureCategoryNote(Note note, Note oldNote, string userId)
{
EventRequestBaseModel result = null;
Guid? scenarioId = null;
Guid? projectId = null;
var parentid = note?.DomainId ?? oldNote?.DomainId;
var expCatId = note?.ParentId ?? oldNote?.ParentId;
var expCatName = String.Empty;
// Get related Scenario and Project
if (parentid.HasValue && (parentid.Value != Guid.Empty))
{
Scenario scenario;
using (var scenarioManager = new ScenarioManager(DbContext))
{
scenario = scenarioManager.Load(parentid);
}
if (scenario != null)
{
scenarioId = scenario.Id;
projectId = scenario.ParentId;
}
}
// Get parent EC Name
if (expCatId.HasValue && expCatId.Value != Guid.Empty)
{
using (var expCatMngr = new ExpenditureCategoryManager(DbContext))
{
var expCatInfo = expCatMngr.GetExpenditureDetails(expCatId.Value);
expCatName = expCatInfo != null ? expCatInfo.ExpenditureCategoryName : String.Empty;
}
}
if (note != null && oldNote == null)
{
#region Note Created
result = new EventRequestBaseModel
{
EntityId = note.Id,
ProjectId = projectId,
ScenarioId = scenarioId,
ClassificationKey = "ScenarioDetailsNoteAdd",
NewValue = _noteValue(note),
UserId = userId,
Comment = $"Expenditure Category: '{expCatName}'"
};
#endregion
}
if (oldNote != null)
{
if (note != null)
result = new EventRequestBaseModel
{
EntityId = note.Id,
ProjectId = projectId,
ScenarioId = scenarioId,
ClassificationKey = "ScenarioDetailsNoteEdit",
UserId = userId,
NewValue = _noteValue(note),
OldValue = _noteValue(oldNote),
Comment = $"Expenditure Category: '{expCatName}'"
};
else
result = new EventRequestBaseModel
{
EntityId = oldNote.Id,
ProjectId = projectId,
ScenarioId = scenarioId,
ClassificationKey = "ScenarioDetailsNoteDelete",
UserId = userId,
OldValue = _noteValue(oldNote),
Comment = $"Expenditure Category: '{expCatName}'"
};
}
return result;
}
#endregion
}
}