private void butExport_Click(object sender, EventArgs e)
 {
     double exchangeRate;
     Currency pivotCurrency = ServicesProvider.GetInstance().GetCurrencyServices().GetPivot();
     exchangeRate = 1;
     Currency indexCurrency = pivotCurrency;
     if (_CurrencyId > 0)
     {
         indexCurrency = ServicesProvider.GetInstance().GetCurrencyServices().GetCurrency(_CurrencyId);
         if (!pivotCurrency.Equals(indexCurrency))
             exchangeRate =
                 ServicesProvider.GetInstance().GetExchangeRateServices().GetMostRecentlyRate(TimeProvider.Today,indexCurrency);
     }
     if (exchangeRate == 0)
         MessageBox.Show(@"Please enter an exchange rate", @"Information", MessageBoxButtons.OK,
                         MessageBoxIcon.Information);
     else
     {
         ReportService rs = ReportService.GetInstance();
         Report report = rs.GetReportByName("Accounting_Balances.zip");
         if (!report.IsLoaded) return;
         report.AddParam("PIVOT_CURRENCY", pivotCurrency.Code);
         report.AddParam("INDEX_CURRENCY", indexCurrency.Code);
         report.AddParam("currencyId", _CurrencyId);
         rs.LoadReport(report);
         ReportViewerForm frmViewer = new ReportViewerForm(report);
         frmViewer.Show();
     }
 }
        private void activeLoansToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                var reportName = (sender as ToolStripMenuItem).Tag.ToString();
                var report = ReportService.GetInstance().GetReportByName(reportName);
                var reportParamsForm = new ReportParamsForm(report.Params, report.Title);

                if (reportParamsForm.ShowDialog() != DialogResult.OK) return;

                var progressForm = new ReportLoadingProgressForm();
                progressForm.Show();

                var bw = new BackgroundWorker
                {
                    WorkerReportsProgress = true,
                    WorkerSupportsCancellation = true,
                };
                bw.DoWork += (obj, args) =>
                {
                    ReportService.GetInstance().LoadReport(report);
                    bw.ReportProgress(100);
                };
                bw.RunWorkerCompleted += (obj, args) =>
                {
                    progressForm.Close();
                    if (args.Error != null)
                    {
                        Fail(args.Error.Message);
                        return;
                    }
                    if (args.Cancelled) return;

                    report.OpenCount++;
                    report.SaveOpenCount();
                    var reportViewer = new ReportViewerForm(report);
                    reportViewer.Show();
                };
                bw.RunWorkerAsync(report);
            }
            catch (Exception ex)
            {
                Fail(ex.Message);
            }
        }
        protected void ScanReports(bool LookForUnusedDatasets)
        {
            string sCurrentFile = string.Empty;
            try
            {
                UIHierarchy solExplorer = this.ApplicationObject.ToolWindows.SolutionExplorer;
                UIHierarchyItem hierItem = ((UIHierarchyItem)((System.Array)solExplorer.SelectedItems).GetValue(0));
                SolutionClass solution = hierItem.Object as SolutionClass;

                List<string> lstRdls = new List<string>();
                if (hierItem.Object is Project)
                {
                    Project p = (Project)hierItem.Object;
                    lstRdls.AddRange(GetRdlFilesInProjectItems(p.ProjectItems, true));
                }
                else if (hierItem.Object is ProjectItem)
                {
                    ProjectItem pi = hierItem.Object as ProjectItem;
                    Project p = pi.SubProject;
                    lstRdls.AddRange(GetRdlFilesInProjectItems(p.ProjectItems, true));
                }
                else if (solution != null)
                {
                    foreach (Project p in solution.Projects)
                    {
                        lstRdls.AddRange(GetRdlFilesInProjectItems(p.ProjectItems, true));
                    }
                }

                List<UsedRsDataSets.RsDataSetUsage> lstDataSets = new List<UsedRsDataSets.RsDataSetUsage>();
                foreach (string file in lstRdls)
                {
                    sCurrentFile = file;
                    UsedRsDataSets urds = new UsedRsDataSets(file);
                    foreach (UsedRsDataSets.RsDataSet ds in urds.DataSets)
                    {
                        if (LookForUnusedDatasets && ds.Usages.Count == 0)
                        {
                            UsedRsDataSets.RsDataSetUsage u = new UsedRsDataSets.RsDataSetUsage();
                            u.ReportName = ds.ReportName;
                            u.DataSetName = ds.DataSetName;
                            lstDataSets.Add(u);
                        }
                        else if (!LookForUnusedDatasets && ds.Usages.Count > 0)
                        {
                            foreach (string usage in ds.Usages)
                            {
                                UsedRsDataSets.RsDataSetUsage u = new UsedRsDataSets.RsDataSetUsage();
                                u.ReportName = ds.ReportName;
                                u.DataSetName = ds.DataSetName;
                                u.Usage = usage;
                                lstDataSets.Add(u);
                            }
                        }
                    }
                }

                if (lstDataSets.Count == 0)
                {
                    if (LookForUnusedDatasets)
                        MessageBox.Show("All datasets are in use.", "BIDS Helper Unused Datasets Report");
                    else
                        MessageBox.Show("No datasets found.", "BIDS Helper Used Datasets Report");
                }
                else
                {
                    ReportViewerForm frm = new ReportViewerForm();
                    frm.ReportBindingSource.DataSource = lstDataSets;
                    if (LookForUnusedDatasets)
                    {
                        frm.Report = "SSRS.UnusedDatasets.rdlc";
                        frm.Caption = "Unused Datasets Report";
                    }
                    else
                    {
                        frm.Report = "SSRS.UsedDatasets.rdlc";
                        frm.Caption = "Used Datasets Report";
                    }
                    Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
                    reportDataSource1.Name = "BIDSHelper_SSRS_RsDataSetUsage";
                    reportDataSource1.Value = frm.ReportBindingSource;
                    frm.ReportViewerControl.LocalReport.DataSources.Add(reportDataSource1);

                    frm.WindowState = FormWindowState.Maximized;
                    frm.Show();
                }
            }
            catch (System.Exception ex)
            {
                string sError = string.Empty;
                if (!string.IsNullOrEmpty(sCurrentFile)) sError += "Error while scanning report: " + sCurrentFile + "\r\n";
                while (ex != null)
                {
                    sError += ex.Message + "\r\n" + ex.StackTrace + "\r\n\r\n";
                    ex = ex.InnerException;
                }
                MessageBox.Show(sError);
            }
        }
        /// <summary>
        /// Determines if the command should be displayed or not.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        //public override bool DisplayCommand(UIHierarchyItem item)
        //{
        //    try
        //    {
        //        UIHierarchy solExplorer = this.ApplicationObject.ToolWindows.SolutionExplorer;
        //        if (((System.Array)solExplorer.SelectedItems).Length != 1)
        //            return false;

        //        UIHierarchyItem hierItem = ((UIHierarchyItem)((System.Array)solExplorer.SelectedItems).GetValue(0));
        //        string sFileName = ((ProjectItem)hierItem.Object).Name.ToLower();
        //        if (sFileName.EndsWith(".bim"))
        //        {
        //            return true;
        //        }

        //        return (((ProjectItem)hierItem.Object).Object is Cube);
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}


        public override void Exec()
        {
            try
            {
                UIHierarchy     solExplorer = this.ApplicationObject.ToolWindows.SolutionExplorer;
                UIHierarchyItem hierItem    = (UIHierarchyItem)((System.Array)solExplorer.SelectedItems).GetValue(0);
                ProjectItem     projItem    = (ProjectItem)hierItem.Object;

                string sFileName = ((ProjectItem)hierItem.Object).Name.ToLower();
#if DENALI || SQL2014
                Microsoft.AnalysisServices.BackEnd.DataModelingSandbox sandbox = null;
#else
                //Microsoft.AnalysisServices.BackEnd.DataModelingSandboxAmo sandbox = null;
                DataModelingSandboxWrapper sandbox = null;
#endif
                bool bIsTabular = false;
                Cube cub        = null;
                if (projItem.Object is Cube)
                {
                    cub = (Cube)projItem.Object;
                }
                else if (sFileName.EndsWith(".bim"))
                {
#if DENALI || SQL2014
                    sandbox    = TabularHelpers.GetTabularSandboxFromBimFile(this, true);
                    cub        = sandbox.Cube;
                    bIsTabular = true;
                    Microsoft.AnalysisServices.BackEnd.IDataModelingObjectCollection <Microsoft.AnalysisServices.BackEnd.DataModelingMeasure> measures = sandbox.Measures;
#else
                    sandbox = new DataModelingSandboxWrapper(this);
                    //sandbox = TabularHelpers.GetTabularSandboxAmoFromBimFile(this, true);
                    //cub = sandbox.Cube;
                    bIsTabular = true;
                    //Microsoft.AnalysisServices.BackEnd.IDataModelingObjectCollection<Microsoft.AnalysisServices.BackEnd.DataModelingMeasure> measures = sandbox.Cube.AllMeasures;
#endif
                }
                else
                {
                    //if you are launching this feature from the Dimension Usage tab, but some other item in Solution Explorer is highlighted, then this code works and the above doesn't
                    projItem = this.ApplicationObject.ActiveWindow.ProjectItem;
                    cub      = (Cube)projItem.Object;
                }

                DialogResult res = MessageBox.Show("Would you like a detailed report?\r\n\r\nPress Yes to see a detailed dimension usage report.\r\n\r\nPress No to see a summary level Bus Matrix dimension usage report.", "BIDS Helper - Printer Friendly Dimension Usage - Which Report Type?", MessageBoxButtons.YesNo);

                ReportViewerForm frm = new ReportViewerForm();

                if (bIsTabular)
                {
                    bool bIsBusMatrix = (res == DialogResult.No);
                    System.Collections.Generic.List <DimensionUsage> list = PrinterFriendlyDimensionUsage.GetTabularDimensionUsage(sandbox, bIsBusMatrix);
                    DeploymentSettings _deploymentSettings = new DeploymentSettings(projItem);

                    //reset the database and cube name per the deployment settings
                    foreach (DimensionUsage du in list)
                    {
                        du.DatabaseName = _deploymentSettings.TargetDatabase;
                        du.CubeName     = _deploymentSettings.TargetCubeName;
                    }

                    frm.ReportBindingSource.DataSource = list;
                }
                else
                {
                    frm.ReportBindingSource.DataSource = PrinterFriendlyDimensionUsage.GetDimensionUsage(cub);
                }

                if (res == DialogResult.No)
                {
                    frm.Report = "SSAS.PrinterFriendlyDimensionUsageBusMatrix.rdlc";
                }
                else
                {
                    frm.Report = "SSAS.PrinterFriendlyDimensionUsage.rdlc";
                }

                Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
                reportDataSource1.Name  = "BIDSHelper_DimensionUsage";
                reportDataSource1.Value = frm.ReportBindingSource;
                frm.ReportViewerControl.LocalReport.DataSources.Add(reportDataSource1);
                if (res == DialogResult.No)
                {
                    frm.ReportViewerControl.LocalReport.ReportEmbeddedResource = "BIDSHelper.PrinterFriendlyDimensionUsageBusMatrix.rdlc";
                    frm.Caption = "Printer Friendly Dimension Usage Bus Matrix";
                }
                else
                {
                    frm.ReportViewerControl.LocalReport.ReportEmbeddedResource = "BIDSHelper.PrinterFriendlyDimensionUsage.rdlc";
                    frm.Caption = "Printer Friendly Dimension Usage";
                }
                frm.Show();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        public override void Exec()
        {
            try
            {
                UIHierarchy     solExplorer = this.ApplicationObject.ToolWindows.SolutionExplorer;
                UIHierarchyItem hierItem    = (UIHierarchyItem)((System.Array)solExplorer.SelectedItems).GetValue(0);
                ProjectItem     projItem    = (ProjectItem)hierItem.Object;
                DsvColumnResult results     = null;
                string          sFileName   = ((ProjectItem)hierItem.Object).Name.ToLower();

                if (sFileName.EndsWith(".bim"))
                {
                    Microsoft.AnalysisServices.BackEnd.DataModelingSandbox sandbox = TabularHelpers.GetTabularSandboxFromBimFile(this, true);
#if DENALI || SQL2014
                    DataSourceViewCollection dsvs = sandbox.Database.DataSourceViews;
                    foreach (DataSourceView o in dsvs)
                    {
                        results = DsvHelpers.IterateDsvColumns(o);
                    }
#else
                    if (sandbox.IsTabularMetadata)
                    {
                        Microsoft.AnalysisServices.BackEnd.EditMappingUtility util = new Microsoft.AnalysisServices.BackEnd.EditMappingUtility(sandbox);
                        results = new DsvColumnResult(null);

                        foreach (Microsoft.AnalysisServices.BackEnd.DataModelingTable table in sandbox.Tables)
                        {
                            if (table.IsCalculated || table.IsPushedData)
                            {
                                continue;
                            }

                            foreach (Microsoft.AnalysisServices.BackEnd.DataModelingColumn col in table.Columns)
                            {
                                if (col.IsRowNumber || col.IsCalculated)
                                {
                                    continue;
                                }
                                results.UsedColumns.Add(new UnusedColumnsPlugin.UsedColumn(col));
                            }
                        }
                    }
                    else //AMO Tabular
                    {
                        DataSourceViewCollection dsvs = sandbox.AMOServer.Databases[sandbox.DatabaseID].DataSourceViews;
                        foreach (DataSourceView o in dsvs)
                        {
                            results = DsvHelpers.IterateDsvColumns(o);
                        }
                    }
#endif
                }
                else
                {
                    DataSourceView dsv = (DataSourceView)projItem.Object;
                    results = DsvHelpers.IterateDsvColumns(dsv);
                }


                ReportViewerForm frm = new ReportViewerForm();
                frm.ReportBindingSource.DataSource = results.UsedColumns;
                frm.Report = "SSAS.UsedColumns.rdlc";
                Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
                reportDataSource1.Name  = "BIDSHelper_UsedColumn";
                reportDataSource1.Value = frm.ReportBindingSource;
                frm.ReportViewerControl.LocalReport.DataSources.Add(reportDataSource1);
                frm.ReportViewerControl.LocalReport.ReportEmbeddedResource = "SSAS.UsedColumns.rdlc";

                frm.Caption     = "Used Columns Report";
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public override void Exec()
        {
            try
            {
                UIHierarchy     solExplorer = this.ApplicationObject.ToolWindows.SolutionExplorer;
                UIHierarchyItem hierItem    = (UIHierarchyItem)((System.Array)solExplorer.SelectedItems).GetValue(0);
                ProjectItem     projItem    = (ProjectItem)hierItem.Object;
                DsvColumnResult results     = null;
                string          sFileName   = ((ProjectItem)hierItem.Object).Name.ToLower();

                if (sFileName.EndsWith(".bim"))
                {
                    Microsoft.AnalysisServices.BackEnd.DataModelingSandbox sandbox = TabularHelpers.GetTabularSandboxFromBimFile(this, true);
#if DENALI || SQL2014
                    DataSourceViewCollection dsvs = sandbox.Database.DataSourceViews;
                    foreach (DataSourceView o in dsvs)
                    {
                        results = DsvHelpers.IterateDsvColumns(o);
                    }
#else
                    if (sandbox.IsTabularMetadata)
                    {
                        Microsoft.AnalysisServices.BackEnd.EditMappingUtility util = new Microsoft.AnalysisServices.BackEnd.EditMappingUtility(sandbox);
                        results = new DsvColumnResult(null);
                        TabularHelpers.EnsureDataSourceCredentials(sandbox);

                        foreach (Microsoft.AnalysisServices.BackEnd.DataModelingTable table in sandbox.Tables)
                        {
                            if (table.IsCalculated || table.IsPushedData)
                            {
                                continue;
                            }

                            //new 1200 models don't appear to have an equivalent of the DSV where the list of columns from the SQL query are cached, so we will have to get the columns from executing (schema only) the SQL query
                            var conn = ((Microsoft.AnalysisServices.BackEnd.RelationalDataStorage)((util.GetDataSourceConnection(util.GetDataSourceID(table.Id), sandbox)))).DataSourceConnection;
                            conn.Open();
                            System.Data.Common.DbCommand cmd = conn.CreateCommand();
                            cmd.CommandText    = sandbox.GetSourceQueryDefinition(table.Id);
                            cmd.CommandTimeout = 0;
                            cmd.Prepare();
                            System.Data.Common.DbDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly);
                            DataTable tbl = reader.GetSchemaTable();
                            for (int i = 0; i < tbl.Rows.Count; i++)
                            {
                                string sColumnName = Convert.ToString(tbl.Rows[i]["ColumnName"]);
                                Type   oDataType   = (Type)tbl.Rows[i]["DataType"];
                                bool   bFound      = false;
                                foreach (Microsoft.AnalysisServices.BackEnd.DataModelingColumn col in table.Columns)
                                {
                                    if (col.IsCalculated || col.IsRowNumber)
                                    {
                                        continue;
                                    }
                                    if (sColumnName == col.DBColumnName)
                                    {
                                        bFound = true;
                                        break;
                                    }
                                }
                                if (!bFound)
                                {
                                    DataTable  t = new DataTable(table.Name);
                                    DataColumn c = t.Columns.Add(sColumnName, oDataType);

                                    results.UnusedColumns.Add("[" + t.TableName + "].[" + sColumnName + "]", new UnusedColumn(c, null));
                                }
                            }
                        }
                    }
                    else //AMO Tabular
                    {
                        DataSourceViewCollection dsvs = sandbox.AMOServer.Databases[sandbox.DatabaseID].DataSourceViews;
                        foreach (DataSourceView o in dsvs)
                        {
                            results = DsvHelpers.IterateDsvColumns(o);
                        }
                    }
#endif
                }
                else
                {
                    DataSourceView dsv = (DataSourceView)projItem.Object;
                    results = DsvHelpers.IterateDsvColumns(dsv);
                }

                if (results == null || results.UnusedColumns == null || results.UnusedColumns.Count == 0)
                {
                    MessageBox.Show("There are no unused columns.", "BIDS Helper - Unused Columns Report");
                    return;
                }

                ReportViewerForm frm = new ReportViewerForm();
                frm.ReportBindingSource.DataSource = results.UnusedColumns.Values;
                frm.Report = "SSAS.UnusedColumns.rdlc";
                Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
                reportDataSource1.Name  = "BIDSHelper_UnusedColumn";
                reportDataSource1.Value = frm.ReportBindingSource;
                frm.ReportViewerControl.LocalReport.DataSources.Add(reportDataSource1);
                frm.ReportViewerControl.LocalReport.ReportEmbeddedResource = "SSAS.UnusedColumns.rdlc";

                frm.Caption     = "Unused Columns Report";
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }