Пример #1
0
        private static ProductAttributeLocalized DBMapping(DBProductAttributeLocalized dbItem)
        {
            if (dbItem == null)
            {
                return(null);
            }

            var item = new ProductAttributeLocalized();

            item.ProductAttributeLocalizedId = dbItem.ProductAttributeLocalizedId;
            item.ProductAttributeId          = dbItem.ProductAttributeId;
            item.LanguageId  = dbItem.LanguageId;
            item.Name        = dbItem.Name;
            item.Description = dbItem.Description;

            return(item);
        }
        /// <summary>
        /// Update a localized product attribute
        /// </summary>
        /// <param name="productAttributeLocalized">Localized product attribute</param>
        public void UpdateProductAttributeLocalized(ProductAttributeLocalized productAttributeLocalized)
        {
            if (productAttributeLocalized == null)
            {
                throw new ArgumentNullException("productAttributeLocalized");
            }

            productAttributeLocalized.Name        = CommonHelper.EnsureNotNull(productAttributeLocalized.Name);
            productAttributeLocalized.Name        = CommonHelper.EnsureMaximumLength(productAttributeLocalized.Name, 100);
            productAttributeLocalized.Description = CommonHelper.EnsureNotNull(productAttributeLocalized.Description);
            productAttributeLocalized.Description = CommonHelper.EnsureMaximumLength(productAttributeLocalized.Description, 400);

            bool allFieldsAreEmpty = string.IsNullOrEmpty(productAttributeLocalized.Name) &&
                                     string.IsNullOrEmpty(productAttributeLocalized.Description);


            if (!_context.IsAttached(productAttributeLocalized))
            {
                _context.ProductAttributeLocalized.Attach(productAttributeLocalized);
            }

            if (allFieldsAreEmpty)
            {
                //delete if all fields are empty
                _context.DeleteObject(productAttributeLocalized);
                _context.SaveChanges();
            }
            else
            {
                _context.SaveChanges();
            }

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(PRODUCTATTRIBUTES_PATTERN_KEY);
                _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTES_PATTERN_KEY);
                _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTEVALUES_PATTERN_KEY);
            }
        }
        /// <summary>
        /// Inserts a localized product attribute
        /// </summary>
        /// <param name="productAttributeLocalized">Localized product attribute</param>
        public void InsertProductAttributeLocalized(ProductAttributeLocalized productAttributeLocalized)
        {
            if (productAttributeLocalized == null)
            {
                throw new ArgumentNullException("productAttributeLocalized");
            }

            productAttributeLocalized.Name        = CommonHelper.EnsureNotNull(productAttributeLocalized.Name);
            productAttributeLocalized.Name        = CommonHelper.EnsureMaximumLength(productAttributeLocalized.Name, 100);
            productAttributeLocalized.Description = CommonHelper.EnsureNotNull(productAttributeLocalized.Description);
            productAttributeLocalized.Description = CommonHelper.EnsureMaximumLength(productAttributeLocalized.Description, 400);



            _context.ProductAttributeLocalized.AddObject(productAttributeLocalized);
            _context.SaveChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(PRODUCTATTRIBUTES_PATTERN_KEY);
                _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTES_PATTERN_KEY);
                _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTEVALUES_PATTERN_KEY);
            }
        }
        protected void SaveLocalizableContent(ProductAttribute productAttribute)
        {
            if (productAttribute == null)
                return;

            if (!this.HasLocalizableContent)
                return;

            foreach (RepeaterItem item in rptrLanguageDivs.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    var txtLocalizedName = (TextBox)item.FindControl("txtLocalizedName");
                    var txtLocalizedDescription = (TextBox)item.FindControl("txtLocalizedDescription");
                    var lblLanguageId = (Label)item.FindControl("lblLanguageId");

                    int languageId = int.Parse(lblLanguageId.Text);
                    string name = txtLocalizedName.Text;
                    string description = txtLocalizedDescription.Text;

                    bool allFieldsAreEmpty = (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(description));

                    var content = this.ProductAttributeService.GetProductAttributeLocalizedByProductAttributeIdAndLanguageId(productAttribute.ProductAttributeId, languageId);
                    if (content == null)
                    {
                        if (!allFieldsAreEmpty && languageId > 0)
                        {
                            //only insert if one of the fields are filled out (avoid too many empty records in db...)
                            content = new ProductAttributeLocalized()
                            {
                                ProductAttributeId = productAttribute.ProductAttributeId,
                                LanguageId = languageId,
                                Name = name,
                                Description = description
                            };
                            this.ProductAttributeService.InsertProductAttributeLocalized(content);
                        }
                    }
                    else
                    {
                        if (languageId > 0)
                        {
                            content.LanguageId = languageId;
                            content.Name = name;
                            content.Description = description;
                            this.ProductAttributeService.UpdateProductAttributeLocalized(content);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Update a localized product attribute
        /// </summary>
        /// <param name="productAttributeLocalized">Localized product attribute</param>
        public void UpdateProductAttributeLocalized(ProductAttributeLocalized productAttributeLocalized)
        {
            if (productAttributeLocalized == null)
                throw new ArgumentNullException("productAttributeLocalized");

            productAttributeLocalized.Name = CommonHelper.EnsureNotNull(productAttributeLocalized.Name);
            productAttributeLocalized.Name = CommonHelper.EnsureMaximumLength(productAttributeLocalized.Name, 100);
            productAttributeLocalized.Description = CommonHelper.EnsureNotNull(productAttributeLocalized.Description);
            productAttributeLocalized.Description = CommonHelper.EnsureMaximumLength(productAttributeLocalized.Description, 400);

            bool allFieldsAreEmpty = string.IsNullOrEmpty(productAttributeLocalized.Name) &&
                string.IsNullOrEmpty(productAttributeLocalized.Description);

            if (!_context.IsAttached(productAttributeLocalized))
                _context.ProductAttributeLocalized.Attach(productAttributeLocalized);

            if (allFieldsAreEmpty)
            {
                //delete if all fields are empty
                _context.DeleteObject(productAttributeLocalized);
                _context.SaveChanges();
            }
            else
            {
                _context.SaveChanges();
            }

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(PRODUCTATTRIBUTES_PATTERN_KEY);
                _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTES_PATTERN_KEY);
                _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTEVALUES_PATTERN_KEY);
            }
        }
        /// <summary>
        /// Inserts a localized product attribute
        /// </summary>
        /// <param name="productAttributeLocalized">Localized product attribute</param>
        public void InsertProductAttributeLocalized(ProductAttributeLocalized productAttributeLocalized)
        {
            if (productAttributeLocalized == null)
                throw new ArgumentNullException("productAttributeLocalized");

            productAttributeLocalized.Name = CommonHelper.EnsureNotNull(productAttributeLocalized.Name);
            productAttributeLocalized.Name = CommonHelper.EnsureMaximumLength(productAttributeLocalized.Name, 100);
            productAttributeLocalized.Description = CommonHelper.EnsureNotNull(productAttributeLocalized.Description);
            productAttributeLocalized.Description = CommonHelper.EnsureMaximumLength(productAttributeLocalized.Description, 400);

            _context.ProductAttributeLocalized.AddObject(productAttributeLocalized);
            _context.SaveChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(PRODUCTATTRIBUTES_PATTERN_KEY);
                _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTES_PATTERN_KEY);
                _cacheManager.RemoveByPattern(PRODUCTVARIANTATTRIBUTEVALUES_PATTERN_KEY);
            }
        }