public CatalogProduct[] GetByIds(string[] itemIds, ItemResponseGroup respGroup, string catalogId = null) { var retVal = _productService.GetByIds(itemIds, respGroup, catalogId).ToList(); var foundIds = retVal.Select(x => x.Id).ToArray(); var taobaoProductIds = itemIds.Except(foundIds).Concat(retVal.Where(x => x.CatalogId.EqualsInvariant(_taobaoCatalog.Id)).Select(x => x.Id)).ToArray(); foreach (var taobaoProductId in taobaoProductIds) { var request = new ItemGetRequest() { NumIid = Convert.ToInt64(taobaoProductId), Fields = @"num_iid,title,nick,type,cid,seller_cids,props,pic_url,num,modified,product_id,item_img,prop_img,sku,video,outer_id,skus, desc", }; var itemGetResponse = _topClient.Execute <ItemGetResponse>(request); if (itemGetResponse.Item != null) { var topItem = itemGetResponse.Item; var product = new dataModel.Item() { CatalogId = _taobaoCatalog.Id }.ToCoreModel(base.AllCachedCatalogs, base.AllCachedCategories); product.Id = topItem.NumIid.ToString(); product.Catalog = _taobaoCatalog; product.CatalogId = _taobaoCatalog.Id; product.Code = topItem.Num.ToString(); product.Name = topItem.Title; product.Images = new Image[] { new Image { Url = topItem.PicUrl } }; product.Reviews = new EditorialReview[] { new EditorialReview { ReviewType = "Description", LanguageCode = "en-US", Content = topItem.Desc } }; var existProduct = retVal.FirstOrDefault(x => x.Id == topItem.NumIid.ToString()); if (existProduct != null) { existProduct.Images = product.Images; existProduct.Reviews = product.Reviews; } else { retVal.Add(product); } } } return(retVal.ToArray()); }
private CatalogProduct ConvertToProduct(External.CatalogModule.Web.CatalogModuleApi.Models.Product dto) { var retVal = new dataModel.Item() { CatalogId = dto.CatalogId }.ToCoreModel(base.AllCachedCatalogs, base.AllCachedCategories); retVal.InjectFrom(dto); if (!dto.Images.IsNullOrEmpty()) { retVal.Images = dto.Images.Select(x => new Image().InjectFrom(x) as Image).ToList(); } if (!dto.Reviews.IsNullOrEmpty()) { retVal.Reviews = dto.Reviews.Select(x => new EditorialReview().InjectFrom(x) as EditorialReview).ToList(); } if (!dto.Outlines.IsNullOrEmpty()) { retVal.Outlines = dto.Outlines.Select(x => ConvertToOutline(x)).ToList(); } return(retVal); }
/// <summary> /// Patch changes /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <param name="pkMap"></param> public static void Patch(this coreModel.CatalogProduct source, dataModel.Item target, PrimaryKeyResolvingMap pkMap) { if (target == null) { throw new ArgumentNullException(nameof(target)); } // TODO: temporary solution because partial update replaced not nullable properties in db entity if (source.IsBuyable != null) { target.IsBuyable = source.IsBuyable.Value; } if (source.IsActive != null) { target.IsActive = source.IsActive.Value; } if (source.TrackInventory != null) { target.TrackInventory = source.TrackInventory.Value; } if (source.MinQuantity != null) { target.MinQuantity = source.MinQuantity.Value; } if (source.MaxQuantity != null) { target.MaxQuantity = source.MaxQuantity.Value; } if (source.EnableReview != null) { target.EnableReview = source.EnableReview.Value; } target.CatalogId = string.IsNullOrEmpty(source.CatalogId) ? null : source.CatalogId; target.CategoryId = string.IsNullOrEmpty(source.CategoryId) ? null : source.CategoryId; target.Name = source.Name; target.Code = source.Code; target.ManufacturerPartNumber = source.ManufacturerPartNumber; target.Gtin = source.Gtin; target.ProductType = source.ProductType; target.MaxNumberOfDownload = source.MaxNumberOfDownload; target.DownloadType = source.DownloadType; target.HasUserAgreement = source.HasUserAgreement; target.DownloadExpiration = source.DownloadExpiration; target.Vendor = source.Vendor; target.TaxType = source.TaxType; target.WeightUnit = source.WeightUnit; target.Weight = source.Weight; target.MeasureUnit = source.MeasureUnit; target.PackageType = source.PackageType; target.Height = source.Height; target.Length = source.Length; target.Width = source.Width; target.ShippingType = source.ShippingType; target.Priority = source.Priority; target.ParentId = source.MainProductId; var dbSource = source.ToDataModel(pkMap); #region Assets if (!dbSource.Assets.IsNullCollection()) { dbSource.Assets.Patch(target.Assets, (sourceAsset, targetAsset) => sourceAsset.Patch(targetAsset)); } #endregion #region Images if (!dbSource.Images.IsNullCollection()) { dbSource.Images.Patch(target.Images, (sourceImage, targetImage) => sourceImage.Patch(targetImage)); } #endregion #region ItemPropertyValues if (!dbSource.ItemPropertyValues.IsNullCollection()) { dbSource.ItemPropertyValues.Patch(target.ItemPropertyValues, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region Links if (!dbSource.CategoryLinks.IsNullCollection()) { dbSource.CategoryLinks.Patch(target.CategoryLinks, new CategoryItemRelationComparer(), (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region EditorialReviews if (!dbSource.EditorialReviews.IsNullCollection()) { dbSource.EditorialReviews.Patch(target.EditorialReviews, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region Association if (!dbSource.Associations.IsNullCollection()) { var associationComparer = AnonymousComparer.Create((dataModel.Association x) => x.AssociationType + ":" + x.AssociatedItemId + ":" + x.AssociatedCategoryId); dbSource.Associations.Patch(target.Associations, associationComparer, (sourceAssociation, targetAssociation) => sourceAssociation.Patch(targetAssociation)); } #endregion }
/// <summary> /// Converting to model type /// </summary> /// <returns></returns> public static coreModel.CatalogProduct ToCoreModel(this dataModel.Item dbItem, dataModel.Catalog[] allCatalogs, dataModel.Category[] allCategories, bool convertChildrens = true) { var retVal = new coreModel.CatalogProduct(); retVal.InjectFrom(dbItem); retVal.Catalog = allCatalogs.First(x => x.Id == dbItem.CatalogId).ToCoreModel(); if (dbItem.CategoryId != null) { retVal.Category = allCategories.First(x => x.Id == dbItem.CategoryId) .ToCoreModel(allCatalogs, allCategories); } retVal.MainProductId = dbItem.ParentId; if (dbItem.Parent != null) { retVal.MainProduct = dbItem.Parent.ToCoreModel(allCatalogs, allCategories, convertChildrens: false); } retVal.IsActive = dbItem.IsActive; retVal.IsBuyable = dbItem.IsBuyable; retVal.TrackInventory = dbItem.TrackInventory; retVal.MaxQuantity = (int)dbItem.MaxQuantity; retVal.MinQuantity = (int)dbItem.MinQuantity; //Links retVal.Links = dbItem.CategoryLinks.Select(x => x.ToCoreModel(allCatalogs, allCategories)).ToList(); //Images retVal.Images = dbItem.Images.OrderBy(x => x.SortOrder).Select(x => x.ToCoreModel()).ToList(); //Inherit images from parent product (if its not set) if (!retVal.Images.Any() && retVal.MainProduct != null && retVal.MainProduct.Images != null) { retVal.Images = retVal.MainProduct.Images.Select(x => x.Clone()).OfType <coreModel.Image>().ToList(); foreach (var image in retVal.Images) { image.Id = null; image.IsInherited = true; } } //Assets retVal.Assets = dbItem.Assets.OrderBy(x => x.CreatedDate).Select(x => x.ToCoreModel()).ToList(); //Inherit images from parent product (if its not set) if (!retVal.Assets.Any() && retVal.MainProduct != null && retVal.MainProduct.Assets != null) { retVal.Assets = retVal.MainProduct.Assets.Select(x => x.Clone()).OfType <coreModel.Asset>().ToList(); foreach (var asset in retVal.Assets) { asset.Id = null; asset.IsInherited = true; } } // EditorialReviews retVal.Reviews = dbItem.EditorialReviews.Select(x => x.ToCoreModel()).ToList(); //inherit editorial reviews from main product and do not inherit if variation loaded within product if (!retVal.Reviews.Any() && retVal.MainProduct != null && retVal.MainProduct.Reviews != null && convertChildrens) { retVal.Reviews = retVal.MainProduct.Reviews.Select(x => x.Clone()).OfType <coreModel.EditorialReview>().ToList(); foreach (var review in retVal.Reviews) { review.Id = null; review.IsInherited = true; } } // Associations retVal.Associations = dbItem.Associations.Select(x => x.ToCoreModel(allCatalogs, allCategories)).OrderBy(x => x.Priority).ToList(); //TaxType category inheritance if (retVal.TaxType == null && retVal.Category != null) { retVal.TaxType = retVal.Category.TaxType; } retVal.Properties = new List <coreModel.Property>(); //Properties inheritance retVal.Properties.AddRange(retVal.Category != null ? retVal.Category.Properties : retVal.Catalog.Properties); foreach (var property in retVal.Properties) { property.IsInherited = true; } //Sort properties by name retVal.Properties = retVal.Properties.OrderBy(x => x.Name).ToList(); //Self item property values retVal.PropertyValues = dbItem.ItemPropertyValues.OrderBy(x => x.Name).Select(x => x.ToCoreModel()).ToList(); foreach (var propertyValue in retVal.PropertyValues.ToArray()) { //Try to find property meta information propertyValue.Property = retVal.Properties.FirstOrDefault(x => x.IsSuitableForValue(propertyValue)); //Return each localized value for selected 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 = propertyValue.TryGetAllLocalizedDictValues(); foreach (var localizedDictValue in localizedDictValues) { if (!retVal.PropertyValues.Any(x => x.ValueId == localizedDictValue.ValueId && x.LanguageCode == localizedDictValue.LanguageCode)) { retVal.PropertyValues.Add(localizedDictValue); } } } //inherit not overriden property values from main product if (retVal.MainProduct != null && retVal.MainProduct.PropertyValues != null) { var mainProductPopValuesGroups = retVal.MainProduct.PropertyValues.GroupBy(x => x.PropertyName); foreach (var group in mainProductPopValuesGroups) { //Inherit all values if not overriden if (!retVal.PropertyValues.Any(x => x.PropertyName.EqualsInvariant(group.Key))) { foreach (var inheritedpropValue in group) { inheritedpropValue.Id = null; inheritedpropValue.IsInherited = true; retVal.PropertyValues.Add(inheritedpropValue); } } } } if (convertChildrens) { // Variations retVal.Variations = new List <coreModel.CatalogProduct>(); foreach (var variation in dbItem.Childrens) { var productVariation = variation.ToCoreModel(allCatalogs, allCategories, convertChildrens: false); productVariation.MainProduct = retVal; productVariation.MainProductId = retVal.Id; retVal.Variations.Add(productVariation); } } return(retVal); }
/// <summary> /// Converting to foundation type /// </summary> /// <param name="product"></param> /// <param name="pkMap"></param> /// <returns></returns> public static dataModel.Item ToDataModel(this coreModel.CatalogProduct product, PrimaryKeyResolvingMap pkMap) { var retVal = new dataModel.Item(); pkMap.AddPair(product, retVal); retVal.InjectFrom(product); if (product.StartDate == default(DateTime)) { retVal.StartDate = DateTime.UtcNow; } retVal.IsActive = product.IsActive ?? true; retVal.IsBuyable = product.IsBuyable ?? true; retVal.TrackInventory = product.TrackInventory ?? true; retVal.MaxQuantity = product.MaxQuantity ?? 0; retVal.MinQuantity = product.MinQuantity ?? 0; retVal.ParentId = product.MainProductId; //Constant fields //Only for main product retVal.AvailabilityRule = (int)coreModel.AvailabilityRule.Always; retVal.MinQuantity = 1; retVal.MaxQuantity = 0; retVal.CatalogId = product.CatalogId; retVal.CategoryId = string.IsNullOrEmpty(product.CategoryId) ? null : product.CategoryId; #region ItemPropertyValues if (product.PropertyValues != null) { retVal.ItemPropertyValues = new ObservableCollection <dataModel.PropertyValue>(); retVal.ItemPropertyValues.AddRange(product.PropertyValues.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Assets if (product.Assets != null) { retVal.Assets = new ObservableCollection <dataModel.Asset>(product.Assets.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Images if (product.Images != null) { retVal.Images = new ObservableCollection <dataModel.Image>(product.Images.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Links if (product.Links != null) { retVal.CategoryLinks = new ObservableCollection <dataModel.CategoryItemRelation>(); retVal.CategoryLinks.AddRange(product.Links.Select(x => x.ToDataModel(product))); } #endregion #region EditorialReview if (product.Reviews != null) { retVal.EditorialReviews = new ObservableCollection <dataModel.EditorialReview>(); retVal.EditorialReviews.AddRange(product.Reviews.Where(x => !x.IsInherited).Select(x => x.ToDataModel(retVal, pkMap))); } #endregion #region Associations if (product.Associations != null) { retVal.Associations = new ObservableCollection <dataModel.Association>(product.Associations.Select(x => x.ToDataModel())); } #endregion return(retVal); }
/// <summary> /// Converting to foundation type /// </summary> /// <param name="itemAsset">The item asset.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">itemAsset</exception> public static dataModel.EditorialReview ToDataModel(this coreModel.EditorialReview review, dataModel.Item product, PrimaryKeyResolvingMap pkMap) { if (review == null) { throw new ArgumentNullException("review"); } var retVal = new dataModel.EditorialReview(); pkMap.AddPair(review, retVal); retVal.InjectFrom(review); retVal.ItemId = product.Id; retVal.Source = review.ReviewType; retVal.ReviewState = (int)coreModel.ReviewState.Active; retVal.Locale = review.LanguageCode; return(retVal); }
/// <summary> /// Converting to foundation type /// </summary> /// <param name="catalog"></param> /// <returns></returns> public static dataModel.Item ToDataModel(this coreModel.CatalogProduct product, PrimaryKeyResolvingMap pkMap) { var retVal = new dataModel.Item(); pkMap.AddPair(product, retVal); retVal.InjectFrom(product); if(product.StartDate == default(DateTime)) { retVal.StartDate = DateTime.UtcNow; } retVal.IsActive = product.IsActive ?? true; retVal.IsBuyable = product.IsBuyable ?? true; retVal.TrackInventory = product.TrackInventory ?? true; retVal.MaxQuantity = product.MaxQuantity ?? 0; retVal.MinQuantity = product.MinQuantity ?? 0; retVal.ParentId = product.MainProductId; //Constant fields //Only for main product retVal.AvailabilityRule = (int)coreModel.AvailabilityRule.Always; retVal.MinQuantity = 1; retVal.MaxQuantity = 0; retVal.CatalogId = product.CatalogId; retVal.CategoryId = String.IsNullOrEmpty(product.CategoryId) ? null : product.CategoryId; #region ItemPropertyValues if (product.PropertyValues != null) { retVal.ItemPropertyValues = new ObservableCollection<dataModel.PropertyValue>(); retVal.ItemPropertyValues.AddRange(product.PropertyValues.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Assets if (product.Assets != null) { retVal.Assets = new ObservableCollection<dataModel.Asset>(product.Assets.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Images if (product.Images != null) { retVal.Images = new ObservableCollection<dataModel.Image>(product.Images.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Links if (product.Links != null) { retVal.CategoryLinks = new ObservableCollection<dataModel.CategoryItemRelation>(); retVal.CategoryLinks.AddRange(product.Links.Select(x => x.ToDataModel(product))); } #endregion #region EditorialReview if (product.Reviews != null) { retVal.EditorialReviews = new ObservableCollection<dataModel.EditorialReview>(); retVal.EditorialReviews.AddRange(product.Reviews.Where(x => !x.IsInherited).Select(x => x.ToDataModel(retVal, pkMap))); } #endregion #region Associations if (product.Associations != null) { retVal.AssociationGroups = new ObservableCollection<dataModel.AssociationGroup>(); var associations = product.Associations.ToArray(); for (int order = 0; order < associations.Count(); order++) { var association = associations[order]; var associationGroup = retVal.AssociationGroups.FirstOrDefault(x => x.Name == association.Name); if (associationGroup == null) { associationGroup = new dataModel.AssociationGroup { Name = association.Name, Description = association.Description, Priority = 1, }; retVal.AssociationGroups.Add(associationGroup); } var foundationAssociation = association.ToDataModel(); foundationAssociation.Priority = order; associationGroup.Associations.Add(foundationAssociation); } } #endregion return retVal; }
/// <summary> /// Patch changes /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void Patch(this coreModel.CatalogProduct source, dataModel.Item target, PrimaryKeyResolvingMap pkMap) { if (target == null) { throw new ArgumentNullException("target"); } //TODO: temporary solution because partial update replaced not nullable properties in db entity if (source.IsBuyable != null) { target.IsBuyable = source.IsBuyable.Value; } if (source.IsActive != null) { target.IsActive = source.IsActive.Value; } if (source.TrackInventory != null) { target.TrackInventory = source.TrackInventory.Value; } if (source.MinQuantity != null) { target.MinQuantity = source.MinQuantity.Value; } if (source.MaxQuantity != null) { target.MaxQuantity = source.MaxQuantity.Value; } //Handle three valuable states (null, empty and have value states) for case when need reset catalog or category if (source.CatalogId == String.Empty) { target.CatalogId = null; } if (source.CategoryId == String.Empty) { target.CategoryId = null; } var patchInjectionPolicy = new PatchInjection <dataModel.Item>(x => x.Name, x => x.Code, x => x.ManufacturerPartNumber, x => x.Gtin, x => x.ProductType, x => x.WeightUnit, x => x.Weight, x => x.MeasureUnit, x => x.Height, x => x.Length, x => x.Width, x => x.EnableReview, x => x.MaxNumberOfDownload, x => x.DownloadExpiration, x => x.DownloadType, x => x.HasUserAgreement, x => x.ShippingType, x => x.TaxType, x => x.Vendor, x => x.CatalogId, x => x.CategoryId); var dbSource = source.ToDataModel(pkMap); target.InjectFrom(patchInjectionPolicy, dbSource); #region Assets if (!dbSource.Assets.IsNullCollection()) { dbSource.Assets.Patch(target.Assets, (sourceAsset, targetAsset) => sourceAsset.Patch(targetAsset)); } #endregion #region Images if (!dbSource.Images.IsNullCollection()) { dbSource.Images.Patch(target.Images, (sourceImage, targetImage) => sourceImage.Patch(targetImage)); } #endregion #region ItemPropertyValues if (!dbSource.ItemPropertyValues.IsNullCollection()) { dbSource.ItemPropertyValues.Patch(target.ItemPropertyValues, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region Links if (!dbSource.CategoryLinks.IsNullCollection()) { dbSource.CategoryLinks.Patch(target.CategoryLinks, new CategoryItemRelationComparer(), (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region EditorialReviews if (!dbSource.EditorialReviews.IsNullCollection()) { dbSource.EditorialReviews.Patch(target.EditorialReviews, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region Association if (!dbSource.AssociationGroups.IsNullCollection()) { var associationComparer = AnonymousComparer.Create((dataModel.AssociationGroup x) => x.Name); dbSource.AssociationGroups.Patch(target.AssociationGroups, associationComparer, (sourceGroup, targetGroup) => sourceGroup.Patch(targetGroup)); } #endregion }
/// <summary> /// Converting to foundation type /// </summary> /// <param name="catalog"></param> /// <returns></returns> public static dataModel.Item ToDataModel(this coreModel.CatalogProduct product, PrimaryKeyResolvingMap pkMap) { var retVal = new dataModel.Item(); pkMap.AddPair(product, retVal); retVal.InjectFrom(product); if (product.StartDate == default(DateTime)) { retVal.StartDate = DateTime.UtcNow; } retVal.IsActive = product.IsActive ?? true; retVal.IsBuyable = product.IsBuyable ?? true; retVal.TrackInventory = product.TrackInventory ?? true; retVal.MaxQuantity = product.MaxQuantity ?? 0; retVal.MinQuantity = product.MinQuantity ?? 0; retVal.ParentId = product.MainProductId; //Constant fields //Only for main product retVal.AvailabilityRule = (int)coreModel.AvailabilityRule.Always; retVal.MinQuantity = 1; retVal.MaxQuantity = 0; retVal.CatalogId = product.CatalogId; retVal.CategoryId = String.IsNullOrEmpty(product.CategoryId) ? null : product.CategoryId; #region ItemPropertyValues if (product.PropertyValues != null) { retVal.ItemPropertyValues = new ObservableCollection <dataModel.PropertyValue>(); retVal.ItemPropertyValues.AddRange(product.PropertyValues.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Assets if (product.Assets != null) { retVal.Assets = new ObservableCollection <dataModel.Asset>(product.Assets.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Images if (product.Images != null) { retVal.Images = new ObservableCollection <dataModel.Image>(product.Images.Where(x => !x.IsInherited).Select(x => x.ToDataModel(pkMap))); } #endregion #region Links if (product.Links != null) { retVal.CategoryLinks = new ObservableCollection <dataModel.CategoryItemRelation>(); retVal.CategoryLinks.AddRange(product.Links.Select(x => x.ToDataModel(product))); } #endregion #region EditorialReview if (product.Reviews != null) { retVal.EditorialReviews = new ObservableCollection <dataModel.EditorialReview>(); retVal.EditorialReviews.AddRange(product.Reviews.Where(x => !x.IsInherited).Select(x => x.ToDataModel(retVal, pkMap))); } #endregion #region Associations if (product.Associations != null) { retVal.AssociationGroups = new ObservableCollection <dataModel.AssociationGroup>(); var associations = product.Associations.ToArray(); for (int order = 0; order < associations.Count(); order++) { var association = associations[order]; var associationGroup = retVal.AssociationGroups.FirstOrDefault(x => x.Name == association.Name); if (associationGroup == null) { associationGroup = new dataModel.AssociationGroup { Name = association.Name, Description = association.Description, Priority = 1, }; retVal.AssociationGroups.Add(associationGroup); } var foundationAssociation = association.ToDataModel(); foundationAssociation.Priority = order; associationGroup.Associations.Add(foundationAssociation); } } #endregion return(retVal); }
/// <summary> /// Patch changes /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void Patch(this coreModel.CatalogProduct source, dataModel.Item target) { if (target == null) { throw new ArgumentNullException("target"); } //TODO: temporary solution because partial update replaced not nullable properties in db entity if (source.IsBuyable != null) { target.IsBuyable = source.IsBuyable.Value; } if (source.IsActive != null) { target.IsActive = source.IsActive.Value; } if (source.TrackInventory != null) { target.TrackInventory = source.TrackInventory.Value; } if (source.MinQuantity != null) { target.MinQuantity = source.MinQuantity.Value; } if (source.MaxQuantity != null) { target.MaxQuantity = source.MaxQuantity.Value; } var patchInjectionPolicy = new PatchInjection <dataModel.Item>(x => x.CategoryId, x => x.Name, x => x.Code, x => x.ManufacturerPartNumber, x => x.Gtin, x => x.ProductType, x => x.WeightUnit, x => x.Weight, x => x.MeasureUnit, x => x.Height, x => x.Length, x => x.Width, x => x.EnableReview, x => x.MaxNumberOfDownload, x => x.DownloadExpiration, x => x.DownloadType, x => x.HasUserAgreement, x => x.ShippingType, x => x.TaxType, x => x.Vendor); var dbSource = source.ToDataModel(); target.InjectFrom(patchInjectionPolicy, dbSource); #region Assets if (!dbSource.Assets.IsNullCollection()) { dbSource.Assets.Patch(target.Assets, (sourceAsset, targetAsset) => sourceAsset.Patch(targetAsset)); } #endregion #region Images if (!dbSource.Images.IsNullCollection()) { dbSource.Images.Patch(target.Images, (sourceImage, targetImage) => sourceImage.Patch(targetImage)); } #endregion #region ItemPropertyValues if (!dbSource.ItemPropertyValues.IsNullCollection()) { //Need skip inherited properties without overridden value if (source.MainProduct != null) { var dbParent = source.MainProduct.ToDataModel(); var parentPropValues = dbParent.ItemPropertyValues.ToLookup(x => x.Name + "-" + x.ToString()); var variationPropValues = dbSource.ItemPropertyValues.ToLookup(x => x.Name + "-" + x.ToString()); dbSource.ItemPropertyValues = new ObservableCollection <dataModel.ItemPropertyValue>(variationPropValues.Where(x => !parentPropValues.Contains(x.Key)).SelectMany(x => x)); } dbSource.ItemPropertyValues.Patch(target.ItemPropertyValues, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region Links if (!dbSource.CategoryLinks.IsNullCollection()) { dbSource.CategoryLinks.Patch(target.CategoryLinks, new CategoryItemRelationComparer(), (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region EditorialReviews if (!dbSource.EditorialReviews.IsNullCollection()) { dbSource.EditorialReviews.Patch(target.EditorialReviews, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } #endregion #region Association if (!dbSource.AssociationGroups.IsNullCollection()) { var associationComparer = AnonymousComparer.Create((dataModel.AssociationGroup x) => x.Name); dbSource.AssociationGroups.Patch(target.AssociationGroups, associationComparer, (sourceGroup, targetGroup) => sourceGroup.Patch(targetGroup)); } #endregion }
/// <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 foundation type /// </summary> /// <param name="itemAsset">The item asset.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">itemAsset</exception> public static dataModel.EditorialReview ToDataModel(this coreModel.EditorialReview review, dataModel.Item product) { if (review == null) { throw new ArgumentNullException("review"); } var retVal = new dataModel.EditorialReview(); var id = retVal.Id; retVal.InjectFrom(review); if (review.Id == null) { retVal.Id = id; } retVal.ItemId = product.Id; retVal.Source = review.ReviewType; retVal.ReviewState = (int)coreModel.ReviewState.Active; retVal.Locale = review.LanguageCode; return(retVal); }