Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arguments"></param>
        /// <param name="connection"></param>
        /// <returns></returns>
        private static Domain.Application GetApplication(XmlCastReport arguments, WSConnection connection)
        {
            List <Domain.Application> applications;

            using (CastDomainBLL castDomainBLL = new CastDomainBLL(connection))
            {
                applications = castDomainBLL.GetApplications();
            }

            if (arguments.Database != null && arguments.Domain == null)
            {
                return(applications.FirstOrDefault(_ => _.Name == arguments.Application.Name && _.AdgDatabase == arguments.Database.Name));
            }

            if (arguments.Database == null && arguments.Domain != null)
            {
                return(applications.FirstOrDefault(_ => _.Name == arguments.Application.Name && _.DomainId == arguments.Domain.Name));
            }

            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            // For code readability I keep this redundancy
            if (arguments.Database != null && arguments.Domain != null)
            {
                return(applications.FirstOrDefault(_ => _.Name == arguments.Application.Name && _.AdgDatabase == arguments.Database.Name && _.DomainId == arguments.Domain.Name));
            }

            return(applications.FirstOrDefault(_ => _.Name == arguments.Application.Name));
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        /// <param name="help"></param>
        /// <returns></returns>
        private static int DoWork(string[] args, out string help)
        {
            LogHelper.Instance.LogInfo("Read arguments.");
            XmlCastReport arguments = ReadArguments(args, out help);

            if (!string.IsNullOrEmpty(help))
            {
                return(-1);
            }

            if (arguments.Culture != null)
            {
                SetCulture(arguments.Culture.Name);
            }

            string pathFile = GenerateReport(arguments, out help);

            if (!string.IsNullOrEmpty(help))
            {
                LogHelper.Instance.LogError(help);
                if (help.Contains("Webservice can't be access or is bad formatted."))
                {
                    return(-2);
                }
                return(-3);
            }

            LogHelper.Instance.LogInfo($"Report successfully generated in {pathFile}");
            return(0);
        }
Beispiel #3
0
 private static void ConvertToPdfIfNeeded(XmlCastReport arguments, ref string reportPath, string tmpReportFile)
 {
     // convert docx or pptx to pdf
     if (reportPath.Contains(".pdf"))
     {
         if (tmpReportFile.Contains(".docx"))
         {
             try
             {
                 Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                 Document wordDocument = appWord.Documents.Open(tmpReportFile);
                 wordDocument.ExportAsFixedFormat(reportPath, WdExportFormat.wdExportFormatPDF);
                 wordDocument.Close();
                 appWord.Quit();
             }
             catch (Exception e)
             {
                 // Error if office not installed, then do not save as pdf
                 LogHelper.Instance.LogWarn("Report cannot be saved as pdf : " + e.Message);
                 reportPath = reportPath.Replace(".pdf", Path.GetExtension(arguments.Template.Name));
                 File.Copy(tmpReportFile, reportPath, true);
             }
         }
         else if (tmpReportFile.Contains(".pptx"))
         {
             try
             {
                 Microsoft.Office.Interop.PowerPoint.Application appPowerpoint = new Microsoft.Office.Interop.PowerPoint.Application();
                 Presentation appPres = appPowerpoint.Presentations.Open(tmpReportFile);
                 appPres.ExportAsFixedFormat(reportPath, PpFixedFormatType.ppFixedFormatTypePDF);
                 appPres.Close();
                 appPowerpoint.Quit();
             }
             catch (Exception e)
             {
                 // Error if office not installed, then do not save as pdf
                 LogHelper.Instance.LogWarn("Report cannot be saved as pdf : " + e.Message);
                 reportPath = reportPath.Replace(".pdf", Path.GetExtension(arguments.Template.Name));
                 File.Copy(tmpReportFile, reportPath, true);
             }
         }
         else
         {
             string report = reportPath.Replace(".pdf", Path.GetExtension(arguments.Template.Name));
             File.Copy(tmpReportFile, report, true);
         }
     }
     else
     {
         //Copy report file to the selected destination
         File.Copy(tmpReportFile, reportPath, true);
     }
 }
Beispiel #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="arguments"></param>
 private static void SetFileName(XmlCastReport arguments)
 {
     if (arguments.File == null)
     {
         arguments.File = new XmlTagName()
         {
             Name = null
         };
     }
     if (string.IsNullOrEmpty(arguments.File.Name))
     {
         arguments.File.Name = $"{Path.GetFileNameWithoutExtension(arguments.Template.Name)}_{DateTime.Now:yyyy-MM-dd_hh-mm-ss}{Path.GetExtension(arguments.Template.Name)}";
     }
 }
Beispiel #5
0
        /// <summary>
        /// Read arguments from Main
        /// </summary>
        /// <param name="pArg"></param>
        /// <param name="pShowHelp"></param>
        /// <returns></returns>
        public static XmlCastReport ReadArguments(string[] pArg, out string pShowHelp)
        {
            pShowHelp = null;
            XmlCastReport argument = null;

            // Get Arguments File Name
            using (CRArgumentReader reader = new CRArgumentReader())
            {
                bool show;
                argument = reader.Load(pArg, out show);
                if (show)
                {
                    pShowHelp = reader.Help;
                }
            }

            return(argument);
        }
Beispiel #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="arguments"></param>
 private static void SetFileName(XmlCastReport arguments)
 {
     if (arguments.File == null)
     {
         arguments.File = new XmlTagName()
         {
             Name = null
         };
     }
     if (string.IsNullOrEmpty(arguments.File.Name))
     {
         arguments.File.Name = string.Format
                                   ("{0}_{1}{2}"
                                   , Path.GetFileNameWithoutExtension(arguments.Template.Name)
                                   , DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss")
                                   , Path.GetExtension(arguments.Template.Name)
                                   );
     }
 }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arguments"></param>
        /// <param name="help"></param>
        /// <returns></returns>
        private static string GenerateReport(XmlCastReport arguments, out string help)
        {
            if (arguments.ReportType != null)
            {
                string reportPath    = string.Empty;
                string tmpReportFile = string.Empty;

                try
                {
                    help = string.Empty;

                    //Get RG settings
                    var settings = SettingsBLL.GetSetting();
                    LogHelper.Instance.LogInfo("RG settings have been read successfully");

                    //Initialize temporary directory
                    string workDirectory = SettingsBLL.GetApplicationPath();

                    //Initialize Web services

                    var connection = new WSConnection(arguments.Webservice.Name, arguments.Username.Name, arguments.Password.Name, string.Empty)
                    {
                        ApiKey = arguments.ApiKey?.Name.Equals("true") ?? false
                    };
                    using (CommonBLL commonBLL = new CommonBLL(connection))
                    {
                        if (!commonBLL.CheckService())
                        {
                            help = $"Webservice can't be access or is bad formatted. Url:{arguments.Webservice.Name}";
                            return(string.Empty);
                        }
                    }
                    LogHelper.Instance.LogInfo("Web services Initialized successfully");

                    List <Domain.Application> _apps = new List <Domain.Application>();

                    try
                    {
                        using (CastDomainBLL castDomainBLL = new CastDomainBLL(connection))
                        {
                            _apps = castDomainBLL.GetCommonTaggedApplications(arguments.Tag?.Name);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.LogInfo("Error occured while trying get applications for the portfolio : " + ex.Message);
                    }

                    Domain.Application[] _selectedApps = _apps.ToArray <Domain.Application>();
                    LogHelper.Instance.LogInfo("Applications in the portfolio found successfully");
                    string[] _appsToIgnorePortfolioResult = PortfolioBLL.BuildPortfolioResult(connection, _selectedApps);
                    LogHelper.Instance.LogInfo("Build result for the portfolio");
                    List <Domain.Application> _n_apps = new List <Domain.Application>();
                    //Remove from Array the Ignored Apps
                    foreach (Domain.Application app in _selectedApps)
                    {
                        int intAppYes = 0;
                        foreach (string s in _appsToIgnorePortfolioResult)
                        {
                            if (s == app.Name)
                            {
                                intAppYes = 1;
                                break;
                            }
                            else
                            {
                                intAppYes = 0;
                            }
                        }

                        if (intAppYes == 0)
                        {
                            _n_apps.Add(app);
                        }
                    }
                    Domain.Application[] _n_selectedApps = _n_apps.ToArray();

                    List <Snapshot> _snapshots = new List <Snapshot>();

                    try
                    {
                        using (CastDomainBLL castDomainBLL = new CastDomainBLL(connection))
                        {
                            _snapshots = castDomainBLL.GetAllSnapshots(_n_selectedApps);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.LogInfo("Error occured while trying get snapshots of applications for the portfolio : " + ex.Message);
                    }
                    LogHelper.Instance.LogInfo("Snapshots in the portfolio found successfully");
                    List <Snapshot> _n_snaps = new List <Snapshot>();
                    if (_snapshots != null)
                    {
                        Snapshot[] _selectedApps_snapshots = _snapshots.ToArray <Snapshot>();
                        var        _snapsToIgnore          = PortfolioSnapshotsBLL.BuildSnapshotResult(connection, _selectedApps_snapshots, true);
                        LogHelper.Instance.LogInfo("Build result for snapshots in portfolio");

                        foreach (Snapshot snap in _selectedApps_snapshots)
                        {
                            int intRemoveYes = 0;
                            foreach (string s in _snapsToIgnore)
                            {
                                if (s == snap.Href)
                                {
                                    intRemoveYes = 1;
                                    break;
                                }
                                else
                                {
                                    intRemoveYes = 0;
                                }
                            }
                            if (intRemoveYes == 0)
                            {
                                _n_snaps.Add(snap);
                            }
                        }

                        Snapshot[] _n_selectedApps_snapshots = _n_snaps.ToArray();

                        string tmpReportFileFlexi = string.Empty;

                        try
                        {
                            //Create temporary report
                            tmpReportFile = PathUtil.CreateTempCopy(workDirectory, Path.Combine(settings.ReportingParameter.TemplatePath, "Portfolio", arguments.Template.Name));
                            if (tmpReportFile.Contains(".xlsx"))
                            {
                                tmpReportFileFlexi = PathUtil.CreateTempCopyFlexi(workDirectory, arguments.Template.Name);
                            }
                            //Build report
                            ReportData reportData;
                            if (arguments.Category != null && arguments.Tag != null)
                            {
                                reportData = new ReportData
                                {
                                    FileName         = tmpReportFile,
                                    Application      = null,
                                    CurrentSnapshot  = null,
                                    PreviousSnapshot = null,
                                    RuleExplorer     = new RuleBLL(connection),
                                    CurrencySymbol   = "$",
                                    ServerVersion    = CommonBLL.GetServiceVersion(connection),
                                    Applications     = _n_selectedApps,
                                    Category         = arguments.Category.Name,
                                    Tag                 = arguments.Tag.Name,
                                    Snapshots           = _n_selectedApps_snapshots,
                                    IgnoresApplications = _appsToIgnorePortfolioResult,
                                    IgnoresSnapshots    = _snapsToIgnore,
                                    Parameter           = settings.ReportingParameter
                                };
                            }
                            else if (arguments.Category != null && arguments.Tag == null)
                            {
                                reportData = new ReportData
                                {
                                    FileName         = tmpReportFile,
                                    Application      = null,
                                    CurrentSnapshot  = null,
                                    PreviousSnapshot = null,
                                    RuleExplorer     = new RuleBLL(connection),
                                    CurrencySymbol   = "$",
                                    ServerVersion    = CommonBLL.GetServiceVersion(connection),
                                    Applications     = _n_selectedApps,
                                    Category         = arguments.Category.Name,
                                    Tag                 = null,
                                    Snapshots           = _n_selectedApps_snapshots,
                                    IgnoresApplications = _appsToIgnorePortfolioResult,
                                    IgnoresSnapshots    = _snapsToIgnore,
                                    Parameter           = settings.ReportingParameter
                                };
                            }
                            else if (arguments.Category == null && arguments.Tag != null)
                            {
                                reportData = new ReportData
                                {
                                    FileName         = tmpReportFile,
                                    Application      = null,
                                    CurrentSnapshot  = null,
                                    PreviousSnapshot = null,
                                    RuleExplorer     = new RuleBLL(connection),
                                    CurrencySymbol   = "$",
                                    ServerVersion    = CommonBLL.GetServiceVersion(connection),
                                    Applications     = _n_selectedApps,
                                    Category         = null,
                                    Tag                 = arguments.Tag.Name,
                                    Snapshots           = _n_selectedApps_snapshots,
                                    IgnoresApplications = _appsToIgnorePortfolioResult,
                                    IgnoresSnapshots    = _snapsToIgnore,
                                    Parameter           = settings.ReportingParameter
                                };
                            }
                            else
                            {
                                reportData = new ReportData
                                {
                                    FileName         = tmpReportFile,
                                    Application      = null,
                                    CurrentSnapshot  = null,
                                    PreviousSnapshot = null,
                                    RuleExplorer     = new RuleBLL(connection),
                                    CurrencySymbol   = "$",
                                    ServerVersion    = CommonBLL.GetServiceVersion(connection),
                                    Applications     = _n_selectedApps,
                                    Category         = null,
                                    Tag                 = null,
                                    Snapshots           = _n_selectedApps_snapshots,
                                    IgnoresApplications = _appsToIgnorePortfolioResult,
                                    IgnoresSnapshots    = _snapsToIgnore,
                                    Parameter           = settings.ReportingParameter
                                };
                            }

                            using (IDocumentBuilder docBuilder = BuilderFactory.CreateBuilder(reportData, tmpReportFile))
                            {
                                docBuilder.BuildDocument();
                            }
                            LogHelper.Instance.LogInfo("Report generated successfully");

                            //Set filte report
                            SetFileName(arguments);

                            reportPath = Path.Combine(string.IsNullOrEmpty(settings.ReportingParameter.GeneratedFilePath)
                                ? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                                : settings.ReportingParameter.GeneratedFilePath, arguments.File.Name);

                            if (tmpReportFile.Contains(".xlsx"))
                            {
                                tmpReportFile = tmpReportFileFlexi;
                            }

                            ConvertToPdfIfNeeded(arguments, ref reportPath, tmpReportFile);
                        }
                        finally
                        {
                            if (!string.IsNullOrEmpty(tmpReportFile))
                            {
                                File.Delete(tmpReportFile);
                            }
                        }
                    }


                    return(reportPath);
                }
                catch (Exception ex)
                {
                    help = $"An exception occured : {ex}";
                    return(string.Empty);
                }
                finally
                {
                    if (!string.IsNullOrEmpty(tmpReportFile))
                    {
                        File.Delete(tmpReportFile);
                    }
                }
            }
            else
            {
                string tmpReportFile      = string.Empty;
                string tmpReportFileFlexi = string.Empty;

                try
                {
                    help = string.Empty;

                    //Get RG settings
                    var settings = SettingsBLL.GetSetting();
                    LogHelper.Instance.LogInfo("RG settings have been read successfully");

                    //Initialize temporary directory
                    string workDirectory = SettingsBLL.GetApplicationPath();
                    tmpReportFile = PathUtil.CreateTempCopy(workDirectory, Path.Combine(settings.ReportingParameter.TemplatePath, arguments.Template.Name));
                    if (tmpReportFile.Contains(".xlsx"))
                    {
                        tmpReportFileFlexi = PathUtil.CreateTempCopyFlexi(workDirectory, Path.Combine(settings.ReportingParameter.TemplatePath, arguments.Template.Name));
                    }
                    //Initialize Web services

                    var connection = new WSConnection(arguments.Webservice.Name, arguments.Username.Name, arguments.Password.Name, string.Empty)
                    {
                        ApiKey = arguments.ApiKey?.Name.Equals("true") ?? false
                    };
                    using (CommonBLL commonBLL = new CommonBLL(connection))
                    {
                        if (!commonBLL.CheckService())
                        {
                            help = $"Webservice can't be access or is bad formatted. Url:{arguments.Webservice.Name}";
                            return(string.Empty);
                        }
                    }
                    LogHelper.Instance.LogInfo("Web services Initialized successfully");


                    //Initialize Application
                    Domain.Application application = GetApplication(arguments, connection);
                    if (application == null)
                    {
                        help = arguments.Application != null ? $"Application {arguments.Application.Name} can't be found." : "Application not set in arguments.";
                        return(string.Empty);
                    }
                    LogHelper.Instance.LogInfo($"Application {arguments.Application.Name} Initialized successfully");

                    //Initialize snapshots
                    SetSnapshots(connection, application);
                    if (application.Snapshots == null)
                    {
                        help = "There is no snapshots for this application.";
                        return(string.Empty);
                    }
                    LogHelper.Instance.LogInfo($"List of Snapshots from {arguments.Application.Name} Initialized successfully");

                    //Build Application results
                    ApplicationBLL.BuildApplicationResult(connection, application);
                    LogHelper.Instance.LogInfo($"Application {arguments.Application.Name} results built successfully");


                    //Set current snapshot
                    Snapshot currentSnapshot = GetSnapshotOrDefault(arguments.Snapshot.Current, arguments.Snapshot.CurrentId, application.Snapshots, 0);
                    if (currentSnapshot == null)
                    {
                        help = $"Current snapshot {arguments.Snapshot.Current.Name} can't be found";
                        return(string.Empty);
                    }
                    LogHelper.Instance.LogInfo($"Current snapshot {currentSnapshot.Name} initialized successfully");

                    //Build current snapshot results
                    SnapshotBLL.BuildSnapshotResult(connection, currentSnapshot, true);
                    LogHelper.Instance.LogInfo($"Result of current snapshot {currentSnapshot.Name} built successfully");

                    //Set previous snapshot

                    Snapshot prevSnapshot = GetSnapshotOrDefault(arguments.Snapshot.Previous, arguments.Snapshot.PreviousId, application.Snapshots, -1);
                    if (prevSnapshot != null)
                    {
                        LogHelper.Instance.LogInfo($"Previous snapshot {prevSnapshot.Name} Initialized successfully");

                        //Build previous snapshot results
                        SnapshotBLL.BuildSnapshotResult(connection, prevSnapshot, false);
                        LogHelper.Instance.LogInfo($"Result of previous snapshot {prevSnapshot.Name}  built successfully");
                    }
                    else
                    {
                        if (arguments.Snapshot.Previous == null && arguments.Snapshot.PreviousId == null)
                        {
                            prevSnapshot = application.Snapshots.OrderByDescending(_ => _.Annotation.Date).Where(_ => _.Annotation.Date.DateSnapShot < currentSnapshot.Annotation.Date.DateSnapShot).ElementAtOrDefault(0);
                            if (prevSnapshot == null)
                            {
                                LogHelper.Instance.LogInfo("No Previous snapshot.");
                            }
                            else
                            {
                                SnapshotBLL.BuildSnapshotResult(connection, prevSnapshot, false);
                                LogHelper.Instance.LogInfo($"Result of previous snapshot {prevSnapshot.Name}  built successfully");
                            }
                        }
                        else
                        {
                            help = "Previous snapshot can't be found";
                            return(string.Empty);
                        }
                    }

                    //Build report
                    ReportData reportData = new ReportData
                    {
                        FileName         = tmpReportFile,
                        Application      = application,
                        CurrentSnapshot  = currentSnapshot,
                        PreviousSnapshot = prevSnapshot,
                        Parameter        = settings.ReportingParameter,
                        RuleExplorer     = new RuleBLL(connection),
                        SnapshotExplorer = new SnapshotBLL(connection, currentSnapshot),
                        CurrencySymbol   = "$",
                        ServerVersion    = CommonBLL.GetServiceVersion(connection)
                    };

                    using (IDocumentBuilder docBuilder = BuilderFactory.CreateBuilder(reportData, tmpReportFileFlexi))
                    {
                        docBuilder.BuildDocument();
                    }
                    LogHelper.Instance.LogInfo("Report generated successfully");

                    //Set filte report
                    SetFileName(arguments);

                    var reportPath = Path.Combine(string.IsNullOrEmpty(settings.ReportingParameter.GeneratedFilePath)
                        ? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                        : settings.ReportingParameter.GeneratedFilePath, arguments.File.Name);

                    if (tmpReportFile.Contains(".xlsx"))
                    {
                        tmpReportFile = tmpReportFileFlexi;
                    }

                    ConvertToPdfIfNeeded(arguments, ref reportPath, tmpReportFile);

                    LogHelper.Instance.LogInfo("Report moved to generation directory successfully");

                    return(reportPath);
                }
                catch (Exception ex)
                {
                    help = $"An exception occured : {ex}";
                    return(string.Empty);
                }
                finally
                {
                    if (!string.IsNullOrEmpty(tmpReportFile))
                    {
                        File.Delete(tmpReportFile);
                    }
                }
            }
        }
Beispiel #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arguments"></param>
        /// <param name="help"></param>
        /// <returns></returns>
        private static string GenerateReport(XmlCastReport arguments, out string help)
        {
            if (arguments.ReportType != null)
            {
                string reportPath    = string.Empty;
                string tmpReportFile = string.Empty;

                try
                {
                    help = string.Empty;

                    //Get RG settings
                    var settings = SettingsBLL.GetSetting();
                    LogHelper.Instance.LogInfo("RG settings have been read successfully");

                    //Initialize temporary directory
                    string workDirectory = SettingsBLL.GetApplicationPath();
                    //tmpReportFile = PathUtil.CreateTempCopy(workDirectory, Path.Combine(settings.ReportingParameter.TemplatePath, arguments.Template.Name));
                    //tmpReportFile = PathUtil.CreateTempCopy(workDirectory, arguments.Template.Name);

                    //Initialize Web services

                    var connection = new WSConnection(arguments.Webservice.Name, arguments.Username.Name, arguments.Password.Name, string.Empty);
                    using (CommonBLL commonBLL = new CommonBLL(connection))
                    {
                        if (!commonBLL.CheckService())
                        {
                            help = string.Format("Webservice can't be access or is bad formatted. Url:{0} Username:{1} Password:{2}", arguments.Webservice.Name, arguments.Username.Name, arguments.Password.Name);
                            return(string.Empty);
                        }
                    }
                    LogHelper.Instance.LogInfo("Web services Initialized successfully");

                    List <Application> Apps = new List <Application>();

                    try
                    {
                        using (CastDomainBLL castDomainBLL = new CastDomainBLL(connection))
                        {
                            if (arguments.Tag == null)
                            {
                                Apps = castDomainBLL.GetCommonTaggedApplications(null);
                            }
                            else
                            {
                                Apps = castDomainBLL.GetCommonTaggedApplications(arguments.Tag.Name);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.LogInfo("Error occured while trying get applications for the portfolio");
                    }

                    Application[] SelectedApps = Apps.ToArray <Application>();
                    LogHelper.Instance.LogInfo("Applications is the portfolio found successfully");
                    string[] AppsToIgnorePortfolioResult = PortfolioBLL.BuildPortfolioResult(connection, SelectedApps);
                    LogHelper.Instance.LogInfo("Build result for the portfolio");
                    string[]           SnapsToIgnore = null;
                    List <Application> N_Apps        = new List <Application>();
                    //Remove from Array the Ignored Apps
                    for (int i = 0; i < SelectedApps.Count(); i++)
                    {
                        int intAppYes = 0;
                        foreach (string s in AppsToIgnorePortfolioResult)
                        {
                            if (s == SelectedApps[i].Name)
                            {
                                intAppYes = 1;
                                break;
                            }
                            else
                            {
                                intAppYes = 0;
                            }
                        }

                        if (intAppYes == 0)
                        {
                            N_Apps.Add(SelectedApps[i]);
                        }
                    }
                    Application[] N_SelectedApps = N_Apps.ToArray();

                    List <Snapshot> Snapshots = new List <Snapshot>();

                    try
                    {
                        using (CastDomainBLL castDomainBLL = new CastDomainBLL(connection))
                        {
                            Snapshots = castDomainBLL.GetAllSnapshots(N_SelectedApps);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.LogInfo("Error occured while trying get snapshots of applications for the portfolio");
                    }
                    LogHelper.Instance.LogInfo("Snapshots is the portfolio found successfully");
                    List <Snapshot> N_Snaps = new List <Snapshot>();
                    if (Snapshots != null)
                    {
                        Snapshot[] SelectedApps_Snapshots = Snapshots.ToArray <Snapshot>();
                        SnapsToIgnore = PortfolioSnapshotsBLL.BuildSnapshotResult(connection, SelectedApps_Snapshots, true);
                        LogHelper.Instance.LogInfo("Build result for snapshots in portfolio");

                        for (int i = 0; i < SelectedApps_Snapshots.Count(); i++)
                        {
                            int intRemoveYes = 0;
                            foreach (string s in SnapsToIgnore)
                            {
                                if (s == SelectedApps_Snapshots[i].Href)
                                {
                                    intRemoveYes = 1;
                                    break;
                                }
                                else
                                {
                                    intRemoveYes = 0;
                                }
                            }
                            if (intRemoveYes == 0)
                            {
                                N_Snaps.Add(SelectedApps_Snapshots[i]);
                            }
                        }

                        Snapshot[] N_SelectedApps_Snapshots = N_Snaps.ToArray();

                        //GenerateReportPortfolio(N_SelectedApps, N_SelectedApps_Snapshots);
                        //string tmpReportFile = String.Empty;
                        string tmpReportFileFlexi = String.Empty;

                        try
                        {
                            //Create temporary report
                            //string workDirectory = SettingsBLL.GetApplicationPath();
                            //tmpReportFile = PathUtil.CreateTempCopy(workDirectory + "\\Portfolio\\", arguments.Template.Name);
                            tmpReportFile = PathUtil.CreateTempCopy(workDirectory, Path.Combine(settings.ReportingParameter.TemplatePath + "\\Portfolio", arguments.Template.Name));
                            if (tmpReportFile.Contains(".xlsx"))
                            {
                                tmpReportFileFlexi = PathUtil.CreateTempCopyFlexi(workDirectory, arguments.Template.Name);
                            }
                            //Build report
                            ReportData reportData = new ReportData();
                            if (arguments.Category != null && arguments.Tag != null)
                            {
                                reportData = new ReportData()
                                {
                                    FileName         = tmpReportFile,
                                    Application      = null,
                                    CurrentSnapshot  = null,
                                    PreviousSnapshot = null,
                                    RuleExplorer     = new RuleBLL(connection),
                                    CurrencySymbol   = "$",
                                    Applications     = N_SelectedApps,
                                    Category         = arguments.Category.Name,
                                    Tag                 = arguments.Tag.Name,
                                    snapshots           = N_SelectedApps_Snapshots,
                                    IgnoresApplications = AppsToIgnorePortfolioResult,
                                    IgnoresSnapshots    = SnapsToIgnore
                                };
                            }
                            else if (arguments.Category != null && arguments.Tag == null)
                            {
                                reportData = new ReportData()
                                {
                                    FileName         = tmpReportFile,
                                    Application      = null,
                                    CurrentSnapshot  = null,
                                    PreviousSnapshot = null,
                                    RuleExplorer     = new RuleBLL(connection),
                                    CurrencySymbol   = "$",
                                    Applications     = N_SelectedApps,
                                    Category         = arguments.Category.Name,
                                    Tag                 = null,
                                    snapshots           = N_SelectedApps_Snapshots,
                                    IgnoresApplications = AppsToIgnorePortfolioResult,
                                    IgnoresSnapshots    = SnapsToIgnore
                                };
                            }
                            else if (arguments.Category == null && arguments.Tag != null)
                            {
                                reportData = new ReportData()
                                {
                                    FileName         = tmpReportFile,
                                    Application      = null,
                                    CurrentSnapshot  = null,
                                    PreviousSnapshot = null,
                                    RuleExplorer     = new RuleBLL(connection),
                                    CurrencySymbol   = "$",
                                    Applications     = N_SelectedApps,
                                    Category         = null,
                                    Tag                 = arguments.Tag.Name,
                                    snapshots           = N_SelectedApps_Snapshots,
                                    IgnoresApplications = AppsToIgnorePortfolioResult,
                                    IgnoresSnapshots    = SnapsToIgnore
                                };
                            }
                            else
                            {
                                reportData = new ReportData()
                                {
                                    FileName         = tmpReportFile,
                                    Application      = null,
                                    CurrentSnapshot  = null,
                                    PreviousSnapshot = null,
                                    RuleExplorer     = new RuleBLL(connection),
                                    CurrencySymbol   = "$",
                                    Applications     = N_SelectedApps,
                                    Category         = null,
                                    Tag                 = null,
                                    snapshots           = N_SelectedApps_Snapshots,
                                    IgnoresApplications = AppsToIgnorePortfolioResult,
                                    IgnoresSnapshots    = SnapsToIgnore
                                };
                            }

                            using (IDocumentBuilder docBuilder = BuilderFactory.CreateBuilder(reportData, tmpReportFile))
                            {
                                docBuilder.BuildDocument();
                            }
                            LogHelper.Instance.LogInfo("Report generated successfully");

                            //Set filte report
                            SetFileName(arguments);

                            if (string.IsNullOrEmpty(settings.ReportingParameter.GeneratedFilePath))
                            {
                                reportPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), arguments.File.Name);
                            }
                            else
                            {
                                reportPath = Path.Combine(settings.ReportingParameter.GeneratedFilePath, arguments.File.Name);
                            }


                            if (tmpReportFile.Contains(".xlsx"))
                            {
                                tmpReportFile = tmpReportFileFlexi;
                            }

                            //Copy report file to the selected destination
                            File.Copy(tmpReportFile, reportPath, true);
                        }
                        catch (Exception ex)
                        {
                            reportPath = String.Empty;

                            throw (ex);
                        }
                        finally
                        {
                            if (!String.IsNullOrEmpty(tmpReportFile))
                            {
                                File.Delete(tmpReportFile);
                            }
                        }
                    }


                    return(reportPath);
                }
                catch (Exception ex)
                {
                    help = string.Format("An exception occured : {0}", ex);
                    return(string.Empty);
                }
                finally
                {
                    if (!string.IsNullOrEmpty(tmpReportFile))
                    {
                        File.Delete(tmpReportFile);
                    }
                }
            }
            else
            {
                string tmpReportFile = string.Empty;

                try
                {
                    help = string.Empty;

                    //Get RG settings
                    var settings = SettingsBLL.GetSetting();
                    LogHelper.Instance.LogInfo("RG settings have been read successfully");

                    //Initialize temporary directory
                    string workDirectory = SettingsBLL.GetApplicationPath();
                    tmpReportFile = PathUtil.CreateTempCopy(workDirectory, Path.Combine(settings.ReportingParameter.TemplatePath, arguments.Template.Name));

                    //Initialize Web services

                    var connection = new WSConnection(arguments.Webservice.Name, arguments.Username.Name, arguments.Password.Name, string.Empty);
                    using (CommonBLL commonBLL = new CommonBLL(connection))
                    {
                        if (!commonBLL.CheckService())
                        {
                            help = string.Format("Webservice can't be access or is bad formatted. Url:{0} Username:{1} Password:{2}", arguments.Webservice.Name, arguments.Username.Name, arguments.Password.Name);
                            return(string.Empty);
                        }
                    }
                    LogHelper.Instance.LogInfo("Web services Initialized successfully");


                    //Initialize Application
                    Application application = GetApplication(arguments.Application.Name, connection);
                    if (application == null)
                    {
                        help = string.Format("Application {0} can't be found.", arguments.Application.Name);
                        return(string.Empty);
                    }
                    LogHelper.Instance.LogInfo(string.Format("Application {0} Initialized successfully", arguments.Application.Name));

                    //Initialize snapshots
                    SetSnapshots(connection, application);
                    if (application.Snapshots == null)
                    {
                        help = "There is no snapshots for this application.";
                        return(string.Empty);
                    }
                    LogHelper.Instance.LogInfo(string.Format("List of Snapshots Initialized successfully", arguments.Application.Name));

                    //Build Application results
                    ApplicationBLL.BuildApplicationResult(connection, application);
                    LogHelper.Instance.LogInfo(string.Format("Application results built successfully", arguments.Application.Name));


                    //Set current snapshot
                    Snapshot currentSnapshot = GetSnapshotOrDefault(arguments.Snapshot.Current, application.Snapshots, 0);
                    if (currentSnapshot == null)
                    {
                        help = string.Format("Current snapshot {0} can't be found", arguments.Snapshot.Current.Name);
                        return(string.Empty);
                    }
                    LogHelper.Instance.LogInfo(string.Format("Current snapshot {0} initialized successfully", currentSnapshot.Name));

                    //Build current snapshot results
                    SnapshotBLL.BuildSnapshotResult(connection, currentSnapshot, true);
                    LogHelper.Instance.LogInfo(string.Format("Result of current snapshot {0} built successfully", currentSnapshot.Name));

                    //Set previous snapshot
                    Snapshot prevSnapshot = GetSnapshotOrDefault(arguments.Snapshot.Previous, application.Snapshots, 1);
                    if (arguments.Snapshot.Previous != null && !string.IsNullOrEmpty(arguments.Snapshot.Previous.Name) && prevSnapshot == null)
                    {
                        help = string.Format("Previous snapshot {0} can't be found", arguments.Snapshot.Previous.Name);
                        return(string.Empty);
                    }
                    if (prevSnapshot != null)
                    {
                        LogHelper.Instance.LogInfo(string.Format("Previous snapshot {0} Initialized successfully", prevSnapshot.Name));
                    }

                    //Build previous snapshot results
                    if (prevSnapshot != null)
                    {
                        SnapshotBLL.BuildSnapshotResult(connection, prevSnapshot, false);
                        LogHelper.Instance.LogInfo(string.Format("Result of previous snapshot {0}  built successfully", prevSnapshot.Name));
                    }


                    //Build report
                    ReportData reportData = new ReportData()
                    {
                        FileName         = tmpReportFile,
                        Application      = application,
                        CurrentSnapshot  = currentSnapshot,
                        PreviousSnapshot = prevSnapshot,
                        Parameter        = settings.ReportingParameter,
                        RuleExplorer     = new RuleBLL(connection),
                        SnapshotExplorer = new SnapshotBLL(connection, currentSnapshot),
                        CurrencySymbol   = "$"
                    };

                    using (IDocumentBuilder docBuilder = BuilderFactory.CreateBuilder(reportData, ""))
                    {
                        docBuilder.BuildDocument();
                    }
                    LogHelper.Instance.LogInfo("Report generated successfully");

                    //Set filte report
                    SetFileName(arguments);

                    string reportPath;
                    if (string.IsNullOrEmpty(settings.ReportingParameter.GeneratedFilePath))
                    {
                        reportPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), arguments.File.Name);
                    }
                    else
                    {
                        reportPath = Path.Combine(settings.ReportingParameter.GeneratedFilePath, arguments.File.Name);
                    }


                    File.Copy(tmpReportFile, reportPath, true);
                    LogHelper.Instance.LogInfo("Report moved to generation directory successfully");

                    return(reportPath);
                }
                catch (Exception ex)
                {
                    help = string.Format("An exception occured : {0}", ex);
                    return(string.Empty);
                }
                finally
                {
                    if (!string.IsNullOrEmpty(tmpReportFile))
                    {
                        File.Delete(tmpReportFile);
                    }
                }
            }
        }