public override void CreatePersonSite(string personId, string siteId, bool?isAdministrator, bool?isManager,
                                              bool?isAssigned, bool isDefault)
        {
            // Create a new record in the PersonSite table.
            using (var transaction = new TransactionScope(_configuration))
            {
                var psDS = new PersonSiteDataStore(transaction);
                var ps   = new PersonSite();
                var pDS  = new PersonDataStore(transaction);
                ps.Person = pDS.FindByKey(personId);
                var sDS = new SiteDataStore(transaction);
                ps.Site = sDS.FindByKey(siteId);
                if (isAdministrator != null)
                {
                    ps.IsAdministrator = (bool)isAdministrator;
                }
                if (isManager != null)
                {
                    ps.IsManager = (bool)isManager;
                }
                if (isAssigned != null)
                {
                    ps.IsAssigned = (bool)isAssigned;
                }
                ps.IsDefault = isDefault;
                psDS.Insert(ps);

                transaction.Commit();
            }
        }
        private IList <Site> GetSitesByPerson(Person person, bool isAdmin, bool isManager)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                IList <Site>       runningList = new List <Site>();
                var                ppDS        = new PersonSiteDataStore(transaction);
                IList <PersonSite> pps         = ppDS.FindSitesByPerson(person.Id, false);
                foreach (PersonSite ppt in pps)
                {
                    if (isAdmin)
                    {
                        if (!ppt.IsAdministrator)
                        {
                            continue;
                        }
                    }
                    if (isManager)
                    {
                        if (!ppt.IsManager)
                        {
                            continue;
                        }
                    }

                    IEnumerable <Site> q = from x in runningList where x.Id.Equals(ppt.Site.Id) select x;
                    if (q.Count() == 0)
                    {
                        runningList.Add(ppt.Site);
                    }
                }
                return(runningList);
            }
        }
        public override IList <Person> GetAllPersonsInRegion(Region region, bool recursive)
        {
            IList <Person> persons = new List <Person>();

            using (var transaction = new TransactionScope(_configuration))
            {
                // Find all the sites for this region, and sub regions.
                IList <Site> sites = SiteManager.GetAllSitesByRegion(region, recursive);
                foreach (Site site in sites)
                {
                    var ds = new PersonSiteDataStore(transaction);
                    IList <PersonSite> personSites = ds.FindPersonsBySite(site.Id, false);
                    IList <string>     sysAdmins   = GetAdminIds();
                    foreach (PersonSite personSite in personSites)
                    {
                        // Only consider if this person is assigned to this site.
                        if (personSite.IsAssigned)
                        {
                            if (persons.FirstOrDefault(x => x.Id.Equals(personSite.Person.Id)) == null)
                            {
                                if (!sysAdmins.Contains(personSite.Person.Id))
                                {
                                    persons.Add(personSite.Person);
                                }
                            }
                        }
                    }
                }
            }
            return(persons);
        }
 public override PersonSite GetPersonSiteByPersonAndSite(Person person, Site site, bool isAssigned)
 {
     using (var transaction = new TransactionScope(_configuration))
     {
         var ds = new PersonSiteDataStore(transaction);
         return(ds.FindByPersonAndSite(person.Id, site.Id, isAssigned).FirstOrDefault());
     }
 }
 public override void UpdatePersonSite(PersonSite personSite)
 {
     using (var transaction = new TransactionScope(_configuration))
     {
         var prDS = new PersonSiteDataStore(transaction);
         prDS.Update(personSite);
         transaction.Commit();
     }
 }
        public override IList <Person> GetPersonsByRegionName(string regionName, bool recursive)
        {
            IList <Region> regions = SiteManager.GetAllRegionsByName(regionName);

            using (var transaction = new TransactionScope(_configuration))
            {
                IList <Person> persons = new List <Person>();
                foreach (Region region in regions)
                {
                    IList <Site> sites = SiteManager.GetAllSitesByRegion(region, recursive);

                    // Find all persons at these sites.
                    var psDS = new PersonSiteDataStore(transaction);
                    foreach (Site site in sites)
                    {
                        IList <PersonSite> personAtSites = psDS.FindPersonsBySite(site.Id, false);
                        if (persons == null)
                        {
                            IEnumerable <Person> ppl =
                                from x in personAtSites
                                select x.Person;

                            persons = ppl.ToList <Person>();
                        }
                        else
                        {
                            foreach (PersonSite personSite in personAtSites)
                            {
                                persons.Add(personSite.Person);
                            }
                        }
                    }

                    if (recursive)
                    {
                        // Find all child regions.
                        var            rDS          = new RegionDataStore(transaction);
                        IList <Region> childRegions = rDS.FindByParentRegion(region.Id);
                        foreach (Region subRegion in childRegions)
                        {
                            IList <Person> subPersons = GetPersonsByRegionName(subRegion.Name, recursive);
                            foreach (Person subPerson in subPersons)
                            {
                                persons.Add(subPerson);
                            }
                        }
                    }
                }
                return(persons);
            }
        }
        public override void UpdatePersonSite(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 ds = new PersonSiteDataStore(transaction);
                IList <PersonSite> prs = ds.FindSitesByPerson(personId, false);

                foreach (PersonSite 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)
                        {
                            ds.Delete(pr.Id);
                        }
                        else
                        {
                            ds.Update(pr);
                        }
                    }
                    else if (!isManager.HasValue)
                    {
                        // Only modify the manager values
                        pr.IsAdministrator = (bool)isAdministrator;

                        if (!pr.IsAdministrator && !pr.IsManager)
                        {
                            ds.Delete(pr.Id);
                        }
                        else
                        {
                            ds.Update(pr);
                        }
                    }
                }
                transaction.Commit();
            }
        }
        public override void DeletePersonFromSite(string personId, string siteId)
        {
            // Delete a record in the PersonSite table.
            using (var transaction = new TransactionScope(_configuration))
            {
                var psDS = new PersonSiteDataStore(transaction);
                IList <PersonSite> pss = psDS.FindByPersonAndSite(personId, siteId, false);

                foreach (PersonSite item in pss)
                {
                    item.Deleted = true;
                    psDS.Update(item);
                }

                transaction.Commit();
            }
        }
        public override bool IsPersonInPersonSite(string personId)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var ds = new PersonSiteDataStore(transaction);
                IList <PersonSite> peoples = ds.FindSitesByPerson(personId, false);

                if (peoples.Count > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
 public override Site GetDefaultSiteByPerson(Person person)
 {
     using (TransactionScope transaction = new TransactionScope(_configuration))
     {
         PersonSiteDataStore ppDS        = new PersonSiteDataStore(transaction);
         IList <PersonSite>  pps         = ppDS.FindSitesByPerson(person.Id, true);
         PersonSite          defaultSite = pps.FirstOrDefault(x => x.IsDefault);
         if (defaultSite != null)
         {
             return(defaultSite.Site);
         }
         else
         {
             return(null);
         }
     }
 }
        public override bool IsPersonInPersonSite(Person person, Site site)
        {
            using (var transaction = new TransactionScope(_configuration))
            {
                var ds = new PersonSiteDataStore(transaction);
                IList <PersonSite> peoples = ds.FindSitesByPerson(person.Id, false);

                IEnumerable <PersonSite> q = from x in peoples where x.Site.Id.Equals(site.Id) select x;

                if (q.Count() > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        public override IList <Person> GetAllPersonsInSite(Site site)
        {
            IList <Person> persons = new List <Person>();

            using (var transaction = new TransactionScope(_configuration))
            {
                // Find all the sites for this region, and sub regions.
                var ds = new PersonSiteDataStore(transaction);
                IList <PersonSite> personSites = ds.FindPersonsBySite(site.Id, false);
                IList <string>     sysAdmins   = GetAdminIds();
                foreach (PersonSite personSite in personSites)
                {
                    if (persons.FirstOrDefault(x => x.Id.Equals(personSite.Person.Id)) == null)
                    {
                        if (!sysAdmins.Contains(personSite.Person.Id))
                        {
                            persons.Add(personSite.Person);
                        }
                    }
                }
            }
            return(persons);
        }
        public override IList <Person> GetPersonsByRegionType(RegionType regionType)
        {
            IList <Region> regions = SiteManager.GetAllRegionsByRegionType(regionType);

            using (var transaction = new TransactionScope(_configuration))
            {
                IList <Person> persons = new List <Person>();
                foreach (Region region in regions)
                {
                    IList <Site> sites = SiteManager.GetAllSitesByRegion(region, false);

                    // Find all persons at these sites.
                    var psDS = new PersonSiteDataStore(transaction);
                    foreach (Site site in sites)
                    {
                        IList <PersonSite> personAtSites = psDS.FindPersonsBySite(site.Id, false);
                        if (persons == null)
                        {
                            IEnumerable <Person> ppl =
                                from x in personAtSites
                                select x.Person;

                            persons = ppl.ToList <Person>();
                        }
                        else
                        {
                            foreach (PersonSite personSite in personAtSites)
                            {
                                persons.Add(personSite.Person);
                            }
                        }
                    }
                }
                return(persons);
            }
        }