예제 #1
0
        private static ProductAttribute DBMapping(DBProductAttribute dbItem)
        {
            if (dbItem == null)
            {
                return(null);
            }

            var item = new ProductAttribute();

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

            return(item);
        }
        private static ProductAttributeCollection DBMapping(DBProductAttributeCollection dbCollection)
        {
            if (dbCollection == null)
            {
                return(null);
            }

            ProductAttributeCollection collection = new ProductAttributeCollection();

            foreach (DBProductAttribute dbItem in dbCollection)
            {
                ProductAttribute item = DBMapping(dbItem);
                collection.Add(item);
            }

            return(collection);
        }
        /// <summary>
        /// Updates the product attribute
        /// </summary>
        /// <param name="ProductAttributeID">Product attribute identifier</param>
        /// <param name="Name">The name</param>
        /// <param name="Description">The description</param>
        /// <returns>Product attribute </returns>
        public static ProductAttribute UpdateProductAttribute(int ProductAttributeID, string Name,
                                                              string Description)
        {
            DBProductAttribute dbItem = DBProviderManager <DBProductAttributeProvider> .Provider.UpdateProductAttribute(ProductAttributeID,
                                                                                                                        Name, Description);

            ProductAttribute productAttribute = DBMapping(dbItem);

            if (ProductAttributeManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(PRODUCTATTRIBUTES_PATTERN_KEY);
                NopCache.RemoveByPattern(PRODUCTVARIANTATTRIBUTES_PATTERN_KEY);
                NopCache.RemoveByPattern(PRODUCTVARIANTATTRIBUTEVALUES_PATTERN_KEY);
            }

            return(productAttribute);
        }
        public ProductAttribute SaveInfo()
        {
            ProductAttribute productAttribute = this.ProductAttributeService.GetProductAttributeById(this.ProductAttributeId);

            if (productAttribute != null)
            {
                productAttribute.Name = txtName.Text;
                productAttribute.Description = txtDescription.Text;
                this.ProductAttributeService.UpdateProductAttribute(productAttribute);
            }
            else
            {
                productAttribute = new ProductAttribute()
                {
                    Name = txtName.Text,
                    Description = txtDescription.Text
                };
                this.ProductAttributeService.InsertProductAttribute(productAttribute);
            }

            SaveLocalizableContent(productAttribute);

            return productAttribute;
        }
        /// <summary>
        /// Inserts a product attribute
        /// </summary>
        /// <param name="productAttribute">Product attribute</param>
        public void InsertProductAttribute(ProductAttribute productAttribute)
        {
            if (productAttribute == null)
            {
                throw new ArgumentNullException("productAttribute");
            }

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



            _context.ProductAttributes.AddObject(productAttribute);
            _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>
        /// Updates the product attribute
        /// </summary>
        /// <param name="productAttribute">Product attribute</param>
        public void UpdateProductAttribute(ProductAttribute productAttribute)
        {
            if (productAttribute == null)
                throw new ArgumentNullException("productAttribute");

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

            if (!_context.IsAttached(productAttribute))
                _context.ProductAttributes.Attach(productAttribute);

            _context.SaveChanges();

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