private void btn_generate_Click(object sender, EventArgs e) { Boolean msgsend = true; try { Dictionary <string, object> parameters = new Dictionary <string, object>(); #region getEmailUserServerPasword string sEmailServer = ""; string sEmailUser = ""; string sEmailPassword = ""; var getServerDet = (from emailSer in context.emrcorcmps where emailSer.emrcmpcpcd.Equals(txt_company.Text.TrimEnd()) select emailSer).SingleOrDefault(); if (getServerDet != null) { sEmailServer = Utility.NullToString(getServerDet.emrcmpemsr); sEmailUser = Utility.NullToString(getServerDet.emrcmpesur); sEmailPassword = Utility.NullToString(getServerDet.emrcmpespd); } if (String.IsNullOrWhiteSpace(sEmailServer)) { sEmailServer = Settings.Default.DefaultEmailServerName; sEmailUser = Settings.Default.DefaultEmailServerLogin; sEmailPassword = Settings.Default.DefaultEmailServerPasword; } /* Local Testing Only */ #if DEBUG sEmailServer = "mail13.infoware.com.hk"; #endif #endregion string email = txt_email.Text; string sEmail = ""; string sEmailMsg = ""; if (Utility.NullToString(txt_email.Text) != "") { sEmail = Utility.NullToString(txt_email.Text); } else { sEmail = GetAllMailByGroupCode(txt_company.Text.TrimEnd(), txt_branch.Text.TrimEnd(), txt_GroupCode.Text.TrimEnd()); } sEmailMsg = "Dear user,\n\n" + "Here is the background job reporting process, and find the attached report.\n\n" + "Regards,\n" + "webmaster\n"; String sFileDirectory = Settings.Default.OutputFilePath + Path.DirectorySeparatorChar + txt_rptJobId.Text.TrimEnd(); if (!Directory.Exists(sFileDirectory)) { Directory.CreateDirectory(sFileDirectory); } String sFilePath = sFileDirectory + Path.DirectorySeparatorChar + txt_filename.Text + "." + cbx_fileformat.SelectedItem.ToString(); String sModuleID = txt_module.Text.Trim(); String sProgramID = txt_program.Text.Trim(); Type reportType = Type.GetType("IS21DotNet.Reports." + sModuleID + "." + sProgramID + "Report"); BaseReport xrreport = (BaseReport)Activator.CreateInstance(reportType); Dictionary <string, object> reportParameters = new Dictionary <string, object>(); foreach (DataGridViewRow row in dgv_param.Rows) { String sKey = Utility.NullToString(row.Cells[0].Value); String sType = Utility.NullToString(row.Cells[3].Value).ToUpper(); Object oValue = row.Cells[1].Value; reportParameters[sKey] = Program.CaseValueByDataType(oValue, sType); } xrreport.SetParameters(reportParameters); xrreport.TranslateLabels(Translation.Instance, Settings.Default.Language); xrreport.RequestParameters = false; if (cbx_fileformat.SelectedItem.Equals("pdf")) { xrreport.ExportToPdf(sFilePath); } else if (cbx_fileformat.SelectedItem.Equals("xls")) { xrreport.ExportToXls(sFilePath); } else if (cbx_fileformat.SelectedItem.Equals("xlsx")) { xrreport.ExportToXlsx(sFilePath); } Email emailService = Email.Instance; EmailServerInfo emailInfo = new EmailServerInfo(); emailInfo.ServerName = sEmailServer; emailInfo.UserID = sEmailUser; emailInfo.Password = sEmailPassword; List <String> attachmentList = new List <string>(); attachmentList.Add(sFilePath); emailService.SendEmail(sEmail, "Reporting Mail for report " + txt_program.Text.Trim(), sEmailMsg, attachmentList, emailInfo); msgsend = true; } catch (System.Exception ex) { Utility.WriteLog(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + ", ReportName: " + txt_program.Text.Trim() + ", Cannot generate Report : Failed\n Exception Message: " + ex.Message, sLogFile); Utility.WriteLog("\n" + ex.StackTrace, sLogFile); msgsend = false; } if (msgsend == true) { MessageBox.Show("Email Send Successfully "); } else { MessageBox.Show("Email Not Sent : ERROR ...Please check the Log"); } }
static void Main(System.String[] argss) { string[] args = { "report", "334", "PCD", "0000003,0000005,Job0000000001" }; Dictionary <string, object> dicto = new Dictionary <string, object>(); Utility.SetDefaultFormat(Thread.CurrentThread.CurrentCulture.Name); if (args.Length == 0) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ReportingForm()); } else { if (args[0].ToLower() == "report") { //Call and execute ALL the valid report jobs string sLogFile = ""; InfowareDataContext context = new InfowareDataContext(); IQueryable <rptmallst> rptMaster = from rptmaster in context.rptmallsts where rptmaster.rptmltenab.Equals("Y") orderby rptmaster.rptmltseqn ascending select rptmaster; IQueryable <rptJobMst_rptDetail> progRptQuery = from rptlist in context.rptjobmsts from rptdetail in context.rptmaldtls where rptlist.rptmstbrcd == rptdetail.rptmdlbrcd && rptlist.rptmstcpcd == rptdetail.rptmdlcpcd && rptlist.rptmstjbcd == rptdetail.rptmdljbcd && rptdetail.rptmdlenab.Equals("Y") select new rptJobMst_rptDetail { detail = rptdetail, master = rptlist }; Dictionary <String, EmailServerInfo> emailInfoDict = new Dictionary <string, EmailServerInfo>(); emailInfoDict = (from rptlist in progRptQuery join emailSer in context.emrcorcmps on rptlist.detail.rptmdlcpcd equals emailSer.emrcmpcpcd select new { Key = rptlist.detail.rptmdlcpcd.Trim(), Value = new EmailServerInfo { ServerName = emailSer.emrcmpemsr == null ? "" : emailSer.emrcmpemsr.Trim(), UserID = emailSer.emrcmpesur == null ? "" : emailSer.emrcmpesur.Trim(), Password = emailSer.emrcmpespd == null ? "" : emailSer.emrcmpespd.Trim() } }).Distinct().ToDictionary(p => p.Key, p => p.Value); Email emailService = Email.Instance; EmailServerInfo emailInfo = new EmailServerInfo(); if (String.IsNullOrWhiteSpace(emailInfo.ServerName)) { emailInfo.ServerName = Settings.Default.DefaultEmailServerName; emailInfo.UserID = Settings.Default.DefaultEmailServerLogin; emailInfo.Password = Settings.Default.DefaultEmailServerPasword; } List <rptJobMst_rptDetail> progRptList = new List <rptJobMst_rptDetail>(); List <rptmallst> progRptMasterList = new List <rptmallst>(); if (args.Length == 3) { if (!string.IsNullOrEmpty(args[1]) && !string.IsNullOrEmpty(args[2])) { rptMaster = rptMaster.Where(x => x.rptmltcpcd == args[1] && x.rptmltbrcd == args[2]); progRptQuery = progRptQuery.Where(x => x.detail.rptmdlcpcd == args[1] && x.detail.rptmdlbrcd == args[2]); } } if (args.Length == 4) { String[] code = args[3].Split(','); List <string> codeList = new List <string>(); for (int i = 0; i < code.Length; i++) { if (code[i].Trim() != "") { codeList.Add(code[i]); } } rptMaster = rptMaster.Where(x => x.rptmltcpcd == args[1] && x.rptmltbrcd == args[2] && codeList.Contains(x.rptmltmlcd)); progRptQuery = progRptQuery.Where(x => x.detail.rptmdlcpcd == args[1] && x.detail.rptmdlbrcd == args[2] && codeList.Contains(x.detail.rptmdlmlcd)); } progRptMasterList = rptMaster.ToList(); foreach (var value in progRptMasterList) { sLogFile = Settings.Default.DefaultLogPath + Path.DirectorySeparatorChar + value.rptmltcpcd + Path.DirectorySeparatorChar + value.rptmltbrcd + Path.DirectorySeparatorChar + value.rptmltmlcd + Path.DirectorySeparatorChar + "log_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt"; progRptList = progRptQuery.Where(x => x.detail.rptmdlmlcd == value.rptmltmlcd && x.detail.rptmdlcpcd == value.rptmltcpcd && x.detail.rptmdlbrcd == value.rptmltbrcd).ToList(); Dictionary <String, List <string> > attachDic = new Dictionary <string, List <string> >(); List <string> attachmentList = new List <string>(); string result = ""; foreach (var rptValue in progRptList) { String sCpcd = Utility.NullToString(rptValue.detail.rptmdlcpcd.TrimEnd()); String sBrcd = Utility.NullToString(rptValue.detail.rptmdlbrcd.TrimEnd()); String sJbcd = Utility.NullToString(rptValue.detail.rptmdljbcd.TrimEnd()); String sModuleID = Utility.NullToString(rptValue.master.rptmstmdcd); String sProgramID = Utility.NullToString(rptValue.master.rptmstpgid); String sReportDesc = Utility.NullToString(rptValue.master.rptmstdesc); if (sReportDesc == "") { sReportDesc = sProgramID; } string sepMail = Utility.NullToString(value.rptmltemal.TrimEnd()); IQueryable <rptjobpam> paramQuery = from rptpar in context.rptjobpams where rptpar.rptpamjbcd.Equals(sJbcd) && rptpar.rptpamcpcd.Equals(sCpcd) && rptpar.rptpambrcd.Equals(sBrcd) orderby rptpar.rptpampram ascending select rptpar; #region reportGeneration try { String sFileDirectory = Settings.Default.OutputFilePath + Path.DirectorySeparatorChar + sCpcd + Path.DirectorySeparatorChar + sBrcd + Path.DirectorySeparatorChar + sJbcd; if (!Directory.Exists(sFileDirectory)) { Directory.CreateDirectory(sFileDirectory); } string prefix = ""; if (Utility.NullToString(rptValue.master.rptmstrpnm) != "") { prefix = Utility.NullToString(rptValue.master.rptmstrpnm); } else { prefix = Utility.NullToString(rptValue.master.rptmstpgid); } string fileNameValue = prefix + "_" + DateTime.Now.ToString("yyyyMMdd") + "_" + DateTime.Now.ToString("HHmmss"); String sFilePath = sFileDirectory + Path.DirectorySeparatorChar + fileNameValue + "." + rptValue.master.rptmstfmcd.TrimEnd(); //if program id end with data, use the new approach if (sProgramID.Trim().EndsWith("Data")) { Type reportType = Type.GetType("IS21DotNet.Reports." + sModuleID + "." + sProgramID + ""); BaseReportData report = (BaseReportData)Activator.CreateInstance(reportType); Dictionary <string, object> reportParameters = new Dictionary <string, object>(); foreach (var row in paramQuery) //neeed to be ordered { String sKey = Utility.NullToString(row.rptpampram); String sType = Utility.NullToString(row.rptpamvtyp).ToUpper(); Object oValue = Utility.NullToString(row.rptpampval); reportParameters[sKey] = CaseValueByDataType(oValue, sType); } report.createFile(report.export(reportParameters, sFilePath), sFileDirectory, fileNameValue + "." + rptValue.master.rptmstfmcd.TrimEnd()); attachmentList.Add(sFilePath); } else if (sProgramID.Trim().EndsWith("Alert")) { Dictionary <string, object> reportParameters = new Dictionary <string, object>(); Dictionary <string, object> rptLocc = new Dictionary <string, object>(); foreach (var row in paramQuery) //neeed to be ordered { String sKey = Utility.NullToString(row.rptpampram); String sType = Utility.NullToString(row.rptpamvtyp).ToUpper(); Object oValue = Utility.NullToString(row.rptpampval); reportParameters[sKey] = CaseValueByDataType(oValue, sType); } Type reportType = Type.GetType("IS21DotNet.Reports.Alert." + sProgramID + ""); BaseAlertData report = (BaseAlertData)Activator.CreateInstance(reportType, reportParameters); if (!report.IsAttachFile) { result += report.ExportToHTML(reportParameters) + "<br>"; } else { report.createFile(report.export(reportParameters, sFilePath), sFileDirectory, fileNameValue + "." + rptValue.master.rptmstfmcd.TrimEnd()); attachmentList.Add(sFilePath); } } else { Type reportType = Type.GetType("IS21DotNet.Reports." + sModuleID + "." + sProgramID + "Report"); BaseReport xrreport = (BaseReport)Activator.CreateInstance(reportType); Dictionary <string, object> reportParameters = new Dictionary <string, object>(); foreach (var row in paramQuery) { String sKey = Utility.NullToString(row.rptpampram); String sType = Utility.NullToString(row.rptpamvtyp).ToUpper(); Object oValue = Utility.NullToString(row.rptpampval); reportParameters[sKey] = CaseValueByDataType(oValue, sType); } xrreport.SetParameters(reportParameters); xrreport.TranslateLabels(Translation.Instance, Settings.Default.Language); xrreport.RequestParameters = false; if (rptValue.master.rptmstfmcd.TrimEnd().Equals("pdf")) { xrreport.ExportToPdf(sFilePath); } else if (rptValue.master.rptmstfmcd.TrimEnd().Equals("xls")) { xrreport.ExportToXls(sFilePath); } else if (rptValue.master.rptmstfmcd.TrimEnd().Equals("xlsx")) { xrreport.ExportToXlsx(sFilePath); } attachmentList.Add(sFilePath); } } catch (System.Exception ex) { Utility.WriteLog(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + ", ReportName: " + sReportDesc + ", Cannot generate Report : Failed\n Exception Message: " + ex.Message + ex.InnerException + "\n" + ex.StackTrace, sLogFile); } #endregion //} } if (emailInfoDict.ContainsKey(value.rptmltcpcd.TrimEnd())) { emailInfo = emailInfoDict[value.rptmltcpcd.TrimEnd()]; } string message = GetMessage(value.rptmltmsgh, value.rptmltmsgc, value.rptmltmsgf, "", ""); if (attachmentList.Count > 0 || result != "") { if (result != "") { string sCpcdName = ""; try { sCpcdName = Utility.NullToString(context.emrcorcmps .Where(a => a.emrcmpcpcd == value.rptmltcpcd.TrimEnd()) .Select(b => b.emrcmpfnmp).SingleOrDefault(), ""); } catch { sCpcdName = ""; } message = GetMessage(value.rptmltmsgh, value.rptmltmsgc, value.rptmltmsgf, result, sCpcdName); } emailService.SendEmail(value.rptmltemal, value.rptmltsubj, message, attachmentList, emailInfo); } } } } }