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> /// 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="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 }