public void RefreshCachedReports(string CompanyNameShort) { Common.LogWriter.Instance.WriteEventLogEntry("Start RefreshCachedReports: " + CompanyNameShort); lock (this) { // check if already being handled if (_refreshCachedRequests.Contains(CompanyNameShort)) { return; } _refreshCachedRequests.Add(CompanyNameShort); } int count = 0; try { FI.Common.DataAccess.IUsersDA userDao = DataAccessFactory.Instance.GetUsersDA(); FI.Common.DataAccess.IOlapReportsDA rptDao = DataAccessFactory.Instance.GetOlapReportsDA(); decimal companyId = userDao.GetCompanyIdByShortName(CompanyNameShort); if (companyId <= 0) { return; } Common.Data.FIDataTable tbl = rptDao.GetCashedReportsToRefresh(companyId); if (tbl != null) { foreach (DataRow row in tbl.Rows) { User usr = new User((decimal)row["user_id"], false); OlapReport rpt = usr.ReportSystem.GetReport((decimal)row["rpt_id"], typeof(OlapReport), true) as OlapReport; rpt.Execute(); count++; } } } catch (Exception exc) { Common.LogWriter.Instance.WriteEventLogEntry(exc); throw exc; } finally { _refreshCachedRequests.Remove(CompanyNameShort); Common.LogWriter.Instance.WriteEventLogEntry(string.Format("End RefreshCachedReports: Company={0}, Count={1}", CompanyNameShort, count)); } }
public void SendDistribution(decimal distributionId, DateTime getCacheFrom, Guid olapTaskGuid, out bool isFromCache) { try { isFromCache = false; FI.DataAccess.Distributions dacObj = DataAccessFactory.Instance.GetDistributionsDA(); decimal userId = dacObj.GetDistributionOwnerId(distributionId); if (userId <= 0) { return; } User user = new User(userId, true); Distribution distr = user.DistributionSystem.GetDistribution(distributionId, true); Report report = distr.Report; if (report.IsProxy) { report.Open(); } if (report.State == Report.StateEnum.Open) { OlapReport olapRpt = report as OlapReport; if (olapRpt != null) { olapRpt.Execute(olapTaskGuid); } else { report.Execute(); } } Contact contact = distr.Contact; if (contact.IsProxy) { contact.Fetch(); } SendReport(report, new Contact[] { contact }, distr.Format, getCacheFrom, out isFromCache); } catch (Exception exc) { Common.LogWriter.Instance.WriteEventLogEntry(exc); throw exc; } }
public void SendReport(Report report, Guid olapTaskGuid, Contact[] contacts, Report.ExportFormat Format, DateTime minCacheTimestamp, DateTime currentTimestamp, out bool isFromCache) { isFromCache = false; if (contacts.Length == 0) { return; } if (report.IsProxy) { report.Open(); } string fileNamePattern = report.GetType().Name + "_" + report.ID.ToString() + "_"; string newFileName = fileNamePattern + currentTimestamp.ToString("yyyyMMddHHmmssfff") + "." + Format.ToString(); string cacheLookupFileName = fileNamePattern + minCacheTimestamp.ToString("yyyyMMddHHmmssfff") + "." + Format.ToString(); string filePath = null; string reportString = null; // lookup cached report if (minCacheTimestamp != DateTime.MinValue && minCacheTimestamp != DateTime.MaxValue) { string[] lookupPaths = Directory.GetFiles(FI.Common.AppConfig.TempDir, fileNamePattern + "*." + Format.ToString()); if (lookupPaths != null) { foreach (string path in lookupPaths) { string file = Path.GetFileName(path); if (file.Length == cacheLookupFileName.Length && file.CompareTo(cacheLookupFileName) > 0) { filePath = FI.Common.AppConfig.TempDir + @"\" + file; isFromCache = true; break; } } } } // no cache, execute if (filePath == null) { if (report.State == Report.StateEnum.Open) { OlapReport olapRpt = report as OlapReport; if (olapRpt != null) { olapRpt.Execute(olapTaskGuid); } else { report.Execute(); } } filePath = FI.Common.AppConfig.TempDir + @"\" + newFileName; report.Export(filePath, Format); } // send to contacts foreach (Contact cnt in contacts) { if (Format == Report.ExportFormat.HTML && reportString == null) { if (cnt.DistributionFormat == Contact.DistributionFormatEnum.MessageBody || cnt.DistributionFormat == Contact.DistributionFormatEnum.Body_And_Attachment) { StreamReader sr = new StreamReader(filePath, System.Text.Encoding.Unicode, true); if (sr != null) { reportString = sr.ReadToEnd(); sr.Close(); } } } //send via email try { if (cnt.IsProxy) { cnt.Fetch(); } // message object System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new System.Net.Mail.MailAddress(FI.Common.AppConfig.SmtpSender); msg.To.Add(cnt.EMail); msg.Subject = report.Name + " (" + report.Description + ")"; //OpenSmtp.Mail.MailMessage msg=new OpenSmtp.Mail.MailMessage(); //msg.From=new OpenSmtp.Mail.EmailAddress(FI.Common.AppConfig.SmtpSender); //msg.To.Add(new OpenSmtp.Mail.EmailAddress(cnt.EMail)); //msg.Subject=report.Name + " (" + report.Description + ")"; // attachment if ordered or report is not html if (cnt.DistributionFormat == Contact.DistributionFormatEnum.Attachment || cnt.DistributionFormat == Contact.DistributionFormatEnum.Body_And_Attachment || Format != Report.ExportFormat.HTML) { //OpenSmtp.Mail.Attachment att=new OpenSmtp.Mail.Attachment(filePath); //att.Encoding=System.Web.Mail.MailEncoding.UUEncode; System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(filePath); msg.Attachments.Add(att); } // message body (if retport is html) if (Format == Report.ExportFormat.HTML && (cnt.DistributionFormat == Contact.DistributionFormatEnum.MessageBody || cnt.DistributionFormat == Contact.DistributionFormatEnum.Body_And_Attachment)) { //msg.HtmlBody=reportString; msg.Body = reportString; msg.IsBodyHtml = true; } // msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0"); //This is crucial. put 0 there // msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 90); //OpenSmtp.Mail.SmtpConfig.LogToText=false; System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(); smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Host = FI.Common.AppConfig.SmtpServer; smtp.Timeout = 600000; // 10 minutes if (FI.Common.AppConfig.SmtpPort > 0) { smtp.Port = FI.Common.AppConfig.SmtpPort; } //OpenSmtp.Mail.Smtp smtp=new OpenSmtp.Mail.Smtp(); //smtp.SendTimeout=600; //smtp.Host=FI.Common.AppConfig.SmtpServer; if (FI.Common.AppConfig.SmtpUserName != null && FI.Common.AppConfig.SmtpUserName != "") { smtp.Credentials = new System.Net.NetworkCredential(FI.Common.AppConfig.SmtpUserName, FI.Common.AppConfig.SmtpPassword); //smtp.Username=FI.Common.AppConfig.SmtpUserName; //smtp.Password=FI.Common.AppConfig.SmtpPassword; } smtp.Send(msg); //smtp.SendMail(msg); // System.Web.Mail.SmtpMail.SmtpServer=FI.Common.AppConfig.SmtpServer; // System.Web.Mail.SmtpMail.Send(msg); } catch (Exception exc) { // because real exception is inside: while (exc.InnerException != null) { exc = exc.InnerException; } Common.LogWriter.Instance.WriteEventLogEntry(exc); throw exc; } } }