public void SaveAddressInformation(PersonViewModel person, Address address, Family family)
        {
            if (address == null)
            {
                address = new Address { Created = DateTime.Now, Changed = DateTime.Now };
                family.Address = address;
            }

            if (address.Line1 != person.Address1 ||
                address.Line2 != person.Address2 ||
                address.Line3 != person.Address3 ||
                address.Line4 != person.Address4)
                address.Changed = DateTime.Now;

            address.Line1 = person.Address1 ?? string.Empty;
            address.Line2 = person.Address2 ?? string.Empty;
            address.Line3 = person.Address3 ?? string.Empty;
            address.Line4 = person.Address4 ?? string.Empty;
            address.Lat = person.Lat;
            address.Long = person.Lng;

            Context.SaveChanges();
        }
        public static void SaveGroupSettings(Person currentPerson, GroupDto groupSettings)
        {
            using (var context = new oikonomosEntities(ConfigurationManager.ConnectionStrings["oikonomosEntities"].ConnectionString))
            {
                //Check to see if the address already exists

                var address = new Address();

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

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

                address.Line1 = groupSettings.Address1 ?? string.Empty;
                address.Line2 = groupSettings.Address2 ?? string.Empty;
                address.Line3 = groupSettings.Address3 ?? string.Empty;
                address.Line4 = groupSettings.Address4 ?? string.Empty;
                address.AddressType = groupSettings.AddressType ?? string.Empty;
                address.Lat = groupSettings.Lat;
                address.Long = groupSettings.Lng;
                address.Changed = DateTime.Now;

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

                var group = (from g in context.Groups
                             where g.GroupId == groupSettings.GroupId
                             select g).FirstOrDefault();

                group.Address = address;

                context.SaveChanges();
            }
        }
        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 Address object.
 /// </summary>
 /// <param name="addressId">Initial value of the AddressId property.</param>
 /// <param name="line1">Initial value of the Line1 property.</param>
 /// <param name="line2">Initial value of the Line2 property.</param>
 /// <param name="line3">Initial value of the Line3 property.</param>
 /// <param name="line4">Initial value of the Line4 property.</param>
 /// <param name="created">Initial value of the Created property.</param>
 /// <param name="changed">Initial value of the Changed property.</param>
 /// <param name="lat">Initial value of the Lat property.</param>
 /// <param name="long">Initial value of the Long property.</param>
 public static Address CreateAddress(global::System.Int32 addressId, global::System.String line1, global::System.String line2, global::System.String line3, global::System.String line4, global::System.DateTime created, global::System.DateTime changed, global::System.Decimal lat, global::System.Decimal @long)
 {
     Address address = new Address();
     address.AddressId = addressId;
     address.Line1 = line1;
     address.Line2 = line2;
     address.Line3 = line3;
     address.Line4 = line4;
     address.Created = created;
     address.Changed = changed;
     address.Lat = lat;
     address.Long = @long;
     return address;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Addresses EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAddresses(Address address)
 {
     base.AddObject("Addresses", address);
 }
        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");
            }
        }