Example #1
0
            /// <summary>
            /// Gets the reason code settings dictionary.
            /// </summary>
            /// <param name="context">The context.</param>
            /// <returns>The reason code settings dictionary.</returns>
            private static ReasonCodeSettings GetReasonCodeSettings(RequestContext context)
            {
                ReasonCodeSettings settings = null;

                GetReasonCodeSettingsDataRequest getReasonCodeSettingsDataRequest = new GetReasonCodeSettingsDataRequest(QueryResultSettings.SingleRecord);

                settings = context.Runtime.Execute <SingleEntityDataServiceResponse <ReasonCodeSettings> >(getReasonCodeSettingsDataRequest, context).Entity;

                // Reason code settings should be available for retail store.
                if (settings == null)
                {
                    var     getChannelByIdDataRequest = new GetChannelByIdDataRequest(context.GetPrincipal().ChannelId);
                    Channel currentChannel            = context.Runtime.Execute <SingleEntityDataServiceResponse <Channel> >(getChannelByIdDataRequest, context).Entity;

                    if (currentChannel.OrgUnitType == RetailChannelType.RetailStore)
                    {
                        throw new DataValidationException(
                                  DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectNotFound,
                                  "The required reason code settings not found.");
                    }

                    // Use default settings for non-retail-store channels.
                    settings = ReasonCodeSettings.DefaultSettings;
                }

                return(settings);
            }
            private SingleEntityDataServiceResponse <ReasonCodeSettings> GetReasonCodeSettings(GetReasonCodeSettingsDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.QueryResultSettings, "request.QueryResultSettings");

                ReasonCodeL2CacheDataStoreAccessor level2CacheDataAccessor = GetReasonCodeL2CacheDataStoreAccessor(request.RequestContext);

                bool found;
                bool updateL2Cache;
                ReasonCodeSettings result = DataManager.GetDataFromCache(() => level2CacheDataAccessor.GetReasonCodeSettings(request.QueryResultSettings.ColumnSet), out found, out updateL2Cache);

                if (!found)
                {
                    var query = new SqlPagedQuery(QueryResultSettings.SingleRecord)
                    {
                        Select = request.QueryResultSettings.ColumnSet,
                        From   = ReasonCodeSettingsViewName,
                        Where  = "CHANNELID = @channelId",
                    };

                    query.Parameters["@channelId"] = request.RequestContext.GetPrincipal().ChannelId;

                    using (DatabaseContext databaseContext = new DatabaseContext(request.RequestContext))
                    {
                        result = databaseContext.ReadEntity <ReasonCodeSettings>(query).SingleOrDefault();

                        updateL2Cache &= result != null;
                    }
                }

                if (updateL2Cache)
                {
                    level2CacheDataAccessor.PutReasonCodeSettings(result);
                }

                return(new SingleEntityDataServiceResponse <ReasonCodeSettings>(result));
            }