using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Results.Entities; using NHibernate; using log4net; using Oracle.DataAccess.Client; // ODP.NET Oracle managed provider using Oracle.DataAccess.Types; using TBF.BenchControl.DB.SensusOracle; namespace TBF.Forms { public partial class PreviousResultsDlg : Form { static readonly ILog log = LogManager.GetLogger(typeof(PreviousResultsDlg)); ISession session; int currentBatchNr; IList batches; public PreviousResultsDlg() { InitializeComponent(); currentBatchNr = Program.LocalSettings.BatchNr; SendAgainHandler += delegate(object sndr, PreviousResultIdEventArgs args) { if (InvokeRequired) { Invoke(new EventHandler(DoSendAgain), sndr, args); } else DoSendAgain(sndr, args); }; } private void PreviousResultsDlg_Load(object sender, EventArgs e) { Localize(); session = Results.DB.CreateSession(); batches = session.QueryOver() .WhereRestrictionOn(x => x.BatchNr) .IsBetween(Math.Max(1, currentBatchNr - 25)) .And(Math.Max(1, currentBatchNr - 1)) .OrderBy(x => x.BatchNr).Asc .List(); Redraw(); } void Localize() { Text = "Predošlé výsledky"; closeButton.Text = TBF.Resources.Strings.CloseBtnText; } public event EventHandler SendAgainHandler; /// /// Called from PreviousResultCtrl when 'Send again' button pressed. /// public void OnSendAgain(object sender, PreviousResultIdEventArgs data) { if (SendAgainHandler == null) return; try { SendAgainHandler(sender, data); } catch (Exception e) { log.Error("SendAgainHandler(...) failed", e); } } public void DoSendAgain(object sender, PreviousResultIdEventArgs data) { foreach (var b in batches) { if (b.BatchNr == data.BatchNr) { #if IPERLST //System.IO.StreamWriter logger; //string fname = "C:\\TBF\\Logs\\test.log"; //try { logger = new System.IO.StreamWriter(System.IO.File.Create(fname)); } //catch //{ // logger = null; // log.ErrorFormat("Was not able to open LU log file {0}", fname); //} //Database.WriteLogsToDisk(logger, b); OracleConnection conn = (Database.DBCfg.DBUsed == DBUsed.Production) ? new OracleConnection("Data Source=STARA01.WORLD;User Id=deltachef;Password=deltachef;") : new OracleConnection("Data Source=STARA_TEST.WORLD;User Id=deltachef;Password=deltachef;"); if (Database.WriteResultsToDatabase(conn, b, true) == Retv.OK) { b.RsltsSent = true; /// Result was successfully sent session.SaveOrUpdate(b); /// Update MySQL DB session.Flush(); (sender as PreviousResultCtrl).Sent = true; /// Update UI Redraw(); } else { MessageBox.Show("Nepodarilo sa uložiť, pozrite log-súbor", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); } #endif } } } void Redraw() { SuspendLayout(); flowLayoutPanel1.FlowDirection = FlowDirection.TopDown; flowLayoutPanel1.Controls.Clear(); if (batches == null || batches.Count == 0) return; foreach (var b in batches) { int passedCount = 0; int failedCount = 0; foreach (var wm in b.WaterMeters) { if (wm.Passed) { passedCount++; } else { failedCount++; } } if (passedCount + failedCount == 0) continue; flowLayoutPanel1.Controls.Add(new PreviousResultCtrl( this, b.BatchNr, b.StartTime, b.EndTime, b.ProcedureName, b.WaterMeters[0].PurchaseOrder, passedCount, failedCount, b.RsltsSent)); } ResumeLayout(); } private void closeButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }