/// <summary>
 /// Constructor with Crystal report document
 /// </summary>
 /// <param name="rp"></param>
 /// <param name="session"></param>
 /// <param name="accpacReport"></param>
 public SageWebReportDocument(ReportDocument rp, IBusinessEntitySession session, ACCPAC.Advantage.Report accpacReport)
 {
     CrystalReportDocument = rp;
     Session      = session;
     AccpacReport = accpacReport;
 }
        /// <summary>
        /// Execute the report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            var    token             = Request.QueryString["token"];
            var    sessionId         = Request.QueryString["session"];
            string reportDocumentKey = "ReportDocument_" + token;

            // set the page hidden variable.
            hiddenToken.Value     = token;
            hiddenSessionId.Value = sessionId;

            var report = GetReport(token, sessionId);

            if (report == null)
            {
                errorLabel.Text = CommonResx.ReportGenFailedMessage;
                return;
            }

            CommonUtil.SetCulture(report.Context.Language);

            // Check whether this report belongs to the same user.If not don't do anything.
            if (report.Context.SessionId != sessionId)
            {
                errorLabel.Text = CommonResx.NotAuthorizedMesage;
                return;
            }

            report.Context.Container = ConfigurationHelper.Container;

            bool isNew;

            var reportDocument = InMemoryCacheProvider.Instance.Get <SageWebReportDocument>(reportDocumentKey);

            if (reportDocument == null)
            {
                using (var watcher = new WADLogWatcher("ReportWatcher"))
                {
                    var session = BusinessPoolManager.GetSession(report.Context, DBLinkType.Company, out isNew);

                    watcher.Print("Session Created.");

                    // Throw an exception if the Crystal Reports template file is not found
                    var output       = GetReportTemplatePath(report.Name, session.UserLanguage);
                    var reportPath   = output.Item1;
                    var filenameOnly = output.Item2;
                    if (!File.Exists(reportPath))
                    {
                        //
                        // Security Consideration:
                        //    Do not use the full path in the message. Use only the filename
                        //
                        var msg = string.Format(CommonResx.Template_ReportCouldNotBeLocated, filenameOnly);
                        throw new MissingFileException(msg);
                    }

                    ACCPAC.Advantage.Report accpacReport = session.SelectReport(report.Name, report.ProgramId, report.MenuId);
                    foreach (var parameter in report.Parameters)
                    {
                        var value = GetValue(parameter);
                        accpacReport.SetParam(parameter.Id, value);
                    }

                    var userId = session.GetSession().UserID;
                    EvictUserWatcher.AddUserIdToPauseEviction(userId);

                    try
                    {
                        // Use lock to fix CRM multiple users concurrency printing issues.
                        lock (Lock)
                        {
                            var doc = accpacReport.GetReportDocument();
                            reportDocument = new SageWebReportDocument(doc, session, accpacReport);
                        }
                    }
                    finally
                    {
                        // Make sure we remove that from EvictUserWatcher no matter what happen
                        EvictUserWatcher.RemoveUserIdFromPauseEviction(userId);
                    }

                    watcher.Print("Report Document Created.");

                    // store the report object in the memory
                    InMemoryCacheProvider.Instance.Set(reportDocumentKey,
                                                       reportDocument,
                                                       ConfigurationHelper.ReportCacheExpirationInMinutes);
                }
            }

            if (reportDocument != null)
            {
                CrystalReportViewerSage300.ReportSource = reportDocument.CrystalReportDocument;
                CrystalReportViewerSage300.DataBind();
                SetLogoPath(reportDocument, report);
            }

            if (!IsPostBack && report.ReportProcessType.HasFlag(ReportProcessType.OnLoad))
            {
                Process(report);
            }
        }