/// <summary>
        /// Patch CatalogBase type
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        public static void Patch(this dataModel.CatalogBase source, dataModel.CatalogBase target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            var patchInjectionPolicy = new PatchInjection <dataModel.CatalogBase>(x => x.Name, x => x.DefaultLanguage);

            target.InjectFrom(patchInjectionPolicy, source);

            //Languages patch
            var sourceCatalog = source as dataModel.Catalog;
            var targetCatalog = target as dataModel.Catalog;

            if (sourceCatalog != null && targetCatalog != null && !sourceCatalog.CatalogLanguages.IsNullCollection())
            {
                var languageComparer = AnonymousComparer.Create((dataModel.CatalogLanguage x) => x.Language);
                sourceCatalog.CatalogLanguages.Patch(targetCatalog.CatalogLanguages, languageComparer,
                                                     (sourceLang, targetlang) => sourceLang.Patch(targetlang));
            }

            //Property values
            if (sourceCatalog != null && !sourceCatalog.CatalogPropertyValues.IsNullCollection())
            {
                sourceCatalog.CatalogPropertyValues.Patch(targetCatalog.CatalogPropertyValues, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue));
            }
        }
예제 #2
0
 public dataModel.CatalogBase GetCatalogById(string catalogId)
 {
     dataModel.CatalogBase retVal = Catalogs.OfType <dataModel.Catalog>()
                                    .Include(x => x.CatalogLanguages)
                                    .Include(x => x.CatalogPropertyValues)
                                    .Include(x => x.PropertySet.PropertySetProperties.Select(y => y.Property))
                                    .FirstOrDefault(x => x.Id == catalogId);
     if (retVal == null)
     {
         retVal = Catalogs.OfType <dataModel.VirtualCatalog>()
                  .Include(x => x.IncommingLinks)
                  .FirstOrDefault(x => x.Id == catalogId);
     }
     return(retVal);
 }
예제 #3
0
        public dataModel.Property[] GetCatalogProperties(dataModel.CatalogBase catalogBase)
        {
            var retVal  = new List <dataModel.Property>();
            var catalog = catalogBase as dataModel.Catalog;

            if (catalog != null)
            {
                if (catalog.PropertySet == null && catalog.PropertySetId != null)
                {
                    catalog = GetCatalogById(catalogBase.Id) as dataModel.Catalog;
                }
                if (catalog.PropertySet != null)
                {
                    retVal.AddRange(catalog.PropertySet.PropertySetProperties.Select(x => x.Property));
                }
            }
            return(retVal.ToArray());
        }
        /// <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);
        }
예제 #5
0
        private void SearchCategories(coreModel.SearchCriteria criteria, coreModel.SearchResult result)
        {
            // TODO: optimize for performance, need to eliminate number of database queries
            // 1. Catalog should either be passed or loaded using caching
            // 2. Categories should be loaded by passing array of ids instead of parallel loading one by one
            using (var repository = _catalogRepositoryFactory())
            {
                var query = repository.Categories;

                dataModel.CatalogBase dbCatalog = null;
                var isVirtual = false;

                if (!String.IsNullOrEmpty(criteria.CatalogId))
                {
                    dbCatalog = repository.GetCatalogById(criteria.CatalogId);
                    isVirtual = dbCatalog is dataModel.VirtualCatalog;

                    query = query.Where(x => x.CatalogId == criteria.CatalogId);
                }

                if (!String.IsNullOrEmpty(criteria.Keyword))
                {
                    query = query.Where(x => x.Name.Contains(criteria.Keyword) || x.Code.Contains(criteria.Keyword));
                }
                else if (!String.IsNullOrEmpty(criteria.CategoryId))
                {
                    if (isVirtual)
                    {
                        var dbCategory = repository.GetCategoryById(criteria.CategoryId);
                        //Need return all linked categories also
                        var allLinkedPhysicalCategoriesIds = dbCategory.IncommingLinks.Select(x => x.SourceCategoryId).ToArray();
                        query = repository.Categories;
                        if (allLinkedPhysicalCategoriesIds.Any())
                        {
                            if (criteria.HideDirectLinedCategories)
                            {
                                query = query.Where(x => x.ParentCategoryId == criteria.CategoryId || allLinkedPhysicalCategoriesIds.Contains(x.ParentCategoryId));
                            }
                            else
                            {
                                query = query.Where(x => x.ParentCategoryId == criteria.CategoryId || allLinkedPhysicalCategoriesIds.Contains(x.Id));
                            }
                        }
                        else
                        {
                            query = query.Where(x => x.ParentCategoryId == criteria.CategoryId);
                        }
                    }
                    else
                    {
                        query = query.Where(x => x.ParentCategoryId == criteria.CategoryId);
                    }
                }
                else if (!String.IsNullOrEmpty(criteria.Code))
                {
                    query = query.Where(x => x.Code == criteria.Code);
                }
                else if (!String.IsNullOrEmpty(criteria.SeoKeyword))
                {
                    var urlKeyword = _commerceService.GetSeoByKeyword(criteria.SeoKeyword).Where(x => x.ObjectType == typeof(coreModel.Category).Name).FirstOrDefault();
                    if (urlKeyword == null)
                    {
                        query = query.Where(x => false);
                    }
                    else
                    {
                        query = query.Where(x => x.Id == urlKeyword.ObjectId);
                    }
                }
                else if (!String.IsNullOrEmpty(criteria.CatalogId))
                {
                    query = query.Where(x => x.CatalogId == criteria.CatalogId && (x.ParentCategoryId == null || criteria.GetAllCategories));
                    if (isVirtual)
                    {
                        //Need return only catalog linked categories
                        var allLinkedCategoriesIds = ((dataModel.VirtualCatalog)dbCatalog).IncommingLinks
                                                     .Where(x => x.TargetCategoryId == null)
                                                     .Select(x => x.SourceCategoryId);
                        //Search in all catalogs
                        query = repository.Categories;
                        query = query.Where(x => (x.CatalogId == criteria.CatalogId && (x.ParentCategoryId == null || criteria.GetAllCategories)) || allLinkedCategoriesIds.Contains(x.Id));
                    }
                }

                var categoryIds = query.Select(x => x.Id).ToArray();

                var categories      = new ConcurrentBag <coreModel.Category>();
                var parallelOptions = new ParallelOptions
                {
                    MaxDegreeOfParallelism = 10
                };
                Parallel.ForEach(categoryIds, parallelOptions, (x) =>
                {
                    var category = _categoryService.GetById(x);

                    categories.Add(category);
                });
                //Must order by priority
                result.Categories = categories.OrderByDescending(x => x.Priority).ThenBy(x => x.Name).ToList();
            }
        }