/// <summary> /// Converting to foundation type /// </summary> /// <param name="catalog"></param> /// <returns></returns> public static dataModel.Catalog ToDataModel(this coreModel.Catalog catalog, PrimaryKeyResolvingMap pkMap) { if (catalog == null) { throw new ArgumentNullException("catalog"); } if (catalog.DefaultLanguage == null) { throw new NullReferenceException("DefaultLanguage"); } var retVal = new dataModel.Catalog(); pkMap.AddPair(catalog, retVal); if (catalog.PropertyValues != null) { retVal.CatalogPropertyValues = new ObservableCollection <dataModel.PropertyValue>(); retVal.CatalogPropertyValues.AddRange(catalog.PropertyValues.Select(x => x.ToDataModel(pkMap))); } retVal.InjectFrom(catalog); retVal.Virtual = catalog.IsVirtual; retVal.DefaultLanguage = catalog.DefaultLanguage.LanguageCode; if (catalog.Languages != null) { retVal.CatalogLanguages = new ObservableCollection <dataModel.CatalogLanguage>(); retVal.CatalogLanguages.AddRange(catalog.Languages.Select(x => x.ToDataModel())); } return(retVal); }
public static moduleModel.Catalog ToModuleModel(this webModel.Catalog catalog) { var retVal = new moduleModel.Catalog(); retVal.InjectFrom(catalog); if (catalog.Languages != null) { retVal.Languages = catalog.Languages.Select(x => x.ToModuleModel()).ToList(); } if (catalog.Properties != null) { retVal.PropertyValues = new List <moduleModel.PropertyValue>(); foreach (var property in catalog.Properties) { foreach (var propValue in property.Values) { propValue.ValueType = property.ValueType; //Need populate required fields propValue.PropertyName = property.Name; retVal.PropertyValues.Add(propValue.ToModuleModel()); } } } return(retVal); }
public static webModel.Catalog ToWebModel(this moduleModel.Catalog catalog, bool convertProps = true) { var retVal = new webModel.Catalog(); //Do not use omu.InjectFrom for performance reasons retVal.Id = catalog.Id; retVal.Name = catalog.Name; retVal.IsVirtual = catalog.IsVirtual; retVal.Properties = new List <webModel.Property>(); if (catalog.Languages != null) { retVal.Languages = catalog.Languages.Select(x => x.ToWebModel()).ToList(); } if (convertProps) { //Need add property for each meta info if (catalog.Properties != null) { foreach (var property in catalog.Properties) { var webModelProperty = property.ToWebModel(); //Reset dict values to decrease response size webModelProperty.DictionaryValues = null; webModelProperty.Values = new List <webModel.PropertyValue>(); webModelProperty.IsManageable = true; webModelProperty.IsReadOnly = property.Type != moduleModel.PropertyType.Catalog; retVal.Properties.Add(webModelProperty); } } //Populate property for property values if (catalog.PropertyValues != null) { var sort = false; foreach (var propValue in catalog.PropertyValues.Select(x => x.ToWebModel())) { var property = retVal.Properties.FirstOrDefault(x => x.Id == propValue.PropertyId); if (property == null) { property = retVal.Properties.FirstOrDefault(x => x.Name.EqualsInvariant(propValue.PropertyName)); } if (property == null) { //Need add dummy property for each value without property property = new webModel.Property(propValue, catalog.Id, moduleModel.PropertyType.Catalog); retVal.Properties.Add(property); sort = true; } property.Values.Add(propValue); } if (sort) { retVal.Properties = retVal.Properties.OrderBy(x => x.Name).ToList(); } } } return(retVal); }
public static moduleModel.Catalog ToModuleModel(this webModel.Catalog catalog) { var retVal = new moduleModel.Catalog(); retVal.InjectFrom(catalog); if (catalog.Languages != null) { retVal.Languages = catalog.Languages.Select(x => x.ToModuleModel()).ToList(); } if (catalog.Properties != null) { retVal.PropertyValues = new List<moduleModel.PropertyValue>(); foreach (var property in catalog.Properties) { foreach (var propValue in property.Values) { propValue.ValueType = property.ValueType; //Need populate required fields propValue.PropertyName = property.Name; retVal.PropertyValues.Add(propValue.ToModuleModel()); } } } return retVal; }
/// <summary> /// Converting to foundation type /// </summary> /// <param name="catalog"></param> /// <returns></returns> public static dataModel.CatalogBase ToDataModel(this coreModel.Catalog catalog) { if (catalog == null) { throw new ArgumentNullException("catalog"); } if (catalog.DefaultLanguage == null) { throw new NullReferenceException("DefaultLanguage"); } dataModel.CatalogBase retVal; dataModel.Catalog dbCatalog = null; dataModel.VirtualCatalog dbVirtCatalog = null; if (catalog.Virtual) { dbVirtCatalog = new dataModel.VirtualCatalog(); retVal = dbVirtCatalog; } else { dbCatalog = new dataModel.Catalog(); if (catalog.PropertyValues != null) { dbCatalog.CatalogPropertyValues = new ObservableCollection <dataModel.CatalogPropertyValue>(); dbCatalog.CatalogPropertyValues.AddRange(catalog.PropertyValues.Select(x => x.ToDataModel <dataModel.CatalogPropertyValue>()).OfType <dataModel.CatalogPropertyValue>()); } retVal = dbCatalog; } //Because EF mapping schema not automatically linked foreign keys, we should generate manually id and set links manually var id = retVal.Id; retVal.InjectFrom(catalog); if (catalog.Id == null) { retVal.Id = id; } retVal.DefaultLanguage = catalog.DefaultLanguage.LanguageCode; if (dbCatalog != null && catalog.Languages != null) { dbCatalog.CatalogLanguages = new ObservableCollection <dataModel.CatalogLanguage>(); foreach (var dbCatalogLanguage in catalog.Languages.Select(x => x.ToDataModel())) { dbCatalogLanguage.CatalogId = retVal.Id; dbCatalog.CatalogLanguages.Add(dbCatalogLanguage); } } retVal.Name = catalog.Name; return(retVal); }
/// <summary> /// Converting to model type /// </summary> /// <param name="catalogBase"></param> /// <returns></returns> public static coreModel.Catalog ToCoreModel(this dataModel.Catalog dbCatalog, bool convertProps = true) { if (dbCatalog == null) { throw new ArgumentNullException("catalog"); } var retVal = new coreModel.Catalog(); retVal.Id = dbCatalog.Id; retVal.Name = dbCatalog.Name; retVal.IsVirtual = dbCatalog.Virtual; retVal.Languages = new List <coreModel.CatalogLanguage>(); var defaultLanguage = (new dataModel.CatalogLanguage { Language = string.IsNullOrEmpty(dbCatalog.DefaultLanguage) ? "en-us" : dbCatalog.DefaultLanguage }).ToCoreModel(retVal); defaultLanguage.IsDefault = true; retVal.Languages = new List <coreModel.CatalogLanguage>(); retVal.Languages.Add(defaultLanguage); //populate additional languages foreach (var catalogLanguage in dbCatalog.CatalogLanguages.Where(x => x.Language != defaultLanguage.LanguageCode).Select(x => x.ToCoreModel(retVal))) { catalogLanguage.Catalog = retVal; retVal.Languages.Add(catalogLanguage); } if (convertProps) { retVal.PropertyValues = dbCatalog.CatalogPropertyValues.Select(x => x.ToCoreModel()).ToList(); //Self properties retVal.Properties = dbCatalog.Properties.Where(x => x.CategoryId == null).OrderBy(x => x.Name).Select(x => x.ToCoreModel(new dataModel.Catalog[] { dbCatalog }, new dataModel.Category[] { })).ToList(); //Next need set Property in PropertyValues objects foreach (var propValue in retVal.PropertyValues.ToArray()) { propValue.Property = retVal.Properties.FirstOrDefault(x => x.IsSuitableForValue(propValue)); //Return each localized value for selecte dictionary value //Because multilingual dictionary values for all languages may not stored in db need add it in result manually from property dictionary values var localizedDictValues = propValue.TryGetAllLocalizedDictValues(); foreach (var localizedDictValue in localizedDictValues) { if (!retVal.PropertyValues.Any(x => x.ValueId == localizedDictValue.ValueId && x.LanguageCode == localizedDictValue.LanguageCode)) { retVal.PropertyValues.Add(localizedDictValue); } } } } return(retVal); }
public coreModel.Catalog GetById(string catalogId) { coreModel.Catalog retVal = null; var dbCatalog = base.AllCachedCatalogs.FirstOrDefault(x => x.Id == catalogId); if (dbCatalog != null) { retVal = dbCatalog.ToCoreModel(); } return(retVal); }
public coreModel.Catalog GetById(string catalogId) { coreModel.Catalog retVal = null; using (var repository = _catalogRepositoryFactory()) { var dbCatalogBase = repository.GetCatalogById(catalogId); var dbProperties = repository.GetCatalogProperties(dbCatalogBase); var properties = dbProperties.Select(x => x.ToCoreModel(dbCatalogBase.ToCoreModel(), null)).ToArray(); retVal = dbCatalogBase.ToCoreModel(properties); } return(retVal); }
public coreModel.Catalog Create(coreModel.Catalog catalog) { var dbCatalog = catalog.ToDataModel(); coreModel.Catalog retVal = null; using (var repository = _catalogRepositoryFactory()) { repository.Add(dbCatalog); CommitChanges(repository); } retVal = GetById(dbCatalog.Id); return(retVal); }
public coreModel.Catalog GetById(string catalogId) { coreModel.Catalog retVal = null; using (var repository = _catalogRepositoryFactory()) { var dbCatalog = repository.GetCatalogById(catalogId); if (dbCatalog != null) { retVal = dbCatalog.ToCoreModel(); } } return(retVal); }
private void SaveProducts(coreModel.Catalog catalog, IEnumerable <coreModel.CatalogProduct> csvProducts, ImportNotification notification) { int counter = 0; var notifyProductSizeLimit = 10; notification.TotalCount = csvProducts.Count(); var defaultFulfilmentCenter = _commerceService.GetAllFulfillmentCenters().FirstOrDefault(); var options = new ParallelOptions() { MaxDegreeOfParallelism = 10 }; DetectParents(csvProducts); //First need create main products, second will be created variations foreach (var group in new IEnumerable <coreModel.CatalogProduct>[] { csvProducts.Where(x => x.MainProduct == null), csvProducts.Where(x => x.MainProduct != null) }) { Parallel.ForEach(group, options, csvProduct => { try { SaveProduct(catalog, defaultFulfilmentCenter, csvProduct); } catch (Exception ex) { lock (_lockObject) { notification.ErrorCount++; notification.Errors.Add(ex.ToString()); _notifier.Upsert(notification); } } finally { lock (_lockObject) { //Raise notification each notifyProductSizeLimit category counter++; notification.ProcessedCount = counter; notification.Description = string.Format("Creating products: {0} of {1} created", notification.ProcessedCount, notification.TotalCount); if (counter % notifyProductSizeLimit == 0) { _notifier.Upsert(notification); } } } }); } ; }
public coreModel.Catalog Create(coreModel.Catalog catalog) { var pkMap = new PrimaryKeyResolvingMap(); var dbCatalog = catalog.ToDataModel(pkMap); coreModel.Catalog retVal = null; using (var repository = base.CatalogRepositoryFactory()) { repository.Add(dbCatalog); CommitChanges(repository); pkMap.ResolvePrimaryKeys(); } retVal = GetById(dbCatalog.Id); return(retVal); }
/// <summary> /// Converting to model type /// </summary> /// <param name="catalogBase"></param> /// <returns></returns> public static coreModel.Catalog ToCoreModel(this dataModel.Catalog dbCatalog, bool convertProps = true) { if (dbCatalog == null) throw new ArgumentNullException("catalog"); var retVal = new coreModel.Catalog(); retVal.InjectFrom(dbCatalog); retVal.IsVirtual = dbCatalog.Virtual; retVal.Languages = new List<coreModel.CatalogLanguage>(); var defaultLanguage = (new dataModel.CatalogLanguage { Language = string.IsNullOrEmpty(dbCatalog.DefaultLanguage) ? "en-us" : dbCatalog.DefaultLanguage }).ToCoreModel(retVal); defaultLanguage.IsDefault = true; retVal.Languages = new List<coreModel.CatalogLanguage>(); retVal.Languages.Add(defaultLanguage); //populate additional languages foreach (var catalogLanguage in dbCatalog.CatalogLanguages.Where(x => x.Language != defaultLanguage.LanguageCode).Select(x => x.ToCoreModel(retVal))) { catalogLanguage.Catalog = retVal; retVal.Languages.Add(catalogLanguage); } if (convertProps) { retVal.PropertyValues = dbCatalog.CatalogPropertyValues.Select(x => x.ToCoreModel()).ToList(); //Self properties retVal.Properties = dbCatalog.Properties.Where(x => x.CategoryId == null).OrderBy(x => x.Name).Select(x => x.ToCoreModel()).ToList(); //Next need set Property in PropertyValues objects foreach (var propValue in retVal.PropertyValues.ToArray()) { propValue.Property = retVal.Properties.FirstOrDefault(x => x.IsSuitableForValue(propValue)); //Return each localized value for selecte dictionary value //Because multilingual dictionary values for all languages may not stored in db need add it in result manually from property dictionary values var localizedDictValues = propValue.TryGetAllLocalizedDictValues(); foreach (var localizedDictValue in localizedDictValues) { if (!retVal.PropertyValues.Any(x => x.ValueId == localizedDictValue.ValueId && x.LanguageCode == localizedDictValue.LanguageCode)) { retVal.PropertyValues.Add(localizedDictValue); } } } } return retVal; }
public static webModel.Catalog ToWebModel(this moduleModel.Catalog catalog, bool convertProps = true) { var retVal = new webModel.Catalog(); retVal.InjectFrom(catalog); retVal.Properties = new List <webModel.Property>(); if (catalog.Languages != null) { retVal.Languages = catalog.Languages.Select(x => x.ToWebModel()).ToList(); } if (convertProps) { //Need add property for each meta info if (catalog.Properties != null) { foreach (var property in catalog.Properties) { var webModelProperty = property.ToWebModel(); //Reset dict values to decrease response size webModelProperty.DictionaryValues = null; webModelProperty.Values = new List <webModel.PropertyValue>(); webModelProperty.IsManageable = true; webModelProperty.IsReadOnly = property.Type != moduleModel.PropertyType.Catalog; retVal.Properties.Add(webModelProperty); } } //Populate property for property values if (catalog.PropertyValues != null) { foreach (var propValue in catalog.PropertyValues.Select(x => x.ToWebModel())) { var property = retVal.Properties.FirstOrDefault(x => x.Id == propValue.PropertyId); if (property == null) { //Need add dummy property for each value without property property = new webModel.Property(propValue, catalog.Id, null, moduleModel.PropertyType.Catalog); retVal.Properties.Add(property); } property.Values.Add(propValue); } } } return(retVal); }
private void SaveCategoryTree(coreModel.Catalog catalog, IEnumerable <CsvProduct> csvProducts, ExportImportProgressInfo progressInfo, Action <ExportImportProgressInfo> progressCallback) { var categories = new List <coreModel.Category>(); progressInfo.ProcessedCount = 0; var cachedCategoryMap = new Dictionary <string, Category>(); foreach (var csvProduct in csvProducts.Where(x => x.Category != null && !String.IsNullOrEmpty(x.Category.Path))) { var outline = ""; var productCategoryNames = csvProduct.Category.Path.Split(_categoryDelimiters); string parentCategoryId = null; foreach (var categoryName in productCategoryNames) { outline += "\\" + categoryName; Category category; if (!cachedCategoryMap.TryGetValue(outline, out category)) { var searchCriteria = new SearchCriteria { CatalogId = catalog.Id, CategoryId = parentCategoryId, ResponseGroup = SearchResponseGroup.WithCategories }; category = _searchService.Search(searchCriteria).Categories.FirstOrDefault(x => x.Name == categoryName); } if (category == null) { category = _categoryService.Create(new coreModel.Category() { Name = categoryName, Code = categoryName.GenerateSlug(), CatalogId = catalog.Id, ParentId = parentCategoryId }); //Raise notification each notifyCategorySizeLimit category progressInfo.Description = string.Format("Creating categories: {0} created", ++progressInfo.ProcessedCount); progressCallback(progressInfo); } csvProduct.CategoryId = category.Id; csvProduct.Category = category; parentCategoryId = category.Id; cachedCategoryMap[outline] = category; } } }
/// <summary> /// Converting to model type /// </summary> /// <param name="catalogBase"></param> /// <returns></returns> public static coreModel.Catalog ToCoreModel(this dataModel.CatalogBase catalogBase, coreModel.Property[] properties = null) { if (catalogBase == null) { throw new ArgumentNullException("catalogBase"); } var catalog = catalogBase as dataModel.Catalog; var virtualCatalog = catalogBase as dataModel.VirtualCatalog; var retVal = new coreModel.Catalog(); retVal.InjectFrom(catalogBase); retVal.Virtual = virtualCatalog != null; retVal.Languages = new List <coreModel.CatalogLanguage>(); if (catalog != null) { foreach (var catalogLanguage in catalog.CatalogLanguages.Select(x => x.ToCoreModel(retVal))) { catalogLanguage.Catalog = retVal; catalogLanguage.IsDefault = catalogBase.DefaultLanguage == catalogLanguage.LanguageCode; retVal.Languages.Add(catalogLanguage); } if (properties != null) { retVal.PropertyValues = catalog.CatalogPropertyValues.Select(x => x.ToCoreModel(properties)).ToList(); } } if (virtualCatalog != null) { var catalogLanguage = (new dataModel.CatalogLanguage { Language = catalogBase.DefaultLanguage }).ToCoreModel(retVal); catalogLanguage.IsDefault = true; retVal.Languages.Add(catalogLanguage); } return(retVal); }
public static webModel.Catalog ToWebModel(this moduleModel.Catalog catalog, moduleModel.Property[] properties = null) { var retVal = new webModel.Catalog(); retVal.InjectFrom(catalog); retVal.Properties = new List <webModel.Property>(); if (catalog.Languages != null) { retVal.Languages = catalog.Languages.Select(x => x.ToWebModel()).ToList(); } //Need add property for each meta info if (properties != null) { foreach (var property in properties) { var webModelProperty = property.ToWebModel(); webModelProperty.Values = new List <webModel.PropertyValue>(); webModelProperty.IsManageable = true; webModelProperty.IsReadOnly = property.Type != moduleModel.PropertyType.Catalog; retVal.Properties.Add(webModelProperty); } } //Populate property values if (catalog.PropertyValues != null) { foreach (var propValue in catalog.PropertyValues.Select(x => x.ToWebModel())) { var property = retVal.Properties.FirstOrDefault(x => x.IsSuitableForValue(propValue)); if (property == null) { //Need add dummy property for each value without property property = new webModel.Property(propValue, catalog.Id, null, moduleModel.PropertyType.Catalog); retVal.Properties.Add(property); } property.Values.Add(propValue); } } return(retVal); }
/// <summary> /// Converting to model type /// </summary> /// <param name="catalogBase"></param> /// <returns></returns> public static coreModel.Catalog ToCoreModel(this dataModel.CatalogBase catalogBase, coreModel.Property[] properties = null) { if (catalogBase == null) throw new ArgumentNullException("catalogBase"); var catalog = catalogBase as dataModel.Catalog; var virtualCatalog = catalogBase as dataModel.VirtualCatalog; var retVal = new coreModel.Catalog(); retVal.InjectFrom(catalogBase); retVal.Virtual = virtualCatalog != null; retVal.Languages = new List<coreModel.CatalogLanguage>(); if (catalog != null) { foreach (var catalogLanguage in catalog.CatalogLanguages.Select(x => x.ToCoreModel(retVal))) { catalogLanguage.Catalog = retVal; catalogLanguage.IsDefault = catalogBase.DefaultLanguage == catalogLanguage.LanguageCode; retVal.Languages.Add(catalogLanguage); } if (properties != null) { retVal.PropertyValues = catalog.CatalogPropertyValues.Select(x => x.ToCoreModel(properties)).ToList(); } } if (virtualCatalog != null) { var catalogLanguage = (new dataModel.CatalogLanguage { Language = catalogBase.DefaultLanguage }).ToCoreModel(retVal); catalogLanguage.IsDefault = true; retVal.Languages.Add(catalogLanguage); } return retVal; }
/// <summary> /// Converting to model type /// </summary> /// <param name="catalogBase"></param> /// <returns></returns> public static coreModel.CatalogLanguage ToCoreModel(this dataModel.CatalogLanguage dbLanguage, coreModel.Catalog catalog) { var retVal = new coreModel.CatalogLanguage(); retVal.Id = dbLanguage.Id; retVal.CatalogId = catalog.Id; retVal.LanguageCode = dbLanguage.Language; return(retVal); }
/// <summary> /// Converting to model type /// </summary> /// <param name="dbCategory">The database category base.</param> /// <param name="catalog">The catalog.</param> /// <param name="properties">The properties.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">catalog</exception> public static coreModel.Category ToCoreModel(this dataModel.Category dbCategory, coreModel.Catalog catalog, coreModel.Property[] properties = null, dataModel.Category[] allParents = null) { if (catalog == null) { throw new ArgumentNullException("catalog"); } var retVal = new coreModel.Category(); retVal.InjectFrom(dbCategory); retVal.CatalogId = catalog.Id; retVal.Catalog = catalog; retVal.ParentId = dbCategory.ParentCategoryId; retVal.IsActive = dbCategory.IsActive; retVal.PropertyValues = dbCategory.CategoryPropertyValues.Select(x => x.ToCoreModel(properties)).ToList(); retVal.Virtual = catalog.Virtual; retVal.Links = dbCategory.OutgoingLinks.Select(x => x.ToCoreModel(retVal)).ToList(); if (allParents != null) { retVal.Parents = allParents.Select(x => x.ToCoreModel(catalog)).ToArray(); } //Try to inherit taxType from parent category if (retVal.TaxType == null && retVal.Parents != null) { retVal.TaxType = retVal.Parents.Select(x => x.TaxType).Where(x => x != null).FirstOrDefault(); } #region Images if (dbCategory.Images != null) { retVal.Images = dbCategory.Images.OrderBy(x => x.SortOrder).Select(x => x.ToCoreModel()).ToList(); } #endregion return(retVal); }
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()); } }
/// <summary> /// Converting to model type /// </summary> /// <returns></returns> public static coreModel.CatalogProduct ToCoreModel(this dataModel.Item dbItem, coreModel.Catalog catalog, coreModel.Category category, coreModel.CatalogProduct[] associatedProducts) { var retVal = new coreModel.CatalogProduct(); retVal.InjectFrom(dbItem); retVal.Catalog = catalog; retVal.CatalogId = catalog.Id; if (category != null) { retVal.Category = category; retVal.CategoryId = category.Id; } retVal.MainProductId = dbItem.ParentId; retVal.IsActive = dbItem.IsActive; retVal.IsBuyable = dbItem.IsBuyable; retVal.TrackInventory = dbItem.TrackInventory; retVal.MaxQuantity = (int)dbItem.MaxQuantity; retVal.MinQuantity = (int)dbItem.MinQuantity; #region Links retVal.Links = dbItem.CategoryLinks.Select(x => x.ToCoreModel()).ToList(); #endregion #region Images if (dbItem.Images != null) { retVal.Images = dbItem.Images.OrderBy(x => x.SortOrder).Select(x => x.ToCoreModel()).ToList(); } #endregion #region Assets if (dbItem.Assets != null) { retVal.Assets = dbItem.Assets.OrderBy(x => x.CreatedDate).Select(x => x.ToCoreModel()).ToList(); } #endregion #region Property values if (dbItem.ItemPropertyValues != null) { retVal.PropertyValues = dbItem.ItemPropertyValues.OrderBy(x => x.Name).Select(x => x.ToCoreModel(null)).ToList(); } #endregion #region Variations retVal.Variations = new List <coreModel.CatalogProduct>(); foreach (var variation in dbItem.Childrens) { var productVaraition = variation.ToCoreModel(catalog, category, associatedProducts: null); productVaraition.MainProduct = retVal; productVaraition.MainProductId = retVal.Id; retVal.Variations.Add(productVaraition); } #endregion #region EditorialReviews if (dbItem.EditorialReviews != null) { retVal.Reviews = dbItem.EditorialReviews.Select(x => x.ToCoreModel()).ToList(); } #endregion #region Associations if (dbItem.AssociationGroups != null && associatedProducts != null) { retVal.Associations = new List <coreModel.ProductAssociation>(); foreach (var association in dbItem.AssociationGroups.SelectMany(x => x.Associations)) { var associatedProduct = associatedProducts.FirstOrDefault(x => x.Id == association.ItemId); if (associatedProduct != null) { var productAssociation = association.ToCoreModel(associatedProduct); retVal.Associations.Add(productAssociation); } } } #endregion //TaxType category inheritance if (retVal.TaxType == null && category != null) { retVal.TaxType = category.TaxType; } #region Variation property, assets, review inheritance if (dbItem.Parent != null) { //TaxType from main product inheritance if (dbItem.TaxType == null && dbItem.Parent.TaxType != null) { retVal.TaxType = dbItem.Parent.TaxType; } var allProductPropertyNames = dbItem.Parent.ItemPropertyValues.Select(x => x.Name).Distinct().ToArray(); //Property inheritance if (allProductPropertyNames != null) { //Need copy not overridden property values from main product to variation var overriddenPropertyNames = retVal.PropertyValues.Select(x => x.PropertyName).ToArray(); var inheritedPropertyNames = allProductPropertyNames.Except(overriddenPropertyNames); var dbInheritedPropertyValues = dbItem.Parent.ItemPropertyValues.Where(x => inheritedPropertyNames.Contains(x.Name)); foreach (var dbInheritedPropertyValue in dbInheritedPropertyValues) { //Reset id for correct value override var propertyValue = dbInheritedPropertyValue.ToCoreModel(null); propertyValue.Id = null; retVal.PropertyValues.Add(propertyValue); } } //Image inheritance if (!retVal.Images.Any() && dbItem.Parent.Images != null) { retVal.Images = dbItem.Parent.Images.OrderBy(x => x.SortOrder).Select(x => x.ToCoreModel()).ToList(); foreach (var image in retVal.Images) { //Reset id for correct override image.Id = null; } } //Review inheritance if ((retVal.Reviews == null || !retVal.Reviews.Any()) && dbItem.Parent.EditorialReviews != null) { retVal.Reviews = dbItem.Parent.EditorialReviews.Select(x => x.ToCoreModel()).ToList(); foreach (var review in retVal.Reviews) { //Reset id for correct override review.Id = null; } } } #endregion return(retVal); }
/// <summary> /// Converting to model type /// </summary> /// <param name="catalogBase"></param> /// <returns></returns> public static coreModel.Property ToCoreModel(this dataModel.Property dbProperty, coreModel.Catalog catalog, coreModel.Category category) { if (dbProperty == null) { throw new ArgumentNullException("dbProperty"); } var retVal = new coreModel.Property(); retVal.InjectFrom(dbProperty); retVal.Required = dbProperty.IsRequired; retVal.Multivalue = dbProperty.IsMultiValue; retVal.Multilanguage = dbProperty.IsLocaleDependant; retVal.Dictionary = dbProperty.IsEnum; retVal.ValueType = (coreModel.PropertyValueType)dbProperty.PropertyValueType; retVal.CatalogId = catalog.Id; retVal.Catalog = catalog; retVal.CategoryId = category == null ? null : category.Id; retVal.Category = category; coreModel.PropertyType propertyType; if (!string.IsNullOrEmpty(dbProperty.TargetType) && Enum.TryParse(dbProperty.TargetType, out propertyType)) { retVal.Type = propertyType; } retVal.DisplayNames = catalog.Languages.Select(x => new PropertyDisplayName { LanguageCode = x.LanguageCode }).ToList(); if (dbProperty.PropertyAttributes != null) { retVal.Attributes = new List <coreModel.PropertyAttribute>(); retVal.Attributes.AddRange(dbProperty.PropertyAttributes.Select(x => x.ToCoreModel(retVal))); //Load display names from attributes foreach (var displayNameAttribute in retVal.Attributes.Where(x => x.Name.StartsWith("DisplayName"))) { var languageCode = displayNameAttribute.Name.Substring("DisplayName".Length); var displayName = retVal.DisplayNames.FirstOrDefault(x => String.Equals(x.LanguageCode, languageCode, StringComparison.InvariantCultureIgnoreCase)); if (displayName != null) { displayName.Name = displayNameAttribute.Value; } } } if (dbProperty.PropertyValues != null) { retVal.DictionaryValues = new List <coreModel.PropertyDictionaryValue>(); retVal.DictionaryValues.AddRange(dbProperty.PropertyValues.Select(x => x.ToCoreModel(retVal))); } return(retVal); }
private void SaveProduct(coreModel.Catalog catalog, FulfillmentCenter defaultFulfillmentCenter, CsvProduct csvProduct) { var defaultLanguge = catalog.DefaultLanguage != null ? catalog.DefaultLanguage.LanguageCode : "EN-US"; coreModel.CatalogProduct alreadyExistProduct = null; //For new product try to find them by code if (csvProduct.IsTransient() && !String.IsNullOrEmpty(csvProduct.Code)) { var criteria = new SearchCriteria { CatalogId = catalog.Id, CategoryId = csvProduct.CategoryId, Code = csvProduct.Code, ResponseGroup = SearchResponseGroup.WithProducts | SearchResponseGroup.WithVariations }; var result = _searchService.Search(criteria); alreadyExistProduct = result.Products.FirstOrDefault(); csvProduct.Id = alreadyExistProduct != null ? alreadyExistProduct.Id : csvProduct.Id; } else if (!csvProduct.IsTransient()) { //If id specified need check that product really exist alreadyExistProduct = _productService.GetById(csvProduct.Id, ItemResponseGroup.ItemInfo); } var isNewProduct = alreadyExistProduct == null; csvProduct.CatalogId = catalog.Id; if (String.IsNullOrEmpty(csvProduct.Code)) { csvProduct.Code = _skuGenerator.GenerateSku(csvProduct); } //Set a parent relations if (csvProduct.MainProductId == null && csvProduct.MainProduct != null) { csvProduct.MainProductId = csvProduct.MainProduct.Id; } csvProduct.EditorialReview.LanguageCode = defaultLanguge; csvProduct.SeoInfo.LanguageCode = defaultLanguge; csvProduct.SeoInfo.SemanticUrl = String.IsNullOrEmpty(csvProduct.SeoInfo.SemanticUrl) ? csvProduct.Code : csvProduct.SeoInfo.SemanticUrl; var properties = !String.IsNullOrEmpty(csvProduct.CategoryId) ? _categoryService.GetById(csvProduct.CategoryId, CategoryResponseGroup.WithProperties).Properties : _catalogService.GetById(csvProduct.CatalogId).Properties; if (csvProduct.PropertyValues != null) { //Try to fill properties meta information for values foreach (var propertyValue in csvProduct.PropertyValues) { if (propertyValue.Value != null) { var property = properties.FirstOrDefault(x => String.Equals(x.Name, propertyValue.PropertyName)); if (property != null) { propertyValue.ValueType = property.ValueType; if (property.Dictionary) { property = _propertyService.GetById(property.Id); var dicValue = property.DictionaryValues.FirstOrDefault(x => String.Equals(x.Value, propertyValue.Value)); propertyValue.ValueId = dicValue != null ? dicValue.Id : null; } } } } } if (!isNewProduct) { _productService.Update(new coreModel.CatalogProduct[] { csvProduct }); } else { var newProduct = _productService.Create(csvProduct); csvProduct.Id = newProduct.Id; } //Create price in default price list if (csvProduct.Price.EffectiveValue > 0) { csvProduct.Price.ProductId = csvProduct.Id; if (csvProduct.Price.IsTransient() || _pricingService.GetPriceById(csvProduct.Price.Id) == null) { _pricingService.CreatePrice(csvProduct.Price); } else { _pricingService.UpdatePrices(new Price[] { csvProduct.Price }); } } //Create inventory csvProduct.Inventory.ProductId = csvProduct.Id; csvProduct.Inventory.FulfillmentCenterId = csvProduct.Inventory.FulfillmentCenterId ?? defaultFulfillmentCenter.Id; _inventoryService.UpsertInventory(csvProduct.Inventory); }
/// <summary> /// Converting to model type /// </summary> /// <param name="dbCategoryBase">The database category base.</param> /// <param name="catalog">The catalog.</param> /// <param name="properties">The properties.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">catalog</exception> public static coreModel.Category ToCoreModel(this dataModel.CategoryBase dbCategoryBase, coreModel.Catalog catalog, coreModel.Property[] properties = null, dataModel.Category[] allParents = null) { if (catalog == null) { throw new ArgumentNullException("catalog"); } var retVal = new coreModel.Category(); retVal.InjectFrom(dbCategoryBase); retVal.CatalogId = catalog.Id; retVal.Catalog = catalog; retVal.ParentId = dbCategoryBase.ParentCategoryId; var dbCategory = dbCategoryBase as dataModel.Category; if (dbCategory != null) { retVal.PropertyValues = dbCategory.CategoryPropertyValues.Select(x => x.ToCoreModel(properties)).ToList(); retVal.Virtual = catalog.Virtual; retVal.Links = dbCategory.OutgoingLinks.Select(x => x.ToCoreModel(retVal)).ToList(); } if (allParents != null) { retVal.Parents = allParents.Select(x => x.ToCoreModel(catalog)).ToArray(); } return(retVal); }