using System;
using System.Collections.Generic;
using System.Linq;
using Neo.Afx.ComponentModel;
using Neo.Afx.Services.Audits.Entities;
using System.Reflection;
namespace Neo.Afx.Services.Audits
{
///
/// Audit database context container
///
public class AuditsDatabase : Database
{
///
/// Initializes a new instance of the class.
///
public AuditsDatabase()
: base(new AuditContext())
{
Context.Configuration.AutoDetectChangesEnabled = false;
Context.Configuration.ValidateOnSaveEnabled = false;
}
#region GetAudits
///
/// Gets the audits.
///
/// The entity ID.
/// The item ID.
/// The source ID.
/// The action ID.
/// The order.
///
public IEnumerable GetAudits(int entityID, long itemID, short? sourceID, short? actionID, string order)
{
if (String.IsNullOrEmpty(order) || order.ToLower() != "asc" || order.ToLower() != "desc")
{
order = "asc";
}
var results = (from q in Context.Vw_Audits
where q.EntityID == entityID && q.ItemID == itemID
select q).ToList();
if (sourceID.HasValue && sourceID.Value != -1)
{
results = results.Where(a => a.SourceID == sourceID.Value).ToList();
}
if (actionID.HasValue && actionID.Value != -1)
{
results = results.Where(a => a.ActionID == actionID.Value).ToList();
}
return order == "asc" ? results.OrderBy(a => a.AuditDate) : results.OrderByDescending(a => a.AuditDate);
}
#endregion
#region WriteEntry(int userID, string source, string action, string description, bool isCritical, bool isException, int entityID, long? itemID)
///
/// Writes an entry to the audit log
///
public void WriteEntry(long userID, string source, string action, string description, bool isCritical, bool isException, int entityID, long? itemID)
{
Context.Pr_WriteEntryByID(userID, source, action, description, isCritical, isException, entityID, itemID);
}
#endregion
#region WriteEntries
///
/// Writes mutliple entries to the audit trail
///
///
///
///
///
///
///
public void WriteEntries(short sourceID, short actionID, List changes, int entityID, long itemID, long createdByID)
{
foreach (var change in changes)
{
Context.History.Add(new History()
{
ActionID = actionID,
CreatedByID = createdByID,
CreatedDate = DateTime.Now,
Description = change,
EntityID = entityID,
ItemID = itemID,
SourceID = sourceID
});
}
Context.SaveChanges();
}
#endregion
#region GetEntityDifferences
///
/// Comparexs two objects and returns the differences
///
///
///
///
///
///
public IEnumerable GetEntityDifferences(T obj1, T obj2, string idPropertyName)
{
var type = obj1.GetType().ToString();
var idValue = typeof(T).GetProperty(idPropertyName).GetValue(obj1, null);
PropertyInfo[] properties = typeof(T).GetProperties();
List changes = new List();
if (type.Contains("."))
{
type = type.Substring(type.LastIndexOf(".") + 1);
}
if (idValue == null)
{
idValue = "-1";
}
foreach (PropertyInfo pi in properties)
{
object value1 = typeof(T).GetProperty(pi.Name).GetValue(obj1, null);
object value2 = typeof(T).GetProperty(pi.Name).GetValue(obj2, null);
if (pi.PropertyType == typeof(string) || pi.PropertyType.IsValueType)
{
if (value1 != value2 && (value1 == null || !value1.Equals(value2)))
{
if (value1 == null)
{
value1 = "nothing";
}
if (value2 == null)
{
value2 = "nothing";
}
changes.Add(string.Format("{0}, ID {1}, Property {2} changed from {3} to {4}", type, idValue, pi.Name, value1, value2));
}
}
}
return changes;
}
#endregion
}
}