/// <summary>
 /// Create a new TaxonSpeciesFactViewModel, from a taxon.
 /// </summary>
 /// <param name="user">The user context.</param>
 /// <param name="taxon">The taxon.</param>
 public TaxonSpeciesFactViewModel(IUserContext user, ITaxon taxon)
 {
     Author         = taxon.Author ?? string.Empty;
     CommonName     = taxon.GetCommonName(user).IsNotNull() ? taxon.GetCommonName(user).Name : string.Empty;
     ParentTaxa     = GetParentTaxa(user, taxon);
     ScientificName = taxon.GetScientificName(user).IsNotNull() ? taxon.GetScientificName(user).Name : string.Empty;
     TaxonId        = taxon.Id;
     Category       = taxon.Category.Name;
     //Synonyms = taxon.GetSynonymsViewModel(user);
     RedListCategoryTaxa = new Dictionary <RedListCategory, List <int> >();
 }
Ejemplo n.º 2
0
        // Public members

        public override string GetString(ITaxon taxon)
        {
            string name = taxon.GetCommonName();

            if (string.IsNullOrEmpty(name))
            {
                return(base.GetString(taxon));
            }

            return(name);
        }
Ejemplo n.º 3
0
        public static async Task UpdateTaxonAsync(this SQLiteDatabase database, ITaxon taxon)
        {
            string tableName = GetTableNameForRank(taxon.GetRank());

            if (!string.IsNullOrEmpty(tableName))
            {
                string parentColumnName      = GetFieldNameForRank(taxon.GetParentRank());
                string updateParentColumnStr = string.Empty;

                if (!string.IsNullOrEmpty(parentColumnName) && taxon.ParentId.HasValue)
                {
                    updateParentColumnStr = string.Format(", {0}=$parent_id", parentColumnName);
                }

                using (SQLiteCommand cmd = new SQLiteCommand(string.Format("UPDATE {0} SET name = $name, description = $description, pics = $pics{1}, common_name = $common_name WHERE id = $id",
                                                                           tableName, updateParentColumnStr))) {
                    cmd.Parameters.AddWithValue("$id", taxon.Id);

                    cmd.Parameters.AddWithValue("$name", taxon.Name.ToLowerInvariant());
                    cmd.Parameters.AddWithValue("$description", taxon.Description);
                    cmd.Parameters.AddWithValue("$pics", taxon.GetPictureUrl());

                    // Because this field was added in a database update, it's possible for it to be null rather than the empty string.

                    cmd.Parameters.AddWithValue("$common_name", string.IsNullOrEmpty(taxon.GetCommonName()) ? "" : taxon.GetCommonName().ToLowerInvariant());

                    if (!string.IsNullOrEmpty(parentColumnName) && taxon.ParentId.HasValue)
                    {
                        cmd.Parameters.AddWithValue("$parent_column_name", parentColumnName);
                        cmd.Parameters.AddWithValue("$parent_id", taxon.ParentId);
                    }

                    await database.ExecuteNonQueryAsync(cmd);
                }
            }
        }
Ejemplo n.º 4
0
        private async Task ReplySetTaxonCommonNameAsync(ITaxon taxon, string commonName)
        {
            if (taxon.IsValid())
            {
                taxon.CommonNames.Clear();
                taxon.CommonNames.Add(commonName);

                await Db.UpdateTaxonAsync(taxon);

                if (taxon is ISpecies species)
                {
                    await ReplySuccessAsync($"{species.GetShortName().ToBold()} is now commonly known as the {species.GetCommonName().ToTitle().ToBold()}.");
                }
                else
                {
                    await ReplySuccessAsync($"Members of the {taxon.GetRank().GetName()} {taxon.GetName().ToTitle().ToBold()} are now commonly known as {taxon.GetCommonName().ToTitle().ToBold()}.");
                }
            }
        }
Ejemplo n.º 5
0
        public async Task <IPaginatedMessage> BuildTaxonMessageAsync(ITaxon taxon)
        {
            if (!taxon.IsValid())
            {
                return(null);
            }

            List <string> subItems = new List <string>();

            if (taxon.Rank.Type == TaxonRankType.Species)
            {
                ISpecies species = await Db.GetSpeciesAsync(taxon.Id);

                return(await BuildSpeciesMessageAsync(species));
            }
            else if (taxon.Rank.Type == TaxonRankType.Genus)
            {
                // For genera, get all species underneath it.
                // This will let us check if the species is extinct, and cross it out if that's the case.

                List <ISpecies> speciesList = new List <ISpecies>();

                foreach (ITaxon subtaxon in await Db.GetSubtaxaAsync(taxon))
                {
                    speciesList.Add(await Db.GetSpeciesAsync(subtaxon.Id));
                }

                speciesList.Sort((lhs, rhs) => TaxonFormatter.GetString(lhs, false).CompareTo(TaxonFormatter.GetString(rhs, false)));

                foreach (ISpecies species in speciesList.Where(s => s.IsValid()))
                {
                    subItems.Add(TaxonFormatter.GetString(species));
                }
            }
            else
            {
                // Get all subtaxa under this taxon.

                IEnumerable <ITaxon> subtaxa = await Db.GetSubtaxaAsync(taxon);

                // Add all subtaxa to the list.

                foreach (ITaxon subtaxon in subtaxa)
                {
                    if (subtaxon.Rank.Type == TaxonRankType.Species)
                    {
                        // Do not attempt to count sub-taxa for species.

                        subItems.Add(subtaxon.GetName());
                    }
                    else
                    {
                        // Count the number of species under this taxon.
                        // Taxa with no species under them will not be displayed.

                        long speciesCount = await Db.GetSpeciesCountAsync(subtaxon);

                        if (speciesCount > 0)
                        {
                            // Count the sub-taxa under this taxon.

                            long subtaxaCount = (await Db.GetSubtaxaAsync(subtaxon)).Count();

                            // Add the taxon to the list.

                            if (subtaxaCount > 0)
                            {
                                subItems.Add(string.Format("{0} ({1})", subtaxon.GetName(), subtaxaCount));
                            }
                        }
                    }
                }
            }

            // Generate embed pages.

            string title        = taxon.CommonNames.Count() <= 0 ? taxon.GetName() : string.Format("{0} ({1})", taxon.GetName(), taxon.GetCommonName());
            string fieldTitle   = string.Format("{0} in this {1} ({2}):", taxon.GetChildRank().GetName(true).ToTitle(), taxon.GetRank().GetName().ToLowerInvariant(), subItems.Count());
            string thumbnailUrl = taxon.GetPictureUrl();

            StringBuilder description = new StringBuilder();

            description.AppendLine(taxon.GetDescriptionOrDefault());

            if (subItems.Count() <= 0)
            {
                description.AppendLine();
                description.AppendLine(string.Format("This {0} contains no {1}.", taxon.GetRank().GetName(), taxon.GetChildRank().GetName(true)));
            }

            List <Discord.Messaging.IEmbed> pages = new List <Discord.Messaging.IEmbed>(EmbedUtilities.CreateEmbedPages(fieldTitle, subItems, options: EmbedPaginationOptions.AddPageNumbers));

            if (!pages.Any())
            {
                pages.Add(new Discord.Messaging.Embed());
            }

            IPaginatedMessage paginatedMessage = new PaginatedMessage(pages);

            foreach (Discord.Messaging.IEmbed page in paginatedMessage.Select(m => m.Embed))
            {
                page.Title        = title;
                page.ThumbnailUrl = thumbnailUrl;
                page.Description  = description.ToString();

                if (subItems.Count() > 0 && taxon.GetRank() != TaxonRankType.Genus)
                {
                    page.Footer += string.Format(" — Empty {0} are not listed.", taxon.GetChildRank().GetName(true));
                }
            }

            return(paginatedMessage);
        }