Exemplo n.º 1
0
        public CsvProductMap(CsvProductMappingConfiguration mappingCfg)
        {
            //Dynamical map scalar product fields use by manual mapping information
            foreach (var mappingItem in mappingCfg.PropertyMaps.Where(x => !string.IsNullOrEmpty(x.CsvColumnName) || !string.IsNullOrEmpty(x.CustomValue)))
            {
                var propertyInfo = typeof(CsvProduct).GetProperty(mappingItem.EntityColumnName);
                if (propertyInfo != null)
                {
                    var newMap = new CsvPropertyMap(propertyInfo);
                    newMap.TypeConverterOption(CultureInfo.InvariantCulture);
                    newMap.TypeConverterOption(NumberStyles.Any);
                    newMap.TypeConverterOption(true, "yes", "true");
                    newMap.TypeConverterOption(false, "false", "no");
                    if (!string.IsNullOrEmpty(mappingItem.CsvColumnName))
                    {
                        //Map fields if mapping specified
                        newMap.Name(mappingItem.CsvColumnName);
                    }
                    //And default values if it specified
                    if (mappingItem.CustomValue != null)
                    {
                        var typeConverter = TypeDescriptor.GetConverter(propertyInfo.PropertyType);
                        newMap.Default(typeConverter.ConvertFromString(mappingItem.CustomValue));
                    }
                    PropertyMaps.Add(newMap);
                }
            }

            //Map properties
            if (mappingCfg.PropertyCsvColumns != null && mappingCfg.PropertyCsvColumns.Any())
            {
                // Exporting multiple csv fields from the same property (which is a collection)
                foreach (var propertyCsvColumn in mappingCfg.PropertyCsvColumns)
                {
                    // create CsvPropertyMap manually, because this.Map(x =>...) does not allow
                    // to export multiple entries for the same property
                    var csvPropertyMap = new CsvPropertyMap(typeof(CsvProduct).GetProperty("PropertyValues"));
                    csvPropertyMap.Name(propertyCsvColumn);

                    // create custom converter instance which will get the required record from the collection
                    csvPropertyMap.UsingExpression <ICollection <coreModel.PropertyValue> >(null, propValues =>
                    {
                        var propValue = propValues.FirstOrDefault(x => x.PropertyName == propertyCsvColumn);
                        if (propValue != null)
                        {
                            return(propValue.Value != null ? propValue.Value.ToString() : string.Empty);
                        }
                        return(string.Empty);
                    });

                    PropertyMaps.Add(csvPropertyMap);
                }

                var newPropMap = new CsvPropertyMap(typeof(CsvProduct).GetProperty("PropertyValues"));
                newPropMap.UsingExpression <ICollection <coreModel.PropertyValue> >(null, null).ConvertUsing(x => mappingCfg.PropertyCsvColumns.Select(column => new coreModel.PropertyValue {
                    PropertyName = column, Value = x.GetField <string>(column)
                }).ToList());
                PropertyMaps.Add(newPropMap);
            }
        }
Exemplo n.º 2
0
 private void CheckForPropertyMap(Type typeKey)
 {
     if (!PropertyMaps.ContainsKey(typeKey))
     {
         PropertyMaps.Add(typeKey, new Dictionary <string, PropertyMappingExpression>());
     }
 }
Exemplo n.º 3
0
        public virtual CsvPropertyMap PropertyMap <T>(Expression <Func <T, object> > expression)
        {
            var property       = ReflectionHelper.GetProperty(expression);
            var csvPropertyMap = PropertyMaps.SingleOrDefault(m =>
            {
                if (m.Data.Property == property)
                {
                    return(true);
                }
                if (m.Data.Property.Name != property.Name)
                {
                    return(false);
                }
                if (m.Data.Property.DeclaringType.IsAssignableFrom(property.DeclaringType))
                {
                    return(true);
                }
                return(property.DeclaringType.IsAssignableFrom(m.Data.Property.DeclaringType));
            });

            if (csvPropertyMap != null)
            {
                return(csvPropertyMap);
            }
            var maxIndex = new CsvPropertyMap(property);

            maxIndex.Data.Index = GetMaxIndex() + 1;
            PropertyMaps.Add(maxIndex);
            return(maxIndex);
        }
