/// <summary>
        /// Method that returns a filtered version of this species fact
        /// list. The filtering is done on hosts.
        /// </summary>
        /// <param name="hosts">Hosts for the requested species facts.</param>
        /// <returns>A filtered species fact list.</returns>
        public SpeciesFactList GetSpeciesFactsByHosts(TaxonList hosts)
        {
            if (hosts == null)
            {
                throw new ArgumentException("TaxonList is null", "hosts");
            }

            SpeciesFactList filteredList = new SpeciesFactList();

            if (this.IsNotEmpty())
            {
                foreach (ISpeciesFact fact in this)
                {
                    foreach (ITaxon host in hosts)
                    {
                        if (fact.Host.Id == host.Id)
                        {
                            filteredList.Add(fact);
                            break;
                        }
                    }
                }
            }

            return(filteredList);
        }
示例#2
0
        /// <summary>
        /// Get all taxon names for specified taxa.
        /// The result is sorted in the same order as input taxa.
        /// </summary>
        /// <param name="userContext">The user context.</param>
        /// <param name="taxa">Taxa.</param>
        /// <returns>Taxon names.</returns>
        public override List <TaxonNameList> GetTaxonNames(IUserContext userContext,
                                                           TaxonList taxa)
        {
            Hashtable            allTaxonNamesUnsorted;
            Int32                index;
            List <TaxonNameList> allTaxonNamesSorted, allTaxonNamesNotCached;
            TaxonList            notCachedTaxa;
            TaxonNameList        taxonNames;

            if (userContext.CurrentRole.IsNotNull() &&
                userContext.CurrentRole.Identifier.IsNotEmpty() &&
                userContext.CurrentRole.Identifier.StartsWith(Settings.Default.DyntaxaRevisionRoleIdentifier))
            {
                // Do not cache taxon names if user is in a revision.
                allTaxonNamesSorted = base.GetTaxonNames(userContext, taxa);
            }
            else
            {
                allTaxonNamesUnsorted = new Hashtable();
                notCachedTaxa         = new TaxonList();

                // Get already cached taxon names.
                foreach (ITaxon taxon in taxa)
                {
                    taxonNames = GetTaxonNames(taxon, userContext.Locale);
                    if (taxonNames.IsEmpty())
                    {
                        notCachedTaxa.Add(taxon);
                    }
                    else
                    {
                        allTaxonNamesUnsorted[taxon.Id] = taxonNames;
                    }
                }

                // Get not already cached taxon names.
                if (notCachedTaxa.IsNotEmpty())
                {
                    allTaxonNamesNotCached = base.GetTaxonNames(userContext,
                                                                notCachedTaxa);
                    foreach (TaxonNameList tempTaxonNames in allTaxonNamesNotCached)
                    {
                        allTaxonNamesUnsorted[tempTaxonNames[0].Taxon.Id] = tempTaxonNames;

                        // Cache not already cached taxon names.
                        SetTaxonNames(tempTaxonNames,
                                      tempTaxonNames[0].Taxon,
                                      userContext.Locale);
                    }
                }

                // Create sorted output.
                allTaxonNamesSorted = new List <TaxonNameList>();
                for (index = 0; index < taxa.Count; index++)
                {
                    allTaxonNamesSorted.Add((TaxonNameList)(allTaxonNamesUnsorted[taxa[index].Id]));
                }
            }
            return(allTaxonNamesSorted);
        }
        /// <summary>
        /// Method that returns a filtered version of this
        /// species fact list. The filtering is done on Taxa.
        /// </summary>
        /// <param name="taxa">Taxa for the requested species facts.</param>
        /// <returns>A filtered species fact list.</returns>
        public SpeciesFactList GetSpeciesFactsByTaxa(TaxonList taxa)
        {
            if (taxa == null)
            {
                throw new ArgumentException("TaxonList is null", "taxa");
            }

            SpeciesFactList filteredList = new SpeciesFactList();

            if (this.IsNotEmpty())
            {
                foreach (ISpeciesFact fact in this)
                {
                    foreach (ITaxon taxon in taxa)
                    {
                        if (fact.Taxon.Id == taxon.Id)
                        {
                            filteredList.Add(fact);
                            break;
                        }
                    }
                }
            }

            return(filteredList);
        }
        /// <summary>
        /// Get a copy of this taxon list.
        /// </summary>
        /// <returns>A copy of this taxon list</returns>
        public TaxonList Clone()
        {
            TaxonList taxa;

            taxa = new TaxonList(Optimize);
            taxa.AddRange(this);
            return(taxa);
        }
        /// <summary>
        /// Remove taxa from current species fact data set scope.
        /// The species facts in the data set are updated
        /// to the new species fact data set scope.
        /// </summary>
        /// <param name="userContext">
        /// Information about the user that makes this method call.
        /// </param>
        /// <param name="selection">Changed scope of the data set.</param>
        public virtual void RemoveSelectionTaxa(IUserContext userContext,
                                                TaxonList selection)
        {
            ISpeciesFactDataSetSelection newSelection;

            newSelection = new SpeciesFactDataSetSelection();
            newSelection.Taxa.Merge(selection);
            RemoveSelection(userContext, newSelection);
        }
        /// <summary>
        /// Add hosts to current species fact data set scope.
        /// The species facts in the data set are updated
        /// to the new species fact data set scope.
        /// </summary>
        /// <param name="userContext">
        /// Information about the user that makes this method call.
        /// </param>
        /// <param name="selection">Changed scope of the data set.</param>
        public virtual void AddSelectionHosts(IUserContext userContext,
                                              TaxonList selection)
        {
            ISpeciesFactDataSetSelection newSelection;

            newSelection = new SpeciesFactDataSetSelection();
            newSelection.Hosts.Merge(selection);
            AddSelection(userContext, newSelection);
        }
        /// <summary>
        /// Gets taxa that matches the specified species observation search criteria.
        /// </summary>
        /// <param name="userContext">User context.</param>
        /// <param name="searchCriteria">The species observation search criteria.</param>
        /// <param name="coordinateSystem">Coordinate system used in geometry search criteria.</param>
        /// <returns>Information about taxa.</returns>
        public TaxonList GetTaxaBySearchCriteria(IUserContext userContext,
                                                 ISpeciesObservationSearchCriteria searchCriteria,
                                                 ICoordinateSystem coordinateSystem)
        {
            TaxonList taxa = DataSource.GetTaxaBySearchCriteria(userContext, searchCriteria, coordinateSystem);

            taxa.Sort();
            return(taxa);
        }
示例#8
0
 /// <summary>
 /// Create a species fact data set selection instance.
 /// </summary>
 public SpeciesFactDataSetSelection()
 {
     _factors = new FactorList(true);
     _hosts   = new TaxonList(true);
     _individualCategories = new IndividualCategoryList(true);
     _periods    = new PeriodList(true);
     _references = new ReferenceList(true);
     _taxa       = new TaxonList(true);
 }
        /// <summary>
        /// Add taxon to search criteria.
        /// </summary>
        /// <param name="taxon">The taxon.</param>
        public void Add(ITaxon taxon)
        {
            if (Taxa.IsNull())
            {
                Taxa = new TaxonList();
            }

            Taxa.Add(taxon);
        }
 /// <summary>
 /// Default constructor
 /// </summary>
 public SpeciesFactSearchCriteria()
 {
     FactorDataTypes     = new FactorDataTypeList();
     Factors             = new FactorList();
     FieldSearchCriteria = new SpeciesFactFieldSearchCriteriaList();
     Hosts = new TaxonList();
     IndividualCategories = new IndividualCategoryList();
     Periods    = new PeriodList();
     References = new ReferenceList();
     Taxa       = new TaxonList();
 }
 /// <summary>
 /// Create a species fact data set instance.
 /// </summary>
 public SpeciesFactDataSet()
 {
     Factors = new FactorList(true);
     Hosts   = new TaxonList(true);
     IndividualCategories = new IndividualCategoryList(true);
     Periods        = new PeriodList(true);
     References     = new ReferenceList(true);
     _selection     = new SpeciesFactDataSetSelection();
     _selectionCopy = new SpeciesFactDataSetSelection();
     SpeciesFacts   = new SpeciesFactList();
     Taxa           = new TaxonList(true);
 }
        /// <summary>
        /// Get all unique child taxa.
        /// This method operates on current taxon tree in contrast to
        /// the full taxon tree with all taxon tree nodes.
        /// </summary>
        /// <param name="taxonTreeNode">Current child taxon tree node.</param>
        /// <param name="childTaxa">Accumulated child taxa so far.</param>
        private void GetChildTaxa(ITaxonTreeNode taxonTreeNode,
                                  TaxonList childTaxa)
        {
            // Add the taxon for this taxon tree node.
            childTaxa.Merge(taxonTreeNode.Taxon);

            if (taxonTreeNode.Children.IsNotEmpty())
            {
                // Add taxa for child taxon tree node.
                foreach (ITaxonTreeNode childTaxonTreeNode in taxonTreeNode.Children)
                {
                    GetChildTaxa(childTaxonTreeNode, childTaxa);
                }
            }
        }
        /// <summary>
        /// Get all unique parent taxa.
        /// This method operates on current taxon tree in contrast to
        /// the full taxon tree with all taxon tree nodes.
        /// </summary>
        /// <param name="taxonTreeNode">Current parent taxon tree node.</param>
        /// <param name="parentTaxa">Accumulated parent taxa so far.</param>
        private void GetParentTaxa(ITaxonTreeNode taxonTreeNode,
                                   TaxonList parentTaxa)
        {
            // Add the taxon for this taxon tree node.
            parentTaxa.Merge(taxonTreeNode.Taxon);

            if (taxonTreeNode.Parents.IsNotEmpty())
            {
                // Add taxa for parent taxon tree node.
                foreach (ITaxonTreeNode parentTaxonTreeNode in taxonTreeNode.Parents)
                {
                    GetParentTaxa(parentTaxonTreeNode, parentTaxa);
                }
            }
        }
        /// <summary>
        /// Remove factors, hosts, individual categories, periods or
        /// taxa from current species fact data set scope.
        /// The species facts in the data set are updated
        /// to the new species fact data set scope.
        /// </summary>
        /// <param name="userContext">
        /// Information about the user that makes this method call.
        /// </param>
        /// <param name="selection">Changed scope of the data set.</param>
        public virtual void RemoveSelection(IUserContext userContext,
                                            ISpeciesFactDataSetSelection selection)
        {
            FactorList             factors;
            IndividualCategoryList individualCategories;
            PeriodList             periods;
            ReferenceList          references;
            TaxonList hosts, taxa;

            // Check arguments.
            selection.CheckNotNull("selection");

            // Update selection with existing scope.
            factors = new FactorList();
            factors.Merge(_selection.Factors);
            factors.Remove(selection.Factors);
            selection.Factors = factors;

            hosts = new TaxonList();
            hosts.Merge(_selection.Hosts);
            hosts.Remove(selection.Hosts);
            selection.Hosts = hosts;

            individualCategories = new IndividualCategoryList();
            individualCategories.Merge(_selection.IndividualCategories);
            individualCategories.Remove(selection.IndividualCategories);
            selection.IndividualCategories = individualCategories;

            periods = new PeriodList();
            periods.Merge(_selection.Periods);
            periods.Remove(selection.Periods);
            selection.Periods = periods;

            references = new ReferenceList();
            references.Merge(_selection.References);
            references.Remove(selection.References);
            selection.References = references;

            taxa = new TaxonList();
            taxa.Merge(_selection.Taxa);
            taxa.Remove(selection.Taxa);
            selection.Taxa = taxa;

            // Update species fact data set.
            UpdateSelection(userContext, selection);
        }
        /// <summary>
        /// Get all unique child taxa.
        /// This method operates on current taxon tree in contrast to
        /// the full taxon tree with all taxon tree nodes.
        /// </summary>
        /// <returns>All child taxa.</returns>
        public virtual TaxonList GetChildTaxa()
        {
            TaxonList childTaxa;

            childTaxa = new TaxonList(true);
            if (Children.IsNotEmpty())
            {
                // Add taxa for child taxon tree node.
                foreach (ITaxonTreeNode childTaxonTreeNode in Children)
                {
                    GetChildTaxa(childTaxonTreeNode, childTaxa);
                }
            }

            childTaxa.Sort();
            return(childTaxa);
        }
        /// <summary>
        /// Get all unique parent taxa.
        /// This method operates on current taxon tree in contrast to
        /// the full taxon tree with all taxon tree nodes.
        /// </summary>
        /// <returns>All parent taxa.</returns>
        public virtual TaxonList GetParentTaxa()
        {
            TaxonList parentTaxa;

            parentTaxa = new TaxonList(true);
            if (Parents.IsNotEmpty())
            {
                // Add taxa for parent taxon tree node.
                foreach (ITaxonTreeNode parentTaxonTreeNode in Parents)
                {
                    GetParentTaxa(parentTaxonTreeNode, parentTaxa);
                }
            }

            parentTaxa.Sort();
            return(parentTaxa);
        }
        /// <summary>
        /// Get all unique taxa in this species fact list.
        /// </summary>
        /// <returns>All unique taxa in this species fact list.</returns>
        public TaxonList GetTaxa()
        {
            TaxonList taxa;

            taxa = new TaxonList();
            if (this.IsNotEmpty())
            {
                foreach (ISpeciesFact speciesFact in this)
                {
                    if (speciesFact.Taxon.IsNotNull())
                    {
                        taxa.Merge(speciesFact.Taxon);
                    }
                }

                taxa.Sort();
            }

            return(taxa);
        }
        /// <summary>
        /// Reset species fact data set to no species facts.
        /// This method is used when species fact data set selection
        /// does not contain both factors and taxa.
        /// </summary>
        /// <param name="selection">
        /// Scope of the data set is defined by this
        /// species fact data set selection.
        /// </param>
        private void Reset(ISpeciesFactDataSetSelection selection)
        {
            Factors = new FactorList(true);
            Factors.Merge(selection.Factors);
            Hosts = new TaxonList(true);
            Hosts.Merge(selection.Hosts);
            IndividualCategories = new IndividualCategoryList(true);
            IndividualCategories.Merge(selection.IndividualCategories);
            Periods = new PeriodList(true);
            Periods.Merge(selection.Periods);
            References = new ReferenceList(true);
            References.Merge(selection.References);
            SpeciesFacts = new SpeciesFactList(true);
            Taxa         = new TaxonList();
            Taxa.Merge(selection.Taxa);

            // Save selection information.
            _selection     = (ISpeciesFactDataSetSelection)(selection.Clone());
            _selectionCopy = (ISpeciesFactDataSetSelection)(selection.Clone());
        }
        /// <summary>
        /// Get all unique hosts in this species fact list.
        /// </summary>
        /// <returns>All unique hosts in this species fact list.</returns>
        public TaxonList GetHosts()
        {
            TaxonList hosts;

            hosts = new TaxonList();
            if (this.IsNotEmpty())
            {
                foreach (ISpeciesFact speciesFact in this)
                {
                    if (speciesFact.Host.IsNotNull() &&
                        speciesFact.Host.Id != (Int32)(TaxonId.Life))
                    {
                        hosts.Merge(speciesFact.Host);
                    }
                }

                hosts.Sort();
            }

            return(hosts);
        }
示例#20
0
        /// <summary>
        /// Get taxa with specified ids.
        /// </summary>
        /// <param name="userContext">User context.</param>
        /// <param name="taxonIds">Taxon ids</param>
        /// <returns>Taxa with specified ids.</returns>
        public override TaxonList GetTaxa(IUserContext userContext,
                                          List <Int32> taxonIds)
        {
            ITaxon       taxon;
            List <Int32> notFoundTaxonIds;
            TaxonList    foundTaxa, notFoundTaxa, taxa;

            if (userContext.CurrentRole.IsNotNull() &&
                userContext.CurrentRole.Identifier.IsNotEmpty() &&
                userContext.CurrentRole.Identifier.StartsWith(Settings.Default.DyntaxaRevisionRoleIdentifier))
            {
                // Do not cache taxa if user is in a revision.
                taxa = base.GetTaxa(userContext, taxonIds);
            }
            else
            {
                // Get cached taxa.
                foundTaxa        = new TaxonList(true);
                notFoundTaxa     = null;
                notFoundTaxonIds = new List <Int32>();
                foreach (Int32 taxonId in taxonIds)
                {
                    taxon = GetTaxon(taxonId, userContext.Locale);
                    if (taxon.IsNull())
                    {
                        notFoundTaxonIds.Add(taxonId);
                    }
                    else
                    {
                        foundTaxa.Add(taxon);
                    }
                }

                // Get taxa from data source.
                if (notFoundTaxonIds.IsNotEmpty())
                {
                    notFoundTaxa = base.GetTaxa(userContext, notFoundTaxonIds);
                    foundTaxa.Merge(notFoundTaxa);
                    taxa = new TaxonList(true);
                    foreach (Int32 taxonId in taxonIds)
                    {
                        taxa.Add(foundTaxa.Get(taxonId));
                    }
                }
                else
                {
                    taxa = foundTaxa;
                }

                // Save taxa in cache.
                if (notFoundTaxa.IsNotEmpty())
                {
                    foreach (ITaxon tempTaxon in notFoundTaxa)
                    {
                        SetTaxon(tempTaxon, userContext.Locale);
                    }
                }
            }

            return(taxa);
        }
        /// <summary>
        /// Update information about which factors, hosts,
        /// individual categories, periods, references and taxa
        /// that are used in the species facts.
        /// </summary>
        /// <param name="userContext">
        /// Information about the user that makes this method call.
        /// </param>
        /// <param name="selection">
        /// Scope of the data set is defined by this
        /// species fact data set selection.
        /// </param>
        /// <param name="speciesFacts">Species facts.</param>
        private void UpdateScope(IUserContext userContext,
                                 ISpeciesFactDataSetSelection selection,
                                 SpeciesFactList speciesFacts)
        {
            Factors = new FactorList(true);
            Factors.AddRange(selection.Factors);
            Hosts = new TaxonList(true);
            Hosts.AddRange(selection.Hosts);
            IndividualCategories = new IndividualCategoryList(true);
            IndividualCategories.AddRange(selection.IndividualCategories);
            Periods = new PeriodList(true);
            Periods.AddRange(selection.Periods);
            References = new ReferenceList(true);
            Taxa       = new TaxonList(true);
            Taxa.AddRange(selection.Taxa);

            if (speciesFacts.IsNotEmpty())
            {
                foreach (ISpeciesFact speciesFact in speciesFacts)
                {
                    Factors.Merge(speciesFact.Factor);
                    if (speciesFact.HasHost)
                    {
                        Hosts.Merge(speciesFact.Host);
                    }

                    IndividualCategories.Merge(speciesFact.IndividualCategory);
                    if (speciesFact.HasPeriod)
                    {
                        Periods.Merge(speciesFact.Period);
                    }

                    if (speciesFact.HasReference)
                    {
                        References.Merge(speciesFact.Reference);
                    }

                    Taxa.Merge(speciesFact.Taxon);
                }
            }

            // Set default values if no values are entered.
            if (Hosts.IsEmpty())
            {
                Hosts.Add(CoreData.TaxonManager.GetTaxon(userContext, TaxonId.Life));
            }

            if (IndividualCategories.IsEmpty())
            {
                IndividualCategories.Add(CoreData.FactorManager.GetDefaultIndividualCategory(userContext));
            }

            if (Periods.IsEmpty())
            {
                Periods.AddRange(CoreData.FactorManager.GetPeriods(userContext));
            }

            // Sort all lists.
            Factors.Sort();
            Hosts.Sort();
            IndividualCategories.Sort();
            Periods.Sort();
            References.Sort();
            Taxa.Sort();
        }
示例#22
0
        /// <summary>
        /// Creates a TaxonRelations tree from a taxon relations list.
        /// </summary>
        /// <param name="userContext">The user context.</param>
        /// <param name="taxonRelations">Taxon relations.</param>
        /// <param name="taxa">Taxa list. Use this if you want to be able to find isolated nodes (taxon).</param>
        /// <param name="relationTaxonMustBeValid">If true only relations to valid taxon is added for Validproperties.</param>
        /// <returns>A Taxon relations tree.</returns>
        public static TaxonRelationsTree CreateTaxonRelationsTree(
            IUserContext userContext,
            TaxonRelationList taxonRelations,
            TaxonList taxa,
            bool relationTaxonMustBeValid = false)
        {
            DateTime today = DateTime.Now;
            Dictionary <int, ITaxonRelationsTreeNode> treeNodesDictionary = new Dictionary <int, ITaxonRelationsTreeNode>();
            TaxonRelationsTree tree = new TaxonRelationsTree();

            tree.AllTreeEdgesDictionary    = new Dictionary <int, List <ITaxonRelationsTreeEdge> >();
            tree.AllTreeEdges              = new List <ITaxonRelationsTreeEdge>();
            tree.AllTreeNodes              = new List <ITaxonRelationsTreeNode>();
            tree.NotUsedTreeEdges          = new List <ITaxonRelationsTreeEdge>();
            tree.OriginalTaxonRelationList = taxonRelations;
            tree.OriginalTaxaList          = taxa;

            tree.TreeNodes             = new List <ITaxonRelationsTreeNode>();
            tree.TreeEdges             = new List <ITaxonRelationsTreeEdge>();
            tree.TreeNodesWithoutEdges = new List <ITaxonRelationsTreeNode>();

            foreach (ITaxonRelation taxonRelation in taxonRelations)
            {
                ITaxonRelationsTreeNode parentNode;
                ITaxonRelationsTreeNode childNode;
                if (!treeNodesDictionary.ContainsKey(taxonRelation.ParentTaxon.Id))
                {
                    parentNode = new TaxonRelationsTreeNode(taxonRelation.ParentTaxon);
                    tree.TreeNodes.Add(parentNode);
                    tree.AllTreeNodes.Add(parentNode);
                    treeNodesDictionary.Add(taxonRelation.ParentTaxon.Id, parentNode);
                }
                parentNode = treeNodesDictionary[taxonRelation.ParentTaxon.Id];

                if (!treeNodesDictionary.ContainsKey(taxonRelation.ChildTaxon.Id))
                {
                    childNode = new TaxonRelationsTreeNode(taxonRelation.ChildTaxon);
                    tree.TreeNodes.Add(childNode);
                    tree.AllTreeNodes.Add(childNode);
                    treeNodesDictionary.Add(taxonRelation.ChildTaxon.Id, childNode);
                }
                childNode = treeNodesDictionary[taxonRelation.ChildTaxon.Id];

                ITaxonRelationsTreeEdge treeEdge = new TaxonRelationsTreeEdge(parentNode, childNode, taxonRelation);
                tree.AllTreeEdges.Add(treeEdge);

                // Start DyntaxaTree.AllTreeEdgesDictionary
                if (!tree.AllTreeEdgesDictionary.ContainsKey(parentNode.Taxon.Id))
                {
                    tree.AllTreeEdgesDictionary.Add(parentNode.Taxon.Id, new List <ITaxonRelationsTreeEdge>());
                }
                tree.AllTreeEdgesDictionary[parentNode.Taxon.Id].Add(treeEdge);
                if (!tree.AllTreeEdgesDictionary.ContainsKey(childNode.Taxon.Id))
                {
                    tree.AllTreeEdgesDictionary.Add(childNode.Taxon.Id, new List <ITaxonRelationsTreeEdge>());
                }
                tree.AllTreeEdgesDictionary[childNode.Taxon.Id].Add(treeEdge);
                // End DyntaxaTree.AllTreeEdgesDictionary

                bool isMain  = taxonRelation.IsMainRelation;
                bool isValid = taxonRelation.ValidFromDate <= today && today <= taxonRelation.ValidToDate;

                if (parentNode.AllEdges == null)
                {
                    parentNode.AllEdges = new List <ITaxonRelationsTreeEdge>();
                }
                parentNode.AllEdges.Add(treeEdge);

                if (parentNode.AllChildEdges == null)
                {
                    parentNode.AllChildEdges = new List <ITaxonRelationsTreeEdge>();
                }
                parentNode.AllChildEdges.Add(treeEdge);

                if (childNode.AllEdges == null)
                {
                    childNode.AllEdges = new List <ITaxonRelationsTreeEdge>();
                }
                childNode.AllEdges.Add(treeEdge);

                if (childNode.AllParentEdges == null)
                {
                    childNode.AllParentEdges = new List <ITaxonRelationsTreeEdge>();
                }
                childNode.AllParentEdges.Add(treeEdge);

                if (isMain && isValid && ((relationTaxonMustBeValid && parentNode.Taxon.IsValid && childNode.Taxon.IsValid) || !relationTaxonMustBeValid))
                // vet inte om taxonen måste vara valid...
                {
                    if (parentNode.ValidMainChildren == null)
                    {
                        parentNode.ValidMainChildren = new List <ITaxonRelationsTreeEdge>();
                    }
                    parentNode.ValidMainChildren.Add(treeEdge);

                    if (childNode.ValidMainParents == null)
                    {
                        childNode.ValidMainParents = new List <ITaxonRelationsTreeEdge>();
                    }
                    childNode.ValidMainParents.Add(treeEdge);

                    tree.TreeEdges.Add(treeEdge);
                }
                else if (isMain && !isValid)
                {
                    if (parentNode.NonvalidMainChildren == null)
                    {
                        parentNode.NonvalidMainChildren = new List <ITaxonRelationsTreeEdge>();
                    }
                    parentNode.NonvalidMainChildren.Add(treeEdge);

                    if (childNode.NonvalidMainParents == null)
                    {
                        childNode.NonvalidMainParents = new List <ITaxonRelationsTreeEdge>();
                    }
                    childNode.NonvalidMainParents.Add(treeEdge);

                    tree.TreeEdges.Add(treeEdge);
                }
                else if (!isMain && isValid && ((relationTaxonMustBeValid && parentNode.Taxon.IsValid && childNode.Taxon.IsValid) || !relationTaxonMustBeValid))
                // vet inte om taxonen måste vara valid...
                {
                    if (parentNode.ValidSecondaryChildren == null)
                    {
                        parentNode.ValidSecondaryChildren = new List <ITaxonRelationsTreeEdge>();
                    }
                    parentNode.ValidSecondaryChildren.Add(treeEdge);

                    if (childNode.ValidSecondaryParents == null)
                    {
                        childNode.ValidSecondaryParents = new List <ITaxonRelationsTreeEdge>();
                    }
                    childNode.ValidSecondaryParents.Add(treeEdge);

                    tree.TreeEdges.Add(treeEdge);
                }
                else if (!isMain && !isValid)
                {
                    if (parentNode.NonvalidSecondaryChildren == null)
                    {
                        parentNode.NonvalidSecondaryChildren = new List <ITaxonRelationsTreeEdge>();
                    }
                    parentNode.NonvalidSecondaryChildren.Add(treeEdge);

                    if (childNode.NonvalidSecondaryParents == null)
                    {
                        childNode.NonvalidSecondaryParents = new List <ITaxonRelationsTreeEdge>();
                    }
                    childNode.NonvalidSecondaryParents.Add(treeEdge);

                    tree.TreeEdges.Add(treeEdge);
                }
                else
                {
                    tree.NotUsedTreeEdges.Add(treeEdge);
                    if (parentNode.NotUsedEdges == null)
                    {
                        parentNode.NotUsedEdges = new List <ITaxonRelationsTreeEdge>();
                    }
                    parentNode.NotUsedEdges.Add(treeEdge);

                    if (childNode.NotUsedEdges == null)
                    {
                        childNode.NotUsedEdges = new List <ITaxonRelationsTreeEdge>();
                    }
                    childNode.NotUsedEdges.Add(treeEdge);
                }
            }

            if (taxa != null)
            {
                foreach (var taxon in taxa)
                {
                    if (!treeNodesDictionary.ContainsKey(taxon.Id))
                    {
                        var node = new TaxonRelationsTreeNode(taxon);
                        treeNodesDictionary.Add(taxon.Id, node);
                        tree.AllTreeNodes.Add(node);
                        tree.TreeNodesWithoutEdges.Add(node);
                    }
                }
            }

            // todo - find all root nodes.
            if (treeNodesDictionary.Count > 0)
            {
                ITaxonRelationsTreeNode rootNode;
                // Try to set Biota as root node
                if (treeNodesDictionary.TryGetValue(0, out rootNode))
                {
                    tree.Root = rootNode;
                }
                // Else try set parameter taxon as root
                else if (taxa != null && taxa.Count == 1)
                {
                    if (treeNodesDictionary.TryGetValue(taxa[0].Id, out rootNode))
                    {
                        tree.Root = rootNode;
                    }
                }
            }

            tree.TreeNodeDictionary = treeNodesDictionary;

            return(tree);
        }
        /// <summary>
        /// Method that returns a filtered version of this species fact list. The filtering is done on several parameters.
        /// </summary>
        /// <param name="individualCategories">Individual categories for the requested species facts.</param>
        /// <param name="periods">Periods for the requested species facts.</param>
        /// <param name="hosts">Hosts for the requested species facts.</param>
        /// <param name="taxa">Taxa for the requested species facts.</param>
        /// <param name="factors">Factors for the requested species facts.</param>
        /// <returns>A species fact list.</returns>
        public SpeciesFactList GetSpeciesFacts(IndividualCategoryList individualCategories,
                                               PeriodList periods,
                                               TaxonList hosts,
                                               TaxonList taxa,
                                               FactorList factors)
        {
            SpeciesFactList filteredList = new SpeciesFactList();

            if (this.IsNotEmpty())
            {
                foreach (ISpeciesFact fact in this)
                {
                    bool go = true;

                    if (fact.IndividualCategory != null)
                    {
                        if ((individualCategories != null) && (individualCategories.Count > 0))
                        {
                            if (!individualCategories.Exists(fact.IndividualCategory))
                            {
                                go = false;
                            }
                        }
                    }

                    if (go)
                    {
                        if (fact.Period != null)
                        {
                            if ((periods != null) && (periods.Count > 0))
                            {
                                if (!periods.Exists(fact.Period))
                                {
                                    go = false;
                                }
                            }
                        }
                    }

                    if (go)
                    {
                        if (fact.Host != null)
                        {
                            // For the time being we only accept species facts that dont have a host.
                            go = false;

                            //if ((hosts != null) && (hosts.Count > 0))
                            //{
                            //    if (!hosts.Exists(fact.Host))
                            //        go = false;
                            //}
                        }
                    }

                    if (go)
                    {
                        if (fact.Taxon != null)
                        {
                            if ((taxa != null) && (taxa.Count > 0))
                            {
                                if (!taxa.Exists(fact.Taxon))
                                {
                                    go = false;
                                }
                            }
                        }
                    }

                    if (go)
                    {
                        if (fact.Factor != null)
                        {
                            if ((factors != null) && (factors.Count > 0))
                            {
                                if (!factors.Exists(fact.Factor))
                                {
                                    go = false;
                                }
                            }
                        }
                    }

                    if (go)
                    {
                        filteredList.Add(fact);
                    }
                }
            }

            return(filteredList);
        }