Exemple #1
0
        /// <summary>
        /// Get bird taxon ids.
        /// </summary>
        /// <param name="context">Web service request context</param>
        /// <returns>Bird taxon ids.</returns>
        private DataIdInt32List GetBirdTaxonIds(WebServiceContext context)
        {
            DataIdInt32List birdTaxonIds;
            List <Int32>    childTaxonIds, parentTaxonIds;
            String          cacheKey;

            // Get cached information.
            cacheKey     = Settings.Default.BirdTaxonIdsCacheKey;
            birdTaxonIds = (DataIdInt32List)(context.GetCachedObject(cacheKey));

            if (birdTaxonIds.IsNull())
            {
                // Get information from database.
                parentTaxonIds = new List <Int32>();
                parentTaxonIds.Add((Int32)(TaxonId.Birds));
                childTaxonIds = WebServiceData.TaxonManager.GetChildTaxonIds(context, parentTaxonIds);
                birdTaxonIds  = new DataIdInt32List(true);
                birdTaxonIds.AddRange(childTaxonIds);

                // Add information to cache.
                context.AddCachedObject(cacheKey,
                                        birdTaxonIds,
                                        DateTime.Now + new TimeSpan(0, 12, 0, 0),
                                        CacheItemPriority.High);
            }

            return(birdTaxonIds);
        }
        /// <summary>
        /// Get child taxon ids.
        /// Parent taxon ids are included in the result.
        /// Only valid taxon relations and child taxa are included.
        /// This method only works in web services that
        /// handles species observations.
        /// </summary>
        /// <param name="context">Web service request context.</param>
        /// <param name="parentTaxonIds">Parent taxon ids.</param>
        /// <returns>Child taxon ids.</returns>
        public virtual List <Int32> GetChildTaxonIds(WebServiceContext context,
                                                     List <Int32> parentTaxonIds)
        {
            DataIdInt32List childTaxonIds;
            Dictionary <Int32, DataIdInt32List> taxonTreeRelations;

            childTaxonIds = new DataIdInt32List();
            if (parentTaxonIds.IsNotEmpty())
            {
                taxonTreeRelations = GetTaxonTreeRelations(context);
                foreach (Int32 taxonId in parentTaxonIds)
                {
                    if (taxonTreeRelations.ContainsKey(taxonId))
                    {
                        childTaxonIds.Merge(taxonTreeRelations[taxonId]);
                    }
                    else
                    {
                        childTaxonIds.Merge(taxonId);
                    }
                }
            }

            return(childTaxonIds.GetInt32List());
        }
Exemple #3
0
        /// <summary>
        /// Get ids for all taxa that are used in these species facts.
        /// </summary>
        /// <param name="speciesFacts">Species facts.</param>
        /// <returns>Ids for all taxa that are used in these species facts.</returns>
        private List <Int32> GetTaxonIds(List <WebSpeciesFact> speciesFacts)
        {
            DataIdInt32List taxonIds;

            taxonIds = new DataIdInt32List(true);
            if (speciesFacts.IsNotEmpty())
            {
                foreach (WebSpeciesFact speciesFact in speciesFacts)
                {
                    if (speciesFact.HostId > 0)
                    {
                        taxonIds.Merge(new DataId32(speciesFact.HostId));
                    }

                    taxonIds.Merge(new DataId32(speciesFact.TaxonId));
                }
            }

            return(taxonIds.GetInt32List());
        }
        /// <summary>
        /// The taxon tree relations.
        /// </summary>
        /// <param name="context">Web service request context.</param>
        /// <returns>Taxon tree relations.</returns>
        private Dictionary <Int32, DataIdInt32List> GetTaxonTreeRelations(WebServiceContext context)
        {
            String cacheKey;
            Dictionary <Int32, DataIdInt32List> taxonTreeRelations;
            Int32 childTaxonId, parentTaxonId;

            // Get cached information.
            cacheKey           = Settings.Default.TaxonTreeRelationCacheKey;
            taxonTreeRelations = (Dictionary <Int32, DataIdInt32List>)(context.GetCachedObject(cacheKey));

            if (taxonTreeRelations.IsEmpty())
            {
                // Data not in cache. Get information from database.
                taxonTreeRelations = new Dictionary <Int32, DataIdInt32List>();
                using (DataReader dataReader = context.GetDatabase().GetTaxonTree())
                {
                    while (dataReader.Read())
                    {
                        childTaxonId  = dataReader.GetInt32("ChildTaxonId");
                        parentTaxonId = dataReader.GetInt32("ParentTaxonId");
                        if (!taxonTreeRelations.ContainsKey(parentTaxonId))
                        {
                            taxonTreeRelations[parentTaxonId] = new DataIdInt32List();
                        }

                        taxonTreeRelations[parentTaxonId].Add(childTaxonId);
                    }
                }

                // Add information to cache.
                context.AddCachedObject(cacheKey,
                                        taxonTreeRelations,
                                        DateTime.Now + new TimeSpan(0, 12, 0, 0),
                                        CacheItemPriority.High);
            }

            return(taxonTreeRelations);
        }
        /// <summary>
        /// Update taxon information in database.
        /// New information is read from web service TaxonService.
        /// </summary>
        /// <param name="context">
        /// User context.
        /// </param>
        /// <param name="taxons">
        /// The taxon.
        /// </param>
        /// <param name="taxonTrees">
        /// Contains information about all valid taxon trees.
        /// </param>
        /// <param name="taxonRemarks">
        /// The taxon remarks.
        /// </param>
        private static void UpdateTempTaxonInformation(
            WebServiceContext context,
            List <WebTaxon> taxons,
            IEnumerable <WebTaxonTreeNode> taxonTrees,
            Dictionary <Int32, String> taxonRemarks)
        {
            Dictionary <Int32, TaxonInformation> taxonInformations;
            TaxonInformation taxonInformation;

            ResetTaxaCache();
            foreach (var taxonTreeNode in taxonTrees)
            {
                AddTaxaToCache(taxonTreeNode);
            }

            //load taxon tree with only main parents
            var mainParentsTaxonTree = WebServiceData.TaxonManager.GetTaxonTreesBySearchCriteria(context, new WebTaxonTreeSearchCriteria {
                IsMainRelationRequired = true, IsValidRequired = true
            });
            var dictionaryTaxonTreeNode = ConvertTreeNodeStructure(mainParentsTaxonTree);

            taxonInformations = GetSpeciesFactInformation(context);

            // Create worktable.
            using (var taxaTable = CreateTaxaTable())
            {
                // Add information into table (only once per taxonId)
                var taxonIds = new DataIdInt32List(true);
                foreach (var taxon in taxons)
                {
                    if (taxonIds.Contains(taxon.Id))
                    {
                        continue;
                    }
                    else
                    {
                        taxonIds.Add(taxon.Id);
                    }

                    if (taxonInformations.ContainsKey(taxon.Id))
                    {
                        taxonInformation = taxonInformations[taxon.Id];
                    }
                    else
                    {
                        taxonInformation = new TaxonInformation();
                        taxonInformation.ActionPlanId              = 0;
                        taxonInformation.ConservationRelevant      = false;
                        taxonInformation.DisturbanceRadius         = 0;
                        taxonInformation.DyntaxaTaxonId            = taxon.Id;
                        taxonInformation.Natura2000                = false;
                        taxonInformation.ProtectedByLaw            = false;
                        taxonInformation.ProtectionLevel           = 0;
                        taxonInformation.RedlistCategory           = String.Empty;
                        taxonInformation.SwedishImmigrationHistory = String.Empty;
                        taxonInformation.SwedishOccurrence         = String.Empty;
                    }

                    var row = taxaTable.NewRow();
                    row[0] = taxon.Id; // dyntaxaTaxonId
                    if (!taxon.CategoryId.Equals(CATEGORY_HYBRID))
                    {
                        try
                        {
                            // infraspecificEpithet
                            row[1] = GetPartOfScientificName(taxon.ScientificName, INFRA_SPECIFIC_EPITHET);
                        }
                        catch (Exception)
                        {
                            WebServiceData.LogManager.Log(
                                context,
                                "Problems with taxon id = " + taxon.Id,
                                LogType.Error,
                                null);
                            throw;
                        }
                    }
                    else
                    {
                        row[1] = null;
                    }

                    row[2]  = String.Empty;                           // nameAccordingTo isplanned=0
                    row[3]  = String.Empty;                           // nameAccordingToId isplanned=0
                    row[4]  = String.Empty;                           // namePublishedIn isplanned=0
                    row[5]  = String.Empty;                           // namePublishedInId isplanned=0
                    row[6]  = String.Empty;                           // namePublishedInYear isplanned=0
                    row[7]  = String.Empty;                           // nomenclaturalCode isplanned=0
                    row[8]  = String.Empty;                           // nomenclaturalStatus - Metod som hämtar namn från taxonobjektet
                    row[9]  = GetTaxaOriginalNameFromCache(taxon.Id); // originalNameUsage
                    row[10] = String.Empty;                           // originalNameUsageId
                    row[11] = taxon.ScientificName;                   // scientificName
                    row[12] = taxon.Author;                           // scientificNameAuthorship
                    row[13] = GetTaxonNameGuidFromCache(taxon.Id);    // scientificNameId
                    if (!taxon.CategoryId.Equals(CATEGORY_HYBRID))
                    {
                        // specificEpithet
                        row[14] = GetPartOfScientificName(taxon.ScientificName, SPECIFIC_EPITHET);
                    }
                    else
                    {
                        row[14] = null;
                    }

                    row[15] = taxon.Guid;                                  // taxonConceptId
                    row[16] = String.Empty;                                // taxonConceptStatus
                    row[17] = String.Empty;                                // taxonomicStatus
                    row[18] = GetTaxonCategoryFromCache(taxon.CategoryId); // taxonRank
                    row[19] = taxon.SortOrder;                             // taxonSortOrder
                    row[20] = TAXON_URL_PREFIX + taxon.Id.WebToString();   // taxonURL
                    row[21] = taxon.CommonName;                            // vernacularName
                    row[22] = taxon.CategoryId;

                    //row[23] = 0; // actionplan
                    //row[24] = 0; // conservationRelevant
                    //row[25] = 0; // natura2000
                    //row[26] = 0; // protectedByLaw
                    //row[27] = 0; // protectionLevel
                    //row[28] = String.Empty; // redlistCategory
                    //row[29] = String.Empty; // swedishImmigrationHistory
                    //row[30] = String.Empty; // swedishOccurrence
                    //row[31] = String.Empty; // organismGroup
                    //row[32] = 0; // disturbanceRadius

                    row[23] = taxonInformation.ActionPlanId;                 // actionplan
                    row[24] = taxonInformation.ConservationRelevant ? 1 : 0; // conservationRelevant
                    row[25] = taxonInformation.Natura2000 ? 1 : 0;           // natura2000
                    row[26] = taxonInformation.ProtectedByLaw ? 1 : 0;       // protectedByLaw
                    row[27] = taxonInformation.ProtectionLevel;              // protectionLevel
                    row[28] = taxonInformation.RedlistCategory;              // redlistCategory
                    row[29] = taxonInformation.SwedishImmigrationHistory;    // swedishImmigrationHistory
                    row[30] = taxonInformation.SwedishOccurrence;            // swedishOccurrence
                    row[31] = taxonInformation.OrganismGroup;                // organismGroup
                    row[32] = taxonInformation.DisturbanceRadius;            // disturbanceRadius

                    row[33] = String.Empty;
                    row[34] = String.Empty;
                    row[35] = String.Empty;
                    row[36] = String.Empty;
                    row[37] = String.Empty;
                    row[38] = String.Empty;
                    row[39] = String.Empty;
                    row[40] = String.Empty;

                    if (taxon.IsValid)
                    {
                        // Load TempTaxonTreeNodes into dictionary.
                        //     Dictionary<Int32, MyTaxonTreeNode> dictionaryTaxonTreeNode =  GetTaxonTreeNodes(context);
                        Int32  currentTaxonId           = taxon.Id;
                        String higherClassificationList = String.Empty;
                        String sign = String.Empty;

                        //loop through the tree structure and set the corresponding values
                        if (dictionaryTaxonTreeNode.ContainsKey(currentTaxonId))
                        {
                            while (dictionaryTaxonTreeNode[currentTaxonId].Parents.IsNotEmpty())
                            {
                                currentTaxonId = dictionaryTaxonTreeNode[currentTaxonId].Parents[0].TaxonId;

                                if (dictionaryTaxonTreeNode[currentTaxonId].Parents.IsNotEmpty())
                                {
                                    higherClassificationList =
                                        dictionaryTaxonTreeNode[currentTaxonId].Parents[0].Information
                                        .ScientificName + sign + higherClassificationList;
                                    sign = ";";
                                }

                                switch (dictionaryTaxonTreeNode[currentTaxonId].Information.TaxonCategoryId)
                                {
                                case (Int32)TaxonCategoryId.Class:
                                    // row[34] = String.Empty; //class
                                    row[34] = dictionaryTaxonTreeNode[currentTaxonId].Information.ScientificName;
                                    break;

                                case (Int32)TaxonCategoryId.Family:
                                    // row[35] = String.Empty; //family
                                    row[35] = dictionaryTaxonTreeNode[currentTaxonId].Information.ScientificName;
                                    break;

                                case (Int32)TaxonCategoryId.Genus:
                                    // row[36] = String.Empty; //genus
                                    row[36] = dictionaryTaxonTreeNode[currentTaxonId].Information.ScientificName;
                                    break;

                                case (Int32)TaxonCategoryId.Kingdom:
                                    // row[37] = String.Empty; //kingdom
                                    row[37] = dictionaryTaxonTreeNode[currentTaxonId].Information.ScientificName;
                                    break;

                                case (Int32)TaxonCategoryId.Order:
                                    // row[38] = String.Empty; //order
                                    row[38] = dictionaryTaxonTreeNode[currentTaxonId].Information.ScientificName;
                                    break;

                                case (Int32)TaxonCategoryId.Phylum:
                                    // row[39] = String.Empty; //phylum
                                    row[39] = dictionaryTaxonTreeNode[currentTaxonId].Information.ScientificName;
                                    break;

                                case (Int32)TaxonCategoryId.Subgenus:
                                    // row[40] = String.Empty; //subgenus
                                    row[40] = dictionaryTaxonTreeNode[currentTaxonId].Information.ScientificName;
                                    break;
                                }
                            }
                            row[33] = higherClassificationList;
                        }
                    }

                    row[41] = taxon.IsValid; // isValid

                    if (taxon.IsValid)
                    {
                        row[42] = DBNull.Value;
                    }
                    else
                    {
                        row[42] = TaxonManager.GetNewTaxonId(context, taxon.Id); // newDyntaxaTaxonId
                    }

                    if (taxonRemarks.ContainsKey(taxon.Id))
                    {
                        row[43] = taxonRemarks[taxon.Id];
                    }
                    else
                    {
                        row[43] = "-";
                    }

                    if (taxon.IsValid)
                    {
                        row[44] = taxon.Id;
                    }
                    else
                    {
                        row[44] = TaxonManager.GetCurrentTaxonId(context, taxon.Id); // currentDyntaxaTaxonId
                    }

                    taxaTable.Rows.Add(row);
                }

                // Copy data into database.
                context.GetSpeciesObservationDatabase().AddTableData(context, taxaTable);
            }
        }