Exemplo n.º 4
0
        public void AddPropertyMap(MemberInfo destProperty, IEnumerable <MemberInfo> resolvers)
        {
            var propertyMap = new PropertyMap(destProperty, this);

            propertyMap.ChainMembers(resolvers);

            PropertyMaps.Add(propertyMap);
        }
Exemplo n.º 5
0
        public PropertyMap AddProperty(string property, string columnName)
        {
            var map = new PropertyMap(property, columnName);

            PropertyMaps.Add(map);

            return(map);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Maps a non-member to a CSV field. This allows for writing
        /// data that isn't mapped to a class property/field.
        /// </summary>
        /// <returns>The property mapping.</returns>
        public virtual CsvPropertyMap Map()
        {
            var propertyMap = new CsvPropertyMap(null);

            propertyMap.Data.Index = GetMaxIndex() + 1;
            PropertyMaps.Add(propertyMap);

            return(propertyMap);
        }
Exemplo n.º 7
0
        private void FillPropertyMaps()
        {
            foreach (var map in _propertyMaps)
            {
                PropertyMaps.Add(map);
            }

            foreach (var linqPropertyMap in _linqPropertyMaps)
            {
                LinqPropertyMaps.Add(linqPropertyMap);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Maps a property to a CSV field.
        /// </summary>
        /// <param name="expression">The property to map.</param>
        /// <returns>The property mapping.</returns>
        protected virtual CsvPropertyMap Map(Expression <Func <T, object> > expression)
        {
            var property = ReflectionHelper.GetProperty(expression);

            if (PropertyMaps.Any(m => m.Data.Property == property))
            {
                throw new CsvConfigurationException(string.Format("Property '{0}' has already been mapped.", property.Name));
            }
            var propertyMap = new CsvPropertyMap(property);

            PropertyMaps.Add(propertyMap);
            return(propertyMap);
        }
Exemplo n.º 9
0
        public PropertyMap FindOrCreatePropertyMapFor(MemberInfo destinationProperty)
        {
            var propertyMap = GetExistingPropertyMapFor(destinationProperty);

            if (propertyMap != null)
            {
                return(propertyMap);
            }

            propertyMap = new PropertyMap(destinationProperty, this);

            PropertyMaps.Add(propertyMap);

            return(propertyMap);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Maps a property to a CSV field.
        /// </summary>
        /// <param name="property">The property to map.</param>
        /// <returns>The property mapping.</returns>
        public virtual CsvPropertyMap Map(PropertyInfo property)
        {
            var existingMap = PropertyMaps.Find(property);

            if (existingMap != null)
            {
                return(existingMap);
            }

            var propertyMap = new CsvPropertyMap(property);

            propertyMap.Data.Index = GetMaxIndex() + 1;
            PropertyMaps.Add(propertyMap);

            return(propertyMap);
        }
Exemplo n.º 11
0
        public RelationshipMap(string tblName)
        {
            var baseType = typeof(Entity);
            var props    = baseType.GetProperties();

            foreach (var p in props)
            {
                PropertyMaps.Add(new ManualPropertyMap
                {
                    CaseSensitive = true,
                    ColumnName    = $"{tblName}.{p.Name}",
                    Ignored       = false,
                    PropertyInfo  = p
                });
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Maps a property to a Excel field.
        /// </summary>
        /// <param name="property">Property to map</param>
        /// <returns>The property mapping.</returns>
        private ExcelPropertyMap Map(
            PropertyInfo property)
        {
            var existingMap = PropertyMaps.SingleOrDefault(m => m.Data.Property == property);

            if (existingMap != null)
            {
                return(existingMap);
            }

            var propertyMap = new ExcelPropertyMap(property);

            propertyMap.Data.Index = GetMaxIndex() + 1;
            PropertyMaps.Add(propertyMap);

            return(propertyMap);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Maps a property to a CSV field.
        /// </summary>
        /// <param name="expression">The property to map.</param>
        /// <returns>The property mapping.</returns>
        protected virtual CsvPropertyMap Map(Expression <Func <T, object> > expression)
        {
            var property = ReflectionHelper.GetProperty(expression);

            var existingMap = PropertyMaps.SingleOrDefault(m => m.Data.Property == property);

            if (existingMap != null)
            {
                return(existingMap);
            }

            var propertyMap = new CsvPropertyMap(property);

            propertyMap.Data.Index = GetMaxIndex() + 1;
            PropertyMaps.Add(propertyMap);

            return(propertyMap);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Maps a property/field to a CSV field.
        /// </summary>
        /// <param name="member">The property/field to map.</param>
        /// <param name="useExistingMap">If true, an existing map will be used if available.
        /// If false, a new map is created for the same property/field.</param>
        /// <returns>The property/field mapping.</returns>
        public virtual CsvPropertyMap Map(MemberInfo member, bool useExistingMap = true)
        {
            if (useExistingMap)
            {
                var existingMap = PropertyMaps.Find(member);
                if (existingMap != null)
                {
                    return(existingMap);
                }
            }

            var propertyMap = new CsvPropertyMap(member);

            propertyMap.Data.Index = GetMaxIndex() + 1;
            PropertyMaps.Add(propertyMap);

            return(propertyMap);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Maps a property/field to a CSV field.
        /// </summary>
        /// <param name="classType">The type of the class this map is for. This may not be the same type
        /// as the member.DeclaringType or the current ClassType due to nested property mappings.</param>
        /// <param name="member">The property/field to map.</param>
        /// <param name="useExistingMap">If true, an existing map will be used if available.
        /// If false, a new map is created for the same property/field.</param>
        /// <returns>The property/field mapping.</returns>
        public CsvPropertyMap Map(Type classType, MemberInfo member, bool useExistingMap = true)
        {
            if (useExistingMap)
            {
                var existingMap = PropertyMaps.Find(member);
                if (existingMap != null)
                {
                    return(existingMap);
                }
            }

            var propertyMap = CsvPropertyMap.CreateGeneric(classType, member);

            propertyMap.Data.Index = GetMaxIndex() + 1;
            PropertyMaps.Add(propertyMap);

            return(propertyMap);
        }
Exemplo n.º 16
0
        public virtual CsvPropertyMap PropertyMap <T>(Expression <Func <T, object> > expression)
        {
            var property    = ReflectionHelper.GetProperty(expression);
            var existingMap = PropertyMaps.SingleOrDefault(m =>
                                                           m.Data.Property == property ||
                                                           m.Data.Property.Name == property.Name &&
                                                           (m.Data.Property.DeclaringType.IsAssignableFrom(property.DeclaringType) || property.DeclaringType.IsAssignableFrom(m.Data.Property.DeclaringType)));

            if (existingMap != null)
            {
                return(existingMap);
            }

            var propertyMap = new CsvPropertyMap(property);

            propertyMap.Data.Index = GetMaxIndex() + 1;
            PropertyMaps.Add(propertyMap);

            return(propertyMap);
        }
Exemplo n.º 17
0
            public ProductMap(string[] allColumns, webModel.CsvImportMappingItem[]  mappingConfiguration)
            {
                var attributePropertyNames = allColumns.Except(mappingConfiguration.Select(x => x.CsvColumnName));

                foreach (var mappingConfigurationItem in mappingConfiguration)
                {
                    var propertyInfo = typeof(coreModel.CatalogProduct).GetProperty(mappingConfigurationItem.EntityColumnName);
                    var newMap       = new CsvPropertyMap(propertyInfo);
                    newMap.Name(mappingConfigurationItem.CsvColumnName);
                    PropertyMaps.Add(newMap);
                }
                var categoryMappingItem        = mappingConfiguration.First(x => x.EntityColumnName == "Category");
                var editorialReviewMappingItem = mappingConfiguration.First(x => x.EntityColumnName == "Reviews");

                Map(x => x.Category).ConvertUsing(x => new coreModel.Category {
                    Path = x.GetField <string>(categoryMappingItem.CsvColumnName)
                });
                Map(x => x.Reviews).ConvertUsing(x => new coreModel.EditorialReview[] { new coreModel.EditorialReview {
                                                                                            Content = x.GetField <string>(editorialReviewMappingItem.CsvColumnName)
                                                                                        } });
                Map(x => x.PropertyValues).ConvertUsing(x => attributePropertyNames.Select(column => new coreModel.PropertyValue {
                    PropertyName = column, Value = x.GetField <string>(column)
                }).ToList());
            }
Exemplo n.º 18
0
 protected void AddPropertyMap <TType1, TType2>(string property1, string property2)
 {
     PropertyMaps.Add(new PropertyMap(typeof(TType1), property1, typeof(TType2), property2));
 }
Exemplo n.º 19
0
            public ProductMap(string[] allColumns, webModel.CsvImportConfiguration importConfiguration, coreModel.Catalog catalog)
            {
                var defaultLanguge = catalog.DefaultLanguage != null ? catalog.DefaultLanguage.LanguageCode : "EN-US";

                //Dynamical map scalar product fields use by manual mapping information
                foreach (var mappingConfigurationItem in importConfiguration.MappingItems.Where(x => x.CsvColumnName != null || x.CustomValue != null))
                {
                    var propertyInfo = typeof(coreModel.CatalogProduct).GetProperty(mappingConfigurationItem.EntityColumnName);
                    if (propertyInfo != null)
                    {
                        var newMap = new CsvPropertyMap(propertyInfo);
                        newMap.TypeConverterOption(CultureInfo.InvariantCulture);
                        newMap.TypeConverterOption(NumberStyles.Any);
                        newMap.TypeConverterOption(true, "yes", "false");
                        //Map fields if mapping specified
                        if (mappingConfigurationItem.CsvColumnName != null)
                        {
                            newMap.Name(mappingConfigurationItem.CsvColumnName);
                        }
                        //And default values if it specified
                        if (mappingConfigurationItem.CustomValue != null)
                        {
                            newMap.Default(mappingConfigurationItem.CustomValue);
                        }
                        PropertyMaps.Add(newMap);
                    }
                }

                //Map Sku -> Code
                Map(x => x.Code).ConvertUsing(x =>
                {
                    return(GetCsvField("Sku", x, importConfiguration));
                });

                //Map ParentSku -> main product
                Map(x => x.MainProductId).ConvertUsing(x =>
                {
                    var parentSku = GetCsvField("ParentSku", x, importConfiguration);
                    return(!String.IsNullOrEmpty(parentSku) ? parentSku : null);
                });

                //Map assets (images)
                Map(x => x.Assets).ConvertUsing(x =>
                {
                    var retVal          = new List <coreModel.ItemAsset>();
                    var primaryImageUrl = GetCsvField("PrimaryImage", x, importConfiguration);
                    var altImageUrl     = GetCsvField("AltImage", x, importConfiguration);
                    if (!String.IsNullOrEmpty(primaryImageUrl))
                    {
                        retVal.Add(new coreModel.ItemAsset
                        {
                            Type  = coreModel.ItemAssetType.Image,
                            Group = "primaryimage",
                            Url   = primaryImageUrl
                        });
                    }

                    if (!String.IsNullOrEmpty(altImageUrl))
                    {
                        retVal.Add(new coreModel.ItemAsset
                        {
                            Type  = coreModel.ItemAssetType.Image,
                            Group = "image",
                            Url   = altImageUrl
                        });
                    }

                    return(retVal);
                });

                //Map category
                Map(x => x.Category).ConvertUsing(x => new coreModel.Category {
                    Path = GetCsvField("Category", x, importConfiguration)
                });

                //Map Reviews
                Map(x => x.Reviews).ConvertUsing(x =>
                {
                    var reviews = new List <coreModel.EditorialReview>();
                    var content = GetCsvField("Review", x, importConfiguration);
                    if (!String.IsNullOrEmpty(content))
                    {
                        reviews.Add(new coreModel.EditorialReview {
                            Content = content, LanguageCode = defaultLanguge
                        });
                    }
                    return(reviews);
                });

                //Map Seo
                Map(x => x.SeoInfos).ConvertUsing(x =>
                {
                    var seoInfos       = new List <SeoInfo>();
                    var seoUrl         = GetCsvField("SeoUrl", x, importConfiguration);
                    var seoDescription = GetCsvField("SeoDescription", x, importConfiguration);
                    var seoTitle       = GetCsvField("SeoTitle", x, importConfiguration);
                    if (!String.IsNullOrEmpty(seoUrl) || !String.IsNullOrEmpty(seoTitle) || !String.IsNullOrEmpty(seoDescription))
                    {
                        seoUrl = new string[] { seoUrl, seoDescription, seoTitle }.Where(y => !String.IsNullOrEmpty(y)).FirstOrDefault();
                        seoUrl = seoUrl.Substring(0, Math.Min(seoUrl.Length, 240));
                        seoInfos.Add(new SeoInfo {
                            LanguageCode = defaultLanguge, SemanticUrl = seoUrl.GenerateSlug(), MetaDescription = seoDescription, PageTitle = seoTitle
                        });
                    }
                    return(seoInfos);
                });

                //Map Prices
                Map(x => x.Prices).ConvertUsing(x =>
                {
                    var prices    = new List <Price>();
                    var priceId   = GetCsvField("PriceId", x, importConfiguration);
                    var listPrice = GetCsvField("Price", x, importConfiguration);
                    var salePrice = GetCsvField("SalePrice", x, importConfiguration);
                    var currency  = GetCsvField("Currency", x, importConfiguration);

                    if (!String.IsNullOrEmpty(listPrice) && !String.IsNullOrEmpty(currency))
                    {
                        prices.Add(new Price
                        {
                            Id       = priceId,
                            List     = Convert.ToDecimal(listPrice, CultureInfo.InvariantCulture),
                            Sale     = salePrice != null ? (decimal?)Convert.ToDecimal(listPrice, CultureInfo.InvariantCulture) : null,
                            Currency = EnumUtility.SafeParse <CurrencyCodes>(currency, CurrencyCodes.USD)
                        });
                    }
                    return(prices);
                });

                //Map inventories
                Map(x => x.Inventories).ConvertUsing(x =>
                {
                    var inventories        = new List <InventoryInfo>();
                    var quantity           = GetCsvField("Quantity", x, importConfiguration);
                    var allowBackorder     = GetCsvField("AllowBackorder", x, importConfiguration);
                    var fulfilmentCenterId = GetCsvField("FulfilmentCenterId", x, importConfiguration);

                    if (!String.IsNullOrEmpty(quantity))
                    {
                        inventories.Add(new InventoryInfo
                        {
                            FulfillmentCenterId = fulfilmentCenterId,
                            AllowBackorder      = allowBackorder.TryParse(false),
                            InStockQuantity     = (long)quantity.TryParse(0.0m)
                        });
                    }
                    return(inventories);
                });

                //Map properties
                if (importConfiguration.PropertyCsvColumns != null && importConfiguration.PropertyCsvColumns.Any())
                {
                    Map(x => x.PropertyValues).ConvertUsing(x => importConfiguration.PropertyCsvColumns.Select(column => new coreModel.PropertyValue {
                        PropertyName = column, Value = x.GetField <string>(column)
                    }).ToList());
                }
            }