using System.Collections.Generic; using System.Linq; using Sony.Filtr.Contracts.Definitions; using Sony.Filtr.Core.LastModified; using Sony.Filtr.DistributedCaching; namespace Sony.Filtr.Globalization.v3 { public class MarketGlobalizationHandler { private readonly MarketGlobalizationFactory _factory; private readonly DistributedCacheHandler _cacheHandler; private readonly LastModifiedManager _lastModifiedManager; public MarketGlobalizationHandler(MarketGlobalizationFactory globalizationFactory, DistributedCacheHandler distributedCacheHandler, LastModifiedManager lastModifiedManager) { _factory = globalizationFactory; _cacheHandler = distributedCacheHandler; _lastModifiedManager = lastModifiedManager; } public void SetKey(MarketGlobalizationKey key) { _factory.SetKey(key); SetLastModifiedGlobal(); } public List GetKeys(string context = null) { return _factory.GetKeys(context); } public Dictionary GetTranslations(int applicationId, string context = null, int? apiConsumerId = null) { var cacheKey = GetCacheKey(); var translations = _cacheHandler.Get(cacheKey) as Dictionary>; if (translations == null) { translations = _factory.GetTranslations(); _cacheHandler.Put(cacheKey, translations); } return translations.Where(kvp => (string.IsNullOrWhiteSpace(context) || kvp.Key.Context == context) && (!apiConsumerId.HasValue || kvp.Key.ApiConsumerId == apiConsumerId.Value)) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ContainsKey(applicationId) ? kvp.Value[applicationId] : null); } public void SetTranslation(string key, string context, int apiConsumerId, int applicationId, string value) { _factory.SetTranslation(key, context, apiConsumerId, applicationId, value); SetLastModified(applicationId); ClearCache(); } public MarketGlobalizationKey GetKey(string key, string context, int apiConsumerId) { return GetKeys().Single(k => k.Key == key && k.Context == context && k.ApiConsumerId == apiConsumerId); } public MarketGlobalizationValue GetTranslation(string key, string context, int applicationId) { return GetTranslations(applicationId, context).Single(t => t.Key.Key == key).Value; } //public List GetTranslatedApplicationIds() //{ // return GetTranslations().Select(kvp => kvp.Value.ApplicationId).Distinct().ToList(); //} public void DeleteKey(string key, string context, int apiConsumerId) { var keyObject = GetKey(key, context, apiConsumerId); if (key != null) { _factory.DeleteKey(keyObject); ClearCache(); SetLastModifiedGlobal(); } } public void DeleteValue(string key, string context, int apiConsumerId, int applicationId) { var translation = _factory.GetTranslation(key, context, apiConsumerId, applicationId); if (translation != null) { _factory.DeleteValue(translation); ClearCache(); SetLastModified(applicationId); } } public void SetLastModifiedGlobal() { _lastModifiedManager.SetLastModifyDate(null, EntityType.MarketGlobalization); } public void SetLastModified(int applicationId) { _lastModifiedManager.SetLastModifyDate(applicationId, EntityType.MarketGlobalization); } private string GetCacheKey() { return "MarketGlobalization_v4"; } public void ClearCache() { _cacheHandler.Remove(GetCacheKey()); } } }