Exemplo n.º 1
0
        private void PrepGroups(Report rpt, TableWorkClass wc)
        {
            wc.RecursiveGroup = null;
            if (_TableGroups == null)
            {                   // no tablegroups; check to ensure details is grouped
                if (_Details == null || _Details.Grouping == null)
                {
                    return;                                     // no groups to prepare
                }
            }

            int i = 0;
            // 1) Build array of all GroupExpression objects
            List <GroupExpression> gea = new List <GroupExpression>();
            //    count the number of groups
            int countG = 0;

            if (_TableGroups != null)
            {
                countG = _TableGroups.Items.Count;
            }

            Grouping dg = null;
            Sorting  ds = null;

            if (_Details != null && _Details.Grouping != null)
            {
                dg = _Details.Grouping;
                ds = _Details.Sorting;
                countG++;
            }
            GroupEntry[] currentGroups = new GroupEntry[countG++];
            if (_TableGroups != null)
            {                   // add in the groups for the tablegroup
                foreach (TableGroup tg in _TableGroups.Items)
                {
                    if (tg.Grouping.ParentGroup != null)
                    {
                        wc.RecursiveGroup = tg.Grouping;
                    }
                    tg.Grouping.SetIndex(rpt, i);                               // set the index of this group (so we can find the GroupEntry)
                    currentGroups[i++] = new GroupEntry(tg.Grouping, tg.Sorting, 0);
                    foreach (GroupExpression ge in tg.Grouping.GroupExpressions.Items)
                    {
                        gea.Add(ge);
                    }
                }
            }
            if (dg != null)
            {                   // add in the groups for the details grouping
                if (dg.ParentGroup != null)
                {
                    wc.RecursiveGroup = dg;
                }
                dg.SetIndex(rpt, i);                            // set the index of this group (so we can find the GroupEntry)
                currentGroups[i++] = new GroupEntry(dg, ds, 0);
                foreach (GroupExpression ge in dg.GroupExpressions.Items)
                {
                    gea.Add(ge);
                }
            }

            if (wc.RecursiveGroup != null)
            {
                if (gea.Count != 1)                             // Limitiation of implementation
                {
                    throw new Exception("Error: Recursive groups must be the only group definition.");
                }

                PrepRecursiveGroup(rpt, wc);                    // only one group and it's recursive: optimization
                return;
            }

            // Save the typecodes, and grouping by groupexpression; for later use
            TypeCode[] tcs = new TypeCode[gea.Count];
            Grouping[] grp = new Grouping[gea.Count];
            i = 0;
            foreach (GroupExpression ge in gea)
            {
                grp[i]   = (Grouping)(ge.Parent.Parent);                // remember the group
                tcs[i++] = ge.Expression.GetTypeCode();                 // remember type of expression
            }

            // 2) Loop thru the data, then loop thru the GroupExpression list
            wc.Groups = new List <GroupEntry>();
            object[] savValues  = null;
            object[] grpValues  = null;
            int      rowCurrent = 0;

            foreach (Row row in wc.Data.Data)
            {
                // Get the values for all the group expressions
                if (grpValues == null)
                {
                    grpValues = new object[gea.Count];
                }

                i = 0;
                foreach (GroupExpression ge in gea)
                {
                    if (((Grouping)(ge.Parent.Parent)).ParentGroup == null)
                    {
                        grpValues[i++] = ge.Expression.Evaluate(rpt, row);
                    }
                    else
                    {
                        grpValues[i++] = null;                          // Want all the parentGroup to evaluate equal
                    }
                }

                // For first row we just primed the pump; action starts on next row
                if (rowCurrent == 0)                                    // always start new group on first row
                {
                    rowCurrent++;
                    savValues = grpValues;
                    grpValues = null;
                    continue;
                }

                // compare the values; if change then we have a group break
                for (i = 0; i < savValues.Length; i++)
                {
                    if (Filter.ApplyCompare(tcs[i], savValues[i], grpValues[i]) != 0)
                    {
                        // start a new group; and force a break on every subgroup
                        GroupEntry saveGe = null;
                        for (int j = grp[i].GetIndex(rpt); j < currentGroups.Length; j++)
                        {
                            currentGroups[j].EndRow = rowCurrent - 1;
                            if (j == 0)
                            {
                                wc.Groups.Add(currentGroups[j]);                                                // top group
                            }
                            else if (saveGe == null)
                            {
                                currentGroups[j - 1].NestedGroup.Add(currentGroups[j]);
                            }
                            else
                            {
                                saveGe.NestedGroup.Add(currentGroups[j]);
                            }

                            saveGe           = currentGroups[j];                        // retain this GroupEntry
                            currentGroups[j] = new GroupEntry(currentGroups[j].Group, currentGroups[j].Sort, rowCurrent);
                        }
                        savValues = grpValues;
                        grpValues = null;
                        break;                                  // break out of the value comparison loop
                    }
                }
                rowCurrent++;
            }

            // End of all rows force break on end of rows
            for (i = 0; i < currentGroups.Length; i++)
            {
                currentGroups[i].EndRow = rowCurrent - 1;
                if (i == 0)
                {
                    wc.Groups.Add(currentGroups[i]);                                    // top group
                }
                else
                {
                    currentGroups[i - 1].NestedGroup.Add(currentGroups[i]);
                }
            }

            return;
        }