The class below uses the switch clause in the GetXSLT method. I want it to code it with a more OO approach does anyone have any suggestions, or change to make.
cheers
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Diagnostics;
namespace MetaData
{
/// <summary>
/// MessageController Class
/// This class is used to parse the ORE XML Messages, and identify which set of XSLT transforms to use to
/// process the XML to fill the ORE Message with meta data. Depending on the ORE XML Message an instance
/// of the DAOFacade class is instantiated and the retrieve method is called to access the DB Meta Repository
/// In order to start the process of reading debug strings you should do the following:
/// 1) Set the string XMLMessage property to the ORE XML Message.
/// 2) execute the MessageParser instance method, which instances GetXSLT method;
/// 3) register at least one event handler for the DebugData event;
/// 4) Depending on the Action value, Transform mehtod is executed and/or GetMetaData/ UpdateMetaData methods.
/// </summary>
public class MessageController
{
#region Constructor
public MessageController()
{
mstrXSLTDir = ConfigurationSettings.AppSettings["XSLTDir"];
mstrMetaConn = ConfigurationSettings.AppSettings["MetaConnString"];
}
#endregion
#region Variable Declarations
public static Hashtable xmlEntityCache = new Hashtable();
public static Hashtable xsltCache = new Hashtable();
private string mstrXmlMessage;
private string mstrDACMessage;
private string mstrEntityName;
private string mstrXSLTDir;
private string mstrMetaConn;
private string mstrAction;
#endregion
#region Message Controller Properties
public string xmlAction
{
get
{
return mstrAction;
}
set
{
mstrAction = value;
}
}
public string xmlEntityName
{
get
{
return mstrEntityName;
}
set
{
mstrEntityName = value;
}
}
public string xmlInMessage
{
get
{
return mstrXmlMessage;
}
set
{
mstrXmlMessage = value ;
}
}
public string DACOutMessage
{
get
{
return mstrDACMessage;
}
set
{
mstrDACMessage = value ;
}
}
#endregion
#region Message Parser Method
/// <summary>
/// Message Parser Method retrieves the values of the Entity and Action attributes
/// and uses property mstrXMLMessage to find the Action and Entity value.
/// </summary>
/// <returns>XML message response as string</returns>
public string MessageParser()
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.LoadXml(mstrXmlMessage);
XPathNavigator Nav = xmlDoc.CreateNavigator();
System.Xml.XPath.XPathNodeIterator ActionIterator = Nav.Select("//message/@action");
System.Xml.XPath.XPathNodeIterator EntityIterator = Nav.Select("//message/Entity/@Name");
ActionIterator.MoveNext();
EntityIterator.MoveNext();
mstrAction = ActionIterator.Current.Value.ToString();
//TODO: if Action is Retrieve then check cache for schemas, iterate through entities.
mstrEntityName = EntityIterator.Current.Value.ToString();
return GetXSLT();
}
#endregion
#region Private Methods and Constructors
/// <summary>
/// Parses XML strings to Hashtable
/// </summary>
/// <param name="entityName"></param>
/// <returns>Values of elements in Hashtable</returns>
private bool ParseToHashtable(string entityName)
{
if(xmlEntityCache.ContainsKey(entityName))
{
//return cached schema
return false;
}
else
{
return true;
}
}
/// <summary>
/// Entity Processor performs the main XSLT's and queries database to return schemas as
/// output to DAC or ORE
/// </summary>
/// <returns>XML String for DAC (Schema, DataSet)</returns>
/// <paramref name="No Parameters"/>
public string GetXSLT()
{
string result;
switch (mstrAction)
{
case "CreateEntity":
result = MessageController.Transform(mstrXSLTDir + "\\" + "createEntity_dac.xslt", mstrXmlMessage);
return result;
case "Create":
//result = MessageController.Transform(mstrXSLTDir + "\\" + "DataSetFromDAC.xslt", GetMetaDataSet().GetXml().ToString());
result = MessageController.Transform(mstrXSLTDir + "\\" + "metamessage.xslt", GetMetaDataSet().GetXml().ToString());
return result;
case "Retrieve":
//result = MessageController.Transform(mstrXSLTDir + "\\" + "DataSetFromDAC.xslt",GetMetaDataSet().GetXml().To String());
result = MessageController.Transform(mstrXSLTDir + "\\" + "metamessage.xslt", GetMetaDataSet().GetXml().ToString());
MessageCache.SaveToFileCache(result, mstrEntityName);
return result;
case "Update":
//result = MessageController.Transform(mstrXSLTDir + "\\" + "DataSetFromDAC.xslt",GetMetaDataSet().GetXml().To String());
result = MessageController.Transform(mstrXSLTDir + "\\" + "metamessage.xslt",GetMetaDataSet().GetXml().ToStr ing());
MessageCache.SaveToFileCache(result, mstrEntityName);
return result;
case "RetrieveSchema":
if (ParseToHashtable(mstrEntityName) == false)
{
return xmlEntityCache[mstrEntityName].ToString();
}
else
{
//DataSet dacresult meta query to get entity
//DataSet dacresult1 = new DataSet();
//dacresult1.ReadXml(mstrXSLTDir + "\\" + "oregetschemadac.xml");
result = MessageController.Transform(mstrXSLTDir + "\\" + "OreGetSchema.xslt", GetMetaDataSet().GetXml().ToString());
xmlEntityCache.Add(mstrEntityName, result);
return result;
}
}
return "error";
}
/// <summary>
/// Returns the xml node as a string of all the connection details.
/// </summary>
/// <param name="MetaConnString"></param>
/// <param name="DLLFileName"></param>
private void GetConnection(out string MetaConnString, out string DLLFileName)
{
XmlDocument doc = new XmlDocument();
doc.Load(mstrXSLTDir + "\\" + "Provider_Types1.xml");
DLLFileName = doc.SelectSingleNode("NewDataSet/DataSource[IsMaster = -1]/FileName").InnerXml;
MetaConnString = doc.SelectSingleNode("NewDataSet/DataSource[IsMaster = -1]/ConnectionString").InnerXml;
}
private DataSet GetMetaDataSet()
{
try
{
//Transform entity dataset
string tmpEntityMetaData = MessageController.Transform(mstrXSLTDir + "\\" + "EntityDataSetnew.xslt", mstrXmlMessage);
string tmpEntityMetaDataMessage = MessageController.Transform(mstrXSLTDir + "\\" + "EntityDataSetMeta.xslt", mstrXmlMessage);
//Get entity dataset from DAC giving tmpEntityMetaData
DAOFacade daoentity = new DAOFacade();
DataSet EntityData = new DataSet();
EntityData = daoentity.RetrieveData(tmpEntityMetaData, tmpEntityMetaDataMessage);
//EntityData.ReadXml(mstrXSLTDir + "\\" + "EntityDataSet.xml");
//Transform entity dataset
string tmpAttributeMetaData = MessageController.Transform(mstrXSLTDir + "\\" + "AttributeDataSetnew.xslt", EntityData.GetXml().ToString());
string tmpAttributeMetaDataMessage = MessageController.Transform(mstrXSLTDir + "\\" + "AttributeDataSetMeta.xslt", EntityData.GetXml().ToString());
//Get Attribute dataset from DAC
DataSet AttribData = new DataSet();
DAOFacade daoattribute = new DAOFacade();
AttribData = daoattribute.RetrieveData(tmpAttributeMetaData, tmpAttributeMetaDataMessage);
//AttribData.ReadXml(mstrXSLTDir + "\\" + "AttributeDataSet.xml");
//Merge Datasets (Entity and Attribute)
EntityData.Merge(AttribData);
EntityData.Dispose();
AttribData.Dispose();
return EntityData;
}
catch(Exception ex)
{
throw(ex);
}
}
/// <summary>
/// Inserts entries into MetaData Store.
/// </summary>
/// <param name="oreMessage"></param>
/// <returns></returns>
public DataSet UpdateMetaData(string oreMessage)
{
try
{
//Transform entity dataset
// Transform ORE orignal message to create Meta DataSet to update MetaStore DB.
string tmpEntityMetaData = MessageController.Transform(mstrXSLTDir + "\\" + "CreateEntity.xslt", oreMessage);
//Create new instance of a dataset that will hold the returned Entity/Attribute datatables.
DataSet EntityData = new DataSet();
//EntityData.ReadXmlSchema("C:\\DACXML\\EntityAttrib ute.xsd");
//read the transformed xml in a StringReader
StringReader sr = new StringReader(tmpEntityMetaData);
//sr.Read((tmpEntityMetaData.ToCharArray()),0,tmpEnt ityMetaData.ToCharArray().Length);
EntityData.ReadXmlSchema("C:\\DACXML\\EntityAttrib ute.xsd");
//Fill the dataset with the stream contents.
EntityData.ReadXml(sr);
sr.Close();
//Create a new instance of the DAC facade
//Method call to update MetaStore.
string MetaConn;
string MetaDLLFile;
GetConnection(out MetaConn, out MetaDLLFile);
DAOFacade DAOIns = new DAOFacade();
DAOIns.UpdateDataSet(MetaDLLFile, MetaConn, EntityData);
EntityData.Dispose();
return EntityData;
}
catch(Exception ex)
{
EventLog.WriteEntry("UpdateMetaData", ex.Message);
throw(ex);
}
}
#endregion
#region XSLT Transform Method
/// <summary>
/// Main XSLT Transform Method static method.
/// </summary>
/// <param name="xsltName">Name of xslt file to use in the transform</param>
/// <param name="xmlMessage">xmlMessage to transfom</param>
/// <returns>XML transform string</returns>
public static string Transform(string xsltName, string xmlMessage)
{
try
{
//get the xml doc.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlMessage);
XslTransform xslDoc = new XslTransform();
xslDoc.Load(xsltName);
XsltArgumentList args=new XsltArgumentList();
MemoryStream ms = new MemoryStream();
xslDoc.Transform(xmlDoc,args,ms,new XmlUrlResolver());
ms.Flush();
ms.Position =0;
StreamReader sr = new StreamReader(ms);
string result = sr.ReadToEnd();
sr.Close();
ms.Close();
return result;
}
catch(Exception ex)
{
throw(ex);
}
finally
{
//ms.Close();
}
}
#endregion
}
}
************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...