public IActionResult GetLocalizedProperties(int entityId, string localeKeyGroup)
        {
            var entities = _localizedEntityService.GetLocalizedProperties(entityId, localeKeyGroup);

            //var model = entities.ToModels();
            return(Ok(entities));
        }
        public virtual int CopyAttributeOptions(ProductVariantAttribute productVariantAttribute, int productAttributeOptionsSetId, bool deleteExistingValues)
        {
            Guard.NotNull(productVariantAttribute, nameof(productVariantAttribute));
            Guard.NotZero(productVariantAttribute.Id, nameof(productVariantAttribute.Id));
            Guard.NotZero(productAttributeOptionsSetId, nameof(productAttributeOptionsSetId));

            if (deleteExistingValues)
            {
                var existingValues = productVariantAttribute.ProductVariantAttributeValues.ToList();
                if (!existingValues.Any())
                {
                    existingValues = GetProductVariantAttributeValues(productVariantAttribute.Id).ToList();
                }

                existingValues.Each(x => DeleteProductVariantAttributeValue(x));
            }

            var result           = 0;
            var attributeOptions = _productAttributeOptionRepository.TableUntracked
                                   .Where(x => x.ProductAttributeOptionsSetId == productAttributeOptionsSetId)
                                   .ToList();

            if (!attributeOptions.Any())
            {
                return(result);
            }

            // Do not insert already existing values (identified by name field).
            var existingValueNames = new HashSet <string>(_productVariantAttributeValueRepository.TableUntracked
                                                          .Where(x => x.ProductVariantAttributeId == productVariantAttribute.Id)
                                                          .Select(x => x.Name)
                                                          .ToList());

            Picture picture = null;
            ProductVariantAttributeValue productVariantAttributeValue = null;
            var pictureIds = attributeOptions.Where(x => x.PictureId != 0).Select(x => x.PictureId).Distinct().ToArray();
            var pictures   = _pictureService.GetPicturesByIds(pictureIds, true).ToDictionarySafe(x => x.Id);

            using (_localizedEntityService.BeginScope())
            {
                foreach (var option in attributeOptions)
                {
                    if (existingValueNames.Contains(option.Name))
                    {
                        continue;
                    }

                    productVariantAttributeValue           = option.Clone();
                    productVariantAttributeValue.PictureId = 0;
                    productVariantAttributeValue.ProductVariantAttributeId = productVariantAttribute.Id;

                    // Copy picture.
                    if (option.PictureId != 0 && pictures.TryGetValue(option.PictureId, out picture))
                    {
                        var pictureBinary = _pictureService.LoadPictureBinary(picture);

                        var newPicture = _pictureService.InsertPicture(
                            pictureBinary,
                            picture.MimeType,
                            picture.SeoFilename,
                            picture.IsNew,
                            picture.Width ?? 0,
                            picture.Height ?? 0,
                            picture.IsTransient
                            );

                        productVariantAttributeValue.PictureId = newPicture.Id;
                    }

                    // No scope commit, we need new entity id.
                    _productVariantAttributeValueRepository.Insert(productVariantAttributeValue);
                    ++result;

                    // Copy localized properties too.
                    var optionProperties = _localizedEntityService.GetLocalizedProperties(option.Id, "ProductAttributeOption");

                    foreach (var property in optionProperties)
                    {
                        _localizedEntityService.InsertLocalizedProperty(new LocalizedProperty
                        {
                            EntityId       = productVariantAttributeValue.Id,
                            LocaleKeyGroup = "ProductVariantAttributeValue",
                            LocaleKey      = property.LocaleKey,
                            LocaleValue    = property.LocaleValue,
                            LanguageId     = property.LanguageId
                        });
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        public virtual T LoadSetting <T>() where T : ISettings, new()
        {
            // Try get result from cache
            if (_memoryCache.Contains(CacheTags.Setting, typeof(T).FullName))
            {
                return(_memoryCache.GetObject <T>(CacheTags.Setting, typeof(T).FullName));
            }

            var settings = Activator.CreateInstance <T>();

            foreach (var prop in typeof(T).GetProperties())
            {
                if (!prop.CanRead || !prop.CanWrite)
                {
                    continue;
                }

                if (prop.PropertyType == typeof(LocalizedString))
                {
                    string key     = typeof(T).Name + "." + prop.Name;
                    var    setting = FindByKey <string>(key);
                    if (setting == null)
                    {
                        continue;
                    }

                    var value = new LocalizedString(setting);
                    foreach (var localizedProperty in _localizedEntityService.GetLocalizedProperties(0, typeof(T).Name, prop.Name))
                    {
                        value.Add(localizedProperty.LanguageId, localizedProperty.LocaleValue);
                    }
                    prop.SetValue(settings, value, null);
                }
                else
                {
                    var key     = typeof(T).Name + "." + prop.Name;
                    var setting = FindByKey <string>(key);
                    if (setting == null)
                    {
                        continue;
                    }

                    if (TypeDescriptor.GetConverter(prop.PropertyType).CanConvertFrom(typeof(string)))
                    {
                        if (!TypeDescriptor.GetConverter(prop.PropertyType).IsValid(setting))
                        {
                            continue;
                        }

                        object value = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromInvariantString(setting);

                        prop.SetValue(settings, value, null);
                    }
                    else if (prop.PropertyType.IsClass && prop.PropertyType.IsSerializable)
                    {
                        try
                        {
                            var obj = setting.JsonToObject(prop.PropertyType);
                            prop.SetValue(settings, obj, null);
                        }
                        catch
                        {
                        }
                    }
                }
            }

            _memoryCache.AddObject(CacheTags.Setting, settings, TimeSpan.FromDays(30), typeof(T).FullName);

            return(settings);
        }
        public virtual int CopyAttributeOptions(ProductVariantAttribute productVariantAttribute, int productAttributeOptionsSetId, bool deleteExistingValues)
        {
            Guard.NotNull(productVariantAttribute, nameof(productVariantAttribute));
            Guard.NotZero(productVariantAttribute.Id, nameof(productVariantAttribute.Id));
            Guard.NotZero(productAttributeOptionsSetId, nameof(productAttributeOptionsSetId));

            if (deleteExistingValues)
            {
                var existingValues = productVariantAttribute.ProductVariantAttributeValues.ToList();
                if (!existingValues.Any())
                {
                    existingValues = GetProductVariantAttributeValues(productVariantAttribute.Id).ToList();
                }

                existingValues.Each(x => DeleteProductVariantAttributeValue(x));
            }

            var result           = 0;
            var attributeOptions = _productAttributeOptionRepository.TableUntracked
                                   .Where(x => x.ProductAttributeOptionsSetId == productAttributeOptionsSetId)
                                   .ToList();

            if (!attributeOptions.Any())
            {
                return(result);
            }

            // Do not insert already existing values (identified by name field).
            var existingValueNames = new HashSet <string>(_productVariantAttributeValueRepository.TableUntracked
                                                          .Where(x => x.ProductVariantAttributeId == productVariantAttribute.Id)
                                                          .Select(x => x.Name)
                                                          .ToList());

            using (_localizedEntityService.BeginScope())
            {
                foreach (var option in attributeOptions)
                {
                    if (existingValueNames.Contains(option.Name))
                    {
                        continue;
                    }

                    var pvav = option.Clone();
                    pvav.ProductVariantAttributeId = productVariantAttribute.Id;

                    // No scope commit, we need new entity id.
                    _productVariantAttributeValueRepository.Insert(pvav);
                    ++result;

                    // Copy localized properties too.
                    var optionProperties = _localizedEntityService.GetLocalizedProperties(option.Id, "ProductAttributeOption");

                    foreach (var property in optionProperties)
                    {
                        _localizedEntityService.InsertLocalizedProperty(new LocalizedProperty
                        {
                            EntityId       = pvav.Id,
                            LocaleKeyGroup = "ProductVariantAttributeValue",
                            LocaleKey      = property.LocaleKey,
                            LocaleValue    = property.LocaleValue,
                            LanguageId     = property.LanguageId
                        });
                    }
                }
            }

            return(result);
        }