private IEnumerable<PersonGroupViewModel> PopulatePersonGroupViewModels(IEnumerable<Group> allGroupsInChurch, Group primaryGroup, IEnumerable<PersonGroup> groupsPersonIsIn)
 {
     var returnItems = new List<PersonGroupViewModel>();
     foreach (var g in allGroupsInChurch)
     {
         var personGroupViewModel = new PersonGroupViewModel
             {
                 GroupId = g.GroupId,
                 GroupName = g.Name,
                 IsPrimaryGroup = primaryGroup != null && g.GroupId == primaryGroup.GroupId
             };
         var tgroup = g;
         foreach (var groupIsIn in groupsPersonIsIn.Where(groupIsIn => groupIsIn.GroupId == tgroup.GroupId))
         {
             personGroupViewModel.IsInGroup = true;
             break;
         }
         returnItems.Add(personGroupViewModel);
     }
     return returnItems;
 }
        public static int SaveHomeGroup(Person currentPerson, HomeGroupsViewModel hgvm)
        {
            if (currentPerson.HasPermission(Permissions.EditGroups) || currentPerson.HasPermission(Permissions.AddGroups))
            {
                using (var context = new oikonomosEntities(ConfigurationManager.ConnectionStrings["oikonomosEntities"].ConnectionString))
                {
                    var hg = new Group();
                    if (hgvm.GroupId != 0)
                    {
                        hg = (from g in context.Groups
                              where g.GroupId == hgvm.GroupId
                              select g).FirstOrDefault();
                    }
                    else
                    {
                        hg.ChurchId = currentPerson.ChurchId;
                        hg.Created = DateTime.Now;
                        if (currentPerson.ChurchId == 3) //Ebenezer
                        {
                            hg.GroupTypeId = (int)GroupTypes.LifeGroup;
                        }
                        else
                        {
                            hg.GroupTypeId = (int)GroupTypes.HomeGroup;
                        }
                        context.Groups.AddObject(hg);
                    }

                    hg.Name = hgvm.GroupName;
                    if (hgvm.LeaderId == 0 || string.IsNullOrEmpty(hgvm.LeaderName))
                        hg.LeaderId = null;
                    else
                        hg.LeaderId = hgvm.LeaderId;
                    if (hgvm.AdministratorId == 0 || string.IsNullOrEmpty(hgvm.AdministratorName))
                        hg.AdministratorId = null;
                    else
                        hg.AdministratorId = hgvm.AdministratorId;

                    hg.Changed = DateTime.Now;

                    //Check to see if the address already exists
                    if (hgvm.AddressId>0 || hgvm.Address1 != null || hgvm.Address2 != null || hgvm.Address3 != null || hgvm.Address4 != null || hgvm.SuburbId!=0)
                    {
                        var address = new Address();

                        if (hgvm.AddressId > 0)
                        {
                            address = (from a in context.Addresses
                                       where a.AddressId == hgvm.AddressId
                                       select a).FirstOrDefault();

                            if (address == null) //Should never happen, but just to be sure
                            {
                                address = new Address {Created = DateTime.Now};
                                hgvm.AddressId = 0;
                            }
                        }
                        else
                        {
                            address.Created = DateTime.Now;
                        }

                        address.Line1 = hgvm.Address1 ?? "";
                        address.Line2 = hgvm.Address2 ?? "";
                        address.Line3 = hgvm.Address3 ?? "";
                        address.Line4 = hgvm.Address4 ?? "";
                        address.AddressType = hgvm.AddressType ?? "";
                        address.Lat = hgvm.Lat;
                        address.Long = hgvm.Lng;
                        address.ChurchSuburbId = hgvm.SuburbId != 0 ? hgvm.SuburbId : (int?)null;
                        address.Changed = DateTime.Now;

                        if (hgvm.AddressId == 0)
                        {
                            context.Addresses.AddObject(address);
                        }

                        hg.Address = address;
                    }

                    hg.GroupClassificationId = hgvm.GroupClassificationId == 0 ? (int?)null : hgvm.GroupClassificationId;

                    context.SaveChanges();
                    if (hgvm.OverseeingElderId == 0 || string.IsNullOrEmpty(hgvm.OverseeingElderName))
                    {
                        var linkedPersonToDelete = context.PersonLinkedToGroups.FirstOrDefault(p => p.GroupId == hgvm.GroupId && p.Description == CacheNames.OverseeingElder);
                        if(linkedPersonToDelete!=null)
                            context.PersonLinkedToGroups.DeleteObject(linkedPersonToDelete);
                    }
                    else
                    {
                        var linkedPersonToDelete = context.PersonLinkedToGroups.FirstOrDefault(p => p.GroupId == hgvm.GroupId && p.Description == CacheNames.OverseeingElder);
                        if (linkedPersonToDelete != null)
                        {
                            context.PersonLinkedToGroups.DeleteObject(linkedPersonToDelete);
                        }
                        addNewElder(context, hg.GroupId, hgvm.OverseeingElderId);
                    }
                    context.SaveChanges();
                    return hg.GroupId;
                }
            }

            throw new ApplicationException("You do not have the required permission");
        }
 /// <summary>
 /// Create a new Group object.
 /// </summary>
 /// <param name="groupId">Initial value of the GroupId property.</param>
 /// <param name="churchId">Initial value of the ChurchId property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="groupTypeId">Initial value of the GroupTypeId property.</param>
 /// <param name="created">Initial value of the Created property.</param>
 /// <param name="changed">Initial value of the Changed property.</param>
 public static Group CreateGroup(global::System.Int32 groupId, global::System.Int32 churchId, global::System.String name, global::System.Int32 groupTypeId, global::System.DateTime created, global::System.DateTime changed)
 {
     Group group = new Group();
     group.GroupId = groupId;
     group.ChurchId = churchId;
     group.Name = name;
     group.GroupTypeId = groupTypeId;
     group.Created = created;
     group.Changed = changed;
     return group;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Groups EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToGroups(Group group)
 {
     base.AddObject("Groups", group);
 }
        public static void SaveHomeGroup(Person currentPerson, HomeGroupsViewModel hgvm)
        {
            if (currentPerson.HasPermission(Permissions.EditGroups) || currentPerson.HasPermission(Permissions.AddGroups))
            {
                using (oikonomosEntities context = new oikonomosEntities(ConfigurationManager.ConnectionStrings["oikonomosEntities"].ConnectionString))
                {
                    Group hg = new Group();
                    if (hgvm.GroupId != 0)
                    {
                        hg = (from g in context.Groups
                              where g.GroupId == hgvm.GroupId
                              select g).FirstOrDefault();
                    }
                    else
                    {
                        hg.ChurchId = currentPerson.ChurchId;
                        hg.Created = DateTime.Now;
                        if (currentPerson.ChurchId == 3) //Ebenezer
                        {
                            hg.GroupTypeId = (int)GroupTypes.LifeGroup;
                        }
                        else
                        {
                            hg.GroupTypeId = (int)GroupTypes.HomeGroup;
                        }
                        context.Groups.AddObject(hg);
                    }

                    hg.Name = hgvm.GroupName;
                    if (hgvm.LeaderId == 0 || hgvm.LeaderName == null || hgvm.LeaderName == string.Empty)
                        hg.LeaderId = null;
                    else
                        hg.LeaderId = hgvm.LeaderId;
                    if (hgvm.AdministratorId == 0 || hgvm.AdministratorName == null || hgvm.AdministratorName == string.Empty)
                        hg.AdministratorId = null;
                    else
                        hg.AdministratorId = hgvm.AdministratorId;
                    hg.Changed = DateTime.Now;

                    //Check to see if the address already exists
                    if (hgvm.AddressId>0 || hgvm.Address1 != null || hgvm.Address2 != null || hgvm.Address3 != null || hgvm.Address4 != null || hgvm.SuburbId!=0)
                    {
                        var address = new Address();

                        if (hgvm.AddressId > 0)
                        {
                            address = (from a in context.Addresses
                                       where a.AddressId == hgvm.AddressId
                                       select a).FirstOrDefault();

                            if (address == null) //Should never happen, but just to be sure
                            {
                                address = new Address();
                                address.Created = DateTime.Now;
                                hgvm.AddressId = 0;
                            }
                        }
                        else
                        {
                            address.Created = DateTime.Now;
                        }

                        address.Line1 = hgvm.Address1 == null ? "" : hgvm.Address1;
                        address.Line2 = hgvm.Address2 == null ? "" : hgvm.Address2;
                        address.Line3 = hgvm.Address3 == null ? "" : hgvm.Address3;
                        address.Line4 = hgvm.Address4 == null ? "" : hgvm.Address4;
                        address.AddressType = hgvm.AddressType == null ? "" : hgvm.AddressType;
                        address.Lat = hgvm.Lat == null ? 0 : hgvm.Lat;
                        address.Long = hgvm.Lng == null ? 0 : hgvm.Lng;
                        address.ChurchSuburbId = hgvm.SuburbId != 0 ? hgvm.SuburbId : (int?)null;
                        address.Changed = DateTime.Now;

                        if (hgvm.AddressId == 0)
                        {
                            context.Addresses.AddObject(address);
                        }

                        hg.Address = address;
                    }

                    hg.GroupClassificationId = hgvm.GroupClassificationId == 0 ? (int?)null : hgvm.GroupClassificationId;

                    context.SaveChanges();
                }
            }
            else
            {
                throw new ApplicationException("You do not have the required permission");
            }
        }