/// <summary> /// This is the core automation routine that checks for appointments and processes each. /// </summary> private void RunAutomationCycle() { List <int> apptIDs = new List <int>(); /*********************** get appointments to automate ********************/ SqlDataReader reader; string CustomAutomationString = Configurator.getNodeValue("DatabaseInfo", "AUTOMATION_SQL"); if (string.IsNullOrEmpty(CustomAutomationString) == false) { reader = BCDB2.Instance.ExecuteReader(CustomAutomationString); } else { reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_Automation", null); } try { while (reader.Read()) { if (reader.IsDBNull(0) == false) { apptIDs.Add(reader.GetInt32(0)); } } } catch (Exception e) { Logger.Instance.WriteToLog("Error getting appointment IDs: " + e.ToString()); } finally { if (reader != null) { reader.Close(); } } backgroundWorker1.ReportProgress(0, apptIDs.Count.ToString() + " appointments are marked for automation - " + DateTime.Now.ToString()); /*********************** automation cycle ********************/ foreach (int apptid in apptIDs) { if (backgroundWorker1.CancellationPending) { break; } /*********************** Get Unitnum & set active patient ********************/ string unitnum = ""; ParameterCollection pc = new ParameterCollection("apptID", apptid); using (reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_3_GetUnitnum", pc)) { if (reader != null && reader.Read()) { unitnum = reader.GetValue(0).ToString(); } } SessionManager.Instance.SetActivePatientNoCallback(unitnum, (int)apptid); /*********************** Risk Calculations ********************/ try { backgroundWorker1.ReportProgress(1, "Running risk calculations for appointment: " + apptid.ToString() + "."); SessionManager.Instance.GetActivePatient().RecalculateRisk(); } catch (Exception e) { backgroundWorker1.ReportProgress(1, "Risk calculations FAILED for appointment: " + apptid.ToString() + ". " + e.ToString()); } /*********************** Update BigQueue ********************/ try { backgroundWorker1.ReportProgress(1, "Updating BigQueue for appointment: " + apptid.ToString() + "."); pc = new ParameterCollection("unitnum", unitnum); BCDB2.Instance.RunSPWithParams("sp_3_populateBigQueue", pc); } catch (Exception e) { backgroundWorker1.ReportProgress(1, "Updating BigQueue FAILED for appointment: " + apptid.ToString() + ". " + e.ToString()); } /*********************** Process Queue Documents ********************/ backgroundWorker1.ReportProgress(1, "Processing Queue Documents for appointment: " + apptid.ToString() + "."); pc = new ParameterCollection("apptID", apptid); BCDB2.Instance.RunSPWithParams("sp_processQueueDocuments", pc); /*********************** Run Automation Stored Procedures ********************/ backgroundWorker1.ReportProgress(1, "Run Automation Stored Procedures : " + apptid.ToString() + "."); RiskAppCore.ApptUtils.runAutomationStoredProcedures(apptid); /*********************** export HL7 files ********************/ backgroundWorker1.ReportProgress(1, "Export HL7 File: " + apptid.ToString() + "."); RiskModels.RiskService.exportHL7File(apptid); /*********************** Process Batch Print Queue Documents ********************/ backgroundWorker1.ReportProgress(1, "Processing batch print queue documents for appointment: " + apptid.ToString() + "."); // Make batch + printing a single progress report ??? RiskAppCore.ApptUtils.saveAutomationDocumentsToPrintQueue(apptid); /*********************** Print Documents ********************/ string print_report = ""; try { backgroundWorker1.ReportProgress(1, "Printing documents for appointment: " + apptid.ToString() + "."); // Make batch + printing a single progress report ??? ParameterCollection printDocArgs = new ParameterCollection(); printDocArgs.Add("apptid", apptid); reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_AutomationHtmlDocsToPrint", printDocArgs); while (reader.Read()) { int templateID = -1; string printer = ""; string documentType = ""; string saveLocation = ""; if (reader.IsDBNull(0) == false) { templateID = reader.GetInt32(0); } if (reader.IsDBNull(1) == false) { printer = reader.GetString(1); } if (reader.IsDBNull(2) == false) { documentType = reader.GetString(2); } if (reader.IsDBNull(3) == false) { saveLocation = reader.GetString(3); } /////////////////////////////////////////////////// // documentType defaults to HTML in V3 Automation // /////////////////////////////////////////////////// if (string.IsNullOrEmpty(documentType)) { documentType = "HTML"; } RiskApps3.Model.Clinic.Letters.PrintUtils printUtils = new RiskApps3.Model.Clinic.Letters.PrintUtils(printer); if (printer.ToLower().StartsWith("save as")) { if (string.IsNullOrEmpty(saveLocation) == false) { if (documentType.ToUpper() == "HTML") { if (string.Compare(printer, "save as pdf", true) == 0) { if (printUtils.savePdfDoc(apptid, unitnum, templateID, saveLocation) == true) { print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>"; } } else { if (printUtils.saveHtmlDoc(apptid, unitnum, templateID, saveLocation) == true) { print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>"; } } } else if (documentType.ToUpper() == "WORD") { if (printUtils.saveWordDoc(apptid, unitnum, templateID, saveLocation) == true) { print_report += "Saved document template " + templateID.ToString() + " in " + saveLocation + "<br/>"; } } } } else if (printer == "BATCH") { pc = new ParameterCollection(); pc.Add("apptid", apptid); pc.Add("dateTime", DateTime.Now); pc.Add("templateID", templateID); pc.Add("unitnum", unitnum); pc.Add("documentType", documentType); string sqlStr = "INSERT INTO tblPrintQueue (apptID, created, documentTemplateID, unitnum, automationPrint, automationSave, documentType) VALUES (@apptid, @dateTime, @templateID, @unitnum, 0, 0, @documentType)"; int rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc); print_report += "Sent document template " + templateID.ToString() + " to the batch print queue." + "<br/>"; backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report); } else if (printer == "") { } else if (string.IsNullOrEmpty(printer) == false) { if (documentType.ToUpper() == "WORD") { if (printUtils.printWordDoc(apptid, unitnum, templateID)) { print_report += "Sent document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>"; pc = new ParameterCollection(); pc.Add("apptid", apptid); pc.Add("templateID", templateID); pc.Add("dateTime", DateTime.Now); string sqlStr = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(@apptid, @templateID, @dateTime, 'AUTOMATION');"; BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc); } else { print_report += "Failed sending document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>"; } backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report); } else if (documentType.ToUpper() == "HTML") { HraHtmlDocument hdoc; if ((hdoc = printUtils.printHtmlDoc(apptid, unitnum, templateID)) != null) { print_report += "Sent document template " + templateID.ToString() + " to printer: " + hdoc.targetPrinter + "<br/>"; pc = new ParameterCollection(); pc.Add("apptid", hdoc.apptid); pc.Add("templateID", hdoc.template.documentTemplateID); pc.Add("dateTime", DateTime.Now); string sqlStr = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(@apptid, @templateID, @dateTime, 'AUTOMATION');"; int rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc); backgroundWorker1.ReportProgress(2, hdoc.template.htmlText + Environment.NewLine + "Appt " + apptid.ToString() + ":<br/>" + print_report); } else { print_report += "Failed to send document template " + templateID.ToString() + " to printer: " + printUtils.getPrinter() + "<br/>"; backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report); } } else if (documentType != "HTML" && documentType != "WORD") { print_report += "Could not process document: templateID " + templateID.ToString() + ", printer: " + printer + ", UNKOWN DOCUMENT TYPE: \"" + documentType + "\"<br/>"; backgroundWorker1.ReportProgress(2, "Appt " + apptid.ToString() + ":<br/>" + print_report); } } } } catch (Exception e) { backgroundWorker1.ReportProgress(1, "Printing FAILED for appointment: " + apptid.ToString() + ". " + e.ToString()); } finally { if (reader != null) { reader.Close(); } } /*********************** Mark as printed to complete automation ********************/ try { pc = new ParameterCollection("apptid", apptid); String sqlStr = "UPDATE tblRiskData SET printed = 1 WHERE apptid = @apptid"; int rowsAffected = BCDB2.Instance.ExecuteNonQueryWithParams(sqlStr, pc); } catch (Exception e) { backgroundWorker1.ReportProgress(1, ("Automation - unable to update appt id=" + apptid.ToString() + " printed=1.\n" + e.ToString())); } if (status != null) { try { status.LogStatus(idString, "Automation", "Ok", ("Appt " + apptid + " " + print_report).Trim()); } catch (System.Web.Services.Protocols.SoapException e) { //do nothing } catch (Exception ex) { Logger.Instance.WriteToLog(ex.ToString()); } } } }
public void automate() { if (status != null) { try { //status.LogHeartbeat(this.Text); } catch (Exception ex) { // Logger.Instance.WriteToLog(ex.ToString()); } } List <int> apptIDs = new List <int>(); /*********************** get appointments to automate ********************/ SqlDataReader reader; string CustomAutomationString = Configurator.getNodeValue("DatabaseInfo", "AUTOMATION_SQL"); if (string.IsNullOrEmpty(CustomAutomationString) == false) { reader = BCDB2.Instance.ExecuteReader(CustomAutomationString); } else { reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_Automation", null); } try { while (reader.Read()) { if (reader.IsDBNull(0) == false) { apptIDs.Add(reader.GetInt32(0)); } } } catch (Exception e) { Logger.Instance.WriteToLog("Error getting appointment IDs: " + e.ToString()); } finally { if (reader != null) { reader.Close(); } } if (apptIDs.Count == 0) { ProcessingThread.ReportProgress(0, "No appointments are marked for automation - " + DateTime.Now.ToString()); return; } /*********************** automation cycle ********************/ foreach (int apptid in apptIDs) { if (ProcessingThread.CancellationPending) { return; } ProcessingThread.ReportProgress(0, "Running automation for appt: " + apptid.ToString() + "."); /*********************** Get Unitnum & set active patient ********************/ string unitnum = ""; ParameterCollection pc = new ParameterCollection("apptID", apptid); using (reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_3_GetUnitnum", pc)) //ExecuteReader("SELECT unitnum FROM tblAppointments WHERE apptID = " + apptid)) { if (reader != null && reader.Read()) { unitnum = reader.GetValue(0).ToString(); } } //StatusReport sr = new StatusReport(); //sr.apptid = apptid; //sr.description = "Started Automation cycle"; //ProcessingThread.ReportProgress(300, sr); //StatusReport sr = (StatusReport)e.UserState; //if (status != null) //{ try { //temp debug //status.LogStatus(this.Text, // "Automation", // "Ok", // "tarted Automation cycle " + apptid.ToString()); } catch (Exception ex) { Logger.Instance.WriteToLog(ex.ToString()); } //} SessionManager.Instance.SetActivePatientNoCallback(unitnum, (int)apptid); /*********************** Risk Calculations ********************/ try { ProcessingThread.ReportProgress(1, "Running risk calculations for appointment: " + apptid.ToString() + "."); SessionManager.Instance.GetActivePatient().RecalculateRisk(); } catch (Exception e) { ProcessingThread.ReportProgress(1, "Risk calculations FAILED for appointment: " + apptid.ToString() + ". " + e.ToString()); } /*********************** Update BigQueue ********************/ try { ProcessingThread.ReportProgress(1, "Updating BigQueue for appointment: " + apptid.ToString() + "."); pc = new ParameterCollection("unitnum", unitnum); BCDB2.Instance.RunSPWithParams("sp_3_populateBigQueue", pc); } catch (Exception e) { ProcessingThread.ReportProgress(1, "Updating BigQueue FAILED for appointment: " + apptid.ToString() + ". " + e.ToString()); } /*********************** Process Queue Documents ********************/ ProcessingThread.ReportProgress(1, "Processing Queue Documents for appointment: " + apptid.ToString() + "."); pc = new ParameterCollection("apptID", apptid); BCDB2.Instance.RunSPWithParams("sp_processQueueDocuments", pc); /*********************** Process Automation Save Documents ********************/ ProcessingThread.ReportProgress(1, "Processing Auto Save Documents: " + apptid.ToString() + "."); ApptUtils.saveAutomationDocumentsToPrintQueue(apptid); /*********************** Run Automation Stored Procedures ********************/ ProcessingThread.ReportProgress(1, "Run Automation Stored Procedures : " + apptid.ToString() + "."); ApptUtils.runAutomationStoredProcedures(apptid); /*********************** export HL7 files ********************/ ProcessingThread.ReportProgress(1, "Export HL7 File: " + apptid.ToString() + "."); RiskService.exportHL7File(apptid); /*********************** Print Documents ********************/ try { ProcessingThread.ReportProgress(1, "Printing documents for appointment: " + apptid.ToString() + "."); ParameterCollection printDocArgs = new ParameterCollection(); printDocArgs.Add("apptid", apptid); reader = BCDB2.Instance.ExecuteReaderSPWithParams("sp_AutomationHtmlDocsToPrint", printDocArgs); int templateID = -1; string printer = ""; while (reader.Read()) { if (reader.IsDBNull(0) == false) { templateID = reader.GetInt32(0); } if (reader.IsDBNull(1) == false) { printer = reader.GetString(1); } if (templateID > 0) { if (string.IsNullOrEmpty(printer) == false) { if (printer.ToUpper() != "NO_PRINT") { ProcessingThread.ReportProgress(1, "Printing templateID " + templateID.ToString() + " for apptID " + apptid.ToString() + "."); HtmlDocument hdoc = new HtmlDocument(templateID, unitnum, apptid); hdoc.targetPrinter = printer; hdoc.apptid = apptid; htmlInProgress = true; //try //{ // ////temp debug // //status.LogStatus(this.Text, // // "Automation", // // "Ok", // // "Appt " + hdoc.apptid.ToString() + " Sent doc " + hdoc.template.documentTemplateID.ToString() + " to " + hdoc.targetPrinter); //} //catch (Exception ex) { Logger.Instance.WriteToLog(ex.ToString()); } if (status != null) { try { status.LogStatus(this.Text, "Automation", "Ok", "Appt " + hdoc.apptid.ToString() + " Sent doc " + hdoc.template.documentTemplateID.ToString() + " to " + hdoc.targetPrinter); } catch (System.Web.Services.Protocols.SoapException e) { //do nothing } catch (Exception ex) { Logger.Instance.WriteToLog(ex.ToString()); } } ProcessingThread.ReportProgress(100, hdoc); while (htmlInProgress) { Thread.Sleep(100); } string sqlStr = "INSERT INTO tblDocuments([apptID],[documentTemplateID],[created],[createdBy]) VALUES(" + hdoc.apptid.ToString() + "," + hdoc.template.documentTemplateID + "," + "'" + DateTime.Now + "','AUTOMATION');"; BCDB2.Instance.ExecuteNonQuery(sqlStr); } } } } } catch (Exception e) { ProcessingThread.ReportProgress(1, "Printing FAILED for appointment: " + apptid.ToString() + ". " + e.ToString()); } finally { if (reader != null) { reader.Close(); } } /*********************** Mark as printed to complete automation ********************/ try { String sqlStr = "UPDATE tblRiskData SET printed = 1 WHERE apptid = " + apptid.ToString() + ";"; BCDB2.Instance.ExecuteNonQuery(sqlStr); } catch (Exception e) { ProcessingThread.ReportProgress(1, ("Automation - unable to update appt id=" + apptid.ToString() + " printed=1.\n" + e.ToString())); } ProcessingThread.ReportProgress(200, ""); } }