using System; using System.Collections.Generic; using System.Text; using vlnObjectLibrary; using System.Xml; using System.IO; using System.Data; using System.Data.OleDb; using System.Reflection; namespace vlnServerLibrary { public class vlnServer // This class loads children for a vlnObject { #region Log4Net private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #endregion #region Static Properties private static string m_XmlControlPath = "vlnControl.xml"; private static XmlDocument m_XmlControl; public static string XMLControlPath { get { return m_XmlControlPath; } set { m_XmlControlPath = value; } } public static XmlDocument XMLControl { get { if (m_XmlControl == null) { m_XmlControl = new XmlDocument(); lock (m_XmlControl) { m_XmlControl.Load(m_XmlControlPath); } } return m_XmlControl; } } #endregion private List LoadChildren(vlnObject o) { o.Children = new List(); LoadChildrenFromFolders(o); LoadChildrenFromDB(o,LookUp(o,"select")); return o.Children; } public string GetChildren(string sVlnObject) { using (vlnObject o = new vlnObject(sVlnObject)) { LoadChildren(o); return o.AllChildren(); } } public string GetDataPath() { vlnObject o = new vlnObject(); o.Type="datapath"; o.Title="Data Path"; o.Path=LookUpX(o,".//default/@datapath"); return o.ToString(); } protected string LookUp(vlnObject o,string s) { string xPath = string.Format(".//dbObject[@type='{0}']/@{1}", o.Type, s); return LookUpX(o,xPath); } protected string LookUpP(vlnObject o,string s) { string xPath = string.Format(".//dbObject[@type='{0}']/@{1}", o.Parent.Type, s); return LookUpX(o,xPath); } protected string LookUpX(vlnObject o,string xPath) { return LookUpX(o,xPath, XMLControl); } private void LoadChildrenFromDB(vlnObject o,string sSelect) { if (sSelect != "") { try { string s = LookUpX(o,".//connection/@string"); OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder(s); OleDbConnection dbConn = new OleDbConnection(csb.ConnectionString); string sTable = LookUp(o,"table"); //if (dbConn.State != ConnectionState.Open) dbConn.Open(); OleDbDataAdapter da = new OleDbDataAdapter(sSelect, dbConn); DataSet ds = new DataSet(); da.Fill(ds, sTable); string srt = LookUp(o,"sort"); DataView dv = new DataView(ds.Tables[sTable], LookUp(o,"criteria"), LookUp(o,"sort"), DataViewRowState.CurrentRows); foreach (DataRowView mydrv in dv) { vlnObject vb = o.Add(LookUp(o,"child"), "", o.Path); foreach (DataColumn dc in dv.Table.Columns) { vb.AddProp(dc.ColumnName, mydrv[dc.ColumnName].ToString()); } vb.Title = LookUpP(vb,"title"); o.Children.Add(vb); } } catch (Exception ex) { log.ErrorFormat("{0}\r\n{1}", ex.Message, ex.InnerException); } } } public DataSet GetDataSet(vlnObject o, string sSelect) { if (sSelect != "") { try { OleDbConnection dbConn = new OleDbConnection(LookUpX(o, ".//connection/@string")); OleDbDataAdapter da = new OleDbDataAdapter(sSelect, dbConn); DataSet ds = new DataSet(); da.Fill(ds, LookUp(o, "table")); return ds; } catch (Exception ex) { log.ErrorFormat("{0}\r\n{1}", ex.Message, ex.InnerException); } } return null; } public OleDbDataReader GetDataReader(vlnObject o, string sSelect) { if (sSelect != "") { OleDbConnection dbConn = new OleDbConnection(LookUpX(o, ".//connection/@string")); OleDbCommand cmd = new OleDbCommand(sSelect, dbConn); cmd.CommandType = CommandType.Text; dbConn.Open(); OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return dr; } return null; } private void LoadChildrenFromFolders(vlnObject o) { // First look in the XML to see if there are any folders for the specified item XmlNodeList nl = XMLControl.SelectNodes(string.Format(".//dbObject[@type='{0}']/folders/folder", o.Type)); if (nl.Count != 0) { foreach (XmlNode xn in nl) { LoadChildrenFromFolder(o,xn); } } } private void LoadChildrenFromFolder(vlnObject o, XmlNode xn) { // First look in the XML to see if there are any folders for the specified item string sTmp = LookUpX(o,"@template", xn); if (sTmp != "") { string sException = LookUpX(o,"@exception", xn); string sRequired = LookUpX(o,"@requires", xn); DirectoryInfo di = new DirectoryInfo(o.Path); if (sRequired == "" || di.GetFileSystemInfos(sRequired).Length > 0) { if (sException == "" || di.GetFileSystemInfos(sException).Length == 0) { try { if (sTmp == ".") { LoadChildrenFromFolder(o, xn, di); } else { if(sTmp.Contains("\\")){ string[] sTmps = sTmp.Split('\\'); DirectoryInfo [] dis = di.GetDirectories(sTmps[0]); foreach (DirectoryInfo di2 in dis) { foreach (FileSystemInfo di3 in di2.GetFileSystemInfos(sTmps[1])) { LoadChildrenFromFolder(o, xn, di3); } } } else { foreach (FileSystemInfo di1 in di.GetFileSystemInfos(sTmp)) { LoadChildrenFromFolder(o, xn, di1); } } } } catch (DirectoryNotFoundException ex) { // Ignore this exception log.InfoFormat("Directory {0} Template {1} Error {2}",di, sTmp,ex.Message); } catch (Exception ex) { log.ErrorFormat("Error: {0}\r\n{1}", ex.Message, ex.InnerException); } } } } } private void LoadChildrenFromFolder(vlnObject o,XmlNode xn, FileSystemInfo fi) { vlnObject vb = o.Add(LookUpX(o,"@child", xn), "", fi.FullName); vb.AddProp("filename", fi.Name); vb.Title = GetTitle(fi.FullName, LookUpX(vb,"@title", xn)); o.Children.Add(vb); } protected string GetTitle(string sPath, string sDefault) { string sRetval = sDefault; if (sRetval[0] == '~') { string sTitlePath = sPath + "\\title"; if (File.Exists(sTitlePath)) { StreamReader sr = File.OpenText(sTitlePath); return sr.ReadToEnd(); } sRetval = sRetval.Substring(1); } return sRetval; } protected string LookUpX(vlnObject o, string xPath, XmlNode xn) { XmlNode nd = xn; XmlNode ndf = nd.SelectSingleNode(xPath); while (ndf == null && nd.ParentNode != null) { nd = nd.ParentNode; ndf = nd.SelectSingleNode(xPath); } if (ndf == null) return ""; return o.ProcessString(ndf.InnerText); } } }