Click here to Skip to main content
15,896,557 members
Articles / Web Development / ASP.NET

An Open Source RDL Engine

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
20 Dec 2010CPOL3 min read 83.8K   3.5K   55  
An Open Source RDL engine for rendering reports to WinForms or ASP.NET
using System;
using System.Collections.Generic;
using System.Text;
using RdlEngine;

public delegate object RdlExpressionDeletage();

namespace RdlRuntime
{
    public class RuntimeBase
    {
        // 
        protected RdlEngine.Report _rpt;
        // The list of Field elements.  This is set in Report.Render
        protected Fields Fields;
        // The list of ReportParameters elements.  This is set in Report.Render
        protected Parameters Parameters;
        // The list of named expressions.  This is built in Report.Render
        private List<RdlExpressionDeletage> _expressionList = new List<RdlExpressionDeletage>();
        // At every expression the current context is set to the 
        // current DataSet Filters so the Fields can be
        // resolved appropriately.
        private Context _currentContext;

        public delegate object AggrFn();

        public RuntimeBase(RdlEngine.Report rpt)
        {
            Fields = new Fields(this);
            Parameters = new Parameters(_rpt);
        }

        public void AddFunction(RdlExpressionDeletage expression)
        {
            _expressionList.Add(expression);
        }

        internal Context CurrentContext
        {
            get { return _currentContext; }
        }

        internal object Exec(Int32 key, Context ctxt)
        {
            _currentContext = ctxt;
            return _expressionList[key]();
        }

        internal string ExecAsString(Int32 key, Context ctxt)
        {
            _currentContext = ctxt;
            object val = _expressionList[key]();
            if (Convert.IsDBNull(val))
                return null;
            else
                return val.ToString();
        }

        internal string ExecAsString(Int32 key, Context ctxt, string format)
        {
            _currentContext = ctxt;
            object val = _expressionList[key]();
            if (Convert.IsDBNull(val))
                return null;
            else if (format == null || format == string.Empty)
                return val.ToString();
            else
                return String.Format("{0:" + format + "}", val);
        }

        internal bool ExecAsBoolean(Int32 key, Context ctxt)
        {
            _currentContext = ctxt;
            return (bool)_expressionList[key]();
        }

        internal Int32 ExecAsInt(Int32 key, Context ctxt)
        {
            _currentContext = ctxt;
            return (Int32)_expressionList[key]();
        }

        public object sum(AggrFn fn)
        {
            int rowIndex = _currentContext.RowIndex;
            bool doub = false;
            decimal decValue = 0;
            double doubValue = 0;

            for (int i = 0; i < _currentContext.Rows.Count; i++ )
            {
                _currentContext.RowIndex = i;

                object value = fn();
                if (value is System.Single || value is System.Double && !doub)
                {
                    doub = true;
                    doubValue = (double)decValue;
                }
                if (doub)
                    doubValue += (double)value;
                else
                    decValue += Convert.ToDecimal(value);
            }

            _currentContext.RowIndex = rowIndex;
            return (doub) ? (object)doubValue : (object)decValue;
        }

        public object Count(AggrFn fn)
        {
            return _currentContext.Rows.Count;
        }

        public object CountDistinct(AggrFn fn)
        {
            List<object> valueList = new List<object>();
            int rowIndex = _currentContext.RowIndex;

            for (int i = 0; i < _currentContext.Rows.Count; i++)
            {
                _currentContext.RowIndex = i;
                valueList.Add(fn());
            }
            valueList.Sort(RdlEngine.Utility.ApplyCompare);

            int ct = (valueList.Count == 0)?0:1;
            for (int i = 0; i < valueList.Count - 1; i++)
                if (RdlEngine.Utility.ApplyCompare(valueList[i], valueList[i + 1]) != 0)
                    ct++;

            return ct;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sawiki Software
United States United States
I have been a professional software developer for 25+ years, most of it supporting the business community of Maine. I have been working with MS dotnet since 1.0 beta. I organized Sawiki Software LLC as an outlet for some open source software that I have been working on.

Comments and Discussions