Example #1
0
        public virtual Dictionary <Type, object> ExtractValues(PXCache sender, object row, PXResult res, IEnumerable <Type> fieldTypes)
        {
            Dictionary <Type, object> result = new Dictionary <Type, object>();
            Dictionary <Type, Type>   selectorFieldByTable = new Dictionary <Type, Type>();

            Type lastField = null;

            foreach (Type field in fieldTypes)
            {
                Type tableType = BqlCommand.GetItemType(field);

                if (tableType != null)
                {
                    if (sender.GetItemType().IsAssignableFrom(tableType) || tableType.IsAssignableFrom(sender.GetItemType()))                    //field of the given table or a base dac/table
                    {
                        if (!result.ContainsKey(field))
                        {
                            result.Add(field, GetFieldValue(sender, row, field, res != null));
                        }

                        lastField = field;
                    }
                    else if (lastField != null && typeof(IBqlTable).IsAssignableFrom(BqlCommand.GetItemType(field)))                    //field of any other table
                    {
                        object foreign = null;
                        if (res != null)
                        {
                            //mass processing - The values are searched in the joined resultset.
                            foreign = res[BqlCommand.GetItemType(field)];

                            if (foreign != null)
                            {
                                PXCache fcache = sender.Graph.Caches[foreign.GetType()];
                                if (!result.ContainsKey(field))
                                {
                                    result.Add(field, GetFieldValue(fcache, foreign, field, false));
                                }
                            }
                        }

                        if (foreign == null)
                        {
                            //lazy loading - The values are selected through the selectors, with a call to DB

                            string selectorFieldName;
                            if (selectorFieldByTable.ContainsKey(tableType))
                            {
                                selectorFieldName = selectorFieldByTable[tableType].Name;
                            }
                            else
                            {
                                selectorFieldName = lastField.Name;
                            }

                            foreign = PXSelectorAttribute.Select(sender, row, selectorFieldName);
                            if (foreign == null)
                            {
                                foreach (PXEventSubscriberAttribute attr in sender.GetAttributesReadonly(selectorFieldName))
                                {
                                    PXAggregateAttribute aggatt = attr as PXAggregateAttribute;

                                    if (aggatt != null)
                                    {
                                        PXDimensionSelectorAttribute dimAttr = aggatt.GetAttribute <PXDimensionSelectorAttribute>();
                                        PXSelectorAttribute          selAttr = aggatt.GetAttribute <PXSelectorAttribute>();
                                        if (dimAttr != null)
                                        {
                                            selAttr = dimAttr.GetAttribute <PXSelectorAttribute>();
                                        }

                                        if (selAttr != null)
                                        {
                                            PXView   select = sender.Graph.TypedViews.GetView(selAttr.PrimarySelect, !selAttr.DirtyRead);
                                            object[] pars   = new object[selAttr.ParsCount + 1];
                                            pars[pars.Length - 1] = sender.GetValue(row, selAttr.FieldOrdinal);
                                            foreign = PXSelectorAttribute.SelectSingleBound(select, new object[] { row, sender.Graph.Accessinfo }, pars);
                                        }
                                    }
                                }
                            }

                            if (foreign is PXResult)
                            {
                                foreign = ((PXResult)foreign)[0];
                            }

                            if (foreign != null)
                            {
                                if (!selectorFieldByTable.ContainsKey(tableType))
                                {
                                    selectorFieldByTable.Add(tableType, lastField);
                                    //result.Remove(lastField);
                                }

                                PXCache fcache = sender.Graph.Caches[foreign.GetType()];
                                if (!result.ContainsKey(field))
                                {
                                    result.Add(field, GetFieldValue(fcache, foreign, field, false));
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }