Exemple #1
0
            private GetCountryRegionDataResponse GetCountryRegion(GetCountryRegionDataRequest request)
            {
                ThrowIf.Null(request.QueryResultSettings, "settings");

                string languageId = request.LanguageId;

                if (string.IsNullOrWhiteSpace(languageId))
                {
                    var getDefaultLanguageIdDataRequest = new GetDefaultLanguageIdDataRequest();
                    languageId = request.RequestContext.Runtime.Execute <SingleEntityDataServiceResponse <string> >(getDefaultLanguageIdDataRequest, request.RequestContext).Entity;
                }

                var parameterSet = new ParameterSet();

                parameterSet["nvc_LanguageId"] = languageId;

                Tuple <PagedResult <CountryRegionInfo>, ReadOnlyCollection <AddressFormattingInfo> > countryRegions;

                using (SqlServerDatabaseContext sqlServerDatabaseContext = new SqlServerDatabaseContext(request))
                {
                    countryRegions = sqlServerDatabaseContext.ExecuteStoredProcedure <CountryRegionInfo, AddressFormattingInfo>(GetCountryRegionInfoSprocName, parameterSet);
                }

                return(new GetCountryRegionDataResponse(countryRegions.Item1, countryRegions.Item2));
            }
            /// <summary>
            /// Initializes a new instance of the <see cref="CountryRegionMapper"/> class.
            /// </summary>
            /// <param name="context">The request context.</param>
            public CountryRegionMapper(RequestContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }

                GetDefaultLanguageIdDataRequest languageIdRequest = new GetDefaultLanguageIdDataRequest();
                string languageId = context.Execute <SingleEntityDataServiceResponse <string> >(languageIdRequest).Entity;
                GetCountryRegionDataRequest countryRequest = new GetCountryRegionDataRequest(languageId)
                {
                    QueryResultSettings = QueryResultSettings.AllRecords
                };

                var response = context.Execute <GetCountryRegionDataResponse>(countryRequest);

                IEnumerable <CountryRegionInfo> countryRegions = response.CountryRegionInfo.Results;

                this.mapCountryRegionToIsoCode = countryRegions.ToDictionary(key => key.CountryRegionId, value => value.ISOCode);
                this.mapIsoCodeToCountryRegion = countryRegions.ToDictionary(key => key.ISOCode, value => value.CountryRegionId);
            }
            /// <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));
            }