示例#1
0
        public ActionResult Index(string FileName)
        {
            var result = new HierarchyViewModel();

            if (string.IsNullOrEmpty(FileName))
            {
                result.Error = new Error()
                {
                    Message = "File not specified, Click <a href='/'>here</a> to continue"
                };
                return(View(result));
            }

            try
            {
                using (StreamReader sr = new StreamReader(Server.MapPath(FileName)))
                {
                    var empList = JsonConvert.DeserializeObject <List <Employee> >(sr.ReadToEnd());
                    result.EmployeeHierachyList = Hierarchy.ProcessEmployeeList(empList);
                    result.MaxLevel             = result.EmployeeHierachyList.Max(x => x.Level);
                }
            }
            catch (FileNotFoundException ex)
            {
                result.Error = new Error()
                {
                    Message = ex.Message
                };
            }

            return(View(result));
        }
        private void Collapse(HierarchyViewModel hierarchy, HierarchyPath hierarchyPath)
        {
            HierarchyItemViewModel item = this.FindItem(hierarchy, hierarchyPath);

            if (item != null)
            {
                item.Expanded = false;
                item.Categories.Clear();
            }
        }
        private void Select(HierarchyViewModel hierarchy, HierarchyPath hierarchyPath)
        {
            this.UnselectAll(hierarchy);

            HierarchyItemViewModel item = this.FindItem(hierarchy, hierarchyPath);

            if (item != null)
            {
                item.Selected = true;
            }
        }
        private void Expand(HierarchyViewModel hierarchy, HierarchyPath hierarchyPath, List <LoadCategoriesInstruction> loadCategoriesInstructions)
        {
            CatalogItemViewModel catalog = hierarchy.FirstOrDefault(cata => cata.Catalog.Code.EqualsOrdinalIgnoreCase(hierarchyPath.Catalog));

            if (catalog != null)
            {
                loadCategoriesInstructions.Add(
                    new LoadCategoriesInstruction(
                        catalog.Catalog.CatalogId,
                        hierarchyPath.TargetCatalog ? CatalogHierarchyServices.RootCategoryCode : hierarchyPath.Categories.Last(),
                        this.CreateAppender(hierarchy, hierarchyPath)
                        )
                    );
            }
        }
        private void Initialize(HierarchyViewModel hierarchy, HierarchyPath hierarchyPath, List <LoadCategoriesInstruction> loadCategoriesInstructions)
        {
            IEnumerable <CatalogItemViewModel> newlyLoadedCatalogs = this.RefreshCatalogs(hierarchy)
                                                                     .Where(cata => !cata.Catalog.Code.EqualsOrdinalIgnoreCase(hierarchyPath.Catalog));

            loadCategoriesInstructions.AddRange(
                (hierarchy.Count == 1 ? hierarchy : newlyLoadedCatalogs).Select(
                    cata => new LoadCategoriesInstruction(
                        cata.Catalog.CatalogId,
                        CatalogHierarchyServices.RootCategoryCode,
                        this.CreateAppender(cata)
                        )
                    )
                );
        }
        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;
                }
            }
        }
        private HierarchyItemViewModel FindItem(HierarchyViewModel hierarchy, HierarchyPath hierarchyPath)
        {
            HierarchyItemViewModel item = hierarchy.FirstOrDefault(cata => cata.Catalog.Code.EqualsOrdinalIgnoreCase(hierarchyPath.Catalog));

            if (item != null)
            {
                foreach (String categoryCode in hierarchyPath.Categories)
                {
                    item = item.Categories.FirstOrDefault(cate => cate.Category.Code.EqualsOrdinalIgnoreCase(categoryCode));

                    if (item == null)
                    {
                        break;
                    }
                }
            }

            return(item);
        }
        private void EnsurePath(HierarchyViewModel hierarchy, HierarchyPath hierarchyPath, List <LoadCategoriesInstruction> loadCategoriesInstructions)
        {
            CatalogItemViewModel catalog = hierarchy.FirstOrDefault(cata => cata.Catalog.Code.EqualsOrdinalIgnoreCase(hierarchyPath.Catalog));

            if (catalog != null)
            {
                loadCategoriesInstructions.AddRange(
                    hierarchyPath.Categories
                    .Select((c, i) => new { Code = c, Index = i })
                    .Where(o => this.FindItem(hierarchy, new HierarchyPath(catalog.Catalog.Code, hierarchyPath.Categories.Take(o.Index + 1))) == null)
                    .Select(
                        o => new LoadCategoriesInstruction(
                            catalog.Catalog.CatalogId,
                            o.Index == 0 ? CatalogHierarchyServices.RootCategoryCode : hierarchyPath.Categories.ElementAtOrDefault(o.Index - 1),
                            this.CreateAppender(hierarchy, new HierarchyPath(catalog.Catalog.Code, hierarchyPath.Categories.Take(o.Index)))
                            )
                        )
                    );
            }
        }
        private void Execute(HierarchyViewModel hierarchy, List <LoadCategoriesInstruction> loadCategoriesInstructions)
        {
            if (loadCategoriesInstructions.Any())
            {
                this._webStoreServices.UsingClient(
                    c =>
                {
                    IQueryable <CategoryHierarchy> categoryhierarchiesQuery = c.CatalogClient.CategoryHierarchies.Include(ch => ch.ParentCategory)
                                                                              .Include(ch => ch.Category.Catalog);

                    foreach (ICatalogEventHandler catalogEventHandler in this._catalogEventHandlers)
                    {
                        foreach (Expression <Func <CategoryHierarchy, Object> > include in catalogEventHandler.GetCategoriesInclusions())
                        {
                            categoryhierarchiesQuery = categoryhierarchiesQuery.Include(include);
                        }
                    }

                    CategoryHierarchy[] categoryhierarchies = categoryhierarchiesQuery.Where(this.GetConditions(loadCategoriesInstructions)).ToArray();

                    loadCategoriesInstructions.ForEach(
                        lci =>
                        lci.OnLoad(
                            categoryhierarchies.Where(
                                ch => ch.ParentCategory.Code.EqualsOrdinalIgnoreCase(lci.Parent) &&
                                ch.Category.CatalogId == lci.CatalogId
                                )
                            .OrderBy(ch => ch.Order)
                            .Select(ch => new CategoryItemViewModel {
                        Category = ch.Category
                    })
                            )
                        );
                }
                    );
            }
        }
        private IEnumerable <CatalogItemViewModel> RefreshCatalogs(HierarchyViewModel hierarchy)
        {
            List <CatalogItemViewModel> previouslyLoadedCatalogs = hierarchy.ToList();

            this._webStoreServices.UsingClient(
                c =>
            {
                IQueryable <ExtendedCatalog> catalogsQuery = c.CatalogClient.Catalogs;

                foreach (ICatalogEventHandler catalogEventHandler in this._catalogEventHandlers)
                {
                    foreach (Expression <Func <ExtendedCatalog, Object> > include in catalogEventHandler.GetCatalogsInclusions())
                    {
                        catalogsQuery = catalogsQuery.Include(include);
                    }
                }

                ExtendedCatalog[] catalogs = catalogsQuery.ToArray();

                IEnumerable <CatalogItemViewModel> models = catalogs.OrderBy(cata => cata.Name)
                                                            .Select(
                    cata =>
                {
                    CatalogItemViewModel catalog = previouslyLoadedCatalogs.FirstOrDefault(pcata => pcata.Catalog.CatalogId == cata.CatalogId) ?? new CatalogItemViewModel();
                    catalog.Catalog = cata;
                    return(catalog);
                }
                    );

                hierarchy.Clear();
                hierarchy.AddRange(models);
            }
                );

            return(hierarchy.Where(cata => !previouslyLoadedCatalogs.Any(pcata => pcata.Catalog.CatalogId == cata.Catalog.CatalogId)));
        }
示例#12
0
 public void SetUp()
 {
     _hierarchyViewModel = new HierarchyViewModel();
 }
示例#13
0
 private CatalogItemViewModel GetCatalog(HierarchyViewModel hierarchy, CategoryItemViewModel category)
 {
     return(hierarchy.FirstOrDefault(cata => this.Contains(cata.Categories, category)));
 }
 private void RegisterHierarchyForRequest(CatalogHierarchyPart part, HierarchyViewModel hierarchy)
 {
     this._orchardServices.WorkContext.HttpContext.Items[this.GetHierarchyRequestKey(part)] = hierarchy;
 }
 private Action <IEnumerable <CategoryItemViewModel> > CreateAppender(HierarchyViewModel hierarchy, HierarchyPath hierarchyPath)
 {
     return(this.CreateAppender(() => this.FindItem(hierarchy, hierarchyPath)));
 }