Last active
August 29, 2015 14:25
-
-
Save kohbo/0b93112beb22df3ac144 to your computer and use it in GitHub Desktop.
PK Reporter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Reporting application to parse PK notices | |
* Written By: Juan Menendez | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Runtime.InteropServices; | |
using Outlook = Microsoft.Office.Interop.Outlook; | |
using System.Diagnostics; | |
using System.Reflection; | |
namespace PKReporter | |
{ | |
class Reporter | |
{ | |
static void Main(string[] args) | |
{ | |
Console.ForegroundColor = ConsoleColor.Black; | |
Console.BackgroundColor = ConsoleColor.White; | |
Console.WriteLine("PK Reporter\nWritten by Juan Menendez\n\n"); | |
Console.ResetColor(); | |
System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
Console.WriteLine("Collecting Data from Emails..."); | |
List<string[]> failures = new List<string[]>(); | |
/* | |
* 0: Date | |
* 1: Environment | |
* 2: Patient Location | |
* 3: Facility | |
* 4: Destination Group | |
* 5: Destination | |
* 6: Address | |
* 7: Details | |
*/ | |
Reporter reporter = new Reporter(); | |
Outlook.Application OutlookApp = reporter.GetApplicationObject(); | |
//program.getStoreID(OutlookApp.Session.Stores); | |
Outlook.Store PKStore = OutlookApp.Session.GetStoreFromID("0000000038A1BB1005E5101AA1BB08002B2A56C200006D737073742E646C6C00000000004E495441F9BFB80100AA0037D96E000000005C005C0068006D0061006600730031005C005300680061007200650064005C00530065007200760069006300650020004400650073006B005C0050004B005F0041006C0065007200740073002E007000730074000000"); | |
Outlook.Folder PKAlerts = (Outlook.Folder)PKStore.GetRootFolder(); | |
foreach (Outlook.MailItem item in PKAlerts.Items) | |
{ | |
string[] sourcebody = item.Body.Split('\n'); | |
string[] data = new string[6]; | |
data[0] = item.ReceivedTime.ToString(); | |
for (int index = 0; index < sourcebody.Length - 3; index++) | |
{ | |
data[index + 1] = sourcebody[index].Split(':')[1].Trim(); | |
} | |
failures.Add(data); | |
} | |
String htmlbody = ""; | |
htmlbody += "<style>body{font-size:.8em;}td,th{text-align: center; width:200px;padding: 2px 5px;border-collapse:collapse;}th{background: #CDA000; color:white;}</style>"; | |
htmlbody += "<h2>PK Failure Notice Report</h2>\n"; | |
htmlbody += "<p>" + PKAlerts.Items.Count + " failure notices received since " + PKAlerts.Items.GetFirst().ReceivedTime.ToString(); | |
htmlbody += reporter.GenerateBySiteCountReport(failures); | |
htmlbody += reporter.GenerateBySiteReport(failures); | |
Console.WriteLine("Report Completed in " + stopwatch.ElapsedMilliseconds + "ms."); | |
Console.WriteLine("Creating Report Email..."); | |
Outlook.MailItem mailItem = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem); | |
mailItem.Subject = "PK Failure Report as of " + System.DateTime.Now.ToString(); | |
mailItem.To = "[email protected]"; | |
mailItem.HTMLBody = htmlbody; | |
try | |
{ | |
mailItem.Display(true); | |
} | |
catch (COMException exc) | |
{ | |
Console.BackgroundColor = ConsoleColor.Red; | |
Console.ForegroundColor = System.ConsoleColor.White; | |
Console.WriteLine("Exception Occured. Ensure all dialog boxes are closed and try again."); | |
Console.ResetColor(); | |
Console.ReadKey(); | |
} | |
finally | |
{ | |
Marshal.ReleaseComObject(OutlookApp); | |
OutlookApp = null; | |
} | |
} | |
Outlook.Application GetApplicationObject() | |
{ | |
Outlook.Application application = null; | |
// Check if there is an Outlook process running. | |
if (Process.GetProcessesByName("OUTLOOK").Count() > 0) | |
{ | |
// If so, use the GetActiveObject method to obtain the process and cast it to an Application object. | |
application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; | |
} | |
else | |
{ | |
// If not, create a new instance of Outlook and log on to the default profile. | |
application = new Outlook.Application(); | |
Outlook.NameSpace nameSpace = application.GetNamespace("MAPI"); | |
nameSpace.Logon("", "", Missing.Value, Missing.Value); | |
nameSpace = null; | |
} | |
// Return the Outlook Application object. | |
return application; | |
} | |
string getStoreID(Outlook.Stores stores) | |
{ | |
Console.WriteLine("Enter the number corresponding with the data file holding PK alerts..."); | |
int index = 0; | |
foreach(Outlook.Store store in stores) | |
{ | |
if (store.IsDataFileStore == true) | |
{ | |
Console.WriteLine(index + ": " + store.DisplayName); | |
index++; | |
} | |
} | |
int sel = Console.Read(); | |
return stores[sel].DisplayName; | |
} | |
private void EnumerateStores(Outlook.Stores stores) | |
{ | |
foreach (Outlook.Store store in stores) | |
{ | |
if (store.IsDataFileStore == true) | |
{ | |
Debug.WriteLine(String.Format("Store: " | |
+ store.StoreID | |
+ "\n" + "File Path: " | |
+ store.FilePath + "\n")); | |
} | |
} | |
} | |
private String GenerateBySiteCountReport(List<string[]> data){ | |
String reportHTML = "<table><tr><th>Facility</th><th>Failures</th></tr>"; | |
Console.WriteLine("Generating By Site Count Report..."); | |
Dictionary<string, int> count = new Dictionary<string, int>(); | |
foreach (string[] failure in data) | |
{ | |
if (count.ContainsKey(failure[3])) | |
{ | |
count[failure[3]]++; | |
} | |
else | |
{ | |
count.Add(failure[3], 1); | |
} | |
} | |
foreach (string key in count.Keys) | |
{ | |
reportHTML += "<tr><td>" + key + "</td><td>" + count[key] + "</td></tr>"; | |
} | |
return reportHTML + "</table><br />"; | |
} | |
private String GenerateBySiteReport(List<string[]> data) | |
{ | |
String reportHTML = "<table>"; | |
Console.WriteLine("Generating By Site Detailed Report..."); | |
Dictionary<string, List<string[]>> notices = new Dictionary<string, List<string[]>>(); | |
foreach (string[] report in data) | |
{ | |
if(!notices.ContainsKey(report[3])){ | |
notices.Add(report[3], new List<string[]>()); | |
notices[report[3]].Add(report); | |
} else { | |
notices[report[3]].Add(report); | |
} | |
} | |
foreach(string key in notices.Keys){ | |
reportHTML += "<tr><th colspan=\"6\"><h3>" + key + "</h3></th></tr>"; | |
reportHTML += "<tr><th>Date</th><th>Environment</th><th>Pat. Location</th><th>Facility</th><th>Destination Group</th><th>Destination</th></tr>"; | |
foreach (string[] reports in notices[key]) | |
{ | |
reportHTML += "<tr>"; | |
foreach (string cell in reports) | |
{ | |
reportHTML += "<td>" + cell + "</td>"; | |
} | |
reportHTML += "</tr>"; | |
} | |
} | |
return reportHTML + "</table><br />"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment