public string GetParenTaxa(ITaxon taxon)
        {
            if (taxon == null)
            {
                return("");
            }

            var parents         = new StringBuilder();
            var parentRelations = taxon.GetAllParentTaxonRelations(_user, null, false, false);

            foreach (var taxonRelation in parentRelations)
            {
                var relatedTaxon = taxonRelation.ParentTaxon;
                if (parents.Length > 0)
                {
                    parents.Append("; ");
                }

                parents.Append(relatedTaxon.Category.Name);
                parents.Append(": ");
                parents.Append(relatedTaxon.GetLabel());
            }

            return(parents.ToString());
        }
        /// <summary>
        /// Gets the taxonomic hierarchy, for the specified taxon.
        /// </summary>
        /// <param name="user">The user context.</param>
        /// <param name="taxon">The taxon.</param>
        /// <returns>An ordered list of parent taxa, forming the taxonomic hierarchy.</returns>
        private List <TaxonViewModel> GetParentTaxa(IUserContext user, ITaxon taxon)
        {
            var taxaRelations      = taxon.GetAllParentTaxonRelations(user, null, false, false, true);
            var distinctParentTaxa = taxaRelations.GroupBy(x => x.ParentTaxon.Id).Select(x => x.First().ParentTaxon).Where(x => x.Id > 0).ToList();

            return(distinctParentTaxa.Select(parentTaxon => new TaxonViewModel
            {
                Author = parentTaxon.Author,
                Category = parentTaxon.Category.Name,
                CommonName = parentTaxon.CommonName.IsNotEmpty() ? parentTaxon.CommonName : string.Empty,
                ScientificName = parentTaxon.ScientificName.IsNotEmpty() ? parentTaxon.ScientificName : string.Empty,
                //SortOrder = parentTaxon.SortOrder,
                TaxonId = parentTaxon.Id,
                //CategoryId = parentTaxon.Category.Id
            }).ToList());
        }
        public void GetTaxonParents()
        {
            using (ShimsContext.Create())
            {
                //Arrange
                LoginApplicationUserAndSetSessionVariables();
                SetSwedishLanguage();
                IUserContext userContext = ApplicationUserContextSV;
                ITaxon       taxon       = CoreData.TaxonManager.GetTaxon(userContext, 6008300);
                //Act
                var parents = taxon.GetAllParentTaxonRelations(userContext, null, false, false);

                //Assert
                Assert.IsNotNull(parents);
            }
        }
Ejemplo n.º 4
0
        //private static IPerson GetCreatedByPerson(IUserContext userContext, int createdBy)
        //{
        //    IPerson person;
        //    IUser user;

        //    try
        //    {
        //        user = CoreData.UserManager.GetUser(userContext, createdBy);
        //    }
        //    catch (Exception)
        //    {
        //        // user does not exist in UserAdmin system
        //        user = null;
        //    }

        //    if ((user.IsNotNull()) &&
        //        (user.Type == UserType.Person) &&
        //        (user.PersonId.HasValue))
        //    {
        //        person = CoreData.UserManager.GetPerson(userContext, user.PersonId.Value);
        //    }
        //    else
        //    {
        //        person = null;
        //    }
        //    return person;
        //}

        /// <summary>
        /// The create.
        /// </summary>
        /// <param name="userContext">
        /// The user context.
        /// </param>
        /// <param name="taxon">
        /// The taxon.
        /// </param>
        /// <param name="revisionId">
        /// The revision id.
        /// </param>
        /// <returns>
        /// The <see cref="TaxonSummaryViewModel"/>.
        /// </returns>
        public static TaxonSummaryViewModel Create(IUserContext userContext, ITaxon taxon, int?revisionId)
        {
            var     model = new TaxonSummaryViewModel();
            IPerson person;
            bool    isInRevision      = DyntaxaHelper.IsInRevision(userContext, revisionId);
            bool    isUserTaxonEditor = userContext.IsTaxonEditor();

            model.Id                = taxon.Id.ToString();
            model.Guid              = taxon.Guid ?? string.Empty;
            model.CategoryId        = taxon.Category != null ? taxon.Category.Id : 0;
            model.Category          = taxon.Category != null ? taxon.Category.Name : string.Empty;
            model.IsMicrospecies    = taxon.IsMicrospecies;
            model.CategorySortOrder = taxon.Category != null ? taxon.Category.SortOrder : 0;
            model.ConceptDefinition = taxon.GetConceptDefinition(userContext) ?? "-";
            model.AlertStatus       = (TaxonAlertStatusId)taxon.AlertStatus.Id;
            model.AlertImageUrl     = GetAlertImageUrl(model.AlertStatus);
            person = taxon.GetModifiedByPerson(userContext);
            //IPerson createdByPerson = GetCreatedByPerson(userContext, taxon.CreatedBy);
            //string createdFullName = createdByPerson.FullName;
            if (person.IsNull())
            {
                model.UpdateInformation = string.Format("{0} ({1})", taxon.ModifiedDate.ToShortDateString(), String.Empty);
            }
            else
            {
                model.UpdateInformation = string.Format("{0} ({1})", taxon.ModifiedDate.ToShortDateString(), person.FullName);
            }

            model.ValidToInformation = string.Format("{0} ({1})", taxon.ValidToDate.ToShortDateString(), taxon.ModifiedByPerson);
            model.CreatedInformation = string.Format("{0} ({1})", taxon.CreatedDate.ToShortDateString(), taxon.ModifiedByPerson);
            model.Validity           = GetValidityDescription(taxon);

            if (taxon.ScientificName.IsNotEmpty())
            {
                model.ScientificName = new TaxonNameAuthorViewModel(taxon.ScientificName, taxon.Author);
            }

            if (taxon.CommonName.IsNotEmpty())
            {
                model.CommonName = new TaxonNameAuthorViewModel(taxon.CommonName, string.Empty);
            }

            // Synonyms
            //model.Synonyms = new List<TaxonNameViewModel>();
            //var synonyms = taxon.GetSynonyms(userContext, true);
            //if (synonyms != null)
            //{
            //    foreach (ITaxonName taxonName in synonyms)
            //    {
            //        model.Synonyms.Add(new TaxonNameViewModel(taxonName, taxon));
            //    }
            //}

            model.Synonyms         = taxon.GetSynonymsViewModel(isInRevision, isUserTaxonEditor, false);
            model.ProParteSynonyms = taxon.GetProParteSynonymsViewModel(isInRevision, isUserTaxonEditor);
            model.MisappliedNames  = taxon.GetMisappliedNamesViewModel(isInRevision, isUserTaxonEditor);

            // Other valid common names
            // todo - change implementation?
            //model.OtherValidCommonNames = new List<string>();
            //if (!taxon.CommonName.IsEmpty())
            //{
            //    model.OtherValidCommonNames.AddRange(
            //        from taxonName in taxon.GetTaxonNames(userContext)
            //        where
            //            taxonName.Category.Id == (int)TaxonNameCategoryId.SwedishName &&
            //            taxonName.Version != taxon.GetCommonName(userContext).Version
            //        select taxonName.Name);

            //    // todo - även ha med att namnet är gilitigt. Hur ser man det???
            //}

            model.OtherValidCommonNames = taxon.GetNotRecommendedSwedishNamesViewModel(isInRevision, isUserTaxonEditor);

            // Remove other valid common names from synonyms
            if (model.OtherValidCommonNames.IsNotEmpty())
            {
                List <TaxonNameViewModel> newSynonymList = new List <TaxonNameViewModel>();

                foreach (TaxonNameViewModel synonym in model.Synonyms)
                {
                    if (model.OtherValidCommonNames.All(x => x.Id != synonym.Id))
                    {
                        newSynonymList.Add(synonym);
                    }
                }

                model.Synonyms = newSynonymList;
            }

            // Classification
            var allParentTaxa      = taxon.GetAllParentTaxonRelations(userContext, null, isInRevision, false, true);
            var distinctParentTaxa = allParentTaxa.GroupBy(x => x.ParentTaxon.Id).Select(x => x.First().ParentTaxon).ToList();

            model.Classification = new List <RelatedTaxonViewModel>();
            foreach (ITaxon relatedTaxon in distinctParentTaxa)
            {
                if (relatedTaxon.Category.IsTaxonomic)
                {
                    model.Classification.Add(new RelatedTaxonViewModel(relatedTaxon, relatedTaxon.Category, null));
                }
            }

            // Species fact
            try
            {
                // Dictionary<FactorId, SpeciesFact> dicSpeciesFacts = SpeciesFactHelper.GetSpeciesFacts(taxon, new [] {FactorId.SwedishOccurence, FactorId.SwedishHistory});
                Dictionary <FactorId, SpeciesFact> dicSpeciesFacts = SpeciesFactHelper.GetCommonDyntaxaSpeciesFacts(userContext, taxon);
                model.SwedishHistory    = SpeciesFactHelper.GetFactorValue(dicSpeciesFacts, FactorId.SwedishHistory);
                model.SwedishOccurrence = SpeciesFactHelper.GetFactorValue(dicSpeciesFacts, FactorId.SwedishOccurrence);

                // If swedish occurrence or swedish history is changed in the current revision, then show those values instead.
                if (DyntaxaHelper.IsInRevision(userContext, revisionId))
                {
                    DyntaxaInternalTaxonServiceManager internalTaxonServiceManager =
                        new DyntaxaInternalTaxonServiceManager();

                    // Check if Swedish occurrence is stored in Taxon database in this revision.
                    DyntaxaRevisionSpeciesFact swedishOccurrenceRevisionSpeciesFact =
                        internalTaxonServiceManager.GetDyntaxaRevisionSpeciesFact(
                            userContext,
                            (Int32)FactorId.SwedishOccurrence,
                            taxon.Id,
                            revisionId.Value);
                    if (swedishOccurrenceRevisionSpeciesFact != null)
                    {
                        SpeciesFactModelManager speciesFactModel = new SpeciesFactModelManager(taxon, userContext);
                        TaxonModelManager.UpdateOldSpeciesFactModelWithDyntaxaRevisionSpeciesFactValues(userContext, speciesFactModel.SwedishOccurrenceSpeciesFact, swedishOccurrenceRevisionSpeciesFact);
                        model.SwedishOccurrence = speciesFactModel.SwedishOccurrenceSpeciesFact.GetStatusOriginalLabel();
                    }

                    // Check if Swedish history is stored in Taxon database in this revision.
                    DyntaxaRevisionSpeciesFact swedishHistoryRevisionSpeciesFact =
                        internalTaxonServiceManager.GetDyntaxaRevisionSpeciesFact(
                            userContext,
                            (Int32)FactorId.SwedishHistory,
                            taxon.Id,
                            revisionId.Value);
                    if (swedishHistoryRevisionSpeciesFact != null)
                    {
                        if (swedishHistoryRevisionSpeciesFact.StatusId.HasValue)
                        {
                            SpeciesFactModelManager speciesFactModel = new SpeciesFactModelManager(taxon, userContext);
                            TaxonModelManager.UpdateOldSpeciesFactModelWithDyntaxaRevisionSpeciesFactValues(userContext, speciesFactModel.SwedishHistorySpeciesFact, swedishHistoryRevisionSpeciesFact);
                            model.SwedishHistory = speciesFactModel.SwedishHistorySpeciesFact.GetStatusOriginalLabel();
                        }
                        else // swedish history is deleted in this revision
                        {
                            model.SwedishHistory = "";
                        }
                    }
                }
            }
            catch (Exception)
            {
                // the taxon did not exist in Artfakta
            }

            return(model);
        }