Example #1
0
        public virtual void TryInheritFrom(IEntity parent)
        {
            if (parent is IHasProperties hasProperties)
            {
                //Properties inheritance
                foreach (var parentProperty in hasProperties.Properties)
                {
                    var existProperty = Properties.FirstOrDefault(x => x.IsSame(parentProperty, PropertyType.Product, PropertyType.Variation));
                    if (existProperty != null)
                    {
                        existProperty.TryInheritFrom(parentProperty);
                        existProperty.ActualizeValues();
                    }
                    else
                    {
                        Properties.Add(parentProperty);
                    }
                }
            }

            if (parent is IHasTaxType hasTaxType)
            {
                //TODO: prevent saving the inherited simple values
                //TaxType  inheritance
                if (TaxType == null)
                {
                    TaxType = hasTaxType.TaxType;
                }
            }

            if (parent is CatalogProduct parentProduct)
            {
                var isVariation = GetType().IsAssignableFrom(typeof(Variation));
                //Inherit images from parent product (if its not set)
                if (Images.IsNullOrEmpty() && !parentProduct.Images.IsNullOrEmpty())
                {
                    Images = new List <Image>();
                    foreach (var parentImage in parentProduct.Images)
                    {
                        var image = AbstractTypeFactory <Image> .TryCreateInstance();

                        image.TryInheritFrom(parentImage);
                        Images.Add(image);
                    }
                }

                //Inherit assets from parent product (if its not set)
                if (Assets.IsNullOrEmpty() && !parentProduct.Assets.IsNullOrEmpty())
                {
                    Assets = new List <Asset>();
                    foreach (var parentAsset in parentProduct.Assets)
                    {
                        var asset = AbstractTypeFactory <Asset> .TryCreateInstance();

                        asset.TryInheritFrom(parentAsset);
                        Assets.Add(asset);
                    }
                }

                //inherit editorial reviews from main product and do not inherit if variation loaded within product
                if (!isVariation && Reviews.IsNullOrEmpty() && parentProduct.Reviews != null)
                {
                    Reviews = new List <EditorialReview>();
                    foreach (var parentReview in parentProduct.Reviews)
                    {
                        var review = AbstractTypeFactory <EditorialReview> .TryCreateInstance();

                        review.TryInheritFrom(parentReview);
                        Reviews.Add(review);
                    }
                }
                //inherit not overridden property values from main product
                foreach (var parentProductProperty in parentProduct.Properties)
                {
                    var existProperty = Properties.FirstOrDefault(x => x.IsSame(parentProductProperty, PropertyType.Product, PropertyType.Variation));
                    if (existProperty == null)
                    {
                        var property = AbstractTypeFactory <Property> .TryCreateInstance();

                        property.TryInheritFrom(parentProductProperty);
                        Properties.Add(parentProductProperty);
                    }
                }
                //TODO: prevent saving the inherited simple values
                Width       = Width ?? parentProduct.MainProduct.Width;
                Height      = Height ?? parentProduct.MainProduct.Height;
                Length      = Length ?? parentProduct.MainProduct.Length;
                MeasureUnit = MeasureUnit ?? parentProduct.MainProduct.MeasureUnit;
                Weight      = Weight ?? parentProduct.MainProduct.Weight;
                WeightUnit  = WeightUnit ?? parentProduct.MainProduct.WeightUnit;
                PackageType = PackageType ?? parentProduct.MainProduct.PackageType;

                if (!Variations.IsNullOrEmpty())
                {
                    foreach (var variation in Variations)
                    {
                        variation.TryInheritFrom(this);
                    }
                }
            }
        }
