public void ClearRatingTest()
        {
            _factory.SpeciesStatusRatings.ClearRating(SpeciesName, SchemeName);
            SpeciesStatusRating rating = _factory.SpeciesStatusRatings.GetCurrent(SpeciesName, SchemeName);

            Assert.IsNull(rating);
        }
        /// <summary>
        /// Delete the conservation status scheme with the specified name
        /// </summary>
        /// <param name="name"></param>
        public async Task DeleteAsync(string name)
        {
            // Get the scheme and make sure it exists
            name = _textInfo.ToTitleCase(name.CleanString());
            StatusScheme scheme = await GetAsync(c => c.Name == name);

            if (scheme == null)
            {
                string message = $"Conservation status scheme '{name}' does not exist";
                throw new StatusSchemeDoesNotExistException(message);
            }

            // Check the scheme isn't associated with any species ratings
            SpeciesStatusRating rating = await _factory.Context
                                         .SpeciesStatusRatings
                                         .Include(sr => sr.Rating)
                                         .AsAsyncEnumerable()
                                         .FirstOrDefaultAsync(sr => sr.Rating.StatusSchemeId == scheme.Id);

            if (rating != null)
            {
                string message = $"Cannot delete conservation status scheme '{name}' while it is in use";
                throw new StatusSchemeIsInUseException(message);
            }

            // Find all the ratings associated with this scheme and delete them
            IEnumerable <StatusRating> ratings = _factory.Context.StatusRatings;

            ratings = ratings.Where(r => r.StatusSchemeId == scheme.Id);
            _factory.Context.StatusRatings.RemoveRange(ratings);

            // Delete the scheme
            _factory.Context.StatusSchemes.Remove(scheme);
            await _factory.Context.SaveChangesAsync();
        }
        private void Save(IList <CsvSpeciesStatusRating> records)
        {
            int count = 0;

            foreach (CsvSpeciesStatusRating record in records)
            {
                // Map the current CSV record to a conservation status rating, check it
                // doesn't already exist and store it in the database
                SpeciesStatusRating rating   = SpeciesStatusRating.FromCsv(record);
                SpeciesStatusRating existing = _factory.Context
                                               .SpeciesStatusRatings
                                               .FirstOrDefault(r => (r.Species.Name == rating.Species.Name) &&
                                                               (r.Rating.Name == rating.Rating.Name) &&
                                                               (r.Region == rating.Region) &&
                                                               ((r.Start == rating.Start) || ((r.Start == null) && (rating.Start == null))) &&
                                                               ((r.End == rating.End) || ((r.End == null) && (rating.End == null))));
                if (existing == null)
                {
                    rating = _factory.SpeciesStatusRatings.Add(rating);
                }

                // Notify subscribers
                count++;
                RecordImport?.Invoke(this, new SpeciesStatusDataExchangeEventArgs {
                    RecordCount = count, Rating = rating
                });
            }
        }
        /// <summary>
        /// Set the conservation status rating for a species
        /// </summary>
        /// <param name="speciesName"></param>
        /// <param name="ratingName"></param>
        /// <param name="schemeName"></param>
        /// <returns></returns>
        public async Task <SpeciesStatusRating> SetRatingAsync(string speciesName, string ratingName, string schemeName)
        {
            // Tidy the parameters for direct comparison
            speciesName = _textInfo.ToTitleCase(speciesName.CleanString());
            ratingName  = _textInfo.ToTitleCase(ratingName.CleanString());
            schemeName  = _textInfo.ToTitleCase(schemeName.CleanString());

            // Check the species and rating
            Species species = await _factory.Species.GetAsync(s => s.Name == speciesName);

            if (species == null)
            {
                string message = $"Species '{speciesName}' does not exist";
                throw new SpeciesDoesNotExistException(message);
            }

            StatusRating rating = await _factory.StatusRatings.GetAsync(s => (s.Name == ratingName) && (s.Scheme.Name == schemeName));

            if (rating == null)
            {
                string message = $"Status rating '{ratingName}' does not exist on scheme '{schemeName}'";
                throw new StatusRatingDoesNotExistException(message);
            }

            // See if there's already a rating for this species on the specified scheme
            SpeciesStatusRating speciesRating = await _factory.Context
                                                .SpeciesStatusRatings
                                                .Include(r => r.Rating)
                                                .FirstOrDefaultAsync(r => (r.SpeciesId == species.Id) &&
                                                                     (r.Rating.StatusSchemeId == rating.StatusSchemeId));

            if (speciesRating != null)
            {
                // There is an existing rating on this scheme, so set the end-date for
                // that rating to midnight yesterday
                speciesRating.End = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59).AddDays(-1);

                // This could leave a rating where the start date's greater than the end date. If that's the case,
                // make them the same
                if (speciesRating.Start > speciesRating.End)
                {
                    speciesRating.Start = speciesRating.End;
                }
            }

            // Create the new rating
            SpeciesStatusRating newRating = new SpeciesStatusRating
            {
                SpeciesId      = species.Id,
                StatusRatingId = rating.Id,
                Start          = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0)
            };
            await _factory.Context.SpeciesStatusRatings.AddAsync(newRating);

            await _factory.Context.SaveChangesAsync();

            // Load and return the new rating (to load related entities)
            return(await GetAsync(r => r.Id == newRating.Id));
        }
        public async Task ClearRatingAsycnTest()
        {
            await _factory.SpeciesStatusRatings.ClearRatingAsync(SpeciesName, SchemeName);

            SpeciesStatusRating rating = _factory.SpeciesStatusRatings.GetCurrent(SpeciesName, SchemeName);

            Assert.IsNull(rating);
        }
        /// <summary>
        /// Clear the rating for a species against a scheme
        /// </summary>
        /// <param name="speciesName"></param>
        /// <param name="schemeName"></param>
        public async Task ClearRatingAsync(string speciesName, string schemeName)
        {
            SpeciesStatusRating rating = await GetCurrentAsync(speciesName, schemeName);

            if (rating != null)
            {
                rating.End = DateTime.Now;
                await _factory.Context.SaveChangesAsync();
            }
        }
        /// <summary>
        /// Clear the rating for a species against a scheme
        /// </summary>
        /// <param name="speciesName"></param>
        /// <param name="schemeName"></param>
        public void ClearRating(string speciesName, string schemeName)
        {
            SpeciesStatusRating rating = GetCurrent(speciesName, schemeName);

            if (rating != null)
            {
                rating.End = DateTime.Now;
                _factory.Context.SaveChanges();
            }
        }
Exemple #8
0
 private void PrintRow(SpeciesStatusRating rating, StreamWriter output)
 {
     PrintRow(rating.Species.Name,
              rating.Rating.Scheme.Name,
              rating.Rating.Name,
              GetDateCellValue(rating.Start),
              GetDateCellValue(rating.End),
              false,
              output);
 }
Exemple #9
0
        /// <summary>
        /// Confirm the presence of a BOCC4 rating for the White-Fronted Goose
        /// in imported data
        /// </summary>
        /// <param name="sightings"></param>
        public static void ConfirmWhiteFrontedGooseRating(IEnumerable <SpeciesStatusRating> ratings)
        {
            SpeciesStatusRating rating = ratings.OrderBy(r => r.Start)
                                         .First(s => s.Species.Name == "White-Fronted Goose");

            Assert.AreEqual("Birds", rating.Species.Category.Name);
            Assert.AreEqual("BOCC4", rating.Rating.Scheme.Name);
            Assert.AreEqual("Amber", rating.Rating.Name);
            Assert.AreEqual("United Kingdom", rating.Region);
            Assert.AreEqual(new DateTime(2015, 12, 1, 0, 0, 0), rating.Start);
            Assert.AreEqual(new DateTime(2017, 12, 31, 0, 0, 0), rating.End);
        }
        public void GetCurrentRatingTest()
        {
            SpeciesStatusRating entity = _factory.SpeciesStatusRatings.GetCurrent(SpeciesName, SchemeName);

            Assert.IsNotNull(entity);
            Assert.IsTrue(entity.Id > 0);
            Assert.AreEqual(SpeciesName, entity.Species.Name);
            Assert.AreEqual(RatingName, entity.Rating.Name);
            Assert.AreEqual(SchemeName, entity.Rating.Scheme.Name);

            DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);

            Assert.AreEqual(startDate, entity.Start);
            Assert.IsNull(entity.End);
        }
        /// <summary>
        /// Add a new conservation status rating based on the supplied template
        /// </summary>
        /// <param name="template"></param>
        /// <returns></returns>
        public SpeciesStatusRating Add(SpeciesStatusRating template)
        {
            // Retrieve/create the category andspecies. The logic to create new
            // records or return existing ones is in these methods
            Species species = _factory.Species.Add(template.Species.Name, template.Species.Category.Name);

            // If the scheme doesn't exist, create it
            string       schemeName = _textInfo.ToTitleCase(template.Rating.Scheme.Name.CleanString());
            StatusScheme scheme     = _factory.StatusSchemes.Get(s => s.Name == schemeName);

            if (scheme == null)
            {
                scheme = _factory.StatusSchemes.Add(schemeName);
            }

            // If the rating doesn't exist, create it
            string       ratingName = _textInfo.ToTitleCase(template.Rating.Name.CleanString());
            StatusRating rating     = _factory.StatusRatings.Get(r => (r.Name == ratingName) && (r.StatusSchemeId == scheme.Id));

            if (rating == null)
            {
                rating = _factory.StatusRatings.Add(ratingName, schemeName);
            }

            // Add a new species status rating. Note that we DON'T use the SetRating
            // method as it manipulates prior records and we don't want to do that in
            // this context (import of rating data)
            SpeciesStatusRating newRating = new SpeciesStatusRating
            {
                SpeciesId      = species.Id,
                StatusRatingId = rating.Id,
                Region         = template.Region,
                Start          = template.Start,
                End            = template.End
            };

            _factory.Context.SpeciesStatusRatings.Add(newRating);
            _factory.Context.SaveChanges();

            // Load and return the new rating (to load related entities)
            return(Get(r => r.Id == newRating.Id));
        }
        /// <summary>
        /// Delete the conservation status rating with the specified name
        /// </summary>
        /// <param name="name"></param>
        /// <param name="schemeName"></param>
        public async Task DeleteAsync(string name, string schemeName)
        {
            // Get the scheme and make sure it exists
            schemeName = _textInfo.ToTitleCase(schemeName.CleanString());
            StatusScheme scheme = await _factory.StatusSchemes.GetAsync(s => s.Name == schemeName);

            if (scheme == null)
            {
                string message = $"Status scheme '{schemeName}' does not exist";
                throw new StatusSchemeDoesNotExistException();
            }

            // Get the rating and make sure it exists
            name = _textInfo.ToTitleCase(name.CleanString());
            StatusRating rating = await GetAsync(r => r.Name == name);

            if (rating == null)
            {
                string message = $"Status rating '{name}' does not exist on scheme '{scheme.Name}'";
                throw new StatusRatingDoesNotExistException();
            }

            // Check the rating isn't associated with any species ratings
            SpeciesStatusRating speciesRatings = await _factory.Context
                                                 .SpeciesStatusRatings
                                                 .AsAsyncEnumerable()
                                                 .FirstOrDefaultAsync(sr => sr.StatusRatingId == rating.Id);

            if (speciesRatings != null)
            {
                string message = $"Cannot delete conservation status rating '{name}' on scheme '{scheme.Name}' while it is in use";
                throw new StatusRatingIsInUseException(message);
            }

            // Delete the rating
            _factory.Context.StatusRatings.Remove(rating);
            await _factory.Context.SaveChangesAsync();
        }