terça-feira, 21 de julho de 2015

ReportModule

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using Microsoft.Reporting.WebForms;
using System.Configuration;
using System.IO;
using Lapuinka.Controller.DataBase;

namespace Lapuinka.ReportModule
{
    ///
    /// Report Action
    ///
    public enum REPORT_ACTION
    {
        SIMPLE = 0,
        DETAIL = 1
    };

    #region ReportController
    ///
    ///Creation Date:      17-Nov-2009
    ///Name: ReportController
    ///Version:     1.0.0
    ///Purpose:     Control operation in all Reports
    ///1.0.0         10-May-10    FLL          Documentation and Code Standards
    ///
    public class ReportController
    {
        #region Methods

        #region Search()
        ///
        /// Search
        ///
        /// sqlParams
        /// Represents QueryString
        ///
        public static System.Data.DataTable Search(SqlParameter[] sqlParams, string QueryString)
        {
            #region Connection Prepare
            DataTable odt = new System.Data.DataTable();

            SqlDataAdapter adapter = new SqlDataAdapter(QueryString, ConfigurationManager.AppSettings["ConnectionString"]);
            try
            {
                adapter.SelectCommand.CommandType = CommandType.Text;
#if DEBUG
                string strOut = adapter.SelectCommand.CommandText;
                string split = string.Empty;
#endif

                for (int i = 0; i <= sqlParams.Length - 1; i++)
                {
                    SqlParameter param = new SqlParameter(); //need to Clone
                    param.Value = sqlParams[i].Value;
                    param.ParameterName = sqlParams[i].ParameterName;
                    param.IsNullable = sqlParams[i].IsNullable;
                    param.Direction = sqlParams[i].Direction;
                    param.DbType = sqlParams[i].DbType;
                    param.Precision = sqlParams[i].Precision;

                    #region DEBUG Step 2
#if DEBUG
                    if (!(param.Value is DBNull))
                        strOut += "\n" + split + string.Format("{0}={1}", param.ParameterName, param.Value);
                    else
                        strOut += "\n" + split + string.Format("{0}=null", param.ParameterName);
                    split = ",";
#endif
                    #endregion
                    adapter.SelectCommand.Parameters.Add(param);

                }

                #region DEBUG Step 3
#if DEBUG
                DataBaseController.Logger("ReportController.Search", strOut);
#endif
                #endregion

                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                adapter.Fill(odt);

            }
            catch (System.Exception ex)
            {
                throw ex;

            }
            finally
            {
                if (adapter.SelectCommand.Connection.State != ConnectionState.Closed)
                {
                    adapter.SelectCommand.Connection.Close();
                }
            }
            #endregion

            return odt;

        }
        #endregion

        #region RetrieveDataLocalReport
        ///
        /// Retrieve DataSets
        ///
        ///
        ///
        public static ArrayList RetrieveDataLocalReport(string ReportFile, SqlParameter[] sqlParams)
        {
            StreamReader reader = File.OpenText(ReportFile);
            string input = reader.ReadLine();
            string strQuery = string.Empty;
            string dataSource = string.Empty;
            bool EndQuery = false;
            bool readQuery = false;
            ArrayList list = new ArrayList();

            while (input != null)
            {

                input = reader.ReadLine();
                if (input != null)
                {
                    if (input.Contains("))
                        dataSource = input.Replace(", "").Replace("\">", "").Trim();


                    if (input.Contains("
"))
                    {
                        strQuery += input.Replace("", "").Replace("
", "") + "\n";
                        EndQuery = true;
                        readQuery = false;

                    }
                    else if (input.Contains("") || readQuery)
                    {
                        strQuery += input.Replace("", "").Replace("
", "") + "\n";
                        readQuery = true;
                    }

                    if (EndQuery)
                    {
                        strQuery = strQuery.Replace("\n", string.Empty).Trim();

                        EndQuery = false;
                        try
                        {
                            Microsoft.Reporting.WebForms.ReportDataSource rptDataSource = new ReportDataSource();
                            strQuery = System.Web.HttpUtility.HtmlDecode(strQuery);

                            rptDataSource = new Microsoft.Reporting.WebForms.ReportDataSource(dataSource, Search(sqlParams, strQuery));
                            list.Add(rptDataSource);
                        }
                        catch (System.Exception ex)
                        {
                            throw ex;
                        }
                        strQuery = string.Empty;
                    }
                }
            }
            return list;

        }
        #endregion

        #region IgnoreParam

        ///
        /// Ignore Params
        ///
        ///
        ///
        public static bool IgnoreParam(string Key)
        {
            string[] IgnoreParams = ConfigurationManager.AppSettings["IgnoreParams"].ToString().Split(',');
            for (int i = 0; i <= IgnoreParams.Length - 1; i++)
            {
                if (Key == IgnoreParams[i])
                    return true;
            }
            return false;
        }
        #endregion

        #endregion
    }
    #endregion
}



Postar um comentário