Untitled
unknown
plain_text
2 years ago
4.6 kB
7
Indexable
using Inaspect.Data.Model.MockInspectionV2.Logs;
using Inaspect.Data.Repository.Repositories.MockInspectionV2;
using Inaspect.Data.Service.Helpers.Miv2Logs;
using MethodDecorator.Fody.Interfaces;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Http;
using static Inaspect.Data.Service.Common.Enumerators;
[module: LogInterceptor]
namespace Inaspect.Data.Service.Helpers.Miv2Logs
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor
| AttributeTargets.Assembly | AttributeTargets.Module)]
public class LogInterceptor : Attribute, IMethodDecorator
{
public Miv2Area Area { get; set; }
public Miv2Operation Operation { get; set; }
public QualityStatementLogModel OldQualityStatement { get; set; }
public PreliminaryQuestionLogModel OldPreliminaryQuestionObject { get; set; }
public int UserId { get; set; }
public int Miv2InstanceId { get; set; }
private object[] objectArgs;
readonly IMiv2LogRepository miv2LogRepository = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IMiv2LogRepository)) as IMiv2LogRepository;
public void Init(object instance, MethodBase method, object[] args)
{
objectArgs = args;
}
public void OnEntry()
{
int areaId = GetAreaId();
switch (Area)
{
case Miv2Area.QualityStatement:
if(areaId != 0)
{
var qualityStatementModel = miv2LogRepository.GetQualityStatementLogModel(areaId);
OldQualityStatement = qualityStatementModel;
}
break;
//case Miv2Area.PreliminaryQuestions:
// object preliminaryQuestions = objectArgs[0].GetType().GetProperty("PreliminaryQuestions").GetValue(objectArgs[0], null);
// object preliminaryQuestionsIds = preliminaryQuestions.GetType().GetProperty("Id").GetValue(preliminaryQuestions, null);
// UserId = (int)objectArgs[0].GetType().GetProperty("UserId").GetValue(objectArgs[0], null);
// Miv2InstanceId = (int)objectArgs[0].GetType().GetProperty("Miv2InstanceId").GetValue(objectArgs[0], null);
// var preliminaryQuestionModel = miv2LogRepository.GetQualityStatementLogModel((int)preliminaryQuestionsIds);
// break;
default:
break;
}
}
public void OnException(Exception exception)
{
}
public void OnExit()
{
int areaId = GetAreaId();
switch (Area)
{
case Miv2Area.QualityStatement:
if(areaId != 0)
{
var newQualityStatement = miv2LogRepository.GetQualityStatementLogModel(areaId);
miv2LogRepository.InsertLog(GetLogMessages(OldQualityStatement, newQualityStatement, Operation, UserId, Miv2InstanceId));
}
break;
default:
break;
}
}
private int GetAreaId()
{
int id = 0;
if (objectArgs[0] != null)
{
id = int.TryParse(objectArgs[0].GetType().GetProperty(Area.GetDescription()).GetValue(objectArgs[0], null).ToString(), out id) ? id : 0;
SetUserId();
SetMiv2InstanceId();
}
return id;
}
private void SetMiv2InstanceId()
{
int miv2InstanceId = int.TryParse(objectArgs[0].GetType().GetProperty("Miv2InstanceId").GetValue(objectArgs[0], null).ToString(), out miv2InstanceId) ? miv2InstanceId : 0;
Miv2InstanceId = miv2InstanceId;
}
private void SetUserId()
{
int userId = int.TryParse(objectArgs[0].GetType().GetProperty("UserId").GetValue(objectArgs[0], null).ToString(), out userId) ? userId : 0;
UserId = userId;
}
private List<LogMessage> GetLogMessages<T>(T oldObject, T newObject, Miv2Operation operation, int userId, int miv2InstanceId)
{
List<LogMessage> logMessages = oldObject.DetailedCompare(newObject, operation, userId, miv2InstanceId);
return logMessages;
}
}
}
Editor is loading...