Пример #1
0
        /// <summary>
        /// Addresses the component generator.
        /// </summary>
        /// <param name="addressId">The address identifier.</param>
        /// <param name="profileDefinition">The profile definition.</param>
        /// <param name="context">The context.</param>
        /// <returns>
        /// A <see cref="AddressComponent" />
        /// </returns>
        internal static async Task <AddressComponent> AddressComponentGenerator(string addressId, ProfileDefinition profileDefinition, CommercePipelineExecutionContext context)
        {
            // map properties to AddressComponent
            var addressType      = context.GetPolicy <ProfilePropertiesPolicy>().AddressType;
            var addressComponent = new AddressComponent {
                Name = addressType
            };

            try
            {
                var sqlContext = ConnectionHelper.GetProfilesSqlContext(context.CommerceContext);
                var address    = await sqlContext.GetAddress(addressId);

                addressComponent.Id = addressId;

                var addressProperties = context.GetPolicy <ProfilePropertiesMappingPolicy>().AddressProperties;
                var details           = new EntityView {
                    Name = "Details"
                };

                foreach (var property in profileDefinition.Properties)
                {
                    if (addressProperties.ContainsKey($"{property.GroupName}.{property.Name}"))
                    {
                        var rawValue = address[property.ColumnName] as string;
                        if (string.IsNullOrEmpty(rawValue) || property.Name.Equals("address_id", StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        var addressProperty = addressProperties[$"{property.GroupName}.{property.Name}"];
                        var propertyInfo    = addressComponent.Party.GetType().GetProperty(addressProperty, BindingFlags.Public | BindingFlags.Instance);
                        if (propertyInfo != null && propertyInfo.CanWrite)
                        {
                            propertyInfo.SetValue(addressComponent.Party, rawValue, null);
                        }
                        else
                        {
                            TypeConverter typeConverter = TypeDescriptor.GetConverter(property.OriginalType);
                            var           profileValue  = typeConverter.ConvertFromString(rawValue);
                            details.Properties.Add(new ViewProperty {
                                Name = addressProperty, RawValue = profileValue
                            });
                        }
                    }
                }

                if (details.Properties.Any())
                {
                    addressComponent.View.ChildViews.Add(details);
                }
            }
            catch (Exception ex)
            {
                await context.CommerceContext.AddMessage(
                    context.GetPolicy <KnownResultCodes>().Error,
                    "EntityNotFound",
                    new object[] { addressId, ex },
                    $"Address { addressId } was not found.");

                return(null);
            }

            return(addressComponent);
        }