public ClubCloud_Functionaris SelectFunctionaris([QueryString] Guid?Id)  //(string Id)
        {
            ClubCloud_Functionaris entity = null;

            using (new SPMonitoredScope("Functionarissen SelectFunctionaris"))
            {
                if (SPContext.Current.Web.CurrentUser != null)
                {
                    int bondsnummer;
                    ClubCloud_Setting Settings = null;
                    if (int.TryParse(SPContext.Current.Web.CurrentUser.UserId.NameId, out bondsnummer))
                    {
                        Settings = Client.GetSettingById(bondsnummer);
                    }

                    if (Settings != null && Settings.VerenigingId != null)
                    {
                        Settings.ClubCloud_Vereniging = Client.GetVerenigingById(Settings.VerenigingId.Value, false, Settings);

                        if (Settings.ClubCloud_Vereniging != null)
                        {
                            if (Id == null)
                            {
                                Guid queryId = Guid.Empty;
                                foreach (Parameter where in WhereParameters)
                                {
                                    if (where.Name == "Id")
                                    {
                                        if (Guid.TryParse(where.DefaultValue, out queryId))
                                        {
                                            break;
                                        }
                                    }
                                }

                                Id = queryId;
                            }

                            if (Id == Guid.Empty)
                            {
                            }

                            entity = Client.GetFunctionarisById(Id.Value, false, Settings);

                            if (entity != null || entity.Id != Guid.Empty)
                            {
                                entity.ClubCloud_Functie        = Client.GetFunctieForFunctionarisById(Id.Value, false, Settings);
                                entity.ClubCloud_Vereniging     = Client.GetVerenigingForFunctionarisById(Id.Value, false, Settings);
                                entity.ClubCloud_Gebruiker      = Client.GetGebruikerForFunctionarisById(Id.Value, false, Settings);
                                entity.ClubCloud_District       = Client.GetDistrictForFunctionarisById(Id.Value, false, Settings);
                                entity.ClubCloud_Bestuursorgaan = Client.GetBestuursorgaanForFunctionarisById(Id.Value, false, Settings);
                            }
                        }
                    }
                }
            }

            return(entity);
        }
        public bool DeleteFunctionaris(ClubCloud_Functionaris entity, ClubCloud_Setting settings = null)
        {
            if (settings != null)
            {
                ValidateSettings(ref settings);
            }

            return(DeleteFunctionarisById(entity.Id, settings));
        }
        public ClubCloud_District GetDistrictForFunctionarisById(System.Guid Id, bool refresh = false, ClubCloud_Setting settings = null)
        {
            if (settings != null)
            {
                ValidateSettings(ref settings);
            }

            ClubCloud_Functionaris entity = null;
            ClubCloud_District     result = null;

            using (new SPMonitoredScope("Functionarissen GetDistrictForFunctionarisById"))
            {
                try
                {
                    entity = beheerModel.ClubCloud_Functionarissen.Find(Id);
                    if (entity != null && entity.ClubCloud_District == null)
                    {
                        beheerModel.Entry(entity).Reference(e => e.ClubCloud_District).Load();
                    }

                    if (entity != null)
                    {
                        result = entity.ClubCloud_District;
                    }

                    if (result == null || refresh)
                    {
                        result = GetDistrictForFunctionarisById(settings.Id.ToString(), Id, refresh, settings);
                    }

                    if (result != null)
                    {
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(Logger.Category.Unexpected, ex.Source, ex.Message);
                }
                finally
                {
                    if (entity != null)
                    {
                        try { beheerModel.ObjectContext.Detach(entity); } catch {}
                    }
                    if (result != null)
                    {
                        try { beheerModel.ObjectContext.Detach(result); } catch {}
                    }
                }
            }

            return(new ClubCloud_District());
        }
        public ClubCloud_Bestuursorgaan GetBestuursorgaanForFunctionaris(ClubCloud_Functionaris entity, bool refresh = false, ClubCloud_Setting settings = null)
        {
            if (settings != null)
            {
                ValidateSettings(ref settings);
            }

            ClubCloud_Bestuursorgaan result = null;

            using (new SPMonitoredScope("Functionarissen GetBestuursorgaanForFunctionaris"))
            {
                try
                {
                    entity = beheerModel.ClubCloud_Functionarissen.Find(entity.Id);
                    if (entity != null && entity.ClubCloud_Bestuursorgaan == null)
                    {
                        beheerModel.Entry(entity).Reference(e => e.ClubCloud_Bestuursorgaan).Load();
                    }

                    if (entity != null)
                    {
                        result = entity.ClubCloud_Bestuursorgaan;
                    }

                    if (result == null || refresh && entity != null)
                    {
                        result = GetBestuursorgaanForFunctionaris(settings.Id.ToString(), entity, refresh, settings);
                    }

                    if (result != null)
                    {
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(Logger.Category.Unexpected, ex.Source, ex.Message);
                }
                finally
                {
                    if (entity != null)
                    {
                        try { beheerModel.ObjectContext.Detach(entity); } catch {}
                    }
                    if (result != null)
                    {
                        try { beheerModel.ObjectContext.Detach(result); } catch {}
                    }
                }
            }

            return(new ClubCloud_Bestuursorgaan());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="functie"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        private async Task <bool> CreateOrUpdateRoleFunctionaris(ClubCloud_Functionaris functionaris, ClubCloud_Setting settings = null)
        {
            try
            {
                if (settings == null)
                {
                    return(false);
                }

                if (settings != null)
                {
                    if (functionaris != null && functionaris.ClubCloud_Gebruiker == null)
                    {
                        beheerModel.Entry(functionaris).Reference(e => e.ClubCloud_Gebruiker).Load();
                    }

                    if (functionaris.ClubCloud_Gebruiker == null)
                    {
                        functionaris.ClubCloud_Gebruiker = GetGebruikerById(functionaris.GebruikerId.Value, false, settings);
                    }

                    if (functionaris != null && functionaris.ClubCloud_Functie == null)
                    {
                        beheerModel.Entry(functionaris).Reference(e => e.ClubCloud_Functie).Load();
                    }

                    if (functionaris.ClubCloud_Functie == null)
                    {
                        functionaris.ClubCloud_Functie = GetFunctieById(functionaris.FunctieId.Value, false, settings);
                    }

                    if (functionaris.Actief == ActiefSoort.Actief)
                    {
                        await ZimbraRoleProvider.AddUsersToRolesAsync(new string[] { functionaris.ClubCloud_Gebruiker.Bondsnummer }, new string[] { functionaris.ClubCloud_Functie.Naam });
                    }
                    else
                    {
                        await ZimbraRoleProvider.RemoveUsersFromRolesAsync(new string[] { functionaris.ClubCloud_Gebruiker.Bondsnummer }, new string[] { functionaris.ClubCloud_Functie.Naam });
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public bool SetGebruikerForFunctionarisById(ClubCloud_Gebruiker Gebruiker, System.Guid Id, bool refresh = false, ClubCloud_Setting settings = null)
        {
            if (settings != null)
            {
                ValidateSettings(ref settings);
            }

            ClubCloud_Functionaris entity = null;
            bool succes = false;

            using (new SPMonitoredScope("Functionarissen SetGebruikerForFunctionarisById"))
            {
                try
                {
                    entity = beheerModel.ClubCloud_Functionarissen.Find(Id);

                    if (entity != null && entity.ClubCloud_Gebruiker == null)
                    {
                        beheerModel.Entry(entity).Reference(e => e.ClubCloud_Gebruiker).Load();
                    }

                    if (entity != null)
                    {
                        entity.ClubCloud_Gebruiker = Gebruiker;
                    }

                    beheerModel.SaveChanges();
                    succes = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(Logger.Category.Unexpected, ex.Source, ex.Message);
                }
                finally
                {
                    if (entity != null)
                    {
                        try { beheerModel.ObjectContext.Detach(entity); } catch {}
                    }
                }
            }

            return(succes);
        }
        public bool DeleteFunctionaris(ClubCloud_Functionaris entity)
        {
            using (new SPMonitoredScope("Functionarissen DeleteFunctionaris"))
            {
                if (SPContext.Current.Web.CurrentUser != null)
                {
                    int bondsnummer;
                    ClubCloud_Setting Settings = null;
                    if (int.TryParse(SPContext.Current.Web.CurrentUser.UserId.NameId, out bondsnummer))
                    {
                        Settings = Client.GetSettingById(bondsnummer);
                    }

                    if (Settings != null && Settings.VerenigingId != null)
                    {
                        return(Client.DeleteFunctionaris(entity, Settings));
                    }
                }
            }
            return(false);
        }
        public ClubCloud_Functionaris GetFunctionarisById(System.Guid Id, bool refresh = false, ClubCloud_Setting settings = null)
        {
            if (settings != null)
            {
                ValidateSettings(ref settings);
            }

            ClubCloud_Functionaris result = null;

            using (new SPMonitoredScope("Functionarissen GetFunctionarisById"))
            {
                try
                {
                    result = beheerModel.ClubCloud_Functionarissen.Find(Id);

                    if (result == null || refresh)
                    {
                        result = GetFunctionarisById(settings.Id.ToString(), Id, refresh, settings);
                    }

                    if (result != null)
                    {
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(Logger.Category.Unexpected, ex.Source, ex.Message);
                }
                finally
                {
                    if (result != null)
                    {
                        try { beheerModel.ObjectContext.Detach(result); } catch {}
                    }
                }
            }

            return(new ClubCloud_Functionaris());
        }
        public ClubCloud_Functionaris SetFunctionaris(ClubCloud_Functionaris entity, ClubCloud_Setting settings = null)
        {
            if (settings != null)
            {
                ValidateSettings(ref settings);
            }

            ClubCloud_Functionaris tobeupdated = null;

            using (new SPMonitoredScope("Functionarissen SetFunctionaris"))
            {
                try
                {
                    entity = SetFunctionaris(settings.Id.ToString(), entity, settings);

                    if (entity != null)
                    {
                        beheerModel.ClubCloud_Functionarissen.AddOrUpdate(entity);
                    }

                    beheerModel.SaveChanges();

                    tobeupdated = beheerModel.ClubCloud_Functionarissen.Find(entity.Id);
                    return(tobeupdated);
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(Logger.Category.Unexpected, ex.Source, ex.Message);
                }
                finally
                {
                    if (tobeupdated != null)
                    {
                        try { beheerModel.ObjectContext.Detach(tobeupdated); } catch {}
                    }
                }
            }

            return(null);
        }
        public void UpdateFunctionaris(ClubCloud_Functionaris entity)
        {
            using (new SPMonitoredScope("Functionarissen UpdateFunctionaris"))
            {
                if (SPContext.Current.Web.CurrentUser != null)
                {
                    int bondsnummer;
                    ClubCloud_Setting Settings = null;
                    if (int.TryParse(SPContext.Current.Web.CurrentUser.UserId.NameId, out bondsnummer))
                    {
                        Settings = Client.GetSettingById(bondsnummer);
                    }

                    if (Settings != null && Settings.VerenigingId != null)
                    {
                        entity.VerenigingId = Settings.VerenigingId.Value;
                        Client.SetFunctionaris(entity, Settings);
                        this.OnDataSourceViewChanged(EventArgs.Empty);
                    }
                }
            }
        }
        public bool DeleteFunctionarisById(System.Guid Id, ClubCloud_Setting settings = null)
        {
            if (settings != null)
            {
                ValidateSettings(ref settings);
            }

            bool succes = false;
            ClubCloud_Functionaris tobedeleted = null;

            using (new SPMonitoredScope("Functionarissen DeleteFunctionarisById"))
            {
                try
                {
                    tobedeleted = beheerModel.ClubCloud_Functionarissen.Find(Id);

                    if (tobedeleted != null)
                    {
                        beheerModel.ClubCloud_Functionarissen.Remove(tobedeleted);
                        beheerModel.SaveChanges();
                    }
                    succes = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(Logger.Category.Unexpected, ex.Source, ex.Message);
                }
                finally
                {
                    if (tobedeleted != null)
                    {
                        try { beheerModel.ObjectContext.Detach(tobedeleted); } catch {}
                    }
                }
            }

            return(succes);
        }
        public bool SetBestuursorgaanForFunctionaris(ClubCloud_Bestuursorgaan Bestuursorgaan, ClubCloud_Functionaris entity, bool refresh = false, ClubCloud_Setting settings = null)
        {
            if (settings != null)
            {
                ValidateSettings(ref settings);
            }

            bool succes = false;

            using (new SPMonitoredScope("Functionarissen SetBestuursorgaanForFunctionaris"))
            {
                try
                {
                    entity = beheerModel.ClubCloud_Functionarissen.Find(entity.Id);

                    if (entity != null && entity.ClubCloud_Bestuursorgaan == null)
                    {
                        beheerModel.Entry(entity).Reference(e => e.ClubCloud_Bestuursorgaan).Load();
                    }

                    if (entity != null)
                    {
                        entity.ClubCloud_Bestuursorgaan = Bestuursorgaan;
                    }

                    beheerModel.SaveChanges();
                    succes = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(Logger.Category.Unexpected, ex.Source, ex.Message);
                }
                finally
                {
                    if (entity != null)
                    {
                        try { beheerModel.ObjectContext.Detach(entity); } catch {}
                    }
                }
            }

            return(succes);
        }