private IList <Region> GetRegionsByPerson(Person person, bool isAdmin, bool isManager, bool recursive)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var runningList          = new List <Region>();
                var ppDS                 = new PersonRegionDataStore(transaction);
                IList <PersonRegion> pps = ppDS.FindRegionsByPerson(person.Id);
                foreach (PersonRegion ppt in pps)
                {
                    if (isAdmin)
                    {
                        if (!ppt.IsAdministrator)
                        {
                            continue;
                        }
                    }
                    if (isManager)
                    {
                        if (!ppt.IsManager)
                        {
                            continue;
                        }
                    }

                    IEnumerable <Region> q = from x in runningList where x.Id.Equals(ppt.Region.Id) select x;
                    if (q.Count() == 0)
                    {
                        runningList.Add(ppt.Region);
                    }
                }
                if (recursive)
                {
                    // Find the sub regions, and add them to the runningList.
                    var toConcat = new List <Region>();
                    foreach (Region region in runningList)
                    {
                        foreach (Region childRegion in SiteManager.GetAllRegionsBelow(region))
                        {
                            if (runningList.FirstOrDefault(x => x.Id.Equals(childRegion.Id)) == null)
                            {
                                toConcat.Add(childRegion);
                            }
                        }
                    }
                    runningList.AddRange(toConcat);
                }
                runningList.Sort(new KeyComparer <Region>(x => x.Breadcrumb));
                return(runningList);
            }
        }
        /// <summary>
        /// Modifies the PersonRegion record.
        /// If one of personId or regionId are null, then it is implied that all records for the not null item are to be updated.
        /// If one of isAdministrator or isManager are null, then it is implied that the not null item is to be updated.
        /// If a record is updated so that both isAdministrator and isManager are false, then that record is deleted.
        /// </summary>
        /// <param name="personId"></param>
        /// <param name="regionId"></param>
        /// <param name="isAdministrator"></param>
        /// <param name="isManager"></param>
        public override void UpdatePersonRegion(string personId, bool?isAdministrator, bool?isManager)
        {
            if (isAdministrator == null && isManager == null)
            {
                throw new ArgumentNullException("isAdministrator", "Both isAdministrator and isManager cannot be null");
            }

            using (var transaction = new TransactionScope(_configuration))
            {
                var prDS = new PersonRegionDataStore(transaction);
                IList <PersonRegion> prs = prDS.FindRegionsByPerson(personId);

                foreach (PersonRegion pr in prs)
                {
                    // A value of null for isAdministrator and isManager is equivalent to false.
                    if (!isAdministrator.HasValue)
                    {
                        // Only modify the manager values
                        pr.IsManager = (bool)isManager;

                        if (!pr.IsAdministrator && !pr.IsManager)
                        {
                            prDS.Delete(pr.Id);
                        }
                        else
                        {
                            prDS.Update(pr);
                        }
                    }
                    else if (!isManager.HasValue)
                    {
                        // Only modify the manager values
                        pr.IsAdministrator = (bool)isAdministrator;

                        if (!pr.IsAdministrator && !pr.IsManager)
                        {
                            prDS.Delete(pr.Id);
                        }
                        else
                        {
                            prDS.Update(pr);
                        }
                    }
                }
                transaction.Commit();
            }
        }
        public override bool IsPersonInPersonRegion(string personId)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var ds = new PersonRegionDataStore(transaction);
                IList <PersonRegion> peoples = ds.FindRegionsByPerson(personId);

                if (peoples.Count > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }