/// <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).ConfigureAwait(false);

                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.").ConfigureAwait(false);

                return(null);
            }

            return(addressComponent);
        }
        /// <summary>
        /// Customers the details generator.
        /// </summary>
        /// <param name="customerData">The customer data.</param>
        /// <param name="customer">The customer.</param>
        /// <param name="profileDefinition">The profile definition.</param>
        /// <param name="context">The context.</param>
        /// <returns>
        /// A <see cref="Customer" />
        /// </returns>
        internal static Customer CustomerDetailsGenerator(DataRow customerData, Customer customer, ProfileDefinition profileDefinition, CommercePipelineExecutionContext context)
        {
            var profileProperties     = context.GetPolicy <ProfilePropertiesPolicy>();
            var userMappingProperties = context.GetPolicy <ProfilePropertiesMappingPolicy>().UserProperties;

            var details = new EntityView {
                Name = "Details"
            };

            foreach (var property in profileDefinition?.Properties)
            {
                if (property.Name.Equals(profileProperties?.AccountNumber, StringComparison.OrdinalIgnoreCase) ||
                    property.Name.Equals(profileProperties?.UserIdProperty, StringComparison.OrdinalIgnoreCase) ||
                    property.Name.Equals(profileProperties?.PasswordProperty, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                TypeConverter typeConverter = TypeDescriptor.GetConverter(property.OriginalType);
                var           profileValue  = typeConverter.ConvertFromString(customerData[property.ColumnName] as string);

                if (property.Name.Equals(profileProperties?.AccountStatusProperty, StringComparison.OrdinalIgnoreCase))
                {
                    customer.AccountStatus = profileValue.ToString().Equals("0", StringComparison.Ordinal) ? context.GetPolicy <KnownCustomersStatusesPolicy>()?.InactiveAccount : context.GetPolicy <KnownCustomersStatusesPolicy>()?.RequiresApproval;
                    continue;
                }

                if (profileValue == null || string.IsNullOrEmpty(profileValue.ToString()))
                {
                    continue;
                }

                if (property.TypeName.Equals("PROFILE", StringComparison.OrdinalIgnoreCase))
                {
                    string[] array = profileValue.ToString().Split(';').Skip(1).ToArray();
                    details.Properties.Add(new ViewProperty {
                        Name = $"{property.GroupName}-{property.Name}", RawValue = array
                    });
                }
                else
                {
                    if (!userMappingProperties.ContainsKey($"{property.GroupName}.{property.Name}"))
                    {
                        continue;
                    }

                    var userProperty = userMappingProperties[$"{property.GroupName}.{property.Name}"];
                    var propertyInfo = customer.GetType().GetProperty(userProperty, BindingFlags.Public | BindingFlags.Instance);
                    if (propertyInfo != null && propertyInfo.CanWrite)
                    {
                        propertyInfo.SetValue(customer, profileValue, null);
                    }
                    else
                    {
                        details.Properties.Add(new ViewProperty {
                            Name = userProperty, RawValue = profileValue
                        });
                    }
                }
            }

            if (string.IsNullOrEmpty(customer.Email))
            {
                customer.Email = customer.UserName.Substring(customer.UserName.IndexOf('\\') + 1);
            }

            customer.GetComponent <CustomerDetailsComponent>()?.View.ChildViews.Add(details);
            return(customer);
        }
Example #3
0
        /// <summary>
        /// Gets the profile definition.
        /// </summary>
        /// <param name="definitionName">Name of the definition.</param>
        /// <returns>A <see cref="ProfileDefinition"/></returns>
        internal async Task <ProfileDefinition> GetProfileDefinition(string definitionName)
        {
            // Hook up the SQL server context
            using (var connection = new SqlConnection(this.ConnectionString))
            {
                await connection.OpenAsync();

                var command = connection.CreateCommand();
                command.CommandText = "sp_GetProfileProps";
                command.CommandType = CommandType.StoredProcedure;

                var categoryName = command.CreateParameter();
                categoryName.ParameterName = "@u_cat_name";
                categoryName.DbType        = DbType.String;
                categoryName.Value         = "Profile Definitions";
                command.Parameters.Add(categoryName);

                var profName = command.CreateParameter();
                profName.ParameterName = "@u_prof_name";
                profName.DbType        = DbType.String;
                profName.Value         = definitionName;
                command.Parameters.Add(profName);

                SqlDataAdapter adapter = new SqlDataAdapter(command);

                DataSet ds = new DataSet();
                adapter.Fill(ds);

                connection.Close();

                DataTable propertiesTable = ds.Tables[0];

                // definition name not found
                if (propertiesTable.Rows.Count == 0)
                {
                    return(null);
                }

                var definition = new ProfileDefinition {
                    Name = definitionName
                };

                foreach (DataRow row in propertiesTable.Rows)
                {
                    if (!(row["ClassDefName"] as string).StartsWith("ParentClass_", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var profileProperty = new ProfileProperty
                    {
                        Name             = row["MemberDefName"] as string,
                        DisplayName      = row["DisplayName"] as string,
                        TypeName         = row["TypeName"] as string,
                        ProfileReference = row["ReferenceString"] as string,
                        IsMultiValued    = Convert.ToBoolean(row["IsMultiValued"]),
                        ColumnName       = string.IsNullOrEmpty(row["ColumnName"] as string) ? row["ValColumnName"] as string : row["ColumnName"] as string,
                        IsHidden         = false,
                        IsReadOnly       = false
                    };

                    profileProperty.OriginalType = ComponentsHelper.GetPropertyOriginalType(profileProperty.TypeName);
                    definition.Properties.Add(profileProperty);
                }

                propertiesTable = ds.Tables[1];
                foreach (DataRow row in propertiesTable.Rows)
                {
                    var id       = row["GroupMemDefName"] as string;
                    var property = definition.Properties.FirstOrDefault(p => p.Name.Equals(id, StringComparison.Ordinal));
                    if (property != null)
                    {
                        property.GroupName = row["GroupName"] as string;
                    }
                }

                propertiesTable = ds.Tables[2];
                foreach (DataRow row in propertiesTable.Rows)
                {
                    var id       = row["MemberName"] as string;
                    var property = definition.Properties.FirstOrDefault(p => p.Name.Equals(id, StringComparison.Ordinal));
                    if (property == null)
                    {
                        continue;
                    }

                    switch (row["AttribName"] as string)
                    {
                    case "MaxLength":
                        property.MaxLength = Convert.ToInt32(row["ValStr"]);
                        break;

                    case "MinLength":
                        property.MinLength = Convert.ToInt32(row["ValStr"]);
                        break;
                    }
                }

                return(definition);
            }
        }