public HierarchyViewModel GetModel(CatalogHierarchyPart part)
        {
            HierarchyViewModel hierarchy = this.GetRequestHieararchy(part);

            if (hierarchy == null)
            {
                hierarchy = this.EnsureHierarchy(part);

                Boolean       isTarget      = this._target == part.Id;
                HierarchyPath hierarchyPath = new HierarchyPath(this.PathSeparator, isTarget ? this._path : null);
                List <LoadCategoriesInstruction> loadCategoriesInstructions = new List <LoadCategoriesInstruction>();

                this.Initialize(hierarchy, hierarchyPath, loadCategoriesInstructions);

                if (isTarget)
                {
                    this.EnsurePath(hierarchy, hierarchyPath, loadCategoriesInstructions);

                    if (this.ExpandParameterValue.EqualsOrdinalIgnoreCase(this._action))
                    {
                        this.Expand(hierarchy, hierarchyPath, loadCategoriesInstructions);
                    }

                    this.Execute(hierarchy, loadCategoriesInstructions);

                    if (this.CollapseParameterValue.EqualsOrdinalIgnoreCase(this._action))
                    {
                        this.Collapse(hierarchy, hierarchyPath);
                    }

                    if (this.SelectParameterValue.EqualsOrdinalIgnoreCase(this._action))
                    {
                        this.Select(hierarchy, hierarchyPath);
                    }
                }
                else
                {
                    this.Execute(hierarchy, loadCategoriesInstructions);
                }

                this.EnsureSelection(part, hierarchy);
                this.RegisterHierarchyForRequest(part, hierarchy);
            }

            return(hierarchy);
        }
        private void EnsureSelection(CatalogHierarchyPart part, HierarchyViewModel hierarchy)
        {
            IEnumerable <HierarchyItemViewModel> selectables = (hierarchy.Count == 1 ? hierarchy.SelectMany(cata => cata.Categories) : hierarchy.Cast <HierarchyItemViewModel>()).ToList();

            if (part.GenerateUrls)
            {
                this.UnselectAll(hierarchy);
            }
            else if (!this.HasSelection(selectables))
            {
                HierarchyItemViewModel item = selectables.FirstOrDefault();

                if (item != null)
                {
                    item.Selected = true;
                }
            }
        }
            public CatalogHierarchyTools(CatalogHierarchyPart catalogHierarchyPart, ICatalogHierarchyServices catalogHierarchyServices, IOrchardServices orchardServices)
            {
                this._orchardServices          = orchardServices;
                this._catalogHieratchyPart     = catalogHierarchyPart;
                this._catalogHierarchyServices = catalogHierarchyServices;

                this._targetPath = new Lazy <Uri>(
                    () =>
                {
                    Uri targetPath;

                    if (String.IsNullOrEmpty(this._catalogHieratchyPart.TargetPath))
                    {
                        targetPath = this._orchardServices.WorkContext.HttpContext.Request.Url;
                    }
                    else
                    {
                        String path;
                        String leftPart = this._orchardServices.WorkContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority);

                        if (this._catalogHieratchyPart.TargetPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
                        {
                            path = String.Concat(VirtualPathUtility.ToAbsolute("~/"), this._catalogHieratchyPart.TargetPath.Substring(2));
                        }
                        else if (this._catalogHieratchyPart.TargetPath.StartsWith("/", StringComparison.OrdinalIgnoreCase))
                        {
                            path = this._catalogHieratchyPart.TargetPath;
                        }
                        else
                        {
                            path = String.Concat(this._orchardServices.WorkContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Path), this._catalogHieratchyPart.TargetPath);
                        }

                        targetPath = new Uri(String.Concat(leftPart, path));
                    }

                    return(targetPath);
                }
                    );
            }
 private void RegisterHierarchyForRequest(CatalogHierarchyPart part, HierarchyViewModel hierarchy)
 {
     this._orchardServices.WorkContext.HttpContext.Items[this.GetHierarchyRequestKey(part)] = hierarchy;
 }
 private HierarchyViewModel GetRequestHieararchy(CatalogHierarchyPart part)
 {
     return(this._orchardServices.WorkContext.HttpContext.Items[this.GetHierarchyRequestKey(part)] as HierarchyViewModel);
 }
 private String GetHierarchyRequestKey(CatalogHierarchyPart part)
 {
     return(String.Concat(CatalogHierarchyServices.HierarchyRequestKeyPrefix, part.Id));
 }
 private HierarchyViewModel EnsureHierarchy(CatalogHierarchyPart part)
 {
     return(this._sessionManagerServices.GetFromCommerceContext <HierarchyViewModel>(CatalogHierarchyServices.HierarchyModelSessionKey, part.Id));;
 }