using UserManagement.Data; using UserManagement.Data.Dto; using System; using System.Collections.Generic; using System.Linq; namespace UserManagement.Repository { public class PropertyMappingService : IPropertyMappingService { private Dictionary _loginAuditMapping = new Dictionary(StringComparer.OrdinalIgnoreCase) { { "Id", new PropertyMappingValue(new List() { "Id" } ) }, { "UserName", new PropertyMappingValue(new List() { "UserName" } )}, { "LoginTime", new PropertyMappingValue(new List() { "LoginTime" } )}, { "RemoteIP", new PropertyMappingValue(new List() { "RemoteIP" } )}, { "Status", new PropertyMappingValue(new List() { "Status" } )}, { "Provider", new PropertyMappingValue(new List() { "Provider" } )} }; private Dictionary _userMapping = new Dictionary(StringComparer.OrdinalIgnoreCase) { { "Id", new PropertyMappingValue(new List() { "Id" } ) }, { "UserName", new PropertyMappingValue(new List() { "UserName" } )}, { "Email", new PropertyMappingValue(new List() { "Email" } )}, { "FirstName", new PropertyMappingValue(new List() { "FirstName" } )}, { "LastName", new PropertyMappingValue(new List() { "LastName" } )}, { "PhoneNumber", new PropertyMappingValue(new List() { "PhoneNumber" } )}, { "IsActive", new PropertyMappingValue(new List() { "IsActive" } )} }; private Dictionary _nLogMapping = new Dictionary(StringComparer.OrdinalIgnoreCase) { { "Id", new PropertyMappingValue(new List() { "Id" } ) }, { "MachineName", new PropertyMappingValue(new List() { "MachineName" } )}, { "Logged", new PropertyMappingValue(new List() { "Logged" } )}, { "Level", new PropertyMappingValue(new List() { "Level" } )}, { "Message", new PropertyMappingValue(new List() { "Message" } )}, { "Logger", new PropertyMappingValue(new List() { "Logger" } )}, { "Properties", new PropertyMappingValue(new List() { "Properties" } )}, { "Callsite", new PropertyMappingValue(new List() { "Callsite" } )}, { "Exception", new PropertyMappingValue(new List() { "Exception" } )} }; private Dictionary _reminderMapping = new Dictionary(StringComparer.OrdinalIgnoreCase) { { "Id", new PropertyMappingValue(new List() { "Id" } ) }, { "Subject", new PropertyMappingValue(new List() { "Subject" } )}, { "Message", new PropertyMappingValue(new List() { "Message" } )}, { "Frequency", new PropertyMappingValue(new List() { "Frequency" } )}, { "DocumentName", new PropertyMappingValue(new List() { "Document.Name" } )}, { "StartDate", new PropertyMappingValue(new List() { "StartDate" },true )}, { "EndDate", new PropertyMappingValue(new List() { "EndDate" },true )}, { "CreatedDate", new PropertyMappingValue(new List() { "CreatedDate" } )}, { "IsRepeated", new PropertyMappingValue(new List() { "IsRepeated" } )}, { "IsEmailNotification", new PropertyMappingValue(new List() { "IsEmailNotification" } )}, { "IsActive", new PropertyMappingValue(new List() { "IsActive" } )} }; private Dictionary _reminderSchedulerMapping = new Dictionary(StringComparer.OrdinalIgnoreCase) { { "Id", new PropertyMappingValue(new List() { "Id" } ) }, { "Subject", new PropertyMappingValue(new List() { "Subject" } )}, { "Message", new PropertyMappingValue(new List() { "Message" } )}, { "IsRead", new PropertyMappingValue(new List() { "IsRead" } )}, { "CreatedDate", new PropertyMappingValue(new List() { "CreatedDate" }, true )} }; private Dictionary _notificationPropertyMapping = new Dictionary(StringComparer.OrdinalIgnoreCase) { { "Id", new PropertyMappingValue(new List() { "Id" } ) }, { "UserId", new PropertyMappingValue(new List() { "UserId" } ) }, { "Message", new PropertyMappingValue(new List() { "Message" } ) }, { "CreatedDate", new PropertyMappingValue(new List() { "CreatedDate" } )}, { "IsRead", new PropertyMappingValue(new List() { "IsRead" } )} }; private IList propertyMappings = new List(); public PropertyMappingService() { propertyMappings.Add(new PropertyMapping(_loginAuditMapping)); propertyMappings.Add(new PropertyMapping(_userMapping)); propertyMappings.Add(new PropertyMapping(_nLogMapping)); propertyMappings.Add(new PropertyMapping(_reminderMapping)); propertyMappings.Add(new PropertyMapping(_reminderSchedulerMapping)); propertyMappings.Add(new PropertyMapping(_notificationPropertyMapping)); } public Dictionary GetPropertyMapping () { // get matching mapping var matchingMapping = propertyMappings.OfType>(); if (matchingMapping.Count() == 1) { return matchingMapping.First()._mappingDictionary; } throw new Exception($"Cannot find exact property mapping instance for <{typeof(TSource)},{typeof(TDestination)}"); } public bool ValidMappingExistsFor(string fields) { var propertyMapping = GetPropertyMapping(); if (string.IsNullOrWhiteSpace(fields)) { return true; } // the string is separated by ",", so we split it. var fieldsAfterSplit = fields.Split(','); // run through the fields clauses foreach (var field in fieldsAfterSplit) { // trim var trimmedField = field.Trim(); // remove everything after the first " " - if the fields // are coming from an orderBy string, this part must be // ignored var indexOfFirstSpace = trimmedField.IndexOf(" "); var propertyName = indexOfFirstSpace == -1 ? trimmedField : trimmedField.Remove(indexOfFirstSpace); // find the matching property if (!propertyMapping.ContainsKey(propertyName)) { return false; } } return true; } } }