/// <summary> /// Creates trees of view models that represents the hierarchy of the given taxa. /// A method delegate for creating a taxon view model should be provided. /// If the method returns null, the taxon won't be included in the tree. /// The taxa hierarchy is traversed in a breadth-first manner so the ordering of the taxa in each level is preserved. /// </summary> /// <param name="taxa">The taxa.</param> /// <param name="viewModelBuilder">A method that creates a taxon view model by given taxon. /// If null is returned, the taxon won't be included in the tree.</param> /// <param name="sort">The sort.</param> /// <param name="manager">The taxonomy manager that will be used to retrieve the children.</param> /// <param name="taxaCountLimit">The maximum number of taxa that will be included in the tree.</param> /// <returns></returns> public static IList <TaxonViewModel> BuildTaxaTree( IQueryable <Taxon> taxa, Func <ITaxon, TaxonViewModel> viewModelBuilder, Func <IQueryable <Taxon>, IQueryable <Taxon> > sort, TaxonomyManager manager, int taxaCountLimit) { var sortedTaxa = sort.Invoke(taxa); var trees = new List <TaxonViewModel>(); var currentTaxaCount = 0; foreach (var taxon in sortedTaxa) { var subTree = TaxaViewModelTreeBuilder.BuildTaxaTreeBfs(taxon, viewModelBuilder, sort, manager, taxaCountLimit, ref currentTaxaCount); if (subTree != null) { trees.Add(subTree); } if (taxaCountLimit > 0 && currentTaxaCount >= taxaCountLimit) { break; } } return(trees); }
/// <summary> /// Gets the view models of the given taxa. /// </summary> /// <param name="statistics">The statistics.</param> /// <param name="taxa">The taxa.</param> /// <returns>Returns flat or hierarchical structure based on the widget settings.</returns> protected virtual IList <TaxonViewModel> GetTaxaViewModels(IQueryable <TaxonomyStatistic> statistics, IQueryable <Taxon> taxa) { if (this.FlattenHierarchy) { var sortedTaxa = this.Sort(taxa); return(this.GetFlatTaxaViewModelsWithStatistics(sortedTaxa, statistics)); } return(TaxaViewModelTreeBuilder.BuildTaxaTree( taxa, taxon => { if (!this.HasTranslationInCurrentLanguage(taxon)) { return null; } return this.FilterTaxonByCount(taxon, statistics); }, t => this.Sort(t), this.CurrentTaxonomyManager, this.TaxaCountLimit)); }