diff --git a/PROMS/DataLoader/OutsideTransition.cs b/PROMS/DataLoader/OutsideTransition.cs new file mode 100644 index 00000000..50d63864 --- /dev/null +++ b/PROMS/DataLoader/OutsideTransition.cs @@ -0,0 +1,228 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Data; +using System.Data.OleDb; +//using System.Collections.Specialized; +//using System.Collections.Generic; +//using System.Xml; +//using System.IO; +//using System.Text; +using VEPROMS.CSLA.Library; + +namespace DataLoader +{ + class OutsideTransition + { + private OleDbConnection _DbConnect; + + public OleDbConnection DbConnect + { + get { return _DbConnect; } + set { _DbConnect = value; } + } + + private Dictionary _TranLookup; + + public OutsideTransition(OleDbConnection cn) + { + _DbConnect = cn; + _TranLookup = new Dictionary(); + } + + public ItemInfo GetItem(string key) + { + if (!_TranLookup.ContainsKey(key)) + _TranLookup.Add(key, Find(key)); + return _TranLookup[key]; + } + + private ItemInfo Find(string key) + { + string setid = key.Substring(0, 8); + string procid = key.Substring(8, 8); + string path = GetPath(setid);// Get set path from XTSETID + string proc = GetProc(setid, procid);// Get procedure number from XTPROCID + return Find(path,proc);// Return ItemInfo based upon path and procedure number + } + + private ItemInfo Find(string path, string proc) + { + return OTLookup.Find(path, proc); + } + + private string GetProc(string setid, string procid) + { + string xtproccmd = "SELECT * FROM [xtprocid] WHERE [SETID]='" + setid + "' AND [PROCID]='" + procid + "'"; + OleDbDataAdapter da = new OleDbDataAdapter(xtproccmd, _DbConnect); + DataSet ds = new DataSet(); + da.Fill(ds); + DataTable dt = ds.Tables[0]; + dt.CaseSensitive = true; + DataRow dr = dt.Rows[0]; // there should be only one entry! + return dr["Procnum"].ToString(); // this should be the procedure number! + } + + private string GetPath(string setid) + { + string xtsetcmd = "SELECT * FROM [xtsetid] WHERE [SETID]='" + setid + "'"; + OleDbDataAdapter da = new OleDbDataAdapter(xtsetcmd, _DbConnect); + DataSet ds = new DataSet(); + da.Fill(ds); + DataTable dt = ds.Tables[0]; + dt.CaseSensitive = true; + DataRow dr = dt.Rows[0]; // there should be only one entry! + return dr["Path"].ToString(); // this should be the path to the proc set! + } + + private string GetTitle(string setid, string procid) + { + string xtproccmd = "SELECT * FROM [xtprocid] WHERE [SETID]='" + setid + "' AND [PROCID]='" + procid + "'"; + OleDbDataAdapter da = new OleDbDataAdapter(xtproccmd, _DbConnect); + DataSet ds = new DataSet(); + da.Fill(ds); + DataTable dt = ds.Tables[0]; + dt.CaseSensitive = true; + DataRow dr = dt.Rows[0]; // there should be only one entry! + return dr["Proctitle"].ToString(); // this should be the procedure number! + } + + public string GetDescription(string key) + { + string setid = key.Substring(0, 8); + string procid = key.Substring(8, 8); + string path = GetPath(setid);// Get set path from XTSETID + string proc = GetProc(setid, procid);// Get procedure number from XTPROCID + string title = GetTitle(setid, procid);// Get procedure title from XTPROCID + return string.Format("{0}, {1} in folder {2}",proc,title,path); + } + //private string ProcessOutSideTrans(OleDbConnection cn, string thekey, string pth) + //{ + // string xtransstring = null; + // DataTable dt = null; + // DataSet ds = null; + // OleDbDataAdapter da = null; + + // DataTable dt2 = null; + // DataSet ds2 = null; + // OleDbDataAdapter da2 = null; + + // string tosetid = thekey.Substring(0, 8); + // string toprocid = thekey.Substring(8, 8); + + // /// Get the path to the procedure set from the SETID + // string xtsetcmd = "SELECT * FROM [xtsetid] WHERE [SETID]='" + tosetid + "'"; + // string xtproccmd = "SELECT * FROM [xtprocid] WHERE [SETID]='" + tosetid + "' AND [PROCID]='" + toprocid + "'"; + + // da = new OleDbDataAdapter(xtsetcmd, cn); + // da2 = new OleDbDataAdapter(xtproccmd, cn); + + // // get xtsetid records. + // ds = new DataSet(); + // try + // { + // da.Fill(ds); + // dt = ds.Tables[0]; + // dt.CaseSensitive = true; + // } + // catch (Exception ex) + // { + // log.Error("Error getting XTSETID"); + // log.ErrorFormat("to number = {0}", thekey); + // log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException); + // //return textm; + // } + // DataRow dr = dt.Rows[0]; // there should be only one entry! + // string setpth = dr["Path"].ToString(); // this should be the path to the proc set! + + // // get xtprocid records + // ds2 = new DataSet(); + // try + // { + // da2.Fill(ds2); + // dt2 = ds2.Tables[0]; + // dt2.CaseSensitive = true; + // } + // catch (Exception ex) + // { + // log.Error("Error getting XTPROCID"); + // log.ErrorFormat("to number = {0}", thekey); + // log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException); + // //return textm; + // } + // DataRow dr2 = dt2.Rows[0]; // there should be only one entry! + // string procnum = dr2["Procnum"].ToString(); // this should be the procedure number! + + // xtransstring = setpth + "\\" + procnum; // ex: VEHLP\PROCS\0POP05-EO00 + + // //if (pathsAreEqual(setpth, pth)) + // //{ + // // // Outside Transiton is to this procedure set (not really outsde) + // // // need to find the item id that references the proc number and title + // // // - find the setpth in the doc.version + // //} + // //else + // //return thekey; // is a true outside transition + + // return xtransstring; + //} + } + + internal static class OTLookup + { + private static Dictionary _Lookup = null; + public static ItemInfo Find(string path, string procnum) + { + string key = string.Format(@"{0}\{1}",path,procnum); + if (_Lookup == null) + _Lookup = new Dictionary(); + if (!_Lookup.ContainsKey(key)) + _Lookup.Add(key, FindItem(path, procnum)); + return _Lookup[key]; + } + + private static DocVersionInfoList _DocVersions = null; + private static ItemInfo FindItem(string path, string procnum) + { + if (_DocVersions == null) + _DocVersions = DocVersionInfoList.Get(); + DocVersionInfo dvi = ExactMatch(path); //TODO: Look for an exact match of the paths. + if (dvi == null) dvi = PartialMatch(path);//TODO: If none is found, look for a partial match. + if (dvi != null) return FindItem(dvi, procnum);//TODO: Look for the Procedure number in the procedure list and return item that matches + return null; + } + + private static ItemInfo FindItem(DocVersionInfo dvi, string procnum) + { + foreach (ItemInfo itm in dvi.Procedures) + if (itm.MyContent.Number == procnum) + return itm; + return null; + } + + private static DocVersionInfo PartialMatch(string path) + { + string partial = GetPartial(path); + foreach (DocVersionInfo dvi in _DocVersions) + { + if (dvi.MyFolder.Title.ToUpper().EndsWith(partial.ToUpper())) + return dvi; + } + return null; + } + + private static string GetPartial(string path) + { + string[] parts = path.Split("\\".ToCharArray()); + return "\\" + parts[parts.Length - 2] + "\\" + parts[parts.Length - 1]; + } + + private static DocVersionInfo ExactMatch(string path) + { + foreach (DocVersionInfo dvi in _DocVersions) + if (dvi.MyFolder.Title.ToUpper().EndsWith(path.ToUpper())) + return dvi; + return null; + } + } +}