Example #2
0
        public virtual void TryInheritFrom(IEntity parent)
        {
            this.InheritExcludedProperties(parent as IHasExcludedProperties);

            if (parent is IHasProperties hasProperties)
            {
                //Properties inheritance
                foreach (var parentProperty in hasProperties.Properties ?? Array.Empty <Property>())
                {
                    if (this.HasPropertyExcluded(parentProperty.Name))
                    {
                        continue;
                    }
                    if (Properties == null)
                    {
                        Properties = new List <Property>();
                    }
                    var existProperty = Properties.FirstOrDefault(x => x.IsSame(parentProperty, PropertyType.Product, PropertyType.Variation));
                    if (existProperty == null)
                    {
                        existProperty = AbstractTypeFactory <Property> .TryCreateInstance();

                        Properties.Add(existProperty);
                    }
                    existProperty.TryInheritFrom(parentProperty);

                    existProperty.IsReadOnly = existProperty.Type != PropertyType.Variation && existProperty.Type != PropertyType.Product;
                }
                //Restore sorting order after changes
                if (Properties != null)
                {
                    Properties = Properties.OrderBy(x => x.Name).ToList();
                }
            }

            //TODO: prevent saving the inherited simple values
            //TaxType  inheritance
            if (parent is IHasTaxType hasTaxType && TaxType == null)
            {
                TaxType = hasTaxType.TaxType;
            }

            if (!Variations.IsNullOrEmpty())
            {
                foreach (var variation in Variations)
                {
                    variation.TryInheritFrom(this);
                }
            }

            if (parent is CatalogProduct parentProduct)
            {
                var isVariation = GetType().IsAssignableFrom(typeof(Variation));
                //Inherit images from parent product (if its not set)
                if (Images.IsNullOrEmpty() && !parentProduct.Images.IsNullOrEmpty())
                {
                    Images = new List <Image>();
                    foreach (var parentImage in parentProduct.Images)
                    {
                        var image = AbstractTypeFactory <Image> .TryCreateInstance();

                        image.TryInheritFrom(parentImage);
                        Images.Add(image);
                    }
                }

                //Inherit assets from parent product (if its not set)
                if (Assets.IsNullOrEmpty() && !parentProduct.Assets.IsNullOrEmpty())
                {
                    Assets = new List <Asset>();
                    foreach (var parentAsset in parentProduct.Assets)
                    {
                        var asset = AbstractTypeFactory <Asset> .TryCreateInstance();

                        asset.TryInheritFrom(parentAsset);
                        Assets.Add(asset);
                    }
                }

                //inherit editorial reviews from main product and do not inherit if variation loaded within product
                if (!isVariation && Reviews.IsNullOrEmpty() && parentProduct.Reviews != null)
                {
                    Reviews = new List <EditorialReview>();
                    foreach (var parentReview in parentProduct.Reviews)
                    {
                        var review = AbstractTypeFactory <EditorialReview> .TryCreateInstance();

                        review.TryInheritFrom(parentReview);
                        Reviews.Add(review);
                    }
                }
                //inherit not overridden property values from main product
                foreach (var parentProductProperty in parentProduct.Properties ?? Array.Empty <Property>())
                {
                    var existProperty = Properties.FirstOrDefault(x => x.IsSame(parentProductProperty, PropertyType.Product, PropertyType.Variation));
                    if (existProperty == null)
                    {
                        existProperty = AbstractTypeFactory <Property> .TryCreateInstance();

                        Properties.Add(existProperty);
                    }
                    existProperty.TryInheritFrom(parentProductProperty);
                    existProperty.IsReadOnly = existProperty.Type != PropertyType.Variation && existProperty.Type != PropertyType.Product;

                    //Inherit only parent Product properties  values if own values aren't set
                    if (parentProductProperty.Type == PropertyType.Product && existProperty.Values.IsNullOrEmpty() && !parentProductProperty.Values.IsNullOrEmpty())
                    {
                        existProperty.Values = new List <PropertyValue>();
                        foreach (var parentPropValue in parentProductProperty.Values)
                        {
                            var propValue = AbstractTypeFactory <PropertyValue> .TryCreateInstance();

                            propValue.TryInheritFrom(parentPropValue);
                            existProperty.Values.Add(propValue);
                        }
                    }
                }
                //TODO: prevent saving the inherited simple values
                Width       = parentProduct.Width ?? Width;
                Height      = parentProduct.Height ?? Height;
                Length      = parentProduct.Length ?? Length;
                MeasureUnit = parentProduct.MeasureUnit ?? MeasureUnit;
                Weight      = parentProduct.Weight ?? Weight;
                WeightUnit  = parentProduct.WeightUnit ?? WeightUnit;
                PackageType = parentProduct.PackageType ?? PackageType;
            }
        }