public static Portable.Classes.Location.Location CreateNewLocation(LocationOwnerType ownerType, string name, string address1, string address2, string city, int country, string state, string zip, string website, Guid idOfOwner) { Portable.Classes.Location.Location l = new Portable.Classes.Location.Location(); string log = string.Empty; try { var dc = new ManagementContext(); DataModels.Location.Location location = new DataModels.Location.Location(); if (ownerType == LocationOwnerType.calendar) { var cal = dc.Calendar.Where(x => x.CalendarId == idOfOwner).FirstOrDefault(); cal.Locations.Add(location); } else if (ownerType == LocationOwnerType.shop) { var shop = dc.Merchants.Where(x => x.PrivateManagerId == idOfOwner).FirstOrDefault(); shop.Locations.Add(location); } location.LocationName = name; location.Contact = new DataModels.ContactCard.ContactCard(); Address a = new Address(); a.Address1 = address1; a.Address2 = address2; a.CityRaw = city; a.Country = dc.Countries.Where(x => x.CountryId == country).FirstOrDefault(); a.StateRaw = state; a.ContactCard = location.Contact; var coords = OpenStreetMap.FindLatLongOfAddress(a.Address1, a.Address2, a.Zip, a.CityRaw, a.StateRaw, a.Country != null ? a.Country.Name : string.Empty); a.Coords = new System.Device.Location.GeoCoordinate(); if (coords != null) { log += "not" + coords.Latitude + " " + coords.Longitude; a.Coords.Latitude = coords.Latitude; a.Coords.Longitude = coords.Longitude; } else { a.Coords.Latitude = 0.0; a.Coords.Longitude = 0.0; } a.Coords.Altitude = 0.0; a.Coords.Course = 0.0; a.Coords.HorizontalAccuracy = 1.0; a.Coords.Speed = 0.0; a.Coords.VerticalAccuracy = 1.0; location.Contact.Addresses.Add(a); if (!String.IsNullOrEmpty(website)) { string comType = CommunicationTypeEnum.Website.ToString(); Communication web = new Communication(); web.Data = website; web.CommunicationTypeEnum = (byte)CommunicationTypeEnum.Website; location.Contact.Communications.Add(web); } dc.Locations.Add(location); int c = dc.SaveChanges(); l.LocationId = location.LocationId; l.LocationName = name; Portable.Classes.ContactCard.Address address = new Portable.Classes.ContactCard.Address(); address.Address1 = address1; address.Address2 = address2; address.CityRaw = city; if (a.Country != null) address.Country = a.Country.Name; address.StateRaw = a.StateRaw; address.Zip = a.Zip; l.Contact.Addresses.Add(address); } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: name + " " + address1 + " " + address2 + " " + city + " " + country + " " + state + " " + zip + " " + website + " " + idOfOwner); } return l; }
public static List<RDN.Portable.Classes.Location.Location> GetLocationsOfCalendar(Guid calendarId) { List<RDN.Portable.Classes.Location.Location> newLocations = new List<Portable.Classes.Location.Location>(); try { var dc = new ManagementContext(); var locations = (from xx in dc.Calendar where xx.CalendarId == calendarId select new { xx.CalendarEvents, xx.Locations }).FirstOrDefault(); foreach (var loc in locations.Locations.Where(x => x.IsRemoved == false)) { try { RDN.Portable.Classes.Location.Location l = new Portable.Classes.Location.Location(); l.LocationName = loc.LocationName; l.LocationId = loc.LocationId; if (loc.Contact != null && loc.Contact.Addresses.FirstOrDefault() != null) { var add = loc.Contact.Addresses.FirstOrDefault(); RDN.Portable.Classes.ContactCard.Address a = new RDN.Portable.Classes.ContactCard.Address(); a.Address1 = add.Address1; a.Address2 = add.Address2; a.AddressId = add.AddressId; a.CityRaw = add.CityRaw; if (add.Country != null) a.Country = add.Country.Code; a.IsDefault = add.IsDefault; a.StateRaw = add.StateRaw; a.Zip = add.Zip; l.Contact.Addresses.Add(a); } if (newLocations.Where(x => x.LocationId == l.LocationId).FirstOrDefault() == null) newLocations.Add(l); } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } } foreach (var loc in locations.CalendarEvents) { try { if (loc.Location != null) { RDN.Portable.Classes.Location.Location l = new Portable.Classes.Location.Location(); l.LocationName = loc.Location.LocationName; l.LocationId = loc.Location.LocationId; if (loc.Location.Contact != null && loc.Location.Contact.Addresses.FirstOrDefault() != null) { var add = loc.Location.Contact.Addresses.FirstOrDefault(); RDN.Portable.Classes.ContactCard.Address a = new RDN.Portable.Classes.ContactCard.Address(); a.Address1 = add.Address1; a.Address2 = add.Address2; a.AddressId = add.AddressId; a.CityRaw = add.CityRaw; if (add.Country != null) a.Country = add.Country.Code; a.IsDefault = add.IsDefault; a.StateRaw = add.StateRaw; a.Zip = add.Zip; l.Contact.Addresses.Add(a); } if (newLocations.Where(x => x.LocationId == l.LocationId).FirstOrDefault() == null) newLocations.Add(l); } } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } } } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return newLocations; }