From ef3bd243920f4d1c1abea28131638a05b132414e Mon Sep 17 00:00:00 2001 From: Jake Date: Thu, 8 Sep 2022 19:49:28 +0000 Subject: [PATCH] B2022-107: Added code to update the Loading Screen / Progress Display --- .../Config/ROFSTLookup.cs | 224 ++++++++++++++---- 1 file changed, 172 insertions(+), 52 deletions(-) diff --git a/PROMS/VEPROMS.CSLA.Library/Config/ROFSTLookup.cs b/PROMS/VEPROMS.CSLA.Library/Config/ROFSTLookup.cs index 03d91e3b..9c4b1cbe 100644 --- a/PROMS/VEPROMS.CSLA.Library/Config/ROFSTLookup.cs +++ b/PROMS/VEPROMS.CSLA.Library/Config/ROFSTLookup.cs @@ -6,10 +6,10 @@ using System.Text.RegularExpressions; using System.Data; using System.Data.SqlClient; using System.Linq; +using System.Threading; using Csla.Data; -using System.Threading; -using System.Threading.Tasks; + namespace VEPROMS.CSLA.Library { @@ -106,10 +106,10 @@ namespace VEPROMS.CSLA.Library public int ID; public int ParentID; public int type; - public string title; // gets used for treeview - public string roid; // roid unique identifier - public string appid; // accessory page id - user specified unique id - public string value; // return value, can be multiple values + public string title; // gets used for treeview + public string roid; // roid unique identifier + public string appid; // accessory page id - user specified unique id + public string value; // return value, can be multiple values public rochild[] children; }; @@ -143,9 +143,18 @@ namespace VEPROMS.CSLA.Library private List _lstRoValues; private List _multiRoValues; private Dictionary _dicRoVars; - private ItemInfo _itemInfo; + // B2022-107: Display Progress Bar Messages/Statuses when a new ROFST binary file is loaded into the database + private frmRofstLoadStatus _frmRofstLoadStatus = null; + private Dictionary _dicRoCounts = null; + private bool _showLoadingStatus = true; + + private int _curRoCnt = 0; + private int _dbRoCnt = 0; + private int _totalRoCnt = 0; + private double _pctComplete = 0; + #endregion #region Properties @@ -187,14 +196,14 @@ namespace VEPROMS.CSLA.Library public List Extensions { - get + get { if (_extensions == null) { _extensions = RofstDataGetExtensions(); } - return _extensions; + return _extensions; } } @@ -217,26 +226,35 @@ namespace VEPROMS.CSLA.Library set { _autoCombineSingleRetValues = value; } } + public bool ShowLoadingStatus + { + get { return _showLoadingStatus; } + } + #endregion #region Constructors - public ROFSTLookup(int rofstID, DocVersionInfo dvi, string otherChild) + public ROFSTLookup(int rofstID, DocVersionInfo dvi, string otherChild, bool showLoadingStatus = true) { + // Set Fields/Properties _rofstID = rofstID; MyDocVersionInfo = dvi; _otherChild = otherChild; + _showLoadingStatus = showLoadingStatus; + // Check if ROFST file is already loaded, if not then parse/load Ro Values if (!RofstDataExists(rofstID)) { Load(rofstID); } } - public ROFSTLookup(int rofstID, DocVersionInfo dvi) + public ROFSTLookup(int rofstID, DocVersionInfo dvi, bool showLoadingStatus = true) { _rofstID = rofstID; MyDocVersionInfo = dvi; + _showLoadingStatus = showLoadingStatus; if (!RofstDataExists(rofstID)) { @@ -244,10 +262,11 @@ namespace VEPROMS.CSLA.Library } } - public ROFSTLookup(int rofstID) + public ROFSTLookup(int rofstID, bool showLoadingStatus = true) { _rofstID = rofstID; MyDocVersionInfo = null; + _showLoadingStatus = showLoadingStatus; if (!RofstDataExists(rofstID)) { @@ -609,6 +628,23 @@ namespace VEPROMS.CSLA.Library return new ROFSTLookup.rochild() { ID = -1, type = 0 }; } + public static string CalculateDuration(DateTime dtStart) + { + string duration = string.Empty; + + double totalSecs = TimeSpan.FromTicks(DateTime.Now.Ticks - dtStart.Ticks).TotalSeconds; + int minutes = (totalSecs > 60) ? Convert.ToInt32(totalSecs) / 60 : 0; + double secs = (minutes <= 0) ? totalSecs : (totalSecs - Convert.ToDouble(minutes * 60)); + + if (minutes > 0) duration = string.Format("{0} min(s) ", minutes.ToString("n0")); + if (secs < 0) secs = 0.00; + duration = duration + string.Format("{0,10:#####0.00} sec(s)", secs).Trim(); + + return duration; + + //return string.Format("{0,10:#####0.00}", TimeSpan.FromTicks(DateTime.Now.Ticks - dtStart.Ticks).TotalSeconds); + } + #endregion #endregion @@ -769,7 +805,7 @@ namespace VEPROMS.CSLA.Library cmd.CommandText = "vesp_RofstHeaderFinalizeLoad"; cmd.Parameters.Add(new SqlParameter("@RofstID", SqlDbType.Int)).Value = rofstID; - + cmd.ExecuteNonQuery(); } } @@ -830,7 +866,7 @@ namespace VEPROMS.CSLA.Library cmd.Parameters.Add(new SqlParameter("@type", SqlDbType.Int)).Value = type; cmd.Parameters.Add(new SqlParameter("@title", SqlDbType.VarChar)).Value = title; cmd.Parameters.Add(new SqlParameter("@roid", SqlDbType.VarChar)).Value = roid; - + if (!string.IsNullOrEmpty(appid)) cmd.Parameters.Add(new SqlParameter("@appid", SqlDbType.VarChar)).Value = appid; if (!string.IsNullOrEmpty(value)) @@ -1133,7 +1169,7 @@ namespace VEPROMS.CSLA.Library cmd.Parameters.Add(new SqlParameter("@RofstID", SqlDbType.Int)).Value = rofstID; cmd.Parameters.Add(new SqlParameter("@ValueTypes", SqlDbType.VarChar)).Value = valueTypes.ToCommaDelimString(); - + using (SafeDataReader dr = new SafeDataReader(cmd.ExecuteReader())) { while (dr.Read()) @@ -1379,6 +1415,8 @@ namespace VEPROMS.CSLA.Library for (int i = 0; i < dbs; i++) { + _dbRoCnt = 0; + int offset = hdrOffset + 6 + (i * 30); roh.myDbs[i].dbiID = BitConverter.ToInt16(ab, offset + 0); @@ -1397,6 +1435,8 @@ namespace VEPROMS.CSLA.Library roh.myDbs[i].ID = tmp.ID; roh.myDbs[i].children = tmp.children; + + _dicRoCounts.Add(tableID, _dbRoCnt); } return roh; @@ -1431,8 +1471,6 @@ namespace VEPROMS.CSLA.Library ROFSTLookup.rogrp tmpg = LoadGroup(ab, childOffset, tableID); - //MultipleReturnValuesInheritType(ref tmpg, tmp.type); - tmp.ID = tmpg.ID; tmp.ParentID = tmpg.ParentID; tmp.value = tmpg.value; @@ -1455,10 +1493,10 @@ namespace VEPROMS.CSLA.Library int slen = StringLength(ab, offset + 12); myGrp.value = Encoding.Default.GetString(ab, offset + 12, slen); - //ProcessROReturnValue(ref myGrp, Encoding.Default.GetString(ab, offset + 12, slen), tableID, dvi); - int slen2 = StringLength(ab, offset + 13 + slen); myGrp.appid = Encoding.Default.GetString(ab, offset + 13 + slen, slen2); + + _dbRoCnt++; } return myGrp; @@ -1466,40 +1504,101 @@ namespace VEPROMS.CSLA.Library private void Load(int rofstID) { - //Get Original ROLookup Bytes - byte[] bytes = RofstDataGetRofstLookupBytes(rofstID); + _dicRoCounts = new Dictionary(); - //Convert Bytes to Objects - ROFSTLookup.roHdr roh = ConvertFst2Objects(bytes); + DateTime dtStart = DateTime.Now; + string title = string.Empty; + string displayText = string.Empty; - //Save FstObjects to Database - string userID = "System"; - - //Insert Rofst Header - RofstHeaderInsert(rofstID, roh.hSize, roh.hMonth, roh.hDay, roh.hcYear, roh.hcMonth, - roh.hcDay, roh.hcHour, roh.hcMin, roh.hcSec, roh.hcHund, userID); - - for (int i = 0; i < roh.myDbs.Length; i++) + try { - ROFSTLookup.rodbi rodbi = roh.myDbs[i]; - - //Insert Rofst Database - RofstDatabaseInsert(rofstID, rodbi.dbiID, rodbi.dbiType, rodbi.dbiAW, - rodbi.dbiTitle, rodbi.dbiAP, rodbi.ID, rodbi.ParentID); - - if (rodbi.children != null && rodbi.children.Length > 0) + if (ShowLoadingStatus) { - for (int j = 0; j < rodbi.children.Length; j++) + _frmRofstLoadStatus = new frmRofstLoadStatus(); + _frmRofstLoadStatus.Show(); + _frmRofstLoadStatus.Focus(); + } + + //Get Original ROLookup Bytes + OnProgressChanged("Importing ROFST File...", null, 1, 100); + byte[] bytes = RofstDataGetRofstLookupBytes(rofstID); + + //Convert Bytes to Objects + OnProgressChanged("Converting RO Values to Objects...", null, 5, 100); + ROFSTLookup.roHdr roh = ConvertFst2Objects(bytes); + + //Save FstObjects to Database + string userID = "System"; + + //Insert Rofst Header + OnProgressChanged("Creating Rofst Header...", null, 10, 100); + RofstHeaderInsert(rofstID, roh.hSize, roh.hMonth, roh.hDay, roh.hcYear, roh.hcMonth, + roh.hcDay, roh.hcHour, roh.hcMin, roh.hcSec, roh.hcHund, userID); + + _pctComplete = 10.0; + _totalRoCnt = Convert.ToInt32(_dicRoCounts.Sum(x => x.Value)); + + for (int i = 0; i < roh.myDbs.Length; i++) + { + ROFSTLookup.rodbi rodbi = roh.myDbs[i]; + + _dbRoCnt = (_dicRoCounts.Where(x => x.Key == rodbi.dbiID).Any()) ? Convert.ToInt32(_dicRoCounts.Where(x => x.Key == rodbi.dbiID).FirstOrDefault().Value) : 0; + _curRoCnt = 0; + + title = string.Format("Loading '{0}' Database... ({1} of {2})", rodbi.dbiTitle, i + 1, roh.myDbs.Length); + displayText = string.Format("Processing RO Value... ({0} of {1})", 1, _dbRoCnt.ToString("n0")); + + //Insert Rofst Database + OnProgressChanged(title, displayText, Convert.ToInt32(_pctComplete), 100); + RofstDatabaseInsert(rofstID, rodbi.dbiID, rodbi.dbiType, rodbi.dbiAW, rodbi.dbiTitle, rodbi.dbiAP, rodbi.ID, rodbi.ParentID); + + if (rodbi.children != null && rodbi.children.Length > 0) { - //Insert Rofst Child - LoadChild(rofstID, rodbi.dbiID, rodbi.children[j]); + for (int j = 0; j < rodbi.children.Length; j++) + { + //Insert Rofst Child + LoadChild(rofstID, rodbi.dbiID, rodbi.children[j]); + + displayText = string.Format("Processing RO Values... ({0} of {1})", _curRoCnt.ToString("n0"), _dbRoCnt.ToString("n0")); + OnProgressChanged(displayText, Convert.ToInt32(_pctComplete), 100); + } + } + + // Update Progress Bar Accordingly + if (_totalRoCnt > 0) _pctComplete = _pctComplete + ((Convert.ToDouble(_dbRoCnt) / Convert.ToDouble(_totalRoCnt)) * 80.0); + + OnProgressChanged(displayText, Convert.ToInt32(_pctComplete), 100); + } + + //Finalized Load for Rofst Header / Rebuild Indexes + OnProgressChanged("Finalizing Rofst Header and Rebuilding Indexes...", null, 90, 100); + RofstHeaderFinalizeLoad(rofstID); + + title = string.Format("Successfully Loaded ({0}) RO Values... Total Duration {1}", _totalRoCnt.ToString("n0"), CalculateDuration(dtStart)); + OnProgressChanged(title, null, 100, 100); + } + catch (Exception ex) + { + OnProgressChanged("Error Imported RO Values...", ex.Message, 100, 100); + } + finally + { + try + { + if (_frmRofstLoadStatus != null) + { + _frmRofstLoadStatus.Close(); + _frmRofstLoadStatus.Dispose(); + _frmRofstLoadStatus = null; + } + + if (_dicRoCounts != null || _dicRoCounts.Count > 0) + { + _dicRoCounts = null; } } + catch { } } - - //Finalized Load for Rofst Header / Rebuild Indexes - RofstHeaderFinalizeLoad(rofstID); - } private void LoadChild(int rofstID, int dbiID, ROFSTLookup.rochild child) @@ -1507,6 +1606,9 @@ namespace VEPROMS.CSLA.Library //Insert Rofst Child RofstChildInsert(rofstID, child.ID, child.ParentID, dbiID, child.type, child.title, child.roid, child.appid, child.value); + //Increment RO Count if RoChild has a return value + if (!string.IsNullOrEmpty(child.value)) _curRoCnt++; + if (child.children != null && child.children.Length > 0) { for (int i = 0; i < child.children.Length; i++) @@ -1517,6 +1619,24 @@ namespace VEPROMS.CSLA.Library } } + #region (Parse/Load - Events/Handlers) + + protected virtual void OnProgressChanged(string title, string displayText, int curVal = 0, int maxVal = 100) + { + // If frmRofstLoadStatus is not null then call Update Progress Method + if (_frmRofstLoadStatus != null) + _frmRofstLoadStatus.UpdateProgress(title, displayText, curVal, maxVal); + } + + protected virtual void OnProgressChanged(string displayText, int curVal = 0, int maxVal = 100) + { + // If frmRofstLoadStatus is not null then call Update Progress Method + if (_frmRofstLoadStatus != null) + _frmRofstLoadStatus.UpdateProgress(_frmRofstLoadStatus.Title, displayText, curVal, maxVal); + } + + #endregion + #endregion #region (Convert To Objects) @@ -1600,7 +1720,7 @@ namespace VEPROMS.CSLA.Library re.Offset = (int)dr.GetValue("Offset"); re.RoidExt = (string)dr.GetValue("RoidExt"); re.AccPageExt = (string)dr.GetValue("AccPageExt"); - + return re; } @@ -1665,7 +1785,7 @@ namespace VEPROMS.CSLA.Library child.value = null; } } - + _lstRoValues.Clear(); _multiRoValues.Clear(); @@ -1678,7 +1798,7 @@ namespace VEPROMS.CSLA.Library // C2022-001 new logic to handle new format of Parent/Child RO.FST // The RO.FST will not have a specific child value if that child value is the same as the default value // B2021-093 Don't look for child RO values if "roval" is null - if (string.IsNullOrEmpty(roval)) + if (string.IsNullOrEmpty(roval)) return null; // Clean Up RoValue @@ -1740,7 +1860,7 @@ namespace VEPROMS.CSLA.Library _dicRoVars = new Dictionary(); string tmp = ProcessRO(_myDocVersionInfo == null ? roval : _myDocVersionInfo.ProcessDocVersionSpecificInfo(roval), false); - + if (!string.IsNullOrEmpty(tmp) && _lstRoValues.Count == 0) // was not a multiple return value { _lstRoValues.Add(tmp); @@ -1782,7 +1902,7 @@ namespace VEPROMS.CSLA.Library bool nfnd = false; string pbstr = ProcessBrace(str.Substring(1, cnt - 2), cnt - 2, ref nfnd, multiRtnVal); - if (pbstr == null) + if (pbstr == null) return null; if (nfnd) @@ -1916,7 +2036,7 @@ namespace VEPROMS.CSLA.Library return str; string rtnstr = str; - int indx; + int indx; while ((indx = rtnstr.ToUpper().IndexOf("@HSP(")) > -1) { @@ -2045,7 +2165,7 @@ namespace VEPROMS.CSLA.Library DocVersionConfig dvc = (_myDocVersionInfo != null) ? _myDocVersionInfo.DocVersionConfig : null; if (dvc != null) dvc.SelectedSlave = this.SelectedSlave; - + switch (rc.roid) { case "FFFF00000001":