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

            SpeciesFactList filteredList = new SpeciesFactList();

            if (this.IsNotEmpty())
            {
                foreach (ISpeciesFact fact in this)
                {
                    foreach (IPeriod period in periods)
                    {
                        if (fact.Period.Id == period.Id)
                        {
                            filteredList.Add(fact);
                            break;
                        }
                    }
                }
            }

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

            SpeciesFactList filteredList = new SpeciesFactList();

            if (this.IsNotEmpty())
            {
                foreach (ISpeciesFact fact in this)
                {
                    foreach (IFactor factor in factors)
                    {
                        if (fact.Factor.Id == factor.Id)
                        {
                            filteredList.Add(fact);
                            break;
                        }
                    }
                }
            }

            return(filteredList);
        }
        /// <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>
        /// 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);
        }
        /// <summary>
        /// Method that returns a filtered version of this species fact list.
        /// The filtering is done on individual categories.
        /// </summary>
        /// <param name="individualCategories">Individual categories for the requested species facts.</param>
        /// <returns>A filtered species fact list.</returns>
        public SpeciesFactList GetSpeciesFacts(IndividualCategoryList individualCategories)
        {
            if (individualCategories == null)
            {
                throw new ArgumentException("CategoryList is null", "individualCategories");
            }

            SpeciesFactList filteredList = new SpeciesFactList();

            if (this.IsNotEmpty())
            {
                foreach (ISpeciesFact fact in this)
                {
                    foreach (IIndividualCategory category in individualCategories)
                    {
                        if (fact.IndividualCategory.Id == category.Id)
                        {
                            filteredList.Add(fact);
                            break;
                        }
                    }
                }
            }

            return(filteredList);
        }
 /// <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>
 /// Init calculation of red list criteria.
 /// </summary>
 /// <param name="userContext">
 /// Information about the user that makes this method call.
 /// </param>
 /// <param name="speciesFacts">Species facts to get data from.</param>
 public override void Init(IUserContext userContext,
                           SpeciesFactList speciesFacts)
 {
     if (AllowAutomaticUpdate && !IsInitialized)
     {
         RedListCalculator = new RedListCalculator(userContext,
                                                   Taxon.Category.Name,
                                                   Taxon.Category.NameDefinite);
         RedListCalculator.IsCriteriaCalculated = false;
         base.Init(userContext, speciesFacts);
         SetReadListValues();
     }
 }
        /// <summary>
        /// Get a subset of this species fact list based on parameters.
        /// </summary>
        /// <param name="individualCategory">The individual category for the requested species facts.</param>
        /// <returns>A species fact list.</returns>
        public SpeciesFactList GetSpeciesFacts(IIndividualCategory individualCategory)
        {
            SpeciesFactList speciesFacts = new SpeciesFactList();
            var             subset       = from SpeciesFact speciesFact in this
                                           where speciesFact.IndividualCategory.Id == individualCategory.Id
                                           select speciesFact;

            if (subset.IsNotNull() && subset.Any())
            {
                speciesFacts.AddRange(subset.ToArray());
            }

            return(speciesFacts);
        }
        /// <summary>
        /// Get a subset of this species fact list by quality.
        /// </summary>
        /// <param name="quality">The quality for the requested species facts.</param>
        /// <returns>A species fact list.</returns>
        public SpeciesFactList GetSpeciesFacts(ISpeciesFactQuality quality)
        {
            SpeciesFactList speciesFacts = new SpeciesFactList();
            var             subset       = from SpeciesFact speciesFact in this
                                           where speciesFact.Quality == quality
                                           select speciesFact;

            if (subset.IsNotNull() && subset.Any())
            {
                speciesFacts.AddRange(subset.ToArray());
            }

            return(speciesFacts);
        }
        /// <summary>
        /// Get a subset of this species fact list based on parameters.
        /// </summary>
        /// <param name="period">The period for the requested species facts.</param>
        /// <returns>A species fact list.</returns>
        public SpeciesFactList GetSpeciesFacts(IPeriod period)
        {
            SpeciesFactList speciesFacts = new SpeciesFactList();
            var             subset       = from SpeciesFact speciesFact in this
                                           where speciesFact.Period == period
                                           select speciesFact;

            if (subset.IsNotNull() && subset.Any())
            {
                speciesFacts.AddRange(subset.ToArray());
            }

            return(speciesFacts);
        }
        /// <summary>
        /// Get a subset of this species fact list based on parameters.
        /// </summary>
        /// <param name="factor">The factor for the requested species facts.</param>
        /// <returns>A species fact list.</returns>
        public SpeciesFactList GetSpeciesFacts(IFactor factor)
        {
            SpeciesFactList speciesFacts = new SpeciesFactList();
            var             subset       = from SpeciesFact speciesFact in this
                                           where speciesFact.Factor.Id == factor.Id
                                           orderby speciesFact.Taxon.SortOrder ascending
                                           select speciesFact;

            if (subset.IsNotNull() && subset.Any())
            {
                speciesFacts.AddRange(subset.ToArray());
            }

            return(speciesFacts);
        }
Пример #12
0
        /// <summary>
        /// Update species facts in RAM memory
        /// with latest information from data source.
        /// This method works on species facts both with and without id.
        /// </summary>
        /// <param name="userContext">
        /// Information about the user that makes this method call.
        /// </param>
        /// <param name="speciesFacts">Species facts to update.</param>
        public virtual void UpdateSpeciesFacts(IUserContext userContext,
                                               SpeciesFactList speciesFacts)
        {
            SpeciesFactList speciesFactsFromDataSource,
                            updatedSpeciesFacts;

            // Get updated data from web service.
            speciesFactsFromDataSource = GetSpeciesFacts(userContext, speciesFacts);

            // Update species facts.
            updatedSpeciesFacts = new SpeciesFactList();
            if (speciesFactsFromDataSource.IsNotEmpty())
            {
                foreach (ISpeciesFact speciesFactFromDataSource in speciesFactsFromDataSource)
                {
                    // Get species fact.
                    ISpeciesFact speciesFact = speciesFacts.Get(speciesFactFromDataSource.Identifier);

                    // Update species fact.
                    speciesFact.Update(speciesFactFromDataSource.Id,
                                       speciesFactFromDataSource.Reference,
                                       speciesFactFromDataSource.ModifiedDate,
                                       speciesFactFromDataSource.ModifiedBy,
                                       speciesFactFromDataSource.HasField1,
                                       speciesFactFromDataSource.HasField1 ? speciesFactFromDataSource.Field1.GetDoubleValue() : 0,
                                       speciesFactFromDataSource.HasField2,
                                       speciesFactFromDataSource.HasField2 ? speciesFactFromDataSource.Field2.GetDoubleValue() : 0,
                                       speciesFactFromDataSource.HasField3,
                                       speciesFactFromDataSource.HasField3 ? speciesFactFromDataSource.Field3.GetDoubleValue() : 0,
                                       speciesFactFromDataSource.HasField4,
                                       speciesFactFromDataSource.HasField4 ? speciesFactFromDataSource.Field4.GetStringValue() : null,
                                       speciesFactFromDataSource.HasField5,
                                       speciesFactFromDataSource.HasField5 ? speciesFactFromDataSource.Field5.GetStringValue() : null,
                                       speciesFactFromDataSource.Quality);
                    updatedSpeciesFacts.Add(speciesFact);
                }
            }

            // Update species facts that has been deleted.
            foreach (ISpeciesFact speciesFact in speciesFacts)
            {
                if (speciesFact.HasId &&
                    !updatedSpeciesFacts.Exists(speciesFact.Identifier))
                {
                    speciesFact.Reset(userContext);
                }
            }
        }
        /// <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 a subset of this species fact list based on parameters.
        /// </summary>
        /// <param name="taxon">The taxon for the requested species facts.</param>
        /// <param name="individualCategory">The individual category for the requested species facts.</param>
        /// <param name="factor">The factor for the requested species facts.</param>
        /// <param name="period">The period for the requested species facts.</param>
        /// <returns>A species fact list.</returns>
        public SpeciesFactList GetSpeciesFacts(ITaxon taxon,
                                               IIndividualCategory individualCategory,
                                               IFactor factor,
                                               IPeriod period)
        {
            SpeciesFactList speciesFacts = new SpeciesFactList();
            var             subset       = from SpeciesFact speciesFact in this
                                           where speciesFact.Taxon.Id == taxon.Id &&
                                           speciesFact.IndividualCategory.Id == individualCategory.Id &&
                                           speciesFact.Factor.Id == factor.Id &&
                                           (speciesFact.Period.IsNull() || (speciesFact.Period.Id == period.Id))
                                           select speciesFact;

            if (subset.IsNotNull() && subset.Any())
            {
                speciesFacts.AddRange(subset.ToArray());
            }

            return(speciesFacts);
        }
        /// <summary>
        /// Merge old and new species facts to same list.
        /// </summary>
        /// <param name="speciesFacts">New species facts.</param>
        private void MergeSpeciesFacts(SpeciesFactList speciesFacts)
        {
            if (speciesFacts.IsEmpty())
            {
                return;
            }

            if (SpeciesFacts.IsEmpty())
            {
                SpeciesFacts = speciesFacts;
                return;
            }

            foreach (ISpeciesFact speciesFact in speciesFacts)
            {
                if (!SpeciesFacts.Exists(speciesFact.Identifier))
                {
                    // Add new species fact to old species facts.
                    SpeciesFacts.Add(speciesFact);
                }
            }
        }
Пример #16
0
        /// <summary>
        /// Creates a dictionary where the species facts is grouped by TaxonId and then FactorId.
        /// </summary>
        /// <param name="speciesFactList">The species fact list.</param>
        /// <returns>A dictionary where the species facts is grouped by TaxonId and then FactorId.</returns>
        public static Dictionary <int, Dictionary <FactorId, ISpeciesFact> > ToDictionaryGroupedByTaxonIdThenFactorId(this SpeciesFactList speciesFactList)
        {
            Dictionary <int, Dictionary <FactorId, ISpeciesFact> > speciesFactDictionary = new Dictionary <int, Dictionary <FactorId, ISpeciesFact> >();

            foreach (ISpeciesFact speciesFact in speciesFactList)
            {
                if (!speciesFactDictionary.ContainsKey(speciesFact.Taxon.Id))
                {
                    speciesFactDictionary.Add(speciesFact.Taxon.Id, new Dictionary <FactorId, ISpeciesFact>());
                }

                if (!speciesFactDictionary[speciesFact.Taxon.Id].ContainsKey((FactorId)speciesFact.Factor.Id))
                {
                    speciesFactDictionary[speciesFact.Taxon.Id].Add((FactorId)speciesFact.Factor.Id, speciesFact);
                }
            }

            return(speciesFactDictionary);
        }
Пример #17
0
        /// <summary>
        /// Update values for species facts.
        /// This method updates information in data source but it
        /// does not update the species fact objects in the client.
        /// </summary>
        /// <param name="userContext">
        /// Information about the user that makes this method call.
        /// </param>
        /// <param name="speciesFacts">Species facts to set.</param>
        /// <param name="defaultReference">Reference used if no reference is specified.</param>
        public virtual void UpdateSpeciesFacts(IUserContext userContext,
                                               SpeciesFactList speciesFacts,
                                               IReference defaultReference)
        {
            SpeciesFactList changedSpeciesFacts, createSpeciesFacts, deleteSpeciesFacts, updateSpeciesFacts;

            // Check arguments.
            speciesFacts.CheckNotNull("speciesFact");

            // Get all species facts that should be updated.
            changedSpeciesFacts = new SpeciesFactList();
            foreach (ISpeciesFact speciesFact in speciesFacts)
            {
                if (speciesFact.AllowUpdate && speciesFact.HasChanged)
                {
                    changedSpeciesFacts.Add(speciesFact);
                }
            }

            if (changedSpeciesFacts.IsEmpty())
            {
                // Nothing to update.
                return;
            }

            // Split species facts that should be created, updated
            // or deleted into three different lists.
            createSpeciesFacts = new SpeciesFactList();
            deleteSpeciesFacts = new SpeciesFactList();
            updateSpeciesFacts = new SpeciesFactList();
            foreach (ISpeciesFact speciesFact in changedSpeciesFacts)
            {
                if (!speciesFact.HasId && speciesFact.ShouldBeSaved && !speciesFact.ShouldBeDeleted)
                {
                    // Create new species fact.
                    createSpeciesFacts.Add(speciesFact);
                    continue;
                }

                if (speciesFact.HasId && speciesFact.ShouldBeDeleted)
                {
                    // Delete species fact.
                    deleteSpeciesFacts.Add(speciesFact);
                    continue;
                }

                if (speciesFact.HasId && speciesFact.ShouldBeSaved && !speciesFact.ShouldBeDeleted)
                {
                    // Update species fact.
                    updateSpeciesFacts.Add(speciesFact);
                }
            }

            if (createSpeciesFacts.IsNotEmpty())
            {
                DataSource.CreateSpeciesFacts(userContext, createSpeciesFacts, defaultReference);
            }

            if (deleteSpeciesFacts.IsNotEmpty())
            {
                DataSource.DeleteSpeciesFacts(userContext, deleteSpeciesFacts);
            }

            if (updateSpeciesFacts.IsNotEmpty())
            {
                DataSource.UpdateSpeciesFacts(userContext, updateSpeciesFacts);
            }
        }
Пример #18
0
 /// <summary>
 /// Get species facts with specified identifiers.
 /// Only existing species facts are returned,
 /// e.g. species fact identifiers that does not
 /// match existing species fact does not affect
 /// the returned species facts.
 /// </summary>
 /// <param name="userContext">
 /// Information about the user that makes this method call.
 /// </param>
 /// <param name="speciesFactIdentifiers">
 /// Species facts identifiers. E.g. ISpeciesFact
 /// instances where id for requested combination of
 /// factor, host, individual category, period and taxon
 /// has been set.
 /// Host id is only used together with taxonomic factors.
 /// Period id is only used together with periodic factors.
 /// </param>
 /// <returns>
 /// Existing species facts among the
 /// requested species facts.
 /// </returns>
 public virtual SpeciesFactList GetSpeciesFacts(IUserContext userContext,
                                                SpeciesFactList speciesFactIdentifiers)
 {
     return(DataSource.GetSpeciesFacts(userContext, speciesFactIdentifiers));
 }
        /// <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();
        }
        /// <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);
        }