/// /// Copyright (c) 2018 Sensus Slovensko a.s. /// 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 NHibernate; using FluentNHibernate; using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using Results; using Results.Entities; using Statistics.Resources; using Statistics.UserControls; using System.Threading; using System.Globalization; namespace Statistics { public partial class StatisticsDlg : Form { static ISessionFactory[] sessionFactories = new ISessionFactory[Program.LocalSettings.ConnectionStrings.Length]; public StatisticsDlg() { InitializeComponent(); ///ManageCheckGroupBox(timePeriodCheckBox, timePeriodGroupBox); for (int midId = 1; midId < Program.LocalSettings.MidNames.Length; midId++) { TabPage tp = new TabPage(); tp.Text = Program.LocalSettings.MidNames[midId]; MidOrWaterMetersCtrl ctrl = new MidOrWaterMetersCtrl(); ctrl.MidId = midId; ctrl.ParentDlg = this; ctrl.Dock = DockStyle.Fill; tp.Controls.Add(ctrl); midsTabControl.TabPages.Add(tp); } radioButton1.Checked = true; timePeriodGroupBox.Enabled = false; Localize(); } void Localize() { Text = string.Format("MID {0}", Strings.Statistics); //timePeriodCheckBox.Text = Strings.Time_period; fromLabel1.Text = Strings.From; toLabel1.Text = Strings.To; radioButton1.Text = Strings.Last_week; radioButton2.Text = Strings.Last_month; radioButton3.Text = Strings.Last_3_months; radioButton4.Text = Strings.From_to; } private void StatisticsDlg_Load(object sender, EventArgs e) { Statistics.LocalSettings ls = Program.LocalSettings; Width = (ls.MainWndWidth > 0) ? ls.MainWndWidth : 1000; Height = (ls.MainWndHeight > 0) ? ls.MainWndHeight : 750; Left = (ls.MainWndLeft != 0) ? ls.MainWndLeft : 50; Top = (ls.MainWndTop != 0) ? ls.MainWndTop : 50; switch (ls.SelectedRadioButtonNumber) { case 1: radioButton1.Checked = true; break; case 2: radioButton2.Checked = true; break; case 3: radioButton3.Checked = true; break; case 4: radioButton4.Checked = true; break; } for (int i = 1; i < Program.LocalSettings.MidNames.Length; i++) { (midsTabControl.TabPages[i - 1].Controls[0] as MidOrWaterMetersCtrl).Data2UI(); } for (int i = 0; i < Program.LocalSettings.ConnectionStrings.Length; i++) { string connStr = Program.LocalSettings.GetConnectionString(i); if (!string.IsNullOrEmpty(connStr)) { try { sessionFactories[i] = Fluently.Configure() .Database(MySQLConfiguration.Standard.ConnectionString(connStr)) .Mappings(m => m.FluentMappings.AddFromAssemblyOf()) .ExposeConfiguration(BuildSchema) .BuildSessionFactory(); } catch (Exception) { sessionFactories[i] = null; MessageBox.Show(string.Format("Cannot open one of database of {0}", Program.LocalSettings.GetBenchName(i)), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } } void BuildSchema(NHibernate.Cfg.Configuration config) { /// This NHibernate tool takes a configuration with mapping info and exports a database schema new NHibernate.Tool.hbm2ddl.SchemaExport(config).SetOutputFile("db_schema"); } private void ManageCheckGroupBox(CheckBox chk, GroupBox grp) { /// Make sure the CheckBox isn't in the GroupBox. This will only happen the first time. if (chk.Parent == grp) { grp.Parent.Controls.Add(chk); /// Reparent the CheckBox so it's not in the GroupBox. chk.Location = new Point(chk.Left + grp.Left, chk.Top + grp.Top); /// Adjust the CheckBox's location. chk.BringToFront(); /// Move the CheckBox to the top of the stacking order. } /// Enable or disable the GroupBox. grp.Enabled = chk.Checked; } //private void timePeriodCheckBox_CheckedChanged(object sender, EventArgs e) //{ // ManageCheckGroupBox(timePeriodCheckBox, timePeriodGroupBox); //} /// /// Called by a child form. /// Reads and returns results from DB required by the child form to update it's content. /// /// 0-based bench Id. /// 1-based MID Id. /// Required test results public IList UpdateRequest(int benchId, int midId) { if ((benchId < 0) || (benchId >= Program.LocalSettings.BenchNames.Length) || (sessionFactories[benchId] == null)) { MessageBox.Show(string.Format("Cannot load statistics of {0}", Program.LocalSettings.GetBenchName(benchId))); return null; } Thread thread = new Thread(() => Application.Run(new Forms.ModelessForm() { Title = string.Empty, Message = Strings.searching_, BackgroundColor = Color.LightGreen, FontFamily = "Arial", FontSize = 14, FontStyle = FontStyle.Regular, })); thread.CurrentCulture = CultureInfo.CurrentCulture; thread.CurrentUICulture = CultureInfo.CurrentUICulture; thread.Start(); DateTime from; DateTime to; GetDateRange(out from, out to); try { ISession session = sessionFactories[benchId].OpenSession(); var listOfComponentsWithFlowmeter = session.QueryOver() .Where(x => (x.Flowmeter == Program.LocalSettings.MidNames[midId])) .List(); IList testRslts = new List(); foreach (var cmpnts in listOfComponentsWithFlowmeter) { var listOfRslts = session.QueryOver() .Where(x => ((x.EndTime >= from) && (x.EndTime <= to))) .And(x => (x.Components == cmpnts)) .List(); foreach (var tr in listOfRslts) testRslts.Add(tr); } Forms.ModelessForm.CloseForm(); session.Close(); return testRslts; } catch (Exception exc) { Forms.ModelessForm.CloseForm(); MessageBox.Show(string.Format("Error loading statistics of {0}\r\n{1}", Program.LocalSettings.GetBenchName(benchId), exc.Message)); return null; } } void GetDateRange(out DateTime from, out DateTime to) { if (radioButton1.Checked) { /// Last week from = DateTime.Now.Date.AddDays(-7); to = DateTime.Now; } else if (radioButton2.Checked) { /// Last month from = DateTime.Now.Date.AddDays(-31); to = DateTime.Now; } else if (radioButton3.Checked) { /// Last 3 months from = DateTime.Now.Date.AddDays(-91); to = DateTime.Now; } else { /// From / to from = dateTimeFromPicker.Value.Date; to = dateTimeToPicker.Value.Date.AddDays(1).AddTicks(-1); } } private void radioButton4_CheckedChanged(object sender, EventArgs e) { timePeriodGroupBox.Enabled = radioButton4.Checked; } private void StatisticsDlg_FormClosing(object sender, FormClosingEventArgs e) { LocalSettings ls = Program.LocalSettings; ls.ManiWndMaximized = (WindowState == FormWindowState.Maximized); if (WindowState == FormWindowState.Normal) { ls.MainWndLeft = Location.X; ls.MainWndTop = Location.Y; ls.MainWndWidth = Size.Width; ls.MainWndHeight = Size.Height; } else { /// Maximized or minimized (when restarted, minimized window is restored as normal) ls.MainWndLeft = RestoreBounds.Left; ls.MainWndTop = RestoreBounds.Top; ls.MainWndWidth = RestoreBounds.Width; ls.MainWndHeight = RestoreBounds.Height; } if (radioButton1.Checked) ls.SelectedRadioButtonNumber = 1; else if (radioButton2.Checked) ls.SelectedRadioButtonNumber = 2; else if (radioButton3.Checked) ls.SelectedRadioButtonNumber = 3; else if (radioButton4.Checked) ls.SelectedRadioButtonNumber = 4; else ls.SelectedRadioButtonNumber = 0; for (int i = 1; i < Math.Min(Program.LocalSettings.MidNames.Length, midsTabControl.TabPages.Count + 1); i++) { if (midsTabControl.TabPages[i - 1].Controls.Count > 0 && midsTabControl.TabPages[i - 1].Controls[0] is MidOrWaterMetersCtrl) { (midsTabControl.TabPages[i - 1].Controls[0] as MidOrWaterMetersCtrl).UI2Data(); } } ls.Save(); } } }