public dataModel.Item[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge)
        {
            if (itemIds == null)
            {
                throw new ArgumentNullException("categoriesIds");
            }

            if (!itemIds.Any())
            {
                return(new dataModel.Item[] { });
            }
            //Used breaking query EF performance concept https://msdn.microsoft.com/en-us/data/hh949853.aspx#8
            var retVal = Items.Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray();

            //Load product catalogs separately
            var catalogIds = retVal.Select(x => x.CatalogId).Distinct().ToArray();
            var catalogs   = Catalogs.Include(x => x.CatalogLanguages).Where(x => catalogIds.Contains(x.Id)).ToArray();

            //Load product categories separately
            var categoryIds = retVal.Select(x => x.CategoryId).Where(x => !String.IsNullOrEmpty(x)).Distinct().ToArray();
            var categories  = Categories.Where(x => categoryIds.Contains(x.Id)).ToArray();

            if ((respGroup & coreModel.ItemResponseGroup.Links) == coreModel.ItemResponseGroup.Links)
            {
                var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }
            if ((respGroup & coreModel.ItemResponseGroup.ItemProperties) == coreModel.ItemResponseGroup.ItemProperties)
            {
                var propertyValues = PropertyValues.Where(x => itemIds.Contains(x.ItemId)).ToArray();
                //Load categories with all properties for property inheritance
                categories = GetCategoriesByIds(categoryIds, coreModel.CategoryResponseGroup.WithProperties);
            }
            if ((respGroup & coreModel.ItemResponseGroup.ItemAssets) == coreModel.ItemResponseGroup.ItemAssets)
            {
                var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }
            if ((respGroup & coreModel.ItemResponseGroup.ItemEditorialReviews) == coreModel.ItemResponseGroup.ItemEditorialReviews)
            {
                var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }
            if ((respGroup & coreModel.ItemResponseGroup.Variations) == coreModel.ItemResponseGroup.Variations)
            {
                var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray();
                //For variations loads only info and images
                var variations = Items.Include(x => x.Images).Where(x => variationIds.Contains(x.Id)).ToArray();
                //load variations property values separately
                var variationPropertyValues = PropertyValues.Where(x => variationIds.Contains(x.ItemId)).ToArray();
            }
            if ((respGroup & coreModel.ItemResponseGroup.ItemAssociations) == coreModel.ItemResponseGroup.ItemAssociations)
            {
                var assosiationGroups = AssociationGroups.Include(x => x.Associations).ToArray();
                var assosiatedItemIds = assosiationGroups.SelectMany(x => x.Associations).Select(x => x.ItemId).Distinct().ToArray();
                var assosiationItems  = GetItemByIds(assosiatedItemIds, coreModel.ItemResponseGroup.ItemInfo);
            }
            //Load parents
            var parentIds = retVal.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray();
            var parents   = GetItemByIds(parentIds, respGroup);

            return(retVal);
        }
        public dataModel.Category[] GetCategoriesByIds(string[] categoriesIds, coreModel.CategoryResponseGroup respGroup)
        {
            if (categoriesIds == null)
            {
                throw new ArgumentNullException("categoriesIds");
            }

            if (!categoriesIds.Any())
            {
                return(new dataModel.Category[] { });
            }

            var result = Categories.Include(x => x.Catalog.CatalogLanguages)
                         .Where(x => categoriesIds.Contains(x.Id)).ToArray();

            if ((respGroup & coreModel.CategoryResponseGroup.WithLinks) == coreModel.CategoryResponseGroup.WithLinks)
            {
                var incommingLinks = CategoryLinks.Where(x => categoriesIds.Contains(x.TargetCategoryId)).ToArray();
                var outgoingLinks  = CategoryLinks.Where(x => categoriesIds.Contains(x.SourceCategoryId)).ToArray();
            }

            if ((respGroup & coreModel.CategoryResponseGroup.WithImages) == coreModel.CategoryResponseGroup.WithImages)
            {
                var images = Images.Where(x => categoriesIds.Contains(x.CategoryId)).ToArray();
            }

            if (((respGroup & coreModel.CategoryResponseGroup.WithParents) == coreModel.CategoryResponseGroup.WithParents) ||
                ((respGroup & coreModel.CategoryResponseGroup.WithProperties) == coreModel.CategoryResponseGroup.WithProperties))
            {
                var parentsMap = GetAllCategoriesParents(categoriesIds);
                foreach (var categoryId in categoriesIds)
                {
                    var category = result.FirstOrDefault(x => x.Id == categoryId);
                    if (category != null)
                    {
                        category.AllParents = parentsMap[categoryId];
                    }
                }
            }

            if ((respGroup & coreModel.CategoryResponseGroup.WithProperties) == coreModel.CategoryResponseGroup.WithProperties)
            {
                //Load property values by separate query
                var propertyValues = PropertyValues.Where(x => categoriesIds.Contains(x.CategoryId)).ToArray();
                //Need load inherited from parents categories and catalogs
                var allParents           = result.SelectMany(x => x.AllParents).ToArray();
                var allCategoriesTreeIds = allParents.Select(x => x.Id).Concat(categoriesIds).Distinct().ToArray();
                var allCatalogsIds       = result.Select(x => x.CatalogId).Concat(allParents.Select(x => x.CatalogId)).Distinct().ToArray();

                var categoriesProperties = Properties.Include(x => x.PropertyAttributes)
                                           .Include(x => x.DictionaryValues)
                                           .Where(x => allCategoriesTreeIds.Contains(x.CategoryId)).ToArray();

                var catalogProperties = Properties.Include(x => x.PropertyAttributes)
                                        .Include(x => x.DictionaryValues)
                                        .Where(x => x.CategoryId == null && allCatalogsIds.Contains(x.CatalogId)).ToArray();
            }
            return(result);
        }
        public dataModel.Catalog GetCatalogById(string catalogId)
        {
            var retVal = Catalogs.Include(x => x.CatalogLanguages)
                         .Include(x => x.IncommingLinks)
                         .FirstOrDefault(x => x.Id == catalogId);

            var propertyValues = PropertyValues.Where(x => x.CatalogId == catalogId && x.CategoryId == null).ToArray();
            var properties     = Properties.Include(x => x.PropertyAttributes)
                                 .Include(x => x.DictionaryValues)
                                 .Where(x => x.CatalogId == catalogId && x.CategoryId == null).ToArray();

            return(retVal);
        }
        public dataModel.Catalog[] GetCatalogsByIds(string[] catalogIds)
        {
            var retVal = Catalogs.Include(x => x.CatalogLanguages)
                         .Include(x => x.IncommingLinks)
                         .Where(x => catalogIds.Contains(x.Id))
                         .ToArray();

            var propertyValues = PropertyValues.Where(x => catalogIds.Contains(x.CatalogId) && x.CategoryId == null).ToArray();
            var properties     = Properties.Include(x => x.PropertyAttributes)
                                 .Include(x => x.DictionaryValues)
                                 .Where(x => catalogIds.Contains(x.CatalogId) && x.CategoryId == null).ToArray();

            return(retVal);
        }
        public override string ToString()
        {
            string answer = $"new {Name}({string.Join(", ", ConstructorValues)})";

            if (PropertyValues.Count > 0 || Bindings.Count > 0)
            {
                string propertyValues = "\n{";
                bool   addedProperty  = false;

                foreach (var pVal in PropertyValues.Where(i => i.Value != null))
                {
                    var val = pVal.Value.ToString();
                    if (val == null)
                    {
                        continue;
                    }

                    propertyValues += $"\n    {pVal.Key} = ";

                    propertyValues += IndentNewLines(val, 4);

                    propertyValues += ",";

                    addedProperty = true;
                }

                if (Bindings.Count > 0)
                {
                    foreach (var b in Bindings)
                    {
                        propertyValues += $"\n    {b.Key.PropertyName} = new {b.Key.BindableTypeName}({new ObjectModelString(b.Value)}),";
                    }

                    addedProperty = true;
                }

                // Trim the extra ending ","
                if (addedProperty)
                {
                    propertyValues  = propertyValues.Substring(0, propertyValues.Length - 1);
                    propertyValues += "\n}";
                    answer         += propertyValues;
                }
            }

            return(answer);
        }
        public dataModel.Category[] GetCategoriesByIds(string[] categoriesIds, coreModel.CategoryResponseGroup respGroup)
        {
            if (categoriesIds == null)
            {
                throw new ArgumentNullException("categoriesIds");
            }

            if (!categoriesIds.Any())
            {
                return(new dataModel.Category[] { });
            }

            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithOutlines))
            {
                respGroup |= coreModel.CategoryResponseGroup.WithLinks | coreModel.CategoryResponseGroup.WithParents;
            }

            var result = Categories.Where(x => categoriesIds.Contains(x.Id)).ToArray();

            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithLinks))
            {
                var incommingLinks = CategoryLinks.Where(x => categoriesIds.Contains(x.TargetCategoryId)).ToArray();
                var outgoingLinks  = CategoryLinks.Where(x => categoriesIds.Contains(x.SourceCategoryId)).ToArray();
            }

            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithImages))
            {
                var images = Images.Where(x => categoriesIds.Contains(x.CategoryId)).ToArray();
            }

            //Load category property values by separate query
            var propertyValues = PropertyValues.Where(x => categoriesIds.Contains(x.CategoryId)).ToArray();

            //Load all properties meta information and information for inheritance
            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithProperties))
            {
                var categoriesProperties = Properties.Include(x => x.PropertyAttributes)
                                           .Include(x => x.DictionaryValues)
                                           .Where(x => categoriesIds.Contains(x.CategoryId)).ToArray();
            }

            return(result);
        }
        public string ToCPlusPlusString(List <string> declaredVars)
        {
            var    cppVar         = Var();
            string answer         = "";
            string propertyValues = "";

            answer += $"{DeclareCPlusPlusVar(declaredVars)} = ref new {Name}({string.Join(", ", ConstructorValues)});";

            // If there's complex properties, we need to construct those first
            foreach (var pVal in PropertyValues.Where(i => i.Value is ObjectModelObject))
            {
                var val = pVal.Value.ToCPlusPlusString(declaredVars);
                if (val != null)
                {
                    answer += "\n" + val + "\n";
                    answer += $"{cppVar}->{pVal.Key} = {(pVal.Value as ObjectModelObject).Var()};\n";
                }
            }

            if (PropertyValues.Count > 0)
            {
                foreach (var pVal in PropertyValues.Where(i => i.Value != null && !(i.Value is ObjectModelObject)))
                {
                    var val = pVal.Value.ToCPlusPlusString(declaredVars);
                    if (val == null)
                    {
                        continue;
                    }

                    // If list, we construct an item then add it, then repeat
                    if (pVal.Value is ObjectModelListInitialization)
                    {
                        var listValues = (pVal.Value as ObjectModelListInitialization).Values.Where(i => i != null).ToArray();
                        if (listValues.Length > 0)
                        {
                            propertyValues += "\n";

                            foreach (var listItem in listValues)
                            {
                                // If complex value, need to construct it first
                                if (listItem is ObjectModelObject)
                                {
                                    propertyValues += "\n" + listItem.ToCPlusPlusString(declaredVars);
                                    propertyValues += $"\n{cppVar}->{pVal.Key}->Append({(listItem as ObjectModelObject).Var()});";
                                }
                                else
                                {
                                    propertyValues += $"\n{cppVar}->{pVal.Key}->Append({listItem.ToCPlusPlusString(declaredVars)});";
                                }

                                propertyValues += "\n";
                            }
                        }
                    }

                    else
                    {
                        propertyValues += $"\n{cppVar}->{pVal.Key} = {val};";
                    }
                }
            }

            if (Bindings.Count > 0)
            {
                foreach (var b in Bindings)
                {
                    var enumValue = new ObjectModelEnum(Name + "BindableProperty", b.Key.PropertyName);

                    propertyValues += $"\n{cppVar}->Bindings->Insert({enumValue.ToCPlusPlusString(declaredVars)}, {new ObjectModelString(b.Value).ToCPlusPlusString(declaredVars)});";
                }
            }

            answer += propertyValues;

            return(answer);
        }
        public dataModel.Item[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge)
        {
            if (itemIds == null)
            {
                throw new ArgumentNullException("itemIds");
            }

            if (!itemIds.Any())
            {
                return(new dataModel.Item[] { });
            }

            // Use breaking query EF performance concept https://msdn.microsoft.com/en-us/data/hh949853.aspx#8
            var retVal         = Items.Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray();
            var propertyValues = PropertyValues.Where(x => itemIds.Contains(x.ItemId)).ToArray();

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines))
            {
                respGroup |= coreModel.ItemResponseGroup.Links;
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.Links))
            {
                var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssets))
            {
                var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemEditorialReviews))
            {
                var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.Variations))
            {
                // TODO: Call GetItemByIds for variations recursively (need to measure performance and data amount first)

                var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray();

                // Always load info, images and property values for variations
                var variations = Items.Include(x => x.Images).Where(x => variationIds.Contains(x.Id)).ToArray();
                var variationPropertyValues = PropertyValues.Where(x => variationIds.Contains(x.ItemId)).ToArray();

                if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssets))
                {
                    var variationAssets = Assets.Where(x => variationIds.Contains(x.ItemId)).ToArray();
                }

                if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemEditorialReviews))
                {
                    var variationEditorialReviews = EditorialReviews.Where(x => variationIds.Contains(x.ItemId)).ToArray();
                }
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssociations))
            {
                var assosiations         = Associations.Where(x => itemIds.Contains(x.ItemId)).ToArray();
                var assosiatedProductIds = assosiations.Where(x => x.AssociatedItemId != null)
                                           .Select(x => x.AssociatedItemId).Distinct().ToArray();

                var assosiatedItems = GetItemByIds(assosiatedProductIds, coreModel.ItemResponseGroup.ItemInfo | coreModel.ItemResponseGroup.ItemAssets);
            }

            // Load parents
            var parentIds = retVal.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray();
            var parents   = GetItemByIds(parentIds, respGroup);

            return(retVal);
        }
        public dataModel.Item[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge)
        {
            if (itemIds == null)
            {
                throw new ArgumentNullException("itemIds");
            }

            if (!itemIds.Any())
            {
                return(new dataModel.Item[] { });
            }

            // Use breaking query EF performance concept https://msdn.microsoft.com/en-us/data/hh949853.aspx#8
            var retVal         = Items.Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray();
            var propertyValues = PropertyValues.Where(x => itemIds.Contains(x.ItemId)).ToArray();

            // Load product catalogs separately
            var catalogIds = retVal.Select(x => x.CatalogId).Distinct().ToArray();
            var catalogs   = Catalogs.Include(x => x.CatalogLanguages).Where(x => catalogIds.Contains(x.Id)).ToArray();

            // Load product categories separately
            var categoryIds            = retVal.Select(x => x.CategoryId).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToArray();
            var categoriesReponseGroup = coreModel.CategoryResponseGroup.WithParents;

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines))
            {
                categoriesReponseGroup |= coreModel.CategoryResponseGroup.WithLinks;
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemProperties))
            {
                // Load categories with all properties for property inheritance
                categoriesReponseGroup |= coreModel.CategoryResponseGroup.WithProperties;
            }

            var categories = GetCategoriesByIds(categoryIds, categoriesReponseGroup);

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.Links) || respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines))
            {
                var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }

            // Load all properties meta information and data for inheritance from parent categories and catalog
            if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemProperties))
            {
                // Load catalogs with properties for products not belongs to any category (EF auto populated all Catalog nav properties for all objects)
                foreach (var catalogId in retVal.Where(x => x.CategoryId == null).Select(x => x.CatalogId))
                {
                    var catalog = GetCatalogById(catalogId);
                }
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssets))
            {
                var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemEditorialReviews))
            {
                var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray();
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.Variations))
            {
                var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray();
                // For variations load only info and images
                var variations = Items.Include(x => x.Images).Include(x => x.Assets).Where(x => variationIds.Contains(x.Id)).ToArray();
                // Load variations property values separately
                var variationPropertyValues = PropertyValues.Where(x => variationIds.Contains(x.ItemId)).ToArray();
            }

            if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssociations))
            {
                var assosiationGroups = AssociationGroups.Include(x => x.Associations).ToArray();
                var assosiatedItemIds = assosiationGroups.SelectMany(x => x.Associations).Select(x => x.ItemId).Distinct().ToArray();
                var assosiationItems  = GetItemByIds(assosiatedItemIds, coreModel.ItemResponseGroup.ItemInfo);
            }

            // Load parents
            var parentIds = retVal.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray();
            var parents   = GetItemByIds(parentIds, respGroup);

            return(retVal);
        }
示例#10
0
        public dataModel.Category[] GetCategoriesByIds(string[] categoriesIds, coreModel.CategoryResponseGroup respGroup)
        {
            if (categoriesIds == null)
            {
                throw new ArgumentNullException("categoriesIds");
            }

            if (!categoriesIds.Any())
            {
                return(new dataModel.Category[] { });
            }

            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithOutlines))
            {
                respGroup |= coreModel.CategoryResponseGroup.WithLinks | coreModel.CategoryResponseGroup.WithParents;
            }

            var result = Categories.Include(x => x.Catalog.CatalogLanguages)
                         .Where(x => categoriesIds.Contains(x.Id)).ToArray();

            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithLinks))
            {
                var incommingLinks     = CategoryLinks.Where(x => categoriesIds.Contains(x.TargetCategoryId)).ToArray();
                var outgoingLinks      = CategoryLinks.Where(x => categoriesIds.Contains(x.SourceCategoryId)).ToArray();
                var foreignCatalogsIds = outgoingLinks.Where(x => x.TargetCategoryId == null && x.TargetCatalogId != null)
                                         .Select(x => x.TargetCatalogId).Distinct().ToArray();
                var foreginCategoriesIds = outgoingLinks.Where(x => x.TargetCategoryId != null)
                                           .Select(x => x.TargetCategoryId).Distinct();
                if (!foreginCategoriesIds.IsNullOrEmpty())
                {
                    var linkedCategories = GetCategoriesByIds(foreginCategoriesIds.ToArray(), coreModel.CategoryResponseGroup.WithLinks | coreModel.CategoryResponseGroup.WithParents);
                }
                if (!foreignCatalogsIds.IsNullOrEmpty())
                {
                    var linkedCatalogs = Catalogs.Include(x => x.CatalogLanguages).Where(x => foreignCatalogsIds.Contains(x.Id)).ToArray();
                }
            }

            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithImages))
            {
                var images = Images.Where(x => categoriesIds.Contains(x.CategoryId)).ToArray();
            }

            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithParents) || respGroup.HasFlag(coreModel.CategoryResponseGroup.WithProperties))
            {
                var parentsMap = GetAllCategoriesParents(categoriesIds, coreModel.CategoryResponseGroup.WithLinks);
                foreach (var categoryId in categoriesIds)
                {
                    var category = result.FirstOrDefault(x => x.Id == categoryId);
                    if (category != null)
                    {
                        category.AllParents = parentsMap[categoryId];
                        foreach (var parent in category.AllParents)
                        {
                            parent.AllParents = category.AllParents.TakeWhile(x => x != parent).ToArray();
                        }
                    }
                }
            }

            //Load category property values by separate query
            var propertyValues = PropertyValues.Where(x => categoriesIds.Contains(x.CategoryId)).ToArray();

            //Load all properties meta information and information for inheritance
            if (respGroup.HasFlag(coreModel.CategoryResponseGroup.WithProperties))
            {
                //Need load inherited from parents categories and catalogs
                var allParents           = result.SelectMany(x => x.AllParents).ToArray();
                var allCategoriesTreeIds = allParents.Select(x => x.Id).Concat(categoriesIds).Distinct().ToArray();
                var allCatalogsIds       = result.Select(x => x.CatalogId).Concat(allParents.Select(x => x.CatalogId)).Distinct().ToArray();

                var categoriesProperties = Properties.Include(x => x.PropertyAttributes)
                                           .Include(x => x.DictionaryValues)
                                           .Where(x => allCategoriesTreeIds.Contains(x.CategoryId)).ToArray();

                var catalogProperties = Properties.Include(x => x.PropertyAttributes)
                                        .Include(x => x.DictionaryValues)
                                        .Where(x => x.CategoryId == null && allCatalogsIds.Contains(x.CatalogId)).ToArray();
            }

            return(result);
        }