Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class LocalizedProperty : BaseEntity
- {
- /// <summary>
- /// Gets or sets the entity identifier
- /// </summary>
- public int EntityId { get; set; }
- /// <summary>
- /// Gets or sets the language identifier
- /// </summary>
- public int LanguageId { get; set; }
- /// <summary>
- /// Gets or sets the locale key group
- /// </summary>
- public string LocaleKeyGroup { get; set; }
- /// <summary>
- /// Gets or sets the locale key
- /// </summary>
- public string LocaleKey { get; set; }
- /// <summary>
- /// Gets or sets the locale value
- /// </summary>
- public string LocaleValue { get; set; }
- /// <summary>
- /// Gets the language
- /// </summary>
- public virtual Language Language { get; set; }
- }
- public partial class LocalizedEntityService : ILocalizedEntityService
- {
- #region Constants
- /// <summary>
- /// Key for caching
- /// </summary>
- /// <remarks>
- /// {0} : language ID
- /// {1} : entity ID
- /// {2} : locale key group
- /// {3} : locale key
- /// </remarks>
- private const string LOCALIZEDPROPERTY_KEY = "Nop.localizedproperty.value-{0}-{1}-{2}-{3}";
- /// <summary>
- /// Key for caching
- /// </summary>
- private const string LOCALIZEDPROPERTY_ALL_KEY = "Nop.localizedproperty.all";
- /// <summary>
- /// Key pattern to clear cache
- /// </summary>
- private const string LOCALIZEDPROPERTY_PATTERN_KEY = "Nop.localizedproperty.";
- #endregion
- #region Fields
- private readonly IRepository<LocalizedProperty> _localizedPropertyRepository;
- private readonly ICacheManager _cacheManager;
- private readonly LocalizationSettings _localizationSettings;
- #endregion
- #region Ctor
- /// <summary>
- /// Ctor
- /// </summary>
- /// <param name="cacheManager">Cache manager</param>
- /// <param name="localizedPropertyRepository">Localized property repository</param>
- /// <param name="localizationSettings">Localization settings</param>
- public LocalizedEntityService(ICacheManager cacheManager,
- IRepository<LocalizedProperty> localizedPropertyRepository,
- LocalizationSettings localizationSettings)
- {
- this._cacheManager = cacheManager;
- this._localizedPropertyRepository = localizedPropertyRepository;
- this._localizationSettings = localizationSettings;
- }
- #endregion
- #region Utilities
- /// <summary>
- /// Gets localized properties
- /// </summary>
- /// <param name="entityId">Entity identifier</param>
- /// <param name="localeKeyGroup">Locale key group</param>
- /// <returns>Localized properties</returns>
- protected virtual IList<LocalizedProperty> GetLocalizedProperties(int entityId, string localeKeyGroup)
- {
- if (entityId == 0 || string.IsNullOrEmpty(localeKeyGroup))
- return new List<LocalizedProperty>();
- var query = from lp in _localizedPropertyRepository.Table
- orderby lp.Id
- where lp.EntityId == entityId &&
- lp.LocaleKeyGroup == localeKeyGroup
- select lp;
- var props = query.ToList();
- return props;
- }
- /// <summary>
- /// Gets all cached localized properties
- /// </summary>
- /// <returns>Cached localized properties</returns>
- protected virtual IList<LocalizedPropertyForCaching> GetAllLocalizedPropertiesCached()
- {
- //cache
- string key = string.Format(LOCALIZEDPROPERTY_ALL_KEY);
- return _cacheManager.Get(key, () =>
- {
- var query = from lp in _localizedPropertyRepository.Table
- select lp;
- var localizedProperties = query.ToList();
- var list = new List<LocalizedPropertyForCaching>();
- foreach (var lp in localizedProperties)
- {
- var localizedPropertyForCaching = new LocalizedPropertyForCaching
- {
- Id = lp.Id,
- EntityId = lp.EntityId,
- LanguageId = lp.LanguageId,
- LocaleKeyGroup = lp.LocaleKeyGroup,
- LocaleKey = lp.LocaleKey,
- LocaleValue = lp.LocaleValue
- };
- list.Add(localizedPropertyForCaching);
- }
- return list;
- });
- }
- #endregion
- #region Nested classes
- [Serializable]
- public class LocalizedPropertyForCaching
- {
- public int Id { get; set; }
- public int EntityId { get; set; }
- public int LanguageId { get; set; }
- public string LocaleKeyGroup { get; set; }
- public string LocaleKey { get; set; }
- public string LocaleValue { get; set; }
- }
- #endregion
- #region Methods
- /// <summary>
- /// Deletes a localized property
- /// </summary>
- /// <param name="localizedProperty">Localized property</param>
- public virtual void DeleteLocalizedProperty(LocalizedProperty localizedProperty)
- {
- if (localizedProperty == null)
- throw new ArgumentNullException("localizedProperty");
- _localizedPropertyRepository.Delete(localizedProperty);
- //cache
- _cacheManager.RemoveByPattern(LOCALIZEDPROPERTY_PATTERN_KEY);
- }
- /// <summary>
- /// Gets a localized property
- /// </summary>
- /// <param name="localizedPropertyId">Localized property identifier</param>
- /// <returns>Localized property</returns>
- public virtual LocalizedProperty GetLocalizedPropertyById(int localizedPropertyId)
- {
- if (localizedPropertyId == 0)
- return null;
- return _localizedPropertyRepository.GetById(localizedPropertyId);
- }
- /// <summary>
- /// Find localized value
- /// </summary>
- /// <param name="languageId">Language identifier</param>
- /// <param name="entityId">Entity identifier</param>
- /// <param name="localeKeyGroup">Locale key group</param>
- /// <param name="localeKey">Locale key</param>
- /// <returns>Found localized value</returns>
- public virtual string GetLocalizedValue(int languageId, int entityId, string localeKeyGroup, string localeKey)
- {
- if (_localizationSettings.LoadAllLocalizedPropertiesOnStartup)
- {
- string key = string.Format(LOCALIZEDPROPERTY_KEY, languageId, entityId, localeKeyGroup, localeKey);
- return _cacheManager.Get(key, () =>
- {
- //load all records (we know they are cached)
- var source = GetAllLocalizedPropertiesCached();
- var query = from lp in source
- where lp.LanguageId == languageId &&
- lp.EntityId == entityId &&
- lp.LocaleKeyGroup == localeKeyGroup &&
- lp.LocaleKey == localeKey
- select lp.LocaleValue;
- var localeValue = query.FirstOrDefault();
- //little hack here. nulls aren't cacheable so set it to ""
- if (localeValue == null)
- localeValue = "";
- return localeValue;
- });
- }
- else
- {
- //gradual loading
- string key = string.Format(LOCALIZEDPROPERTY_KEY, languageId, entityId, localeKeyGroup, localeKey);
- return _cacheManager.Get(key, () =>
- {
- var source = _localizedPropertyRepository.Table;
- var query = from lp in source
- where lp.LanguageId == languageId &&
- lp.EntityId == entityId &&
- lp.LocaleKeyGroup == localeKeyGroup &&
- lp.LocaleKey == localeKey
- select lp.LocaleValue;
- var localeValue = query.FirstOrDefault();
- //little hack here. nulls aren't cacheable so set it to ""
- if (localeValue == null)
- localeValue = "";
- return localeValue;
- });
- }
- }
- /// <summary>
- /// Inserts a localized property
- /// </summary>
- /// <param name="localizedProperty">Localized property</param>
- public virtual void InsertLocalizedProperty(LocalizedProperty localizedProperty)
- {
- if (localizedProperty == null)
- throw new ArgumentNullException("localizedProperty");
- _localizedPropertyRepository.Insert(localizedProperty);
- //cache
- _cacheManager.RemoveByPattern(LOCALIZEDPROPERTY_PATTERN_KEY);
- }
- /// <summary>
- /// Updates the localized property
- /// </summary>
- /// <param name="localizedProperty">Localized property</param>
- public virtual void UpdateLocalizedProperty(LocalizedProperty localizedProperty)
- {
- if (localizedProperty == null)
- throw new ArgumentNullException("localizedProperty");
- _localizedPropertyRepository.Update(localizedProperty);
- //cache
- _cacheManager.RemoveByPattern(LOCALIZEDPROPERTY_PATTERN_KEY);
- }
- /// <summary>
- /// Save localized value
- /// </summary>
- /// <typeparam name="T">Type</typeparam>
- /// <param name="entity">Entity</param>
- /// <param name="keySelector">Key selector</param>
- /// <param name="localeValue">Locale value</param>
- /// <param name="languageId">Language ID</param>
- public virtual void SaveLocalizedValue<T>(T entity,
- Expression<Func<T, string>> keySelector,
- string localeValue,
- int languageId) where T : BaseEntity, ILocalizedEntity
- {
- SaveLocalizedValue<T, string>(entity, keySelector, localeValue, languageId);
- }
- public virtual void SaveLocalizedValue<T, TPropType>(T entity,
- Expression<Func<T, TPropType>> keySelector,
- TPropType localeValue,
- int languageId) where T : BaseEntity, ILocalizedEntity
- {
- if (entity == null)
- throw new ArgumentNullException("entity");
- if (languageId == 0)
- throw new ArgumentOutOfRangeException("languageId", "Language ID should not be 0");
- var member = keySelector.Body as MemberExpression;
- if (member == null)
- {
- throw new ArgumentException(string.Format(
- "Expression '{0}' refers to a method, not a property.",
- keySelector));
- }
- var propInfo = member.Member as PropertyInfo;
- if (propInfo == null)
- {
- throw new ArgumentException(string.Format(
- "Expression '{0}' refers to a field, not a property.",
- keySelector));
- }
- string localeKeyGroup = typeof(T).Name;
- string localeKey = propInfo.Name;
- var props = GetLocalizedProperties(entity.Id, localeKeyGroup);
- var prop = props.FirstOrDefault(lp => lp.LanguageId == languageId &&
- lp.LocaleKey.Equals(localeKey, StringComparison.InvariantCultureIgnoreCase)); //should be culture invariant
- var localeValueStr = CommonHelper.To<string>(localeValue);
- if (prop != null)
- {
- if (string.IsNullOrWhiteSpace(localeValueStr))
- {
- //delete
- DeleteLocalizedProperty(prop);
- }
- else
- {
- //update
- prop.LocaleValue = localeValueStr;
- UpdateLocalizedProperty(prop);
- }
- }
- else
- {
- if (!string.IsNullOrWhiteSpace(localeValueStr))
- {
- //insert
- prop = new LocalizedProperty
- {
- EntityId = entity.Id,
- LanguageId = languageId,
- LocaleKey = localeKey,
- LocaleKeyGroup = localeKeyGroup,
- LocaleValue = localeValueStr
- };
- InsertLocalizedProperty(prop);
- }
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement