}         //

        #endregion
        #endregion

        #region GeoCoordinate  =======================
        #region GeoCoordinate Core  =======================


        public int GeoCoordinates_Add(MC.GeoCoordinates entity, int jpId, ref SaveStatus status)
        {
            if (entity == null || string.IsNullOrWhiteSpace(entity.GeoURI))
            {
                return(0);
            }
            entity.ParentId = jpId;
            EM.GeoCoordinate  efEntity = new EM.GeoCoordinate();
            MC.GeoCoordinates existing = new MC.GeoCoordinates();
            List <String>     messages = new List <string>();

            //extract from "http://geonames.org/6255149/"
            string geoNamesId = UtilityManager.ExtractNameValue(entity.GeoURI, ".org", "/", "/");

            if (IsInteger(geoNamesId))
            {
                entity.GeoNamesId = Int32.Parse(geoNamesId);
            }

            if (GeoCoordinates_Exists(entity.ParentId, entity.GeoNamesId, entity.IsException))
            {
                status.AddWarning("Error this Region has aleady been selected.");
                return(0);
            }

            else
            {
                MapToDB(entity, efEntity);
                //ensure parent jp Id is present
                return(GeoCoordinate_Add(efEntity, ref status));
            }
        }
        private static void MapToDB(MC.GeoCoordinates from, EM.GeoCoordinate to)
        {
            to.Id             = from.Id;
            to.JurisdictionId = from.ParentId;

            to.Name          = from.Name;
            to.IsException   = from.IsException;
            to.AddressRegion = from.Region;
            to.Country       = from.Country;
            to.Latitude      = from.Latitude;
            to.Longitude     = from.Longitude;
            to.Url           = from.GeoURI;

            if (from.GeoNamesId > 0)
            {
                to.GeoNamesId = from.GeoNamesId;
            }
            else
            {
                //extract from "http://geonames.org/6255149/"
                string geoNamesId = UtilityManager.ExtractNameValue(from.GeoURI, ".org", "/", "/");
                if (IsInteger(geoNamesId))
                {
                    to.GeoNamesId = Int32.Parse(geoNamesId);
                }
            }
        }
        private static void MapFromDB(EM.GeoCoordinate from, MC.GeoCoordinates to)
        {
            to.Id         = from.Id;
            to.ParentId   = (int)from.JurisdictionId;
            to.GeoNamesId = from.GeoNamesId != null ? ( int )from.GeoNamesId : 0;

            to.Name        = from.Name;
            to.IsException = from.IsException != null ? ( bool )from.IsException : false;
            to.Region      = from.AddressRegion;
            to.Country     = from.Country;
            if (from.Latitude != null)
            {
                to.Latitude = (double)from.Latitude;
            }
            if (from.Longitude != null)
            {
                to.Longitude = ( double )from.Longitude;
            }
            to.GeoURI         = from.Url;
            to.ProfileSummary = to.Name;
            if (!string.IsNullOrWhiteSpace(to.Region))
            {
                to.ProfileSummary += ", " + to.Region;
            }
            if (!string.IsNullOrWhiteSpace(to.Country) && to.Country != to.Name)
            {
                to.ProfileSummary += ", " + to.Country;
            }
        }         //
        /// <summary>
        /// Determine if a geoName already exists for the parent and type (is or is not an exception)
        /// </summary>
        /// <param name="parentUid"></param>
        /// <param name="geoNamesId"></param>
        /// <param name="isException"></param>
        /// <returns></returns>
        public static bool GeoCoordinates_Exists(int parentId, int geoNamesId, bool isException)
        {
            bool isFound = false;

            using (var context = new EntityContext())
            {
                EM.GeoCoordinate item = context.GeoCoordinate
                                        .FirstOrDefault(s => s.JurisdictionId == parentId && s.GeoNamesId == geoNamesId && s.IsException == isException);

                if (item != null && item.Id > 0)
                {
                    isFound = true;
                }
            }

            return(isFound);
        }
        /// <summary>
        /// Check if record (any) exist for the provided GeoNamesId
        /// </summary>
        /// <param name="geoNamesId"></param>
        /// <returns></returns>
        public static MC.GeoCoordinates GeoCoordinates_GetByGeoNamesId(int geoNamesId)
        {
            MC.GeoCoordinates        entity = new MC.GeoCoordinates();
            List <MC.GeoCoordinates> list   = new List <MC.GeoCoordinates>();

            using (var context = new EntityContext())
            {
                EM.GeoCoordinate item = context.GeoCoordinate
                                        .FirstOrDefault(s => s.GeoNamesId == geoNamesId);

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity);
                }
            }

            return(entity);
        }
        /// <summary>
        /// Probably want to combine with region to have access to keys
        /// </summary>
        /// <param name="efEntity"></param>
        /// <param name="statusMessage"></param>
        /// <returns></returns>
        public int GeoCoordinate_Add(EM.GeoCoordinate efEntity, ref SaveStatus status)
        {
            using (var context = new EntityContext())
            {
                try
                {
                    if (efEntity.JurisdictionId < 1)
                    {
                        status.AddWarning("Error - missing a parent identifier");
                        return(0);
                    }

                    efEntity.Created     = System.DateTime.Now;
                    efEntity.LastUpdated = System.DateTime.Now;

                    context.GeoCoordinate.Add(efEntity);

                    // submit the change to database
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        return(efEntity.Id);
                    }
                    else
                    {
                        //?no info on error
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    string message = HandleDBValidationError(dbex, thisClassName + ".GeoCoordinate_Add() ", "GeoCoordinate");
                    status.AddError("Error - the save was not successful. " + message);
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".RelatedRegion_Add(), Name: {0}, ParentId: {1)", efEntity.Name, efEntity.JurisdictionId));
                }
            }

            return(0);
        }
        public static MC.GeoCoordinates GeoCoordinates_GetByUrl(string geoUri)
        {
            MC.GeoCoordinates entity = new MC.GeoCoordinates();
            if (string.IsNullOrWhiteSpace(geoUri))
            {
                return(null);
            }

            geoUri = geoUri.ToLower();
            using (var context = new EntityContext())
            {
                EM.GeoCoordinate item = context.GeoCoordinate
                                        .FirstOrDefault(s => s.Url.ToLower() == geoUri);

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity);
                }
            }

            return(entity);
        }