NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text.RegularExpressions;
using J2BI.Core.Data;
using J2BI.Cross.BTS2.Common.Enums;
using J2BI.Core.Extensions;
using J2BI.Cross.BTS2.DataAccess;
using J2BI.Cross.BTS2.Entities;
using J2BI.Cross.BTS2.Models;
using J2BI.Cross.BTS2.Services.Interfaces;
using log4net;
using Microsoft.Practices.Unity;
using Currency = J2BI.Cross.BTS2.Common.Enums.Currency;
using RequestStatus = J2BI.Cross.BTS2.Common.Enums.RequestStatus;

namespace J2BI.Cross.BTS2.Services
{
public class AuditedBusinessTravelContext : Jet2BusinessTravelContext
{
//private readonly Lazy<IAuditTrailService> _auditTrailService;
private readonly List<string> _ignoredRequestPropertiesList = new List<string>()
{
"Guid",
"SubmittedDateTime",
"ModifiedDateTime",
"StartDate",
"EndDate",
"Reference",
//"NumberOfTbaTravellers",
"NumberOfTbaTravellersDeleted",
"NumberOfTbaTravellersInitial",
"NumberOfTbaTravellersAdded",
"NumberOfTbaTravellersUsed",
"RequestStatusIdInitial",
"RequestStatusId",
"IsAutoSaved",
"ApprovedById",
"ApproverId",
"OwnedById",
"ModifiedBy",
"RejectedBy",
"RejectedById",
"ApprovedOn",
"BookedById",
"RejectReason",
"RejectedOn",
"CancelledById",
"ApprovalLimit"
};

private readonly List<string> _ignoredTravelPropertiesList = new List<string>()
{
"ModifiedDateTime",
//"NumberOfTbaTravellers",
"NumberOfTbaTravellersDeleted",
"NumberOfTbaTravellersInitial",
"NumberOfTbaTravellersAdded",
"NumberOfTbaTravellersUsed",
"TravelStatusId",
"EndDateTime",
"StartTimeFrom",
"PredictedCost",
"IsDeleted",
"AirportId",
"FlightTravelId",
"ModifiedBy",
"ModifiedDateTime",
"CreatedBy",
"BookedTravelLinkId"
};

private readonly List<string> _ignoredBookingInfoPropertiesList = new List<string>()
{
"ProviderId",
"BookingDate"
};

private readonly List<string> _ignoredCancelInfoPropertiesList = new List<string>()
{
"ProviderId",
"CancellationDate",
"CurrencyId",
"CancelledById",
"CancelledByName",
"IsPartiallyCancelled"
};

private readonly List<int> _ignoredRequestStatusesList = new List<int>()
{
(int)RequestStatus.Created,
(int)RequestStatus.Draft,
(int)RequestStatus.DraftAmended,
(int)RequestStatus.Submitted,
//(int)RequestStatus.SubmittedAmended,
};

private readonly List<int> _ignoredTravelStatusesList = new List<int>()
{

};

private readonly IUnityContainer _container;
public AuditedBusinessTravelContext(ILog log)
: base("Jet2BusinessTravel")
{
Database.SetInitializer<AuditedBusinessTravelContext>(null);
if (log != null)
{
Logger = log;
}
}
//public AuditedBusinessTravelContext(ILog log, IUnityContainer container)
// : base("Jet2BusinessTravel")
//{
// Database.SetInitializer<AuditedBusinessTravelContext>(null);
// if (log != null)
// {
// Logger = log;
// }
// _container = container;
//}

#region Public Methods

public override int SaveChanges()
{
var auditService = _container.Resolve<IAuditTrailService>();

var actionedTravelRepo = _container.Resolve<IRepository<ActionedTravel>>();

var addedTravels = ChangeTracker.Entries<Travel>().Where(_ => _.State == EntityState.Added && _.Entity.RequestedTravels.Any(r => !_ignoredRequestStatusesList.Contains(r.Request.RequestStatusId))).ToList();
var modifiedTravels = ChangeTracker.Entries<Travel>().Where(_ => _.State == EntityState.Modified && _.Entity.RequestedTravels.Any(r => !_ignoredRequestStatusesList.Contains(r.Request.RequestStatusId))).ToList();
var removedTravels = ChangeTracker.Entries<Travel>().Where(_ => _.State == EntityState.Deleted).ToList();

var addedBookingInfos = ChangeTracker.Entries<BookingInfo>().Where(_ => _.State == EntityState.Added).ToList();
var modifiedBookingInfos = ChangeTracker.Entries<BookingInfo>().Where(_ => _.State == EntityState.Modified).ToList();

var addedCancelInfos = ChangeTracker.Entries<CancellationInfo>().Where(_ => _.State == EntityState.Added).ToList();
var modifiedCancelInfos = ChangeTracker.Entries<CancellationInfo>().Where(_ => _.State == EntityState.Modified).ToList();

var modifiedRequests = ChangeTracker.Entries<Request>().Where(_ => _.State == EntityState.Modified).ToList();

var removedPersons = ChangeTracker.Entries<Traveller>().Where(_ => _.State == EntityState.Deleted).ToList();
var modifiedPersons = ChangeTracker.Entries<Traveller>().Where(_ => _.State == EntityState.Modified && !_ignoredRequestStatusesList.Contains(_.Entity.Request.RequestStatusId)).ToList();
var addedPersons = ChangeTracker.Entries<Traveller>().Where(_ => _.State == EntityState.Added && !_ignoredRequestStatusesList.Contains(_.Entity.Request.RequestStatusId)).ToList();

var removedTravellers = ChangeTracker.Entries<RequestedTravel>().Where(_ => _.State == EntityState.Deleted).ToList();
var modifiedTravellers = ChangeTracker.Entries<RequestedTravel>().Where(_ => _.State == EntityState.Modified && !_ignoredRequestStatusesList.Contains(_.Entity.Request.RequestStatusId)).ToList();
var addedTravellers = ChangeTracker.Entries<RequestedTravel>().Where(_ => _.State == EntityState.Added && !_ignoredRequestStatusesList.Contains(_.Entity.Request.RequestStatusId)).ToList();

foreach (var modifiedTraveller in modifiedTravellers)
{
if (modifiedTraveller.CurrentValues["IsDeleted"] != modifiedTraveller.OriginalValues["IsDeleted"])
{
if (bool.Parse(modifiedTraveller.CurrentValues["IsDeleted"].ToString()))
{
removedTravellers.Add(modifiedTraveller);
}
else
{
//addedPersons.Add(modifiedPerson);
}
}
}

foreach (var modifiedPerson in modifiedPersons)
{
if (modifiedPerson.CurrentValues["IsDeleted"] != modifiedPerson.OriginalValues["IsDeleted"])
{
if (bool.Parse(modifiedPerson.CurrentValues["IsDeleted"].ToString()))
{
removedPersons.Add(modifiedPerson);
}
else
{
//addedPersons.Add(modifiedPerson);
}
}

if (modifiedPerson.CurrentValues["CostCode"] != modifiedPerson.OriginalValues["CostCode"])
{
var auditTrailItem = new AuditTrailItemModel()
{
PropertyName = string.Format("CostCode for {0} {1}", modifiedPerson.Entity.Person.Forename, modifiedPerson.Entity.Person.Surname),
NewValue = modifiedPerson.CurrentValues["CostCode"].ToString(),
OldValue = modifiedPerson.OriginalValues["CostCode"].ToString(),
};
auditService.RequestChanged(modifiedPerson.Entity.RequestId, new List<AuditTrailItemModel>() {auditTrailItem});
}
}

foreach (var modifiedTravel in modifiedTravels.Where(_ => !_ignoredTravelStatusesList.Contains(_.Entity.TravelStatusId)))
{
var requestedTravel = modifiedTravel.Entity.RequestedTravels.FirstOrDefault();
if (requestedTravel == null || requestedTravel.Request == null)
{
continue;
}

if (modifiedTravel.CurrentValues["TravelStatusId"] != null && modifiedTravel.OriginalValues["TravelStatusId"] != null &&
modifiedTravel.CurrentValues["TravelStatusId"].ToString() != modifiedTravel.OriginalValues["TravelStatusId"].ToString()
&&
(modifiedTravel.CurrentValues["TravelStatusId"].ToString() == "3" ||
modifiedTravel.CurrentValues["TravelStatusId"].ToString() == "5"))
{
auditService.TravelCancelled(modifiedTravel.Entity.Id, modifiedTravel.Entity.TravelTypeId,
requestedTravel.RequestId);
continue;
}

if (modifiedTravel.CurrentValues["IsDeleted"].ToString() != modifiedTravel.OriginalValues["IsDeleted"].ToString())
{
if (bool.Parse(modifiedTravel.CurrentValues["IsDeleted"].ToString()))
{
removedTravels.Add(modifiedTravel);
continue;
}
else
{
//addedTravels.Add(modifiedTravel);
}
}

var auditTrailItems = new List<AuditTrailItemModel>();
foreach (var propertyName in modifiedTravel.CurrentValues.PropertyNames.Where(_ => !_ignoredTravelPropertiesList.Contains(_)))
{
var originalValue = modifiedTravel.OriginalValues[propertyName];
var currentValue = modifiedTravel.CurrentValues[propertyName];
if (originalValue == null)
{
originalValue = string.Empty;
}
if (currentValue == null)
{
currentValue = string.Empty;
}
if (originalValue.ToString() != currentValue.ToString())
{
if (propertyName == "ApprovalLimit" || propertyName == "Cost" || propertyName == "NumberOfTbaTravellers")
{
if (originalValue.ToString() == string.Empty)
{
originalValue = 0;
}
if (currentValue.ToString() == string.Empty)
{
currentValue = 0;
}
if (decimal.Parse(originalValue.ToString()) == decimal.Parse(currentValue.ToString()))
{
continue;
}

currentValue = (decimal)currentValue;//.ToString());
originalValue = (decimal)originalValue;//.ToString());

}

var auditTrailItem = new AuditTrailItemModel()
{
PropertyName = ConvertName(propertyName),
NewValue = ConvertValue(currentValue, propertyName),
OldValue = ConvertValue(originalValue, propertyName),
};
auditTrailItems.Add(auditTrailItem);



}
}
if (auditTrailItems.Any())
{
auditService.TravelChanged(modifiedTravel.Entity.Id, requestedTravel.RequestId, modifiedTravel.Entity.TravelTypeId, auditTrailItems);

}
}

foreach (var modifiedBookingInfo in modifiedBookingInfos)
{
var auditTrailItems = new List<AuditTrailItemModel>();
if (
(modifiedBookingInfo.OriginalValues["CurrencyId"] == null &&
modifiedBookingInfo.OriginalValues["BookingDate"] == null &&
modifiedBookingInfo.OriginalValues["BookingReference"] == null &&
modifiedBookingInfo.OriginalValues["ProviderName"] == null))
{
addedBookingInfos.Add(modifiedBookingInfo);
continue;
}
foreach (var propertyName in modifiedBookingInfo.CurrentValues.PropertyNames.Where(_ => !_ignoredBookingInfoPropertiesList.Contains(_)))
{
var originalValue = modifiedBookingInfo.OriginalValues[propertyName];
var currentValue = modifiedBookingInfo.CurrentValues[propertyName];

if (originalValue == null)
{
originalValue = string.Empty;
}
if (currentValue == null)
{
currentValue = string.Empty;
}
if (originalValue.ToString() != currentValue.ToString())
{
if (propertyName == "Cost")
{
if (originalValue.ToString() == string.Empty)
{
originalValue = 0;
}
if (currentValue.ToString() == string.Empty)
{
currentValue = 0;
}
if (decimal.Parse(originalValue.ToString()) == decimal.Parse(currentValue.ToString()))
{
continue;
}

currentValue = (decimal)currentValue;//.ToString());
originalValue = (decimal)originalValue;//.ToString());
}

if (propertyName == "PaymentType")
{
if (originalValue.ToString() != string.Empty)
{
originalValue = ((PaymentType)(int)originalValue).GetDescription();
}
if (currentValue.ToString() != string.Empty)
{
currentValue = ((PaymentType)(int)currentValue).GetDescription();
}
if (originalValue == currentValue)
{
continue;
}
}

var auditTrailItem = new AuditTrailItemModel()
{
PropertyName = ConvertName(propertyName),
NewValue = ConvertValue(currentValue, propertyName),
OldValue = ConvertValue(originalValue, propertyName),
};
auditTrailItems.Add(auditTrailItem);


}
}
if (auditTrailItems.Any())
{
var actionedTravel =
actionedTravelRepo.FirstOrDefault(_ => _.BookingInfoId == modifiedBookingInfo.Entity.Id);
if (actionedTravel != null)
{
auditService.BookingInfoChanged(actionedTravel.TravelId, actionedTravel.RequestId,
actionedTravel.Travel.TravelTypeId, auditTrailItems);
}
}
}

foreach (var modifiedCancelInfo in modifiedCancelInfos)
{
var auditTrailItems = new List<AuditTrailItemModel>();
foreach (var propertyName in modifiedCancelInfo.CurrentValues.PropertyNames.Where(_ => !_ignoredCancelInfoPropertiesList.Contains(_)))
{
var originalValue = modifiedCancelInfo.OriginalValues[propertyName];
var currentValue = modifiedCancelInfo.CurrentValues[propertyName];
if (originalValue == null)
{
originalValue = string.Empty;
}
if (currentValue == null)
{
currentValue = string.Empty;
}

if (originalValue.ToString() != currentValue.ToString())
{
if (propertyName == "CancellationCost")
{
if (originalValue.ToString() == string.Empty)
{
originalValue = 0;
}
if (currentValue.ToString() == string.Empty)
{
currentValue = 0;
}
if (decimal.Parse(originalValue.ToString()) == decimal.Parse(currentValue.ToString()))
{
continue;
}

currentValue = (decimal)currentValue;//.ToString());
originalValue = (decimal)originalValue;//.ToString());

}

var auditTrailItem = new AuditTrailItemModel()
{
PropertyName = ConvertName(propertyName),
NewValue = ConvertValue(currentValue, propertyName),
OldValue = ConvertValue(originalValue, propertyName),
};
auditTrailItems.Add(auditTrailItem);


}
}
if (auditTrailItems.Any())
{
var actionedTravel =
actionedTravelRepo.FirstOrDefault(_ => _.CancellationInfoId == modifiedCancelInfo.Entity.Id);
if (actionedTravel != null)
{
auditService.CancelInfoChanged(actionedTravel.TravelId, actionedTravel.RequestId,
actionedTravel.Travel.TravelTypeId, auditTrailItems);
}
}
}


foreach (var addedPerson in addedPersons)
{
var person = addedPerson.Entity;
auditService.PersonAdded(person.RequestId, person.Person.Forename + " " + person.Person.Surname);
}

foreach (var addedTraveller in addedTravellers)
{
var person = addedTraveller.Entity;
var request = person.Request;
var travel = person.Travel;
if (request != null && travel != null && travel.Id > 0 && !_ignoredRequestStatusesList.Contains(request.RequestStatusId))
{
auditService.TravellerAdded(person.RequestId, travel.Id, travel.TravelTypeId, person.Person.Forename + " " + person.Person.Surname);
}
}


foreach (var modifiedRequest in modifiedRequests.Where(_ => !_ignoredRequestStatusesList.Contains( _.Entity.RequestStatusId) && _.Entity.ApprovedOn != null))
{
var auditTrailItems = new List<AuditTrailItemModel>();
foreach (var propertyName in modifiedRequest.CurrentValues.PropertyNames.Where( _ => !_ignoredRequestPropertiesList.Contains(_)))
{
var originalValue = modifiedRequest.OriginalValues[propertyName];
var currentValue = modifiedRequest.CurrentValues[propertyName];
if (originalValue == null)
{
originalValue = string.Empty;
}
if (currentValue == null)
{
currentValue = string.Empty;
}
if (originalValue.ToString() != currentValue.ToString())
{
if (propertyName == "ApprovalLimit" || propertyName == "AmendedApprovalLimit" || propertyName == "NumberOfTbaTravellers")
{
if (originalValue.ToString() == string.Empty)
{
originalValue = 0;
}
if (currentValue.ToString() == string.Empty)
{
currentValue = 0;
}
if (decimal.Parse(originalValue.ToString()) == decimal.Parse(currentValue.ToString()))
{
continue;
}

if (propertyName == "AmendedApprovalLimit" && decimal.Parse(currentValue.ToString()) == 0)
{
//don't log amended Indicative Cost reset to zero
continue;
}

currentValue = decimal.Parse(currentValue.ToString());
originalValue = decimal.Parse(originalValue.ToString());


}

var auditTrailItem = new AuditTrailItemModel()
{
PropertyName = ConvertName(propertyName),
NewValue = ConvertValue(currentValue, propertyName),
OldValue = ConvertValue(originalValue, propertyName),
};
auditTrailItems.Add(auditTrailItem);

}
}
if (auditTrailItems.Any())
{
auditService.RequestChanged(modifiedRequest.Entity.Id, auditTrailItems);
}
}

foreach (var removedTraveller in removedTravellers)
{
var person = removedTraveller.Entity;
var request = removedTraveller.Entity.Request;
var travel = removedTraveller.Entity.Travel;
if (request != null && !_ignoredRequestStatusesList.Contains(request.RequestStatusId) && travel != null)
{
auditService.TravellerRemoved(person.RequestId, travel.Id, travel.TravelTypeId, person.Person.Forename + " " + person.Person.Surname);
}
}

var res = base.SaveChanges();

foreach (var addedTravel in addedTravels)
{
var request = addedTravel.Entity.RequestedTravels.FirstOrDefault();

if (request != null && request.Request.ApprovedOn != null)
{
auditService.TravelAdded(addedTravel.Entity.Id, addedTravel.Entity.TravelTypeId,
request.RequestId);
}
}

foreach (var removedTravel in removedTravels)
{
var request = removedTravel.Entity.RequestedTravels.FirstOrDefault();
if (request != null && !_ignoredRequestStatusesList.Contains(request.Request.RequestStatusId))
{
auditService.TravelDeleted(removedTravel.Entity.Id, removedTravel.Entity.TravelTypeId,
request.RequestId);
}
}

foreach (var removedPerson in removedPersons)
{
var person = removedPerson.Entity;
var request = removedPerson.Entity.Request;
if (request != null && !_ignoredRequestStatusesList.Contains(request.RequestStatusId))
{
auditService.PersonRemoved(person.RequestId, person.Person.Forename + " " + person.Person.Surname);
}
}



//foreach (var removedTraveller in removedTravellers)
//{
// var person = removedTraveller.Entity;
// var request = removedTraveller.Entity.Request;
// var travel = removedTraveller.Entity.Travel;
// if (request != null && !_ignoredRequestStatusesList.Contains(request.RequestStatusId) && travel != null)
// {
// auditService.TravellerRemoved(person.RequestId, travel.Id, travel.TravelTypeId, person.Person.Forename + " " + person.Person.Surname);
// }
//}

foreach (var addedBookingInfo in addedBookingInfos)
{
var actionedTravel = actionedTravelRepo.FirstOrDefault(_ => _.BookingInfoId == addedBookingInfo.Entity.Id);
if (actionedTravel != null &&
(addedBookingInfo.Entity.CurrencyId != null ||
addedBookingInfo.Entity.BookingReference != null ||
addedBookingInfo.Entity.ProviderName != null ||
addedBookingInfo.Entity.Cost > 0))
{
auditService.BookingInfoAdded(actionedTravel.TravelId, actionedTravel.Travel.TravelTypeId, actionedTravel.RequestId);
}
}

foreach (var addedCancelInfo in addedCancelInfos)
{
var actionedTravel = actionedTravelRepo.FirstOrDefault(_ => _.CancellationInfoId == addedCancelInfo.Entity.Id);
if (actionedTravel != null)
{
auditService.CancelInfoAdded(actionedTravel.TravelId, actionedTravel.Travel.TravelTypeId, actionedTravel.RequestId);
}
}

return res;
}

#endregion

private string ConvertValue(object valueObj, string name)
{

if (name == "ParkingType")
{
ParkingType val = (ParkingType) valueObj;
return val.GetDescription();
}
if(name == "RoomLayout")
{
RoomLayout val = (RoomLayout)valueObj;
return val.GetDescription();
}
if (name == "ApprovalLimit" || name == "AmendedApprovalLimit" || name == "Cost" || name == "CancellationCost")
{
return string.Format("{0:#.##}", valueObj);
}
if (name == "NumberOfTbaTravellers")
{
return string.Format("{0:#}", valueObj);
}
if (valueObj.ToString().ToLower().Trim() == "true")
{
return "Checked";
}
if (valueObj.ToString().ToLower().Trim() == "false")
{
return "Unchecked";
}
if (name.ToLower().Contains("time") && !name.ToLower().Contains("date") && !string.IsNullOrEmpty(valueObj.ToString()))
{
var val = (DateTime) valueObj;
return val.ToShortTimeString();
}
if (!name.ToLower().Contains("time") && name.ToLower().Contains("date") && !string.IsNullOrEmpty(valueObj.ToString()))
{
var val = (DateTime)valueObj;
return val.ToShortDateString();
}
if (name == "CurrencyId")
{
if (valueObj.ToString() != string.Empty)
{
Currency val = (Currency) valueObj;
return val.GetDescription();
}

}
return valueObj.ToString();
}


private string ConvertName(string name)
{
if (name == "ApprovalLimit")
{
return "Indicative Cost";
}
if (name == "AmendedApprovalLimit")
{
return "Amended Indicative Cost";
}
return Regex.Replace(name, @"((?<=p{Ll})p{Lu})|((?!A)p{Lu}(?>p{Ll}))", " $0");
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.