public void Constructor()
        {
            TaxonSearchCriteria searchCriteria;

            searchCriteria = new TaxonSearchCriteria();
            Assert.IsNotNull(searchCriteria);
        }
        /// <summary>
        /// Cache taxon list information about all valid taxa.
        /// </summary>
        private void LoadTaxonListInformation()
        {
            // Get taxa.
            ITaxonSearchCriteria searchCriteria = new TaxonSearchCriteria();

            searchCriteria.IsValidTaxon = true;
            TaxonList taxa = CoreData.TaxonManager.GetTaxa(mContext, searchCriteria);

            // Init new taxon information items.
            foreach (ITaxon taxon in taxa)
            {
                var taxonListInformation = new TaxonListInformation
                {
                    CommonName       = taxon.CommonName,
                    Id               = taxon.Id,
                    CategoryId       = taxon.Category.Id,
                    ParentCategoryId = taxon.Category.ParentId,
                    ScientificName   = taxon.ScientificName
                };
                mTaxonInformationCache.TaxonInformation[taxon.Id] = taxonListInformation;
            }

            // Add species fact information.
            //GetLandscapeTypeSpeciesFactsToCache();
            //GetOrganismGroupSpeciesFactsToCache();
            GetRedListCategorySpeciesFactsToCache();
            //GetRedListCriteriaSpeciesFactsToCache();
            //GetRedListTaxonCategorySpeciesFactsToCache();
            //GetSwedishOccurrenceSpeciesFactsToCache();
            //GetRedListOldCategorySpeciesFactsToCache();
        }
 private TaxonSearchCriteria GetSearchCriteria(Boolean refresh)
 {
     if (_searchCriteria.IsNull() || refresh)
     {
         _searchCriteria = new TaxonSearchCriteria();
     }
     return(_searchCriteria);
 }
        public List <ITaxon> GetChildTaxa(int[] parentTaxonIds)
        {
            var searchCriteria = new TaxonSearchCriteria()
            {
                TaxonIds         = new List <int>(parentTaxonIds),
                TaxonCategoryIds = new List <int> {
                    (int)TaxonCategoryId.Species
                },
                Scope = TaxonSearchScope.AllChildTaxa
            };

            return(CoreData.TaxonManager.GetTaxa(UserContext, searchCriteria));
        }
        /// <summary>
        /// Get a list with corresponding taxon from taxon ids
        /// </summary>
        /// <param name="taxonIds">The taxon ids.</param>
        /// <returns></returns>
        private List <ITaxon> GetMatches(List <Int32> taxonIds)
        {
            var searchCriteria = new TaxonSearchCriteria();

            searchCriteria.TaxonIds     = new List <int>();
            searchCriteria.IsValidTaxon = true;
            foreach (Int32 id in taxonIds)
            {
                searchCriteria.TaxonIds.Add(id);
            }
            TaxonList taxa = CoreData.TaxonManager.GetTaxa(_user, searchCriteria);

            return((List <ITaxon>)taxa.GetGenericList());
        }
Example #6
0
        /// <summary>
        /// Load information about suitable parent taxa.
        /// </summary>
        /// <param name="userContext">User context.</param>
        private void LoadTaxonInformation(IUserContext userContext)
        {
            ITaxonSearchCriteria searchCriteria;
            List <Int32>         taxonIds;
            TaxonList            parentTaxa;
            TaxonNameList        allTaxonNames;

            _italicStringsInAutomaticTaxonomicParagraph = new List <String>();
            if (_suitableParents.IsEmpty() && _taxon.IsNotNull())
            {
                searchCriteria = new TaxonSearchCriteria();
                taxonIds       = new List <Int32>();
                taxonIds.Add(_taxon.Id);
                searchCriteria.TaxonIds = taxonIds;
                searchCriteria.Scope    = TaxonSearchScope.AllParentTaxa;
                parentTaxa       = CoreData.TaxonManager.GetTaxa(userContext, searchCriteria);
                _suitableParents = new TaxonList();
                foreach (ITaxon parent in parentTaxa)
                {
                    if ((parent.Category.Id == (Int32)(TaxonCategoryId.Kingdom)) ||
                        (parent.Category.Id == (Int32)(TaxonCategoryId.Phylum)) ||
                        (parent.Category.Id == (Int32)(TaxonCategoryId.Class)) ||
                        (parent.Category.Id == (Int32)(TaxonCategoryId.Order)) ||
                        (parent.Category.Id == (Int32)(TaxonCategoryId.Family)))
                    {
                        if (parent.Id != (Int32)(TaxonId.Life))
                        {
                            _suitableParents.Add(parent);
                        }
                    }
                }
            }

            if (_synonyms.IsEmpty() && _taxon.IsNotNull())
            {
                _synonyms     = new TaxonNameList();
                allTaxonNames = CoreData.TaxonManager.GetTaxonNames(userContext, _taxon);
                foreach (ITaxonName name in allTaxonNames)
                {
                    if ((name.Category.Id == 0) &&
                        (name.Status.Id == 0) &&
                        (!name.IsRecommended) &&
                        (name.Name != _taxon.ScientificName))
                    {
                        _italicStringsInAutomaticTaxonomicParagraph.Add(name.Name);
                        _synonyms.Add(name);
                    }
                }
            }
        }
        public void Add_Filter_Data()
        {
            IUserContext userContext = CoreData.UserManager.GetCurrentUser();

            var searchCriteria = new TaxonSearchCriteria();

            searchCriteria.IsValidTaxon = true;
            searchCriteria.TaxonIds     = new List <int>();
            searchCriteria.TaxonIds.Add(1);
            searchCriteria.TaxonIds.Add(2);
            searchCriteria.TaxonIds.Add(3);
            searchCriteria.TaxonIds.Add(4);
            var taxa = CoreData.TaxonManager.GetTaxa(userContext, searchCriteria);

            Assert.AreEqual(4, taxa.Count);
        }
        /// <summary>
        /// Updates the reference relations.
        /// </summary>
        /// <param name="taxon">The source taxon.</param>
        /// <param name="guid">The unique identifier.</param>
        /// <param name="newReferenceList">The new reference list.</param>
        /// <param name="applyMode">The apply mode.</param>
        public void UpdateReferenceRelations(
            ITaxon taxon,
            string guid,
            ReferenceViewModel[] newReferenceList,
            ReferenceApplyMode applyMode)
        {
            ReferenceRelationList allReferenceRelationItemsToCreate = new ReferenceRelationList();
            ReferenceRelationList allReferenceRelationItemsToDelete = new ReferenceRelationList();
            ReferenceRelationList createReferenceRelationItems;
            ReferenceRelationList deleteReferenceRelationItems;

            // Handle source taxon reference relations
            createReferenceRelationItems = GetReferenceRelationsThatWillBeCreated(guid, newReferenceList, ReferenceApplyMode.OnlySelected);
            deleteReferenceRelationItems = GetReferenceRelationsThatWillBeDeleted(guid, newReferenceList, ReferenceApplyMode.OnlySelected);
            allReferenceRelationItemsToCreate.AddRange(createReferenceRelationItems);
            allReferenceRelationItemsToDelete.AddRange(deleteReferenceRelationItems);

            if (applyMode != ReferenceApplyMode.OnlySelected)
            {
                // Get all child taxa
                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                taxonSearchCriteria.TaxonIds = new List <int>();
                taxonSearchCriteria.TaxonIds.Add(taxon.Id);
                taxonSearchCriteria.Scope = TaxonSearchScope.AllChildTaxa;
                TaxonList taxonList = CoreData.TaxonManager.GetTaxa(user, taxonSearchCriteria);
                taxonList.Remove(taxon);

                foreach (ITaxon childTaxon in taxonList)
                {
                    createReferenceRelationItems = GetReferenceRelationsThatWillBeCreated(childTaxon.Guid, newReferenceList, applyMode);
                    deleteReferenceRelationItems = GetReferenceRelationsThatWillBeDeleted(childTaxon.Guid, newReferenceList, applyMode);
                    allReferenceRelationItemsToCreate.AddRange(createReferenceRelationItems);
                    allReferenceRelationItemsToDelete.AddRange(deleteReferenceRelationItems);
                }
            }

            if (allReferenceRelationItemsToCreate.Count > 0 || allReferenceRelationItemsToDelete.Count > 0)
            {
                using (ITransaction transaction = user.StartTransaction())
                {
                    CoreData.ReferenceManager.DeleteReferenceRelations(user, allReferenceRelationItemsToDelete);
                    CoreData.ReferenceManager.CreateReferenceRelations(user, allReferenceRelationItemsToCreate);
                    transaction.Commit();
                }
            }
        }
Example #9
0
        /// <summary>
        /// Updates the cache by getting all relations and all taxa from Taxon service
        /// and create a TaxonRelationTree.
        /// This method is thread safe.
        /// </summary>
        /// <param name="userContext">The user context.</param>
        public static void UpdateCache(IUserContext userContext)
        {
            lock (LockingTarget)
            {
                Debug.WriteLine("Start update Cache {0}", DateTime.Now);
                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                var allTaxa = CoreData.TaxonManager.GetTaxa(userContext, taxonSearchCriteria);

                TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria();
                TaxonRelationList           allRelations   = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria);
                TaxonRelationList       = allRelations;
                CachedTaxonRelationTree = TaxonRelationsTreeManager.CreateTaxonRelationsTree(
                    userContext,
                    allRelations,
                    allTaxa);
                CacheLastUpdatedTime = DateTime.Now;
                Debug.WriteLine("End update Cache {0}", DateTime.Now);
                DyntaxaLogger.WriteMessage("DyntaxaTree updated {0}", DateTime.Now);
            }
        }
        public void CreateCompleteTaxonRelationsTree()
        {
            using (ShimsContext.Create())
            {
                LoginApplicationUserAndSetSessionVariables();
                SetSwedishLanguage();
                IUserContext userContext = ApplicationUserContextSV;

                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                var allTaxa = CoreData.TaxonManager.GetTaxa(userContext, taxonSearchCriteria);

                TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria();
                var allRelations = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria);

                var tree = TaxonRelationsTreeManager.CreateTaxonRelationsTree(userContext, allRelations, allTaxa, false);

                //1012265
                //1011617
                //6001191 ensam
                var    edges    = tree.GetAllChildAndParentEdges(266838);
                string graphviz = GraphvizManager.CreateGraphvizFormatRepresentation(edges);

                // find suspicious hybrids
                List <ITaxonRelationsTreeNode> invalidHybrids;


                // find root nodes
                tree.RootNodes      = new HashSet <ITaxonRelationsTreeNode>();
                tree.ValidRootNodes = new HashSet <ITaxonRelationsTreeNode>();
                foreach (var node in tree.AllTreeNodes)
                {
                    tree.RootNodes.Add(node.RootNode);

                    if (node.RootNode.Taxon.IsValid)
                    {
                        tree.ValidRootNodes.Add(node.RootNode);
                    }
                }
            }
        }
        public void SplitGraphTest()
        {
            using (ShimsContext.Create())
            {
                //Arrange
                LoginApplicationUserAndSetSessionVariables();
                SetSwedishLanguage();
                IUserContext userContext      = ApplicationUserContextSV;
                const int    lumpSplitTaxonId = 2755;
                //TaxonIdBefore: 233285
                //TaxonIdAfter: 2755

                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                TaxonList            allTaxa             = CoreData.TaxonManager.GetTaxa(userContext, taxonSearchCriteria);

                TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria();
                TaxonRelationList           allRelations   = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria);
                TaxonRelationsTree          tree           = TaxonRelationsTreeManager.CreateTaxonRelationsTree(userContext, allRelations,
                                                                                                                allTaxa, false);

                var edges  = tree.GetAllChildAndParentEdges(lumpSplitTaxonId);
                var edges2 = tree.GetAllValidChildAndParentEdges(lumpSplitTaxonId);
                List <ITaxonRelationsTreeNode> sourceNodes = new List <ITaxonRelationsTreeNode> {
                    tree.GetTreeNode(lumpSplitTaxonId)
                };
                GraphVizFormat graphVizFormat = new GraphVizFormat()
                {
                    ShowLumpsAndSplits = true,
                    ShowRelationId     = false
                };

                string graphRepresentation2 = GraphvizManager.CreateGraphvizFormatRepresentation(
                    userContext,
                    tree,
                    edges2,
                    sourceNodes,
                    graphVizFormat);
                int x = 8;
            }
        }
        /// <summary>
        /// Get all child taxon
        /// </summary>
        /// <param name="parentTaxaIds">Id of parent taxon</param>
        /// <returns></returns>
        public List <TaxonSearchResultItemViewModel> GetChildTaxa(int[] parentTaxaIds)
        {
            var searchCriteria = new TaxonSearchCriteria()
            {
                TaxonIds         = new List <int>(parentTaxaIds),
                TaxonCategoryIds = new List <int> {
                    (int)TaxonCategoryId.Species
                },
                Scope = TaxonSearchScope.AllChildTaxa
            };

            var taxa       = CoreData.TaxonManager.GetTaxa(_user, searchCriteria);
            var resultList = (from t in taxa select TaxonSearchResultItemViewModel.CreateFromTaxon(t, false)).Distinct().ToList();

            var speciesFactManager = new SpeciesFactManager(_user);

            var protectedTaxonList = speciesFactManager.GetProtectedTaxons();

            // Set protection level for each taxon; public or not
            resultList.ForEach(t => t.SpeciesProtectionLevel = protectedTaxonList.Any(ptl => ptl.Id == t.TaxonId) ? SpeciesProtectionLevelEnum.Protected1 : SpeciesProtectionLevelEnum.Public);

            return(resultList);
        }
        /// <summary>
        /// Update invalid taxaidlist cache if not present
        /// </summary>
        /// <returns></returns>
        private HashSet <int> UpdateInvalidTaxaIdListCache()
        {
            ITaxonSearchCriteria searchCriteria = new TaxonSearchCriteria()
            {
                IsValidTaxon = false
            };

            var taxonList = CoreData.TaxonManager.GetTaxa(mUserContext, searchCriteria);

            var taxaListTaxonIdsSet = new HashSet <int>();

            foreach (var taxon in taxonList)
            {
                taxaListTaxonIdsSet.Add(taxon.Id);
            }

            AspNetCache.AddCachedObject(
                "allInvalidTaxaIds",
                taxaListTaxonIdsSet,
                DateTime.Now + new TimeSpan(0, 24, 0, 0),
                CacheItemPriority.Normal);

            return(taxaListTaxonIdsSet);
        }
 public TaxonSearchCriteriaTest()
 {
     _searchCriteria = null;
 }
        public void ValidateTaxonRelationsTree()
        {
            using (ShimsContext.Create())
            {
                LoginApplicationUserAndSetSessionVariables();
                SetSwedishLanguage();
                IUserContext userContext = ApplicationUserContextSV;

                ITaxonSearchCriteria taxonSearchCriteria = new TaxonSearchCriteria();
                var allTaxa = CoreData.TaxonManager.GetTaxa(userContext, taxonSearchCriteria);

                TaxonRelationSearchCriteria searchCriteria = new TaxonRelationSearchCriteria();
                var allRelations = CoreData.TaxonManager.GetTaxonRelations(userContext, searchCriteria);
                var tree         = TaxonRelationsTreeManager.CreateTaxonRelationsTree(userContext, allRelations, allTaxa, false);

                List <ITaxonRelationsTreeNode> collectiveTaxa = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> hybridTaxa     = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> artkomplexTaxa = new List <ITaxonRelationsTreeNode>();

                List <ITaxonRelationsTreeNode> validCollectiveTaxa = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> validHybridTaxa     = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> validArtkomplexTaxa = new List <ITaxonRelationsTreeNode>();

                List <ITaxonRelationsTreeNode> possiblyInvalidCollectiveTaxa = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> possiblyInvalidHybridTaxa     = new List <ITaxonRelationsTreeNode>();
                List <ITaxonRelationsTreeNode> possiblyInvalidArtkomplexTaxa = new List <ITaxonRelationsTreeNode>();

                List <ITaxonRelationsTreeNode> notYetHandledHybridTaxa = new List <ITaxonRelationsTreeNode>();


                foreach (var node in tree.AsDepthFirstNodeIterator())
                {
                    if (node.Taxon.Category.Id == 27)
                    {
                        collectiveTaxa.Add(node);

                        // Kollektivtaxon ska ha 0 eller flera barn?
                        // MainChildren = 0 && SecondaryChildren >= 0
                        if (node.ValidMainChildren == null)
                        {
                            validCollectiveTaxa.Add(node);
                        }
                        else
                        {
                            possiblyInvalidCollectiveTaxa.Add(node);
                        }
                    }
                    if (node.Taxon.Category.Id == 21)
                    {
                        hybridTaxa.Add(node);

                        // Hybrider ska ha exakt 1 primär förälder precis som alla andra taxon.
                        // När alla Hybrider har hanterats i revisioner (Mora är på gång att ta tag i det så ska den ha
                        // 1 primär relation oftast under ett släkte (minsta gemensamma nämnaren för föräldrarna)
                        // men ibland även högre upp. Tyvärr verkar det ligga någon regel i Dyntaxa som
                        // förhindrar vissa av dessa saker. För släktet Anodonta kan jag t ex inte lägga till
                        // ett artkomplex (möjligen samma för hybrider) som sekundär förälder.
                        // 2 eller flera sekundära föräldrar. Oftast till arter.

                        // Hybrider ska ha exakt 2 sekundära föräldrar.
                        // och exakt 1 primär förälder.
                        if (node.ValidMainParents != null && node.ValidMainParents.Count == 1 &&
                            node.ValidSecondaryParents != null && node.ValidSecondaryParents.Count >= 2)
                        {
                            validHybridTaxa.Add(node);
                        }
                        else
                        {
                            notYetHandledHybridTaxa.Add(node);
                            //possiblyInvalidHybridTaxa.Add(node);
                        }
                    }
                    if (node.Taxon.Category.Id == 28)
                    {
                        artkomplexTaxa.Add(node);

                        // Artkomplex ska ha 0 eller fler barn?
                        // MainChildren = 0 && SecondaryChildren >= 0
                        if (node.ValidMainChildren == null)
                        {
                            validArtkomplexTaxa.Add(node);
                        }
                        else
                        {
                            possiblyInvalidArtkomplexTaxa.Add(node);
                        }

                        if (node.ValidSecondaryChildren != null && node.ValidSecondaryChildren.Count == 1)
                        {
                            int z = 8;
                        }
                    }
                }
                int t = 8;
            }
        }