コード例 #1
0
            void IPrefetchable <DefinitionParams> .Prefetch(DefinitionParams parameters)
            {
                try
                {
                    PXContext.SetSlot <bool>(PXSelectorAttribute.selectorBypassInit, true);
                    List <object> attributes = new List <object>();
                    List <string> distinct   = new List <string>();
                    PXView        view       = null;
                    IBqlSearch    srch       = null;
                    if (parameters.Selector == null)
                    {
                        PXFieldState state = parameters.Foreign.GetStateExt(null, parameters.Field.Name) as PXFieldState;
                        if (state != null && !String.IsNullOrEmpty(state.ViewName))
                        {
                            view = parameters.Main.Graph.Views[state.ViewName];
                            srch = view.BqlSelect as IBqlSearch;
                        }
                    }
                    else
                    {
                        view = new PXView(parameters.Main.Graph, true, parameters.Selector);
                        srch = view.BqlSelect as IBqlSearch;
                    }
                    if (srch != null)
                    {
                        if (!view.AllowSelect)
                        {
                            throw new PXException(ErrorMessages.NotEnoughRightsToAccessObject, view.GetItemType().Name);
                        }
                        object current = parameters.Main.CreateInstance();
                        foreach (object res in view.SelectMultiBound(new object[] { current }))
                        {
                            object attr = (res is PXResult ? ((PXResult)res)[0] : res);
                            if (attr != null)
                            {
                                string name = view.Cache.GetValue(attr, srch.GetField().Name) as string;
                                if (!String.IsNullOrEmpty(name) && !distinct.Contains(name))
                                {
                                    distinct.Add(name);
                                    attributes.Add(attr);
                                }
                            }
                        }

                        // Creating empty graph to get original field state without overridden attributes on CacheAttached in parameters.Main.Graph
                        PXGraph independentGraph = PXGraph.CreateInstance <AttrGraph>();
                        PXCache independentCache = independentGraph.Caches[parameters.Main.GetItemType()];
                        foreach (IBqlParameter par in view.BqlSelect.GetParameters())
                        {
                            if (par.HasDefault && par.GetReferencedType().IsNested&& BqlCommand.GetItemType(par.GetReferencedType()).IsAssignableFrom(parameters.Main.GetItemType()))
                            {
                                PXFieldState state = independentCache.GetStateExt(null, par.GetReferencedType().Name) as PXFieldState;                                 // graph-independent state
                                if (state != null && !String.IsNullOrEmpty(state.ViewName))
                                {
                                    PXView        parview = independentGraph.Views[state.ViewName];
                                    List <object> pars    = new List <object>();
                                    IBqlSearch    parsrch = parview.BqlSelect as IBqlSearch;
                                    if (parsrch != null)
                                    {
                                        foreach (object res in parview.SelectMultiBound(new object[] { current }))
                                        {
                                            object item = res;
                                            if (item is PXResult)
                                            {
                                                item = PXResult.Unwrap(item, parview.CacheGetItemType());
                                            }
                                            if (item is PXFieldState)
                                            {
                                                item = PXFieldState.UnwrapValue(item);
                                            }
                                            object cls = parview.Cache.GetValue(item, parsrch.GetField().Name);
                                            if (cls != null)
                                            {
                                                parameters.Main.SetValue(current, par.GetReferencedType().Name, cls);
                                                foreach (object parres in view.SelectMultiBound(new object[] { current }))
                                                {
                                                    object attr = (parres is PXResult ? ((PXResult)parres)[0] : parres);
                                                    if (attr != null)
                                                    {
                                                        string name = view.Cache.GetValue(attr, srch.GetField().Name) as string;
                                                        if (!String.IsNullOrEmpty(name) && !distinct.Contains(name))
                                                        {
                                                            distinct.Add(name);
                                                            attributes.Add(attr);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }

                    List <PXFieldState> fields = new List <PXFieldState>();
                    foreach (object attr in attributes)
                    {
                        var fs = CreateFieldState(((string)view.Cache.GetValue(attr, srch.GetField().Name)) + '_' + parameters.FieldName,
                                                  (string)view.Cache.GetValue(attr, parameters.DescriptionField),
                                                  (int)view.Cache.GetValue(attr, parameters.ControlTypeField),
                                                  (string)view.Cache.GetValue(attr, parameters.EntryMaskField),
                                                  (string)view.Cache.GetValue(attr, parameters.ListField));

                        if (fs != null)
                        {
                            fields.Add(fs);
                        }
                    }
                    Fields = fields.ToArray();
                }
                finally
                {
                    PXContext.SetSlot <bool>(PXSelectorAttribute.selectorBypassInit, false);
                }
            }