private EntityDataServiceResponse <ReasonCode> GetReturnOrderReasonCodes(GetReturnOrderReasonCodesDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.QueryResultSettings, "request.QueryResultSettings");

                ReasonCodeL2CacheDataStoreAccessor level2CacheDataAccessor = GetReasonCodeL2CacheDataStoreAccessor(request.RequestContext);

                bool found;
                bool updateL2Cache;
                PagedResult <ReasonCode> result = DataManager.GetDataFromCache(() => level2CacheDataAccessor.GetReturnOrderReasonCodes(request.QueryResultSettings), out found, out updateL2Cache);

                if (!found)
                {
                    var query = new SqlPagedQuery(request.QueryResultSettings)
                    {
                        From    = ReturnOrderReasonCodesViewName,
                        OrderBy = InfoCodeIdColumnName
                    };

                    query.Parameters["@DataAreaId"] = request.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;

                    List <string> whereClauses = new List <string>();
                    whereClauses.Add(@"(DATAAREAID = @DataAreaId)");
                    query.Where = string.Join(" AND ", whereClauses);

                    // Load info codes
                    using (DatabaseContext databaseContext = new DatabaseContext(request.RequestContext))
                    {
                        result = databaseContext.ReadEntity <ReasonCode>(query);
                    }

                    updateL2Cache &= result != null &&
                                     result.Results.Count < MaxCachedCollectionSize;
                }

                if (updateL2Cache)
                {
                    level2CacheDataAccessor.PutReturnOrderReasonCodes(request.QueryResultSettings, result);
                }

                return(new EntityDataServiceResponse <ReasonCode>(result));
            }
            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));
            }
            private static EntityDataServiceResponse <ReasonCode> GetReasonCodes(GetReasonCodesDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.QueryResultSettings, "request.QueryResultSettings");

                string channelLanguageId  = GetDefaultLanguageId(request.RequestContext);
                string employeeLanguageId = GetEmployeeLanguageId(request.RequestContext);

                ReasonCodeL2CacheDataStoreAccessor level2CacheDataAccessor = GetReasonCodeL2CacheDataStoreAccessor(request.RequestContext);

                bool found;
                bool updateL2Cache;
                PagedResult <ReasonCode> result = DataManager.GetDataFromCache(() => level2CacheDataAccessor.GetReasonCodes(request.ReasonCodeIds, channelLanguageId, employeeLanguageId, request.QueryResultSettings), out found, out updateL2Cache);

                if (!found)
                {
                    SortingInfo sortingInfo = request.QueryResultSettings.Sorting;
                    if (!sortingInfo.IsSpecified)
                    {
                        sortingInfo = new SortingInfo(ReasonCode.PriorityColumn, false);
                    }

                    var query = new SqlPagedQuery(request.QueryResultSettings)
                    {
                        OrderBy = sortingInfo.ToString(),
                        From    = ReasonCodeFunctionName,
                        Aliased = true,
                    };

                    AddLanguageIdToQuery(query, request.RequestContext);
                    AddDataAreaIdToQuery(query, request.RequestContext);

                    // Load info codes
                    using (SqlServerDatabaseContext sqlServerDatabaseContext = new SqlServerDatabaseContext(request))
                        using (StringIdTableType groupIds = new StringIdTableType(request.ReasonCodeIds, "GROUPID"))
                        {
                            // the view sets the INFOCODEID to GROUPID when the reason code is not part of a group, so we always query by GROUPID
                            query.Parameters["@tvp_groupIds"] = groupIds.DataTable;

                            result = sqlServerDatabaseContext.ReadEntity <ReasonCode>(query);
                        }

                    // Load sub codes
                    if (result.Results.Any())
                    {
                        QueryResultSettings subCodeSettings = QueryResultSettings.AllRecords;
                        var subcodes = GetReasonSubCodes(result.Results.Select(x => x.ReasonCodeId), null, subCodeSettings, request).Results.ToLookup(x => x.ReasonCodeId);

                        foreach (var infoCode in result.Results)
                        {
                            infoCode.ReasonSubCodes.Clear();
                            infoCode.ReasonSubCodes.AddRange(subcodes[infoCode.ReasonCodeId]);
                        }
                    }

                    updateL2Cache &= result != null && result.Results.Count < MaxCachedCollectionSize;
                }

                if (updateL2Cache)
                {
                    level2CacheDataAccessor.PutReasonCodes(request.ReasonCodeIds, channelLanguageId, employeeLanguageId, request.QueryResultSettings, result);
                }

                return(new EntityDataServiceResponse <ReasonCode>(result));
            }
            private EntityDataServiceResponse <ReasonCode> GetReasonCodesByTableRefType(GetReasonCodesByTableRefTypeDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.QueryResultSettings, "request.QueryResultSettings");

                ReasonCodeL2CacheDataStoreAccessor level2CacheDataAccessor = GetReasonCodeL2CacheDataStoreAccessor(request.RequestContext);

                bool found;
                bool updateL2Cache;
                PagedResult <ReasonCode> result = DataManager.GetDataFromCache(() => level2CacheDataAccessor.GetReasonCodeSpecific(request.TableRefType, request.RefRelation, request.RefRelation2, request.RefRelation3), out found, out updateL2Cache);

                if (!found)
                {
                    var query = new SqlPagedQuery(QueryResultSettings.AllRecords)
                    {
                        From    = ReasonCodeTableSpecificViewName,
                        OrderBy = InfoCodeIdColumnName
                    };

                    var whereClauses = new List <string>();

                    whereClauses.Add(@"(REFTABLEID = @RefTableId)");
                    query.Parameters["@RefTableId"] = request.TableRefType;

                    // Add query clause for ref relations
                    if (!string.IsNullOrWhiteSpace(request.RefRelation))
                    {
                        whereClauses.Add(@"(REFRELATION = @RefRelation)");
                        query.Parameters["@RefRelation"] = request.RefRelation;
                    }

                    if (!string.IsNullOrWhiteSpace(request.RefRelation2))
                    {
                        whereClauses.Add(@"(REFRELATION2 = @RefRelation2)");
                        query.Parameters["@RefRelation2"] = request.RefRelation2;
                    }

                    if (!string.IsNullOrWhiteSpace(request.RefRelation3))
                    {
                        whereClauses.Add(@"(REFRELATION3 = @RefRelation3)");
                        query.Parameters["@RefRelation3"] = request.RefRelation3;
                    }

                    // Compose the where clause
                    if (whereClauses.Count != 0)
                    {
                        query.Where = string.Join(" AND ", whereClauses);
                    }

                    query.OrderBy = "SEQUENCE ASC";

                    var reasonCodes = new Collection <ReasonCode>();

                    // Load specific reason codes only if at least one refRelation specified with tableRefType
                    if (whereClauses.Count >= 2)
                    {
                        PagedResult <ReasonCodeSpecific> reasonCodesSpecific;

                        using (DatabaseContext databaseContext = new DatabaseContext(request.RequestContext))
                        {
                            reasonCodesSpecific = databaseContext.ReadEntity <ReasonCodeSpecific>(query);
                        }

                        if (reasonCodesSpecific != null)
                        {
                            foreach (var reasonCodeSpecific in reasonCodesSpecific.Results)
                            {
                                GetReasonCodesDataRequest getReasonCodesDataRequest = new GetReasonCodesDataRequest(QueryResultSettings.AllRecords, new string[] { reasonCodeSpecific.ReasonCodeId });
                                var machingReasonCodes = request.RequestContext.Runtime.Execute <EntityDataServiceResponse <ReasonCode> >(getReasonCodesDataRequest, request.RequestContext).PagedEntityCollection.Results;

                                foreach (var matchingCode in machingReasonCodes)
                                {
                                    matchingCode.InputRequired     = reasonCodeSpecific.InputRequired;
                                    matchingCode.InputRequiredType = (ReasonCodeInputRequiredType)reasonCodeSpecific.InputRequiredTypeValue;
                                }

                                reasonCodes.AddRange(machingReasonCodes);
                            }
                        }
                    }

                    result = reasonCodes.AsPagedResult();

                    updateL2Cache &= result != null && result.Results.Count < MaxCachedCollectionSize;
                }

                if (updateL2Cache)
                {
                    level2CacheDataAccessor.PutReasonCodeSpecific(request.TableRefType, request.RefRelation, request.RefRelation2, request.RefRelation3, result);
                }

                return(new EntityDataServiceResponse <ReasonCode>(result));
            }