public static bool IsIncluded(this ProductPropertyDefinition property)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            return(!SpecificationConfiguration.ExcludedSpecificationProperty.Contains(property.PropertyName));
        }
Пример #2
0
        protected virtual string GetSpecificationsAttributeValue(ProductPropertyDefinition property, GetProductSpecificationsParam param)
        {
            var variantProperty = param.Product.Variants.Find(v => v.Id == param.VariantId)?.PropertyBag;

            if (variantProperty?.ContainsKey(property.PropertyName) ?? false)
            {
                return(ProductFormatter.FormatValue(property, variantProperty[property.PropertyName], Context.CultureInfo));
            }

            if (param.Product.PropertyBag?.ContainsKey(property.PropertyName) ?? false)
            {
                return(ProductFormatter.FormatValue(property, param.Product.PropertyBag[property.PropertyName], Context.CultureInfo));
            }

            return(string.Empty);
        }
        [TestCase(PropertyDataType.Boolean, "not a boolean", "Default Localized Text Format")] //Fallback to text
        public void WHEN_Passing_Unknown_PropertyName_For_Boolean_SHOULD_Format_Using_BasePropertyTypeKey(PropertyDataType dataType, object value, string expectedFormatted)
        {
            //Arrange
            ProductFormatter          productFormatter = _container.CreateInstance <ProductFormatter>();
            ProductPropertyDefinition ppd = new ProductPropertyDefinition
            {
                PropertyName = GetRandom.String(32),
                DataType     = PropertyDataType.Boolean
            };

            //Act
            string formatted = productFormatter.FormatValue(ppd, value, GetRandomCulture());

            //Assert
            formatted.Should().BeEquivalentTo(expectedFormatted);
        }
        public void WHEN_Passing_Unknown_PropertyName_For_Unknown_DataType_SHOULD_Fallback_To_BasePropertyTypeKey_For_Text()
        {
            //Arrange
            ProductFormatter          productFormatter = _container.CreateInstance <ProductFormatter>();
            ProductPropertyDefinition ppd = new ProductPropertyDefinition
            {
                PropertyName = GetRandom.String(32),
                DataType     = (PropertyDataType)int.MinValue
            };
            object value = new object();

            //Act
            string formatted = productFormatter.FormatValue(ppd, value, GetRandomCulture());

            //Assert
            formatted.Should().BeEquivalentTo("Default Localized Text Format", "This is the default fallback for unknown DataTypes");
        }
        public void WHEN_Passing_Known_PropertyName_SHOULD_Use_Known_Format(PropertyDataType dataType, string expectedFormatted)
        {
            //Arrange
            ProductFormatter          productFormatter = _container.CreateInstance <ProductFormatter>();
            ProductPropertyDefinition ppd = new ProductPropertyDefinition
            {
                PropertyName = "KnownPropertyName",
                DataType     = dataType
            };
            object value = new object();

            //Act
            string formatted = productFormatter.FormatValue(ppd, value, GetRandomCulture());

            //Assert
            formatted.Should().BeEquivalentTo(expectedFormatted);
        }
Пример #6
0
        public virtual string FormatValue(ProductPropertyDefinition property, object value, CultureInfo cultureInfo)
        {
            var valueText = string.Empty;

            switch (property.DataType)
            {
            case PropertyDataType.Boolean:
                if (value is bool)
                {
                    if ((bool)value)
                    {
                        valueText = FormatValueByType(value, string.Format("{0}True", property.PropertyName),
                                                      BasePropertyTypeBooleanTrueResourceKey, cultureInfo);
                    }
                    else
                    {
                        valueText = FormatValueByType(value, string.Format("{0}False", property.PropertyName),
                                                      BasePropertyTypeBooleanFalseResourceKey, cultureInfo);
                    }
                }
                else
                {
                    valueText = FormatValueByType(value, property.PropertyName, BasePropertyTypeTextResourceKey,
                                                  cultureInfo);
                }
                break;

            case PropertyDataType.Currency:
                valueText = FormatValueByType(value, property.PropertyName, BasePropertyTypeCurrencyResourceKey, cultureInfo);
                break;

            case PropertyDataType.Decimal:
                valueText = FormatValueByType(value, property.PropertyName, BasePropertyTypeDecimalResourceKey, cultureInfo);
                break;

            case PropertyDataType.Number:
                valueText = FormatValueByType(value, property.PropertyName, BasePropertyTypeNumberResourceKey, cultureInfo);
                break;

            case PropertyDataType.DateTime:
                if (value is DateTime)
                {
                    valueText = ((DateTime)value).ToShortDateString();
                }
                else
                {
                    valueText = value.ToString();
                }
                valueText = FormatValueByType(valueText, property.PropertyName, BasePropertyTypeDateTimeResourceKey, cultureInfo);
                break;

            case PropertyDataType.Text:
                if (property.Localizable && value is LocalizedString)
                {
                    valueText = FormatValueByType(((LocalizedString)value).GetLocalizedValue(cultureInfo.Name),
                                                  property.PropertyName, BasePropertyTypeTextResourceKey, cultureInfo);
                }
                else
                {
                    valueText = FormatValueByType(value, property.PropertyName, BasePropertyTypeTextResourceKey,
                                                  cultureInfo);
                }
                break;

            case PropertyDataType.Lookup:
                var param = new GetLookupDisplayNameParam
                {
                    CultureInfo = cultureInfo,
                    // since this isn't ViewModel mapping, we con't have access to the delimiter declared as an Attribute
                    Delimiter  = SpecificationConfiguration.MultiValueLookupSeparator + " ",
                    LookupType = LookupType.Product,
                    LookupName = property.LookupDefinition.LookupName,
                    Value      = value.ToString()
                };
                // call this synchronously to avoid async calls up the stack
                // the request is cached, so this shouldn't be a performance burden
                valueText = _lookupService.GetLookupDisplayNameAsync(param).Result;

                valueText = FormatValueByType(valueText, property.PropertyName, BasePropertyTypeLookupResourceKey, cultureInfo);
                break;

            default:
                valueText = FormatValueByType(value, property.PropertyName, BasePropertyTypeTextResourceKey, cultureInfo);
                break;
            }
            return(valueText);
        }
Пример #7
0
        private static ProductDefinition GenerateProductDefinitionWithKva()
        {
            var productDefinition = new ProductDefinition();

            productDefinition.VariantProperties = new List <ProductPropertyDefinition>();

            var color = new ProductPropertyDefinition();

            color.PropertyName = "Colour";
            color.DisplayName  = new LocalizedString();
            color.DisplayName.Add("en-US", "Colors");
            color.DisplayName.Add("fr-CA", "Couleurs");
            color.IsRequired      = true;
            color.DisplayOrder    = 0;
            color.DataType        = PropertyDataType.Text;
            color.DefaultValue    = "Black";
            color.IsKeyVariant    = true;
            color.KeyVariantOrder = 0;

            var size = new ProductPropertyDefinition();

            size.PropertyName = "Size";
            size.DisplayName  = new LocalizedString();
            size.DisplayName.Add("en-US", "Sizes");
            size.DisplayName.Add("fr-CA", "Tailles");
            size.IsRequired = true;
            // size.DisplayOrder = 1;
            size.DisplayOrder     = 0;
            size.DataType         = PropertyDataType.Lookup;
            size.DefaultValue     = "Medium";
            size.IsKeyVariant     = true;
            size.KeyVariantOrder  = 1;
            size.LookupDefinition = new ProductLookupDefinition
            {
                LookupName = "SizeLookup"
            };

            var type = new ProductPropertyDefinition();

            type.PropertyName = "Type";
            type.DisplayName  = new LocalizedString();
            type.DisplayName.Add("en-US", "Types");
            type.DisplayName.Add("fr-CA", "Types");
            type.IsRequired = true;
            // type.DisplayOrder = 2;
            type.DisplayOrder    = 0;
            type.DataType        = PropertyDataType.Text;
            type.DefaultValue    = "A";
            type.IsKeyVariant    = true;
            type.KeyVariantOrder = 2;

            var lookup = new ProductPropertyDefinition();

            lookup.PropertyName = "Lookup1";
            lookup.DisplayName  = new LocalizedString();
            lookup.DisplayName.Add("en-US", "Look Up Title EN");
            lookup.DisplayName.Add("fr-CA", "Look Up Title FR");
            lookup.IsRequired = true;
            //lookup.DisplayOrder = 3;
            lookup.DisplayOrder     = 0;
            lookup.DataType         = PropertyDataType.Lookup;
            lookup.DefaultValue     = null;
            lookup.IsKeyVariant     = true;
            lookup.KeyVariantOrder  = 3;
            lookup.LookupDefinition = new ProductLookupDefinition
            {
                LookupName = "Lookup1Lookup"
            };

            productDefinition.VariantProperties.Add(size);   // Display Order : 1
            productDefinition.VariantProperties.Add(color);  // Display Order : 0
            productDefinition.VariantProperties.Add(type);   // Display Order : 2
            productDefinition.VariantProperties.Add(lookup); // Display Order : 3

            return(productDefinition);
        }
        private static object GetLocalizedKvaDisplayValueFromValue(string cultureName, Variant variant, ProductPropertyDefinition keyVariantAttribute)
        {
            var value     = variant.PropertyBag[keyVariantAttribute.PropertyName];
            var localized = value as LocalizedString;

            if (localized == null)
            {
                return(value);
            }

            var localizedValue = localized.GetLocalizedValue(cultureName);

            if (!string.IsNullOrWhiteSpace(localizedValue))
            {
                return(localizedValue);
            }

            return(variant.PropertyBag[keyVariantAttribute.PropertyName]);
        }
        private static string GetLocalizedKvaDisplayValueFromLookup(Lookup lookup, string cultureName, Variant variant, ProductPropertyDefinition keyVariantAttribute)
        {
            if (lookup == null)
            {
                return(variant.PropertyBag[keyVariantAttribute.PropertyName] as string);
            }

            var firstOrDefault =
                lookup.Values.FirstOrDefault(x => x.Value.Equals(variant.PropertyBag[keyVariantAttribute.PropertyName]));

            if (firstOrDefault == null || firstOrDefault.DisplayName == null)
            {
                return(variant.PropertyBag[keyVariantAttribute.PropertyName] as string);
            }

            var localizedValue = firstOrDefault.DisplayName.GetLocalizedValue(cultureName);

            return(!string.IsNullOrWhiteSpace(localizedValue) ? localizedValue : firstOrDefault.Value);
        }