Exemplo n.º 1
0
        public async Task <string> GetLookupDisplayNameAsync(GetLookupDisplayNameParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            var splitLookups        = param.Value.Split('|');
            var lookupStringBuilder = new StringBuilder();

            for (int i = 0; i < splitLookups.Length; i++)
            {
                var response = await GetDistinctLookup(param.LookupType, param.LookupName, splitLookups[i], param.CultureInfo).ConfigureAwait(false);

                if (response != null)
                {
                    // don't add a space before the first lookup
                    if (i != 0)
                    {
                        lookupStringBuilder.Append(param.Delimiter);
                    }
                    lookupStringBuilder.Append(response);
                }
            }

            return(lookupStringBuilder.ToString());
        }
Exemplo n.º 2
0
        public async Task WHEN_Lookup_Value_Does_Not_Exist_SHOULD_Return_Empty_String()
        {
            var param = new GetLookupDisplayNameParam
            {
                LookupType  = LookupType.Customer,
                LookupName  = "Size",
                Value       = "potato",
                CultureInfo = new CultureInfo("en-CA")
            };
            var lookup = await _lookupService.GetLookupDisplayNameAsync(param);

            lookup.Should().Be(string.Empty);
        }
Exemplo n.º 3
0
        public async Task WHEN_Lookup_Value_Exists_SHOULD_Return_Value()
        {
            var param = new GetLookupDisplayNameParam
            {
                LookupType  = LookupType.Customer,
                LookupName  = "Size",
                Value       = "xlarge",
                CultureInfo = new CultureInfo("en-CA")
            };
            var lookup = await _lookupService.GetLookupDisplayNameAsync(param);

            lookup.Should().Be("X-Large");
        }
Exemplo n.º 4
0
        public async Task WHEN_Lookup_Has_Multiple_Values_SHOULD_Return_Comma_Space_Delimited()
        {
            var param = new GetLookupDisplayNameParam
            {
                LookupType  = LookupType.Customer,
                LookupName  = "Size",
                Value       = "small|medium",
                CultureInfo = new CultureInfo("fr-CA")
            };

            var lookup =
                await
                _lookupService.GetLookupDisplayNameAsync(param);

            lookup.Should().Be("Petit, Moyen");
        }
Exemplo n.º 5
0
        public void WHEN_LookupName_Does_Not_Exist_SHOULD_Throw_InvalidOperationException()
        {
            var repository = Container.GetMock <ILookupRepository>();

            repository.Setup(m => m.GetLookupAsync(It.IsAny <string>())).ReturnsAsync(null);
            Container.GetMock <ILookupRepositoryFactory>()
            .Setup(m => m.CreateLookupRepository(It.IsAny <LookupType>()))
            .Returns(repository.Object);


            var param = new GetLookupDisplayNameParam
            {
                LookupType  = LookupType.Customer,
                LookupName  = "Size",
                Value       = "small|medium",
                CultureInfo = new CultureInfo("fr-CA")
            };

            _lookupService.Awaiting(async ls => await ls.GetLookupDisplayNameAsync(param)).ShouldThrow <InvalidOperationException>();
        }
        private object MapTo(Type viewModelType, object source, CultureInfo culture)
        {
            if (source == null)
            {
                return(null);
            }

            if (!typeof(BaseViewModel).IsAssignableFrom(viewModelType))
            {
                throw new ArgumentException(string.Format("Type '{0}' must derive from BaseViewModel", viewModelType.FullName), "viewModelType");
            }

            var sourceType           = source.GetType();
            var sourceTypeProperties = sourceType.GetProperties();
            var sourcePropertyBag    = GetPropertyBagFromObject(source, sourceTypeProperties);
            var viewModelProperties  = _metadataRegistry.GetViewModelMetadata(viewModelType);
            var viewModel            = (BaseViewModel)Activator.CreateInstance(viewModelType);

            foreach (var viewModelProperty in viewModelProperties)
            {
                var sourceProperty        = sourceTypeProperties.FirstOrDefault(p => p.Name == viewModelProperty.SourcePropertyName);
                var sourcePropertyMatches = sourceProperty != null;

                if (sourcePropertyMatches || IsViewModelPropertyInSourcePropertyBag(sourcePropertyBag, viewModelProperty))
                {
                    object viewModelPropertyValue = null;
                    if (sourcePropertyMatches)
                    {
                        viewModelPropertyValue = MapPropertyFromObject(source, sourceProperty, viewModelProperty, culture);
                    }
                    else if (IsViewModelPropertyInSourcePropertyBag(sourcePropertyBag, viewModelProperty))
                    {
                        viewModelPropertyValue = MapPropertyFromPropertyBag(sourcePropertyBag, viewModelProperty, culture);
                    }

                    if (viewModelProperty.LookupProperty && viewModelPropertyValue != null)
                    {
                        var param = new GetLookupDisplayNameParam
                        {
                            LookupType  = viewModelProperty.LookupType,
                            LookupName  = viewModelProperty.LookupName,
                            Value       = viewModelPropertyValue.ToString(),
                            CultureInfo = culture,
                            Delimiter   = viewModelProperty.LookupDelimiter
                        };
                        // calling blocking .Result because we don't want otmake the mapper async
                        viewModelPropertyValue = _lookupService.GetLookupDisplayNameAsync(param).Result;
                    }
                    var formattedViewModelPropertyValue = LocalizeValue(viewModelPropertyValue, viewModelProperty, culture);
                    formattedViewModelPropertyValue = FormatValue(formattedViewModelPropertyValue, viewModelProperty, culture);
                    viewModelProperty.SetValue(viewModel, formattedViewModelPropertyValue);
                }
                else
                {
                    // Setting null in the case of value types will set its default value instead
                    // See https://msdn.microsoft.com/en-ca/library/xb5dd1f1(v=vs.110).aspx in Remarks section
                    viewModelProperty.SetValue(viewModel, null);
                }
            }
            return(viewModel);
        }
Exemplo n.º 7
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);
        }