Advertisement
HenX

Entity

Jan 17th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.27 KB | None | 0 0
  1.  public partial class LocalizedProperty : BaseEntity
  2.     {
  3.         /// <summary>
  4.         /// Gets or sets the entity identifier
  5.         /// </summary>
  6.         public int EntityId { get; set; }
  7.  
  8.         /// <summary>
  9.         /// Gets or sets the language identifier
  10.         /// </summary>
  11.         public int LanguageId { get; set; }
  12.  
  13.         /// <summary>
  14.         /// Gets or sets the locale key group
  15.         /// </summary>
  16.         public string LocaleKeyGroup { get; set; }
  17.  
  18.         /// <summary>
  19.         /// Gets or sets the locale key
  20.         /// </summary>
  21.         public string LocaleKey { get; set; }
  22.  
  23.         /// <summary>
  24.         /// Gets or sets the locale value
  25.         /// </summary>
  26.         public string LocaleValue { get; set; }
  27.        
  28.         /// <summary>
  29.         /// Gets the language
  30.         /// </summary>
  31.         public virtual Language Language { get; set; }
  32.     }
  33.  
  34.    public partial class LocalizedEntityService : ILocalizedEntityService
  35.     {
  36.         #region Constants
  37.  
  38.         /// <summary>
  39.         /// Key for caching
  40.         /// </summary>
  41.         /// <remarks>
  42.         /// {0} : language ID
  43.         /// {1} : entity ID
  44.         /// {2} : locale key group
  45.         /// {3} : locale key
  46.         /// </remarks>
  47.         private const string LOCALIZEDPROPERTY_KEY = "Nop.localizedproperty.value-{0}-{1}-{2}-{3}";
  48.         /// <summary>
  49.         /// Key for caching
  50.         /// </summary>
  51.         private const string LOCALIZEDPROPERTY_ALL_KEY = "Nop.localizedproperty.all";
  52.         /// <summary>
  53.         /// Key pattern to clear cache
  54.         /// </summary>
  55.         private const string LOCALIZEDPROPERTY_PATTERN_KEY = "Nop.localizedproperty.";
  56.  
  57.         #endregion
  58.  
  59.         #region Fields
  60.  
  61.         private readonly IRepository<LocalizedProperty> _localizedPropertyRepository;
  62.         private readonly ICacheManager _cacheManager;
  63.         private readonly LocalizationSettings _localizationSettings;
  64.  
  65.         #endregion
  66.  
  67.         #region Ctor
  68.  
  69.         /// <summary>
  70.         /// Ctor
  71.         /// </summary>
  72.         /// <param name="cacheManager">Cache manager</param>
  73.         /// <param name="localizedPropertyRepository">Localized property repository</param>
  74.         /// <param name="localizationSettings">Localization settings</param>
  75.         public LocalizedEntityService(ICacheManager cacheManager,
  76.             IRepository<LocalizedProperty> localizedPropertyRepository,
  77.             LocalizationSettings localizationSettings)
  78.         {
  79.             this._cacheManager = cacheManager;
  80.             this._localizedPropertyRepository = localizedPropertyRepository;
  81.             this._localizationSettings = localizationSettings;
  82.         }
  83.  
  84.         #endregion
  85.  
  86.         #region Utilities
  87.  
  88.         /// <summary>
  89.         /// Gets localized properties
  90.         /// </summary>
  91.         /// <param name="entityId">Entity identifier</param>
  92.         /// <param name="localeKeyGroup">Locale key group</param>
  93.         /// <returns>Localized properties</returns>
  94.         protected virtual IList<LocalizedProperty> GetLocalizedProperties(int entityId, string localeKeyGroup)
  95.         {
  96.             if (entityId == 0 || string.IsNullOrEmpty(localeKeyGroup))
  97.                 return new List<LocalizedProperty>();
  98.  
  99.             var query = from lp in _localizedPropertyRepository.Table
  100.                         orderby lp.Id
  101.                         where lp.EntityId == entityId &&
  102.                               lp.LocaleKeyGroup == localeKeyGroup
  103.                         select lp;
  104.             var props = query.ToList();
  105.             return props;
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Gets all cached localized properties
  110.         /// </summary>
  111.         /// <returns>Cached localized properties</returns>
  112.         protected virtual IList<LocalizedPropertyForCaching> GetAllLocalizedPropertiesCached()
  113.         {
  114.             //cache
  115.             string key = string.Format(LOCALIZEDPROPERTY_ALL_KEY);
  116.             return _cacheManager.Get(key, () =>
  117.             {
  118.                 var query = from lp in _localizedPropertyRepository.Table
  119.                             select lp;
  120.                 var localizedProperties = query.ToList();
  121.                 var list = new List<LocalizedPropertyForCaching>();
  122.                 foreach (var lp in localizedProperties)
  123.                 {
  124.                     var localizedPropertyForCaching = new LocalizedPropertyForCaching
  125.                     {
  126.                         Id = lp.Id,
  127.                         EntityId = lp.EntityId,
  128.                         LanguageId = lp.LanguageId,
  129.                         LocaleKeyGroup = lp.LocaleKeyGroup,
  130.                         LocaleKey = lp.LocaleKey,
  131.                         LocaleValue = lp.LocaleValue
  132.                     };
  133.                     list.Add(localizedPropertyForCaching);
  134.                 }
  135.                 return list;
  136.             });
  137.         }
  138.  
  139.         #endregion
  140.  
  141.         #region Nested classes
  142.  
  143.         [Serializable]
  144.         public class LocalizedPropertyForCaching
  145.         {
  146.             public int Id { get; set; }
  147.             public int EntityId { get; set; }
  148.             public int LanguageId { get; set; }
  149.             public string LocaleKeyGroup { get; set; }
  150.             public string LocaleKey { get; set; }
  151.             public string LocaleValue { get; set; }
  152.         }
  153.  
  154.         #endregion
  155.  
  156.         #region Methods
  157.  
  158.         /// <summary>
  159.         /// Deletes a localized property
  160.         /// </summary>
  161.         /// <param name="localizedProperty">Localized property</param>
  162.         public virtual void DeleteLocalizedProperty(LocalizedProperty localizedProperty)
  163.         {
  164.             if (localizedProperty == null)
  165.                 throw new ArgumentNullException("localizedProperty");
  166.  
  167.             _localizedPropertyRepository.Delete(localizedProperty);
  168.  
  169.             //cache
  170.             _cacheManager.RemoveByPattern(LOCALIZEDPROPERTY_PATTERN_KEY);
  171.         }
  172.  
  173.         /// <summary>
  174.         /// Gets a localized property
  175.         /// </summary>
  176.         /// <param name="localizedPropertyId">Localized property identifier</param>
  177.         /// <returns>Localized property</returns>
  178.         public virtual LocalizedProperty GetLocalizedPropertyById(int localizedPropertyId)
  179.         {
  180.             if (localizedPropertyId == 0)
  181.                 return null;
  182.  
  183.             return _localizedPropertyRepository.GetById(localizedPropertyId);
  184.         }
  185.  
  186.         /// <summary>
  187.         /// Find localized value
  188.         /// </summary>
  189.         /// <param name="languageId">Language identifier</param>
  190.         /// <param name="entityId">Entity identifier</param>
  191.         /// <param name="localeKeyGroup">Locale key group</param>
  192.         /// <param name="localeKey">Locale key</param>
  193.         /// <returns>Found localized value</returns>
  194.         public virtual string GetLocalizedValue(int languageId, int entityId, string localeKeyGroup, string localeKey)
  195.         {
  196.             if (_localizationSettings.LoadAllLocalizedPropertiesOnStartup)
  197.             {
  198.                 string key = string.Format(LOCALIZEDPROPERTY_KEY, languageId, entityId, localeKeyGroup, localeKey);
  199.                 return _cacheManager.Get(key, () =>
  200.                 {
  201.                     //load all records (we know they are cached)
  202.                     var source = GetAllLocalizedPropertiesCached();
  203.                     var query = from lp in source
  204.                                 where lp.LanguageId == languageId &&
  205.                                 lp.EntityId == entityId &&
  206.                                 lp.LocaleKeyGroup == localeKeyGroup &&
  207.                                 lp.LocaleKey == localeKey
  208.                                 select lp.LocaleValue;
  209.                     var localeValue = query.FirstOrDefault();
  210.                     //little hack here. nulls aren't cacheable so set it to ""
  211.                     if (localeValue == null)
  212.                         localeValue = "";
  213.                     return localeValue;
  214.                 });
  215.  
  216.             }
  217.             else
  218.             {
  219.                 //gradual loading
  220.                 string key = string.Format(LOCALIZEDPROPERTY_KEY, languageId, entityId, localeKeyGroup, localeKey);
  221.                 return _cacheManager.Get(key, () =>
  222.                 {
  223.                     var source = _localizedPropertyRepository.Table;
  224.                     var query = from lp in source
  225.                                 where lp.LanguageId == languageId &&
  226.                                 lp.EntityId == entityId &&
  227.                                 lp.LocaleKeyGroup == localeKeyGroup &&
  228.                                 lp.LocaleKey == localeKey
  229.                                 select lp.LocaleValue;
  230.                     var localeValue = query.FirstOrDefault();
  231.                     //little hack here. nulls aren't cacheable so set it to ""
  232.                     if (localeValue == null)
  233.                         localeValue = "";
  234.                     return localeValue;
  235.                 });
  236.             }
  237.         }
  238.  
  239.         /// <summary>
  240.         /// Inserts a localized property
  241.         /// </summary>
  242.         /// <param name="localizedProperty">Localized property</param>
  243.         public virtual void InsertLocalizedProperty(LocalizedProperty localizedProperty)
  244.         {
  245.             if (localizedProperty == null)
  246.                 throw new ArgumentNullException("localizedProperty");
  247.  
  248.             _localizedPropertyRepository.Insert(localizedProperty);
  249.  
  250.             //cache
  251.             _cacheManager.RemoveByPattern(LOCALIZEDPROPERTY_PATTERN_KEY);
  252.         }
  253.  
  254.         /// <summary>
  255.         /// Updates the localized property
  256.         /// </summary>
  257.         /// <param name="localizedProperty">Localized property</param>
  258.         public virtual void UpdateLocalizedProperty(LocalizedProperty localizedProperty)
  259.         {
  260.             if (localizedProperty == null)
  261.                 throw new ArgumentNullException("localizedProperty");
  262.  
  263.             _localizedPropertyRepository.Update(localizedProperty);
  264.  
  265.             //cache
  266.             _cacheManager.RemoveByPattern(LOCALIZEDPROPERTY_PATTERN_KEY);
  267.         }
  268.  
  269.         /// <summary>
  270.         /// Save localized value
  271.         /// </summary>
  272.         /// <typeparam name="T">Type</typeparam>
  273.         /// <param name="entity">Entity</param>
  274.         /// <param name="keySelector">Key selector</param>
  275.         /// <param name="localeValue">Locale value</param>
  276.         /// <param name="languageId">Language ID</param>
  277.         public virtual void SaveLocalizedValue<T>(T entity,
  278.             Expression<Func<T, string>> keySelector,
  279.             string localeValue,
  280.             int languageId) where T : BaseEntity, ILocalizedEntity
  281.         {
  282.             SaveLocalizedValue<T, string>(entity, keySelector, localeValue, languageId);
  283.         }
  284.  
  285.         public virtual void SaveLocalizedValue<T, TPropType>(T entity,
  286.             Expression<Func<T, TPropType>> keySelector,
  287.             TPropType localeValue,
  288.             int languageId) where T : BaseEntity, ILocalizedEntity
  289.         {
  290.             if (entity == null)
  291.                 throw new ArgumentNullException("entity");
  292.  
  293.             if (languageId == 0)
  294.                 throw new ArgumentOutOfRangeException("languageId", "Language ID should not be 0");
  295.  
  296.             var member = keySelector.Body as MemberExpression;
  297.             if (member == null)
  298.             {
  299.                 throw new ArgumentException(string.Format(
  300.                     "Expression '{0}' refers to a method, not a property.",
  301.                     keySelector));
  302.             }
  303.  
  304.             var propInfo = member.Member as PropertyInfo;
  305.             if (propInfo == null)
  306.             {
  307.                 throw new ArgumentException(string.Format(
  308.                        "Expression '{0}' refers to a field, not a property.",
  309.                        keySelector));
  310.             }
  311.  
  312.             string localeKeyGroup = typeof(T).Name;
  313.             string localeKey = propInfo.Name;
  314.  
  315.             var props = GetLocalizedProperties(entity.Id, localeKeyGroup);
  316.             var prop = props.FirstOrDefault(lp => lp.LanguageId == languageId &&
  317.                 lp.LocaleKey.Equals(localeKey, StringComparison.InvariantCultureIgnoreCase)); //should be culture invariant
  318.  
  319.             var localeValueStr = CommonHelper.To<string>(localeValue);
  320.            
  321.             if (prop != null)
  322.             {
  323.                 if (string.IsNullOrWhiteSpace(localeValueStr))
  324.                 {
  325.                     //delete
  326.                     DeleteLocalizedProperty(prop);
  327.                 }
  328.                 else
  329.                 {
  330.                     //update
  331.                     prop.LocaleValue = localeValueStr;
  332.                     UpdateLocalizedProperty(prop);
  333.                 }
  334.             }
  335.             else
  336.             {
  337.                 if (!string.IsNullOrWhiteSpace(localeValueStr))
  338.                 {
  339.                     //insert
  340.                     prop = new LocalizedProperty
  341.                     {
  342.                         EntityId = entity.Id,
  343.                         LanguageId = languageId,
  344.                         LocaleKey = localeKey,
  345.                         LocaleKeyGroup = localeKeyGroup,
  346.                         LocaleValue = localeValueStr
  347.                     };
  348.                     InsertLocalizedProperty(prop);
  349.                 }
  350.             }
  351.         }
  352.  
  353.         #endregion
  354.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement