289b2345d8fb5a4dfc43f92808400c8379553721
FAQ/Questions/How-do-I-use-the-Izenda-Reports-report-email-scheduler/FileScheduler.cs
| ... | ... | @@ -0,0 +1,199 @@ |
| 1 | +using Izenda.AdHoc;
|
|
| 2 | +using System;
|
|
| 3 | +using System.Collections.Generic;
|
|
| 4 | +using System.IO;
|
|
| 5 | +using System.Text;
|
|
| 6 | +using System.Web;
|
|
| 7 | +
|
|
| 8 | +public class FileScheduler
|
|
| 9 | +{
|
|
| 10 | + public static void RunScheduledReports(TextWriter log, DateTime dateTime)
|
|
| 11 | + {
|
|
| 12 | + if (AdHocSettings.ResponseServer == "rs.aspx")
|
|
| 13 | + {
|
|
| 14 | + string url = HttpContext.Current.Request.Url.ToString();
|
|
| 15 | + int index = url.LastIndexOfAny(new char[] { '/', '\\' });
|
|
| 16 | + if (index > 0)
|
|
| 17 | + {
|
|
| 18 | + string path = url.Substring(0, index);
|
|
| 19 | + if (!path.EndsWith("/") && !path.EndsWith("\\"))
|
|
| 20 | + path += "/";
|
|
| 21 | + AdHocSettings.ResponseServer = string.Format("{0}{1}", path, "rs.aspx");
|
|
| 22 | + }
|
|
| 23 | + }
|
|
| 24 | +
|
|
| 25 | + string[] tenants = ListTenants();
|
|
| 26 | + RunSchedulerForTenant(log, dateTime, null);
|
|
| 27 | + foreach (string tenant in tenants)
|
|
| 28 | + RunSchedulerForTenant(log, dateTime, tenant);
|
|
| 29 | + }
|
|
| 30 | +
|
|
| 31 | + protected static void RunSchedulerForTenant(TextWriter log, DateTime dateTime, string tenantId)
|
|
| 32 | + {
|
|
| 33 | + AdHocSettings.CurrentUserIsAdmin = true;
|
|
| 34 | + AdHocSettings.CurrentUserTenantId = tenantId;
|
|
| 35 | + try
|
|
| 36 | + {
|
|
| 37 | + string delimiter = HttpContext.Current == null ? Environment.NewLine : "<br/>";
|
|
| 38 | + bool reportsFound = false;
|
|
| 39 | +
|
|
| 40 | + ReportInfo[] reports = AdHocSettings.AdHocConfig.ListReports();
|
|
| 41 | + foreach (ReportInfo reportInfo in reports)
|
|
| 42 | + {
|
|
| 43 | + if (string.IsNullOrEmpty(reportInfo.Name))
|
|
| 44 | + continue;
|
|
| 45 | + ReportSet reportSet = null;
|
|
| 46 | + try
|
|
| 47 | + {
|
|
| 48 | + reportSet = AdHocSettings.AdHocConfig.LoadFilteredReportSet(reportInfo.FullName);
|
|
| 49 | + }
|
|
| 50 | + catch { }
|
|
| 51 | + if (reportSet == null)
|
|
| 52 | + continue;
|
|
| 53 | +
|
|
| 54 | + DateTime schedule = reportSet.Schedule;
|
|
| 55 | + string reportName = reportInfo.Name;
|
|
| 56 | + if (!string.IsNullOrEmpty(reportInfo.Category))
|
|
| 57 | + reportName = reportInfo.Category + "\\" + reportInfo.Name;
|
|
| 58 | + reportSet.ReportName = reportName;
|
|
| 59 | + reportsFound = true;
|
|
| 60 | + if (schedule == Utility.NullDateTime || string.IsNullOrEmpty(reportSet.Recipients))
|
|
| 61 | + {
|
|
| 62 | + log.Write(string.Format("{1}{0} - No Schedule{1}", reportName, delimiter));
|
|
| 63 | + continue;
|
|
| 64 | + }
|
|
| 65 | + if (schedule.Year == DateTime.MaxValue.Year)
|
|
| 66 | + {
|
|
| 67 | + log.Write(string.Format("{1}{0} - Scheduled For Past {1}", reportName, delimiter));
|
|
| 68 | + continue;
|
|
| 69 | + }
|
|
| 70 | + if (schedule > dateTime)
|
|
| 71 | + {
|
|
| 72 | + log.Write(string.Format("{1}{0} - Scheduled In Future({2} @ {3}){1}", reportName, delimiter, schedule.ToShortDateString(), schedule.ToShortTimeString()));
|
|
| 73 | + continue;
|
|
| 74 | + }
|
|
| 75 | + try
|
|
| 76 | + {
|
|
| 77 | + log.Write(string.Format("{1}{0} - Scheduled For This Interval ({2} @ {3}){1}", reportName, delimiter, schedule.ToShortDateString(), schedule.ToShortTimeString()));
|
|
| 78 | + string[] recipients = reportSet.Recipients.Split(',', ';');
|
|
| 79 | +
|
|
| 80 | + byte[] content = null;
|
|
| 81 | + if (recipients.Length > 0)
|
|
| 82 | + {
|
|
| 83 | + log.Write("Preparing content - ");
|
|
| 84 | + content = GetOutput(reportSet);
|
|
| 85 | + DateTime nextSchedule = Utility.GetNextTime(schedule, reportSet.RepeatType).DateTime;
|
|
| 86 | + while (nextSchedule < DateTime.Now)
|
|
| 87 | + nextSchedule = Utility.GetNextTime(nextSchedule, reportSet.RepeatType).DateTime;
|
|
| 88 | + reportSet.Schedule = nextSchedule;
|
|
| 89 | + AdHocSettings.AdHocConfig.SaveReportSet(reportInfo, reportSet);
|
|
| 90 | + log.Write(string.Format("success{0}", delimiter));
|
|
| 91 | + }
|
|
| 92 | +
|
|
| 93 | + log.Write(string.Format("Saving To:{0}", delimiter));
|
|
| 94 | + foreach (string recipient in recipients)
|
|
| 95 | + try
|
|
| 96 | + {
|
|
| 97 | + log.Write(string.Format("{0} - ", recipient));
|
|
| 98 | + SaveOutputToFile(content, recipient, reportSet);
|
|
| 99 | + log.Write(string.Format("success{0}", delimiter));
|
|
| 100 | + }
|
|
| 101 | + catch (Exception ex)
|
|
| 102 | + {
|
|
| 103 | + StringBuilder emessage = new StringBuilder();
|
|
| 104 | + while (ex != null)
|
|
| 105 | + {
|
|
| 106 | + emessage.Append(ex.Message + " ");
|
|
| 107 | + ex = ex.InnerException;
|
|
| 108 | + }
|
|
| 109 | + log.Write(string.Format("failure ({0}){1}", emessage, delimiter));
|
|
| 110 | + }
|
|
| 111 | + log.Write("Done. {0}", delimiter);
|
|
| 112 | + }
|
|
| 113 | + catch (Exception ex)
|
|
| 114 | + {
|
|
| 115 | + StringBuilder emessage = new StringBuilder();
|
|
| 116 | + while (ex != null)
|
|
| 117 | + {
|
|
| 118 | + emessage.Append(ex.Message + " ");
|
|
| 119 | + ex = ex.InnerException;
|
|
| 120 | + }
|
|
| 121 | + log.Write(string.Format("failure ({0}){1}", emessage, delimiter));
|
|
| 122 | + }
|
|
| 123 | + }
|
|
| 124 | + if (!reportsFound)
|
|
| 125 | + log.Write(string.Format("Reports Not Found {0}", delimiter));
|
|
| 126 | + }
|
|
| 127 | + catch { }
|
|
| 128 | + }
|
|
| 129 | +
|
|
| 130 | + protected static string[] ListTenants()
|
|
| 131 | + {
|
|
| 132 | + if (string.IsNullOrEmpty(HttpContext.Current.Request.Params["tenants"]))
|
|
| 133 | + return new string[] { };
|
|
| 134 | + return HttpContext.Current.Request.Params["tenants"].Split(',');
|
|
| 135 | + }
|
|
| 136 | +
|
|
| 137 | + private static byte[] GetOutput(ReportSet rs)
|
|
| 138 | + {
|
|
| 139 | + byte[] res = null;
|
|
| 140 | + ReportOutputGenerator outGenerator = null;
|
|
| 141 | + switch (rs.SendEmailAs)
|
|
| 142 | + {
|
|
| 143 | + case "PDF":
|
|
| 144 | + outGenerator = new ITextSharpPdfGenerator();
|
|
| 145 | + break;
|
|
| 146 | + case "HTML2PDF":
|
|
| 147 | + outGenerator = new Html2PdfGenerator();
|
|
| 148 | + break;
|
|
| 149 | + case "CSV":
|
|
| 150 | + outGenerator = new CsvReportOuputGenerator();
|
|
| 151 | + break;
|
|
| 152 | + case "XLS(MIME)":
|
|
| 153 | + outGenerator = new HtmlOutputGenerator("xls");
|
|
| 154 | + break;
|
|
| 155 | + case "Word(RTF)":
|
|
| 156 | + outGenerator = new HtmlOutputGenerator("rtf");
|
|
| 157 | + break;
|
|
| 158 | + case "HTML":
|
|
| 159 | + outGenerator = new HtmlOutputGenerator("html");
|
|
| 160 | + break;
|
|
| 161 | + }
|
|
| 162 | + res = outGenerator.GenerateOutput(rs).Content;
|
|
| 163 | + return res;
|
|
| 164 | + }
|
|
| 165 | +
|
|
| 166 | + private static void SaveOutputToFile(byte[] output, string folder, ReportSet rs)
|
|
| 167 | + {
|
|
| 168 | + string extension = "";
|
|
| 169 | + switch (rs.SendEmailAs)
|
|
| 170 | + {
|
|
| 171 | + case "PDF":
|
|
| 172 | + extension = "pdf";
|
|
| 173 | + break;
|
|
| 174 | + case "HTML2PDF":
|
|
| 175 | + extension = "pdf";
|
|
| 176 | + break;
|
|
| 177 | + case "CSV":
|
|
| 178 | + extension = "csv";
|
|
| 179 | + break;
|
|
| 180 | + case "XLS(MIME)":
|
|
| 181 | + extension = "xls";
|
|
| 182 | + break;
|
|
| 183 | + case "Word(RTF)":
|
|
| 184 | + extension = "rtf";
|
|
| 185 | + break;
|
|
| 186 | + case "HTML":
|
|
| 187 | + extension = "html";
|
|
| 188 | + break;
|
|
| 189 | + }
|
|
| 190 | +
|
|
| 191 | + string fileName = Path.Combine(folder, rs.ReportName) + "." + extension;
|
|
| 192 | + if (!Directory.Exists(Path.Combine(folder, rs.ReportCategory)))
|
|
| 193 | + Directory.CreateDirectory(Path.Combine(folder, rs.ReportCategory));
|
|
| 194 | + using (FileStream file = File.Create(fileName))
|
|
| 195 | + {
|
|
| 196 | + file.Write(output, 0, output.Length);
|
|
| 197 | + }
|
|
| 198 | + }
|
|
| 199 | +} |
|
| ... | ... | \ No newline at end of file |