public Location Test() { int NewItemId = 0; string Msg = ""; //TEST: Add a new Location var newLoc = new Location("Test Location"); Repositories.LocationRepo.Insert(newLoc); var Result = Repositories.LocationRepo.GetByKey(newLoc.Key); return Result; }
public JsonLocation(Location ConvertedFromLocation) { //Basic Location properties this.Key = ConvertedFromLocation.Key; this.Name = ConvertedFromLocation.Name; this.LocationTypeKey = ConvertedFromLocation.LocationTypeKey; this.LocationTypeName = ConvertedFromLocation.LocationType.Name; this.Latitude = ConvertedFromLocation.Latitude; this.Longitude = ConvertedFromLocation.Longitude; //Address this.Address1 = ConvertedFromLocation.Address.Address1; this.Address2 = ConvertedFromLocation.Address.Address2; this.Locality = ConvertedFromLocation.Address.Locality; this.Region = ConvertedFromLocation.Address.Region; this.PostalCode = ConvertedFromLocation.Address.PostalCode; this.CountryCode = ConvertedFromLocation.Address.CountryCode; this.CustomPropertyData = new List<JsonPropertyData>(); foreach (var Prop in ConvertedFromLocation.PropertyData) { //Check for special props switch (Prop.PropertyAlias) { case Constants.DefaultLocPropertyAlias.Phone: this.Phone = Prop.Value.ToString(); break; case Constants.DefaultLocPropertyAlias.Email: this.Email = Prop.Value.ToString(); break; case Constants.DefaultLocPropertyAlias.Address1: break; case Constants.DefaultLocPropertyAlias.Address2: break; case Constants.DefaultLocPropertyAlias.Locality: break; case Constants.DefaultLocPropertyAlias.Region: break; case Constants.DefaultLocPropertyAlias.PostalCode: break; case Constants.DefaultLocPropertyAlias.CountryCode: break; default: this.CustomPropertyData.Add(new JsonPropertyData(Prop)); //this.AllPropertyData.Add(new JsonPropertyData(Prop)); break; } } }
public LocationDto ToLocationDto(Location entity) { var dto = new LocationDto() { Key = entity.Key, Name = entity.Name, Latitude = entity.Latitude, Longitude = entity.Longitude, GeocodeStatus = entity.GeocodeStatus.ToString(), DbGeogNeedsUpdated = entity.DbGeogNeedsUpdated, LocationTypeKey = entity.LocationTypeKey, UpdateDate = entity.UpdateDate, CreateDate = entity.CreateDate //Viewport = entity.Viewport.ToString(), }; return dto; }
public Location ToLocationEntity(LocationDto dto) { var Entity = new Location() { Key = dto.Key, Name = dto.Name, Latitude = dto.Latitude, Longitude = dto.Longitude, Coordinate = new Coordinate(dto.Latitude, dto.Longitude), GeocodeStatus = DoGeocoding.GetGeocodeStatus(dto.GeocodeStatus), DbGeogNeedsUpdated = dto.DbGeogNeedsUpdated, LocationTypeKey = dto.LocationTypeKey, UpdateDate = dto.UpdateDate, CreateDate = dto.CreateDate //Viewport = new Viewport(dto.Viewport), }; return Entity; }
/// <summary> /// Builds a location from an <see cref="IAddress"/> and <see cref="IGeocodeProviderResponse"/>. /// </summary> /// <param name="address"> /// The address. /// </param> /// <param name="response"> /// The response. /// </param> /// <returns> /// The <see cref="ILocation"/>. /// </returns> public ILocation BuildLocation(IAddress address, IGeocodeProviderResponse response) { //TODO: HLF Fix this //var definitionFactory = new LocationTypeDefinitionFactory(); //var def = definitionFactory.GetEmptyDefaultLocationTypeDefinition(); //def.Fields.Address1().Value = address.Address1; //def.Fields.Address2().Value = address.Address2; //def.Fields.Locality().Value = address.Locality; //def.Fields.Region().Value = address.Region; //def.Fields.PostalCode().Value = address.PostalCode; //def.Fields.CountryCode().Value = address.CountryCode; var location = new Location()//def.Fields { //LocationTypeId = def.Id, GeocodeStatus = response.Status }; if (response.Status != GeocodeStatus.Ok) return location; if (!response.Results.Any()) return location; var result = response.Results.First(); location.Coordinate = new Coordinate() { Latitude = result.Latitude, Longitude = result.Longitude }; location.Viewport = result.Viewport; return location; }
private static StatusMessage ImportRow(DataRow row, LocationType LocType, int GeocodeCount, DataColumnCollection ImportColumns, out int geocodeCountReturn) { string locName = row.Field<string>("LocationName"); var ReturnMsg = new StatusMessage(); ReturnMsg.ObjectName = locName; ReturnMsg.Success = true; var Msg = new StringBuilder(); var geocodeCountNew = GeocodeCount; //Create new Location for row var newLoc = new Location(locName, LocType.Key); Repositories.LocationRepo.Insert(newLoc); try { //Default Props var locationTypeService = new LocationTypeService(); var defaultLocType = locationTypeService.GetLocationType(Constants.DefaultLocationTypeKey); foreach (var prop in defaultLocType.Properties) { string colName = prop.Alias; if (ImportColumns.Contains(colName)) { newLoc.AddPropertyData(colName, row.Field<object>(colName)); } else { newLoc.AddPropertyData(colName, null); Msg.AppendLine(string.Concat("Data for '", colName, "' was not included in the import file.")); } } //Custom Props if (LocType.Key != defaultLocType.Key) { foreach (var prop in LocType.Properties) { string colName = prop.Alias; if (ImportColumns.Contains(colName)) { newLoc.AddPropertyData(colName, row.Field<object>(colName)); } else { newLoc.AddPropertyData(colName, null); Msg.AppendLine(string.Concat("Data for '", colName, "' was not included in the import file.")); } } } // SAVE properties of new location to db try { Repositories.LocationRepo.Update(newLoc); } catch (Exception eSave) { ReturnMsg.Success = false; ReturnMsg.RelatedException = eSave; ReturnMsg.Code = ReturnMsg.Code != "" ? ReturnMsg.Code + ",InsertError" : "InsertError"; Msg.AppendLine("There was a problem saving the new location data."); } //Check for Lat/Long values - import if present if (ImportColumns.Contains("Latitude")) { int convertedInt = 0; Int32.TryParse(row.Field<string>("Latitude"), out convertedInt); newLoc.Latitude = convertedInt; } if (ImportColumns.Contains("Longitude")) { int convertedInt = 0; Int32.TryParse(row.Field<string>("Longitude"), out convertedInt); newLoc.Longitude = convertedInt; } //If Lat/Long are both 0... attempt geocoding if (newLoc.Latitude == 0 && newLoc.Longitude == 0) { //TODO: make dynamic based on provider limit if (GeocodeCount >= MAX_GEOCODE) { ReturnMsg.Success = true; ReturnMsg.Code = "GeocodingProblem"; Msg.AppendLine( "This address exceeded the limits for geo-coding in a batch. Please run maintenance to update geo-codes later."); } else { try { var newCoordinate = DoGeocoding.GetCoordinateForAddress(newLoc.Address); newLoc.Latitude = newCoordinate.Latitude; newLoc.Longitude = newCoordinate.Longitude; geocodeCountNew++; } catch (Exception e1) { ReturnMsg.Success = true; ReturnMsg.RelatedException = e1; ReturnMsg.Code = "GeocodingProblem"; Msg.AppendLine( "There was a problem geo-coding the address. Please run maintenance to update geo-codes later."); LogHelper.Error( typeof(Import), string.Format("Geo-coding error while importing '{0}'", ReturnMsg.ObjectName), e1); } } } // SAVE properties of new location to db again try { Repositories.LocationRepo.Update(newLoc); } catch (Exception eSave) { ReturnMsg.Success = false; ReturnMsg.RelatedException = eSave; ReturnMsg.Code = ReturnMsg.Code != "" ? ReturnMsg.Code + ",InsertError" : "InsertError"; Msg.AppendLine("There was a problem saving the new location data."); } // UPDATE Geography DB field using Lat/Long try { if (newLoc.Latitude != 0 && newLoc.Longitude != 0) { Repositories.LocationRepo.UpdateDbGeography(newLoc); } else { newLoc.DbGeogNeedsUpdated = true; Repositories.LocationRepo.Update(newLoc); } } catch (Exception eGeoDB) { ReturnMsg.Success = true; ReturnMsg.RelatedException = eGeoDB; ReturnMsg.Code = ReturnMsg.Code != "" ? ReturnMsg.Code + ",UnableToUpdateDBGeography" : "UnableToUpdateDBGeography"; Msg.AppendLine("Unable to update the coordinates in the database."); } } catch (Exception ex) { ReturnMsg.Success = false; ReturnMsg.RelatedException = ex; ReturnMsg.Code = ReturnMsg.Code != "" ? ReturnMsg.Code + ",UnknownException" : "UnknownException"; Msg.AppendLine(ex.Message); LogHelper.Error(typeof(Import), string.Format("ImportRow: Error importing location '{0}'", locName), ex); } ReturnMsg.Message = Msg.ToString(); geocodeCountReturn = geocodeCountNew; return ReturnMsg; }
public Location ConvertToLocation() { Location Entity; if (this.Key != Guid.Empty) { //Lookup existing entity Entity = Repositories.LocationRepo.GetByKey(this.Key); //Update Location properties as needed Entity.Name = this.Name; Entity.LocationTypeKey = this.LocationTypeKey; } else { //Create new entity Entity = new Location(Name = this.Name, LocationTypeKey = this.LocationTypeKey); } //Update lat/long Entity.Latitude = this.Latitude; Entity.Longitude = this.Longitude; //Add Address properties Entity.AddPropertyData(Constants.DefaultLocPropertyAlias.Address1, this.Address1); Entity.AddPropertyData(Constants.DefaultLocPropertyAlias.Address2, this.Address2); Entity.AddPropertyData(Constants.DefaultLocPropertyAlias.Locality, this.Locality); Entity.AddPropertyData(Constants.DefaultLocPropertyAlias.Region, this.Region); Entity.AddPropertyData(Constants.DefaultLocPropertyAlias.PostalCode, this.PostalCode); Entity.AddPropertyData(Constants.DefaultLocPropertyAlias.CountryCode, this.CountryCode); //Deal with Properties Entity.AddPropertyData(Constants.DefaultLocPropertyAlias.Phone, this.Phone); Entity.AddPropertyData(Constants.DefaultLocPropertyAlias.Email, this.Email); //Add custom properties foreach (var JsonProp in this.CustomPropertyData) { Entity.AddPropertyData(JsonProp.PropAlias, JsonProp.PropData); } return Entity; }