Esempio n. 1
0
        //===================================================================================
        /// <summary>
        /// This class loads the EvReportSection model from the flat table given in this report object.
        /// The model is a herarchy of EvReportSection Objects.
        /// AFC 6 nov 2009
        /// </summary>
        /// <returns>EvReportSection: a report section model</returns>
        /// <remarks>
        /// This method consists of the following steps:
        ///
        /// 1. Initialize report section properties: top section, section, detail, low section
        ///
        /// 2. Iterate through the report data records.
        ///
        /// 3. Iterate through the columns.
        ///
        /// 4. If column is not a detail column, use detail column functionality
        ///
        /// 5. If not a detail column, use non-detail column functionality
        /// </remarks>
        // ----------------------------------------------------------------------------------
        public EdReportSection loadModel(EdReport report)
        {
            this.writeDebugLogMethod("loadModel method.");
            //
            // This is the main section and contains the whole report.
            // It has some special characteristichs such as the level and the column values.
            //
            EdReportSection wholeReport = getTopSection( );

            //
            // Stores group column value for each level in the report.
            // This value is used to determine when a group value has changed, to then close that group and
            // all child groups including the detailed group.
            //
            String [] lastIndexValue = new String [5];

            //
            // Define a section for each level in the report.
            //
            EdReportSection [] sections = new EdReportSection [5];

            //
            // Define the detailed section separately as it treated separately.
            //
            EdReportSection detail = null;

            //
            // This stores the lower section before the detail. Is used since this section
            // has a specialized format.
            //
            EdReportSection lowerSection = null;

            //
            // True if the section has changed in this row.
            // This is used for initialize sections when iterating trhough columns.
            //
            bool [] sectionHasChangedInThisRow = new bool [5];

            //
            // Iterate through the report data records.
            //
            for (int record = 0; record < report.DataRecords.Count; record++)
            {
                //
                // Get a data report row
                //
                EdReportRow row = report.DataRecords [record];

                //
                // Iterate through the columns.
                //
                for (int column = 0; column < row.ColumnValues.Length; column++)
                {
                    //
                    // Retrieve the curren column
                    //
                    EdReportColumn currentColumn = report.Columns [column];

                    if (currentColumn.HeaderText == String.Empty)
                    {
                        continue;
                    }

                    //
                    // Extract the value of the current column
                    //
                    String currentValue = row.ColumnValues [column];

                    this.writeDebugLogLine("CurrentValue: " + currentValue);

                    int sectionLvl = currentColumn.SectionLvl;

                    //
                    // If the column is not the detail level, then this must be a group column
                    //
                    // So process the column using group column functionality.
                    //
                    if (sectionLvl != EdReportSection.Detail_Level)
                    {
                        //
                        // Test whether the group index has changed its value.
                        //
                        if (currentColumn.GroupingIndex &&
                            lastIndexValue [currentColumn.SectionLvl] != currentValue)
                        {
                            //
                            // Resets all the lower levels.
                            //
                            for (int level = currentColumn.SectionLvl; level < 5; level++)
                            {
                                lastIndexValue [level] = null;
                            }

                            //
                            // Open a new level.
                            //
                            lastIndexValue [currentColumn.SectionLvl] = currentValue;
                            sections [sectionLvl] = new EdReportSection( );

                            sections [sectionLvl].RowNumber = record;
                            //
                            // Stores the parent. If the previous level is the detail, then the parent is the
                            // whole report. If not, the parent is the previous section
                            //
                            sections [sectionLvl].Parent = sectionLvl - 1 == EdReportSection.Detail_Level ? wholeReport : sections [sectionLvl - 1];

                            sections [sectionLvl].SectionLevel        = sectionLvl;
                            sections [sectionLvl].Layout              = EdReportSection.LayoutTypes.Flat;
                            sections [sectionLvl].GroupingColumnValue = currentValue;
                            sectionHasChangedInThisRow [sectionLvl]   = true;
                            if (sections [sectionLvl].Parent == null)
                            {
                                sections [sectionLvl].Parent = new EdReportSection( );
                            }
                            sections [sectionLvl].Parent.ChildrenList.Add(sections [sectionLvl]);
                        }//END Group index has changed.

                        if (sectionHasChangedInThisRow [sectionLvl])
                        {
                            sections [sectionLvl].ColumnValuesByHeaderText.Add(currentColumn.HeaderText, currentValue);
                            sections [sectionLvl].ColumnList.Add(currentColumn);
                        }

                        //
                        // If the lower section has a level less than the current section level,
                        // then the current become the lower section.
                        //
                        if (lowerSection == null || lowerSection.SectionLevel < currentColumn.SectionLvl)
                        {
                            lowerSection = sections [sectionLvl];
                        }
                    }
                    else
                    {
                        //
                        // This is the section lvl 0 ("Detail").
                        // This assumes that the sections are coninuously enumerated.
                        //
                        if (lastIndexValue [currentColumn.SectionLvl] != ("" + record))
                        {
                            if (lowerSection == null)
                            {
                                lowerSection = wholeReport;
                            }

                            lastIndexValue [currentColumn.SectionLvl] = "" + record;

                            detail           = new EdReportSection( );
                            detail.RowNumber = record;
                            detail.Layout    = EdReportSection.LayoutTypes.Tabular;
                            detail.Parent    = lowerSection;

                            if (report.IsAggregated == true)
                            {
                                //
                                // Is the report is aggregated, then the detail is not visible.
                                //
                                detail.Visible = false;

                                detail.OnlyShowHeadersForTotalColumns = true;
                            }//End if is aggregated.

                            //
                            // Process: If the site header of the report is not empty, and the value of this site
                            // is different to the user organization, then dont show the detail.
                            //
                            else if (report.IsUserSiteFiltered == true)
                            {
                                String detailOrganizationId = detail.searchValueByHeaderText(report.SiteColumnHeaderText, true);

                                //
                                // If the user is not site staff, or site trial coordinator, or site principal investigator
                                // dont show the details.
                                //
                            }//End if it is site filtered.
                            else
                            {
                                detail.Visible = true;
                                detail.OnlyShowHeadersForTotalColumns = false;
                            }

                            lowerSection.ChildrenList.Add(detail);
                            lowerSection.DetailColumnsList = detail.ColumnList;
                        }

                        detail.ColumnValuesByHeaderText.Add(
                            currentColumn.HeaderText,
                            currentValue);

                        detail.ColumnList.Add(currentColumn);

                        if (currentColumn.GroupingType == EdReport.GroupingTypes.Total)
                        {
                            detail.addToAcumulator(currentColumn.HeaderText, currentValue);
                        }

                        //
                        // If the grouping type is count, then add 1 to the acumulator
                        // if the value is not empty.
                        //
                        if (currentColumn.GroupingType == EdReport.GroupingTypes.Count)
                        {
                            if (currentValue.Trim( ) != String.Empty)
                            {
                                detail.addToAcumulator(currentColumn.HeaderText, "1");
                            }
                        }
                    }
                }

                for (int i = 0; i < sectionHasChangedInThisRow.Length; i++)
                {
                    sectionHasChangedInThisRow [i] = false;
                }
                lowerSection = null;
            }

            return(wholeReport);
        }//End loadModel method.
        // ==================================================================================
        /// <summary>
        /// This is the method who will return the actual text of the report.
        /// Depending on the implementation, it will return an html report,
        /// an xml report a csv report and so on.
        /// </summary>
        /// <returns>string: a report text</returns>
        /// <remarks>
        /// This method consists of the following steps:
        ///
        /// 1. Loads the herarchy model of Section of Objects.
        ///
        /// 2. If there is no data then do not build the report
        ///
        /// 3. Generates the report header. This includes the titles and the queries.
        ///
        /// 4. Generates the grouped report. To pain a flat report,
        /// only the detail should be specified.
        ///
        /// 5. Return the report content formatted as Csv.
        /// </remarks>
        // ----------------------------------------------------------------------------------
        public override string getReportData( )
        {
            this.LogMethod("getReportData method.");
            //
            // Define the methods variables and objects.
            //
            StringBuilder stCsv = new StringBuilder( );

            //
            // If there is no data then do not build the report
            //
            if (this._report == null)
            {
                this.LogValue("No Data.");
                return(String.Empty);
            }

            //
            // Add the query name to the output data.
            //
            String QueryData  = String.Empty;
            int    queryCount = 0;

            foreach (EdReportQuery query in this._report.Queries)
            {
                if (query == null)
                {
                    continue;
                }

                if (query.FieldName != String.Empty)
                {
                    if (queryCount > 0)
                    {
                        QueryData += this._separator;
                    }
                    QueryData += this.getTextCSV("Q:" + query.FieldName);
                }
                queryCount++;
            }//END query header iteration loop.

            if (QueryData != String.Empty)
            {
                stCsv.Append(QueryData);
                stCsv.Append(this._separator);
            }

            //
            // Output the report column header.
            //
            for (int col = 0; col < this._report.Columns.Count; col++)
            {
                EdReportColumn column = this._report.Columns [col];
                if (col > 0)
                {
                    stCsv.Append(this._separator);
                }
                if (column.ColumnId == String.Empty)
                {
                    stCsv.Append(this.getTextCSV(column.SourceField));
                }
                else
                {
                    stCsv.Append(this.getTextCSV(column.ColumnId));
                }
            }
            stCsv.AppendLine("");



            //
            // Add the query values to the output data.
            //
            QueryData  = String.Empty;
            queryCount = 0;
            foreach (EdReportQuery query in this._report.Queries)
            {
                if (query == null)
                {
                    continue;
                }

                if (query.FieldName != String.Empty)
                {
                    if (queryCount > 0)
                    {
                        QueryData += this._separator;
                    }
                    QueryData += this.getTextCSV(query.Value);
                }
                queryCount++;
            }//END query value iteration loop.

            //
            // Output the report data.
            //
            for (int row = 0; row < this._report.DataRecords.Count; row++)
            {
                if (QueryData != String.Empty)
                {
                    stCsv.Append(QueryData);
                    stCsv.Append(this._separator);
                }

                //
                // the row of data.
                //
                EdReportRow rowData = this._report.DataRecords [row];

                //
                // column interation loop.
                //
                for (int col = 0; col < rowData.ColumnValues.Length; col++)
                {
                    if (col > 0)
                    {
                        stCsv.Append(this._separator);
                    }

                    stCsv.Append(this.getTextCSV(rowData.ColumnValues [col]));
                }//END column interation loop.

                stCsv.AppendLine("");
            }//END row interation loop

            //
            // Return the report content formatted as Csv.
            //
            this.LogMethodEnd("getReportData");
            return(stCsv.ToString());
        }//End getReportData class