Пример #1
0
        public void UpdateGeocodes(XContainer result, Org org)
        {
            if (result == null) throw new ArgumentNullException("result");

            var element = result.Element("geometry");

            if (element == null) return;

            var locationElement = element.Element("location");

            if (locationElement == null) return;

            var lat = locationElement.Element("lat");
            if (lat != null)
            {
                org.Lat = lat.Value.ToNullableDouble();
                org.Modified = DateTime.Now;
            }

            var lng = locationElement.Element("lng");
            if (lng != null)
            {
                org.Lon = lng.Value.ToNullableDouble();
                org.Modified = DateTime.Now;
            }
        }
        public XElement ReadNsoResponseElement(Org org)
        {
            const string pattern = "http://uk-postcodes.com/postcode/{0}.xml";

            var requestUri =
                string.Format(
                    pattern,
                    org.Postcode.RemoveSpaces()
                    );

            var uri = new Uri(requestUri);

            return uri.GetElement("result");
        }
        public List<XElement> ReadElements(Org org)
        {
            var elements = new List<XElement>();

            if (org.Postcode.IsNotNullOrEmpty() && org.Postcode.Length >= 6)
            {
                var nso = ReadNsoResponseElement(org);
                if (nso != null)
                {
                    elements.Add(nso);
                }
            }

            var geocode = ReadGeocodeResponseElement(org);
            elements.Add(geocode);

            return elements;
        }
        public XElement ReadGeocodeResponseElement(Org org)
        {
            // source correct address, using google maps api or similar

            // E.G., https://maps.googleapis.com/maps/api/geocode/xml?address=26%20Smithfield,%20London,%20Greater%20London,%20EC1A%209LB,%20uk&sensor=true&key=AIzaSyC2DCdkPGBtsooyft7sX3P9h2f4uQvLQj0

            var key = ConfigurationHelper.GetValueAsString("GoogleMapsApiKey");
            // "AIzaSyC2DCdkPGBtsooyft7sX3P9h2f4uQvLQj0";

            var requestUri =
                string.Format(
                    "https://maps.google.com/maps/api/geocode/xml?address={0}, {1}, {2}, UK&sensor=false&key={3}",
                    org.TradingName,
                    org.Address,
                    org.Postcode,
                    key
                    );

            var uri = new Uri(requestUri);

            return uri.GetElement("GeocodeResponse");
        }
Пример #5
0
 public void UpdateAuthority(Org org, Int32 id)
 {
     org.AuthorityId = id;
     org.Modified = DateTime.Now;
 }
Пример #6
0
        private void UpdateOrgFromGoogleResponse(XContainer result, Org org)
        {
            if (result == null) throw new ArgumentNullException("result");

            org.Modified = DateTime.Now;

            org.Tried = true;

            UpdateGeocodes(result, org);

            UpdateLocality(result, org);

            UpdateTown(result, org);
        }
Пример #7
0
        private static void UpdateTown(XContainer result, Org org)
        {
            if (result == null) throw new ArgumentNullException("result");

            var townResult =
                result.Elements("address_component").FirstOrDefault(x => x.Value.EndsWith("postal_town"));

            if (townResult == null || townResult.FirstNode == null) return;

            var firstResult = townResult.FirstNode as XElement;

            if (firstResult != null)
            {
                org.Modified = DateTime.Now;
            }
        }
Пример #8
0
        private static void UpdateLocality(XContainer result, Org org)
        {
            if (result == null) throw new ArgumentNullException("result");

            var match =
                result.Elements("address_component")
                    .FirstOrDefault(x => x.Value.EndsWith("localitypolitical"));

            if (match == null || match.FirstNode == null) return;

            var firstResult = match.FirstNode as XElement;

            if (firstResult == null) return;

            org.Locality = firstResult.Value;
            org.Modified = DateTime.Now;
        }
Пример #9
0
        public void UpdateOrgFromLaApiResponse(Org org, XContainer result)
        {
            if (result == null) throw new ArgumentNullException("result");

            org.LaTried = true;

            var administrative = result.Element("administrative");

            if (administrative == null) return;

            var council = administrative.Element("council");

            if (council == null) return;

            var code = council.Element("code");

            if (code == null) return;

            // Card #389 - St Albans
            if (code.Value == "E07000100")
            {
                code.Value = "E07000240";
            }

            // Card #389 - Glasgow City
            if (code.Value == "S12000043")
            {
                code.Value = "S12000046";
            }

            org.LaCode = code.Value;

            org.Modified = DateTime.Now;
        }
Пример #10
0
        public OutputViewModel PrepareOrgModel(
            Org org)
        {
            if (org == null) throw new ArgumentNullException("org");

            var next = org.ExtractNextLink();

            var model = OutputViewModel.CreateOrgOutputViewModel(org, next, "{0}, {1}");

            return model;
        }
Пример #11
0
        private void SourceAndApplyPostcodeApiData(Org org, XElement element)
        {
            if (org == null) throw new ArgumentNullException("org");
            if (element == null) throw new ArgumentNullException("element");

            var result = new XmlResult();

            if (!org.LaTried)
            {
                result = _thirdPartyApiManager.RequestLaApiResponse(element);

                org.LaData = result.Result.ToString();
            }
            else
            {
                result.ResultType = ResultTypeEnum.AlreadyTried;
            }

            if (result.ResultType != ResultTypeEnum.Success) return;

            _commandManager.UpdateOrgFromLaApiResponse(org, element);

            if (org.LaCode == null) return;

            var authority = _queryManager.GetAuthority(org.LaCode);

            if (authority == null) return;

            _commandManager.UpdateAuthority(org, authority.Id);
        }
Пример #12
0
        private void SourceAndApplyGoogleMapsApiData(Org org, XElement element)
        {
            if (org == null) throw new ArgumentNullException("org");
            if (element == null) throw new ArgumentNullException("element");

            var result = new XmlResult();

            if (!org.Tried)
            {
                result = _thirdPartyApiManager.RequestGoogleMapsApiResponse(element);

                org.GoogleMapData = result.Result.ToString();
            }
            else
            {
                result.ResultType = ResultTypeEnum.AlreadyTried;
            }

            if (result.ResultType != ResultTypeEnum.Success) return;

            _commandManager.UpdateOrgFromGoogleResponse(org, element);
        }
Пример #13
0
        private void SourceAndApplyApiData(Org org, XElement element)
        {
            if (org == null) throw new ArgumentNullException("org");
            if (element == null) return;

            if (element.ToString().Contains("GeocodeResponse"))
            {
                SourceAndApplyGoogleMapsApiData(org, element);
            }
            else
            {
                SourceAndApplyPostcodeApiData(org, element);
            }
        }
Пример #14
0
        private void SourceAndApplyApiData(Org org, IEnumerable<XElement> elements)
        {
            if (org == null) throw new ArgumentNullException("org");
            if (elements == null) return;

            foreach (var element in elements)
            {
                SourceAndApplyApiData(org, element);
            }
        }
Пример #15
0
        private void SourceAndApplyApiData(Org org)
        {
            if (org == null) throw new ArgumentNullException("org");

            if (org.HasTriedAllApis) return;

            var elements = _thirdPartyApiManager.ReadElements(org);

            SourceAndApplyApiData(org, elements);
        }
Пример #16
0
        private void CreateOrgFile(Org org)
        {
            if (org == null) throw new ArgumentNullException("org");

            var model = _queryManager.PrepareOrgModel(org);

            WriteFile(model);
        }
Пример #17
0
        public void UpdateOrgFromGoogleResponse(Org org, XContainer element)
        {
            var result = element.Element("result");

            UpdateOrgFromGoogleResponse(result, org);
        }
Пример #18
0
        public static OutputViewModel CreateOrgOutputViewModel(
            Org org,
            PageLinkModel next,
            String descriptionPattern
            )
        {
            if (org == null) throw new ArgumentNullException("org");
            if (next == null) throw new ArgumentNullException("next");

            //var notes = org.Notes.Select(note => new PageLinkModel
            //{
            //    Id = note.Id,
            //    Text = note.Text,
            //    Title = note.Text
            //}).ToList();

            const PageTypeEnum action = PageTypeEnum.Pub;

            var lineage = new Breadcrumb
            {
                Region = new PageLinkModel
                {
                    Filename = org.Authority.ParentAuthority.CleanQualifiedName,
                    Id = org.Id,
                    Text = org.Authority.ParentAuthority.Name,
                    Title = org.Authority.ParentAuthority.Name,
                    Total = org.Authority.ParentAuthority.HauntedPubCount
                },
                Authority = new PageLinkModel
                {
                    Filename = org.Authority.CleanQualifiedName,
                    Id = org.Id,
                    Text = org.Authority.Name,
                    Title = org.Authority.Name,
                    Total = org.Authority.HauntedPubCount
                },
                Locality = new PageLinkModel
                {
                    Filename = org.QualifiedLocalityDashified
                        .Clean(),
                    Id = org.Id,
                    Text = org.Locality,
                    Title = org.Locality
                },
                Organisation = new PageLinkModel
                {
                    Filename = org.Filename,
                    Id = org.Id,
                    Text = org.TradingName,
                    Title = org.TradingName
                }
            };

            var adjusted = new Breadcrumb();

            if (org.IsOutsideUnitedKingdom)
            {
                lineage = adjusted.Swap(lineage);
            }

            var model = new OutputViewModel
            {
                IsMapAvailable = !org.IsOutsideUnitedKingdom,
                Filename = org.Filename,
                JumboTitle = org.JumboTitle,
                PageTitle = org.Title,
                Action = action,
                PageLinks = new List<PageLinkModel>(),
                MetaDescription = org.SeoDescription,
                Description = org.Description,
                ArticleDescription = string.Format(descriptionPattern, org.Address, org.PostcodePrimaryPart),
                Tags = org.Sections,
                Priority = PageTypePriority.Pub,
                Next = next,
                Lat = org.Lat.ToString(),
                Lon = org.Lon.ToString(),
                OtherNames = org.Authority.Orgs
                    .Where(o => o.Address == org.Address
                                && o.Postcode == org.Postcode
                                && o.Id != org.Id)
                    .Select(
                        o => new PageLinkModel
                        {
                            Id = o.Id,
                            Text = o.TradingName,
                            Title = o.TradingName,
                            Filename = o.Filename
                        }
                    ).ToList(),
                Lineage = lineage
            };

            return model;
        }