/// <summary>
            /// Executes WorkFlow associated with retrieving list of countries.
            /// </summary>
            /// <param name="request">Instance of <see cref="GetAddressRequest"/>.</param>
            /// <returns>Instance of <see cref="GetAddressResponse"/>.</returns>
            private GetAddressResponse GetCountries(GetAddressRequest request)
            {
                // setting language Id value to null if language Id not passing
                if (string.IsNullOrWhiteSpace(request.LanguageId))
                {
                    request.LanguageId = null;
                }

                var serviceRequest = new GetCountryRegionsServiceRequest(request.LanguageId, request.QueryResultSettings);

                var serviceResponse = this.Context.Execute <GetCountryRegionsServiceResponse>(serviceRequest);

                return(new GetAddressResponse(serviceResponse.Results));
            }
Example #2
0
            /// <summary>
            /// Gets the county/region information for the given locale/languageId.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>The response.</returns>
            private static GetCountryRegionsServiceResponse GetCountryRegions(GetCountryRegionsServiceRequest request)
            {
                string languageId = (string)request.Filters[AddressServiceConstants.LanguageIdSqlParameter];

                if (!string.IsNullOrWhiteSpace(languageId))
                {
                    // Do not specify the query settings as country request query setting does not apply for languages.
                    GetSupportedLanguagesDataRequest getLanguagesDataRequest = new GetSupportedLanguagesDataRequest();
                    var languages = request.RequestContext.Execute <EntityDataServiceResponse <SupportedLanguage> >(getLanguagesDataRequest).PagedEntityCollection.Results;

                    var languageIds = new HashSet <string>(languages.Select(l => l.LanguageId).ToList());

                    if (!languageIds.Contains(languageId, StringComparer.OrdinalIgnoreCase))
                    {
                        throw new DataValidationException(
                                  DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_UnsupportedLanguage,
                                  string.Format("Unsupported Language Id specified {0}", languageId));
                    }
                }

                var countryRequest = new GetCountryRegionDataRequest(languageId)
                {
                    QueryResultSettings = request.QueryResultSettings
                };
                GetCountryRegionDataResponse countryDataSets = request.RequestContext.Execute <GetCountryRegionDataResponse>(countryRequest);

                ReadOnlyCollection <CountryRegionInfo> results = countryDataSets.CountryRegionInfo.Results;

                if (results.Any())
                {
                    var addressFormattingResults = countryDataSets.AddressFormattingInfo;
                    if (addressFormattingResults != null)
                    {
                        // Construct the lists of addressFormattingInfo per country
                        var addressFormattingDictionary = new Dictionary <string, IList <AddressFormattingInfo> >();

                        foreach (var addressFormattingInfo in addressFormattingResults)
                        {
                            if (!addressFormattingDictionary.ContainsKey(addressFormattingInfo.CountryRegionId))
                            {
                                addressFormattingDictionary[addressFormattingInfo.CountryRegionId] = new List <AddressFormattingInfo>();
                            }

                            addressFormattingDictionary[addressFormattingInfo.CountryRegionId].Add(addressFormattingInfo);
                        }

                        // Now populate the address formatting info to the countries.
                        foreach (CountryRegionInfo country in results)
                        {
                            if (addressFormattingDictionary.ContainsKey(country.CountryRegionId))
                            {
                                country.AddressFormatLines = addressFormattingDictionary[country.CountryRegionId];
                            }
                        }
                    }
                }
                else
                {
                    // If not country is found, that is certainly a data syncronization issue.
                    NetTracer.Warning("No country information could be found. Ensure you run job A/N-1010 properly.");
                }

                return(new GetCountryRegionsServiceResponse(countryDataSets.CountryRegionInfo));
            }