protected void SetupOutsideSqlFetchNarrowingBean(Object pmb, OutsideSqlOption option)
        {
            if (pmb == null || !FetchNarrowingBeanContext.IsTheTypeFetchNarrowingBean(pmb.GetType()))
            {
                return;
            }
            FetchNarrowingBean fetchNarrowingBean = (FetchNarrowingBean)pmb;

            if (option.IsManualPaging)
            {
                fetchNarrowingBean.IgnoreFetchNarrowing();
            }
            FetchNarrowingBeanContext.SetFetchNarrowingBeanOnThread(fetchNarrowingBean);
        }
        // -------------------------------------------------
        //                                     ConditionBean
        //                                     -------------
        protected ConditionBean PreprocessConditionBean(IMethodInvocation invocation)
        {
            OutsideSqlContext outsideSqlContext = GetOutsideSqlContext();

            if (outsideSqlContext != null)
            {
                return(null); // Because it has already finished setting up fetchNarrowingBean for outsideSql here.
            }

            ConditionBean cb = null;

            {
                Object[] args = invocation.Arguments;
                if (args == null || !(args.Length >= 1))
                {
                    return(null);
                }
                Object arg0 = args[0];
                if (arg0 == null)
                {
                    return(null);
                }

                if (!ConditionBeanContext.IsTheArgumentConditionBean(arg0))  // The argument is not condition-bean...
                {
                    if (FetchNarrowingBeanContext.IsTheArgumentFetchNarrowingBean(arg0) && !IsSelectCountIgnoreFetchScopeMethod(invocation))
                    {
                        // Fetch-narrowing-bean and Not select count!
                        FetchNarrowingBeanContext.SetFetchNarrowingBeanOnThread((FetchNarrowingBean)arg0);
                    }
                    return(null);
                }

                cb = (ConditionBean)arg0;
            }

            if (IsSelectCountIgnoreFetchScopeMethod(invocation))
            {
                cb.xsetupSelectCountIgnoreFetchScope();
            }
            else
            {
                FetchNarrowingBeanContext.SetFetchNarrowingBeanOnThread(cb);
            }

            ConditionBeanContext.SetConditionBeanOnThread(cb);
            return(cb);
        }
        public static void RestoreAllContextOnThreadIfExists()
        {
            if (!IsExistContextStackOnThread())
            {
                return;
            }
            Stack <ContextStack> stackOnThread = GetContextStackOnThread();

            if (stackOnThread.Count == 0)
            {
                ClearContextStackOnThread();
                return;
            }
            ContextStack  contextStack = stackOnThread.Pop();
            ConditionBean cb           = contextStack.ConditionBean;

            if (cb != null)
            {
                ConditionBeanContext.SetConditionBeanOnThread(cb);
            }
            OutsideSqlContext outsideSqlContext = contextStack.OutsideSqlContext;

            if (outsideSqlContext != null)
            {
                OutsideSqlContext.SetOutsideSqlContextOnThread(outsideSqlContext);
            }
            FetchNarrowingBean fetchNarrowingBean = contextStack.FetchNarrowingBean;

            if (fetchNarrowingBean != null)
            {
                FetchNarrowingBeanContext.SetFetchNarrowingBeanOnThread(fetchNarrowingBean);
            }
            IDictionary <String, Object> internalMap = contextStack.InternalMap;

            if (internalMap != null)
            {
                InternalMapContext.ClearInternalMapOnThread();
                foreach (String key in internalMap.Keys)
                {
                    Object value = internalMap[key];
                    InternalMapContext.SetObject(key, value);
                }
            }
        }