using System;
using System.Collections.Generic;
using System.Text;
using TierGenerator.Common.Util.Settings;
using TierGenerator.Model.DataBase;
using System.Xml;
using TierGenerator.Common.Exception;
using TierGenerator.Common.Util.MsSql;
namespace TierGenerator.Common.Util.Generate.XmlModel
{
public class XmlBuilder
{
#region Variables
private XmlDocument doc;
private TierGeneratorSettings _settings;
private string _namespace;
private string _serviceName;
private string _infoModel;
private ColumnListItemModel _itemKeyColumn;
private string _listItemModel;
private ColumnListItemModel _listItemKeyColumn;
private string _xmlModelFilePath;
#endregion
#region Properties
public XmlDocument ModelXmlDocument
{
get { return doc; }
}
public string Namespace
{
get { return _namespace; }
set { _namespace = value; }
}
public string ServiceName
{
get { return _serviceName; }
set { _serviceName = value; }
}
public string InfoModel
{
get { return _infoModel; }
set { _infoModel = value; }
}
public ColumnListItemModel ItemKeyColumn
{
get { return _itemKeyColumn; }
set { _itemKeyColumn = value; }
}
public string ListItemModel
{
get { return _listItemModel; }
set { _listItemModel = value; }
}
public ColumnListItemModel ListItemKeyColumn
{
get { return _listItemKeyColumn; }
set { _listItemKeyColumn = value; }
}
public string XmlModelFilePath
{
get { return _xmlModelFilePath; }
set { _xmlModelFilePath = value; }
}
#endregion
public XmlBuilder(TierGeneratorSettings _settings)
{
this._settings = _settings;
}
public void GenerateXmlModel(TableListItemModel table, IList<ColumnListItemModel> tableColumns, TableListItemModel view, IList<ColumnListItemModel> viewColumns)
{
if (_namespace == null)
{
throw new InvalidDataException("Namespace is not defined!");
}
if (_serviceName == null)
{
throw new InvalidDataException("Service name is not defined!");
}
if (_infoModel == null)
{
throw new InvalidDataException("Info model name is not defined!");
}
doc = new XmlDocument();
GenerateXmlModelHeader();
GenerateItemInfoXmlModel(table, tableColumns);
if (view != null && viewColumns != null)
{
GenerateListItemXmlModel(view, viewColumns);
}
XmlModelFilePath = ServiceBuilderHelper.MakeNamespaceFolder(_settings.TierModelPath, Namespace) + ServiceName + ".xml";
doc.Save(XmlModelFilePath);
}
#region Helper Methods
private void GenerateXmlModelHeader()
{
XmlNode root = doc.CreateNode(XmlNodeType.Element, "TierModel", null);
XmlAttribute attrNamespace = doc.CreateAttribute("Namespace");
attrNamespace.Value = Namespace;
root.Attributes.Append(attrNamespace);
XmlAttribute attrServiceName = doc.CreateAttribute("ServiceName");
attrServiceName.Value = ServiceName;
root.Attributes.Append(attrServiceName);
doc.AppendChild(root);
}
#region Item info model declaration
private void GenerateItemInfoXmlModel(TableListItemModel table, IList<ColumnListItemModel> columns)
{
XmlNode item = doc.CreateNode(XmlNodeType.Element, "ItemModel", null);
XmlAttribute tableName = doc.CreateAttribute("DbTable");
tableName.Value = table.Name;
item.Attributes.Append(tableName);
XmlAttribute className = doc.CreateAttribute("ClassName");
className.Value = InfoModel;
item.Attributes.Append(className);
XmlAttribute caching = doc.CreateAttribute("Caching");
caching.Value = Boolean.FalseString;
item.Attributes.Append(caching);
XmlAttribute history = doc.CreateAttribute("History");
history.Value = Boolean.TrueString;
item.Attributes.Append(history);
XmlAttribute equatable = doc.CreateAttribute("Equatable");
equatable.Value = Boolean.TrueString;
item.Attributes.Append(equatable);
XmlAttribute parent = doc.CreateAttribute("Parent");
parent.Value = String.Empty;
item.Attributes.Append(parent);
XmlNode node = doc.CreateNode(XmlNodeType.Element, "Comment", null);
item.AppendChild(node);
GenerateItemInfoPropertiesXmlModel(item, columns);
GenerateItemInfoBasicMethods(item);
doc.DocumentElement.AppendChild(item);
}
private void GenerateItemInfoPropertiesXmlModel(XmlNode itemNode, IList<ColumnListItemModel> columns)
{
foreach (ColumnListItemModel column in columns)
{
MsSqlColumnTypeEnum msSqlColumnTypeEnum = MsSqlColumnHelper.GetMsSqlColumnTypeEnum(column.Type);
bool isFixedLengthStringType = false;
bool isBlob = MsSqlColumnHelper.IsBlob(msSqlColumnTypeEnum);
int length = 0;
if (column.Length == -1)
{
// MAX ( VARCHAR(MAX) or VARBINARY(MAX) )
length = Int32.MaxValue;
}
else
{
length = (int)Math.Round(MsSqlColumnHelper.GetLengthDivider(msSqlColumnTypeEnum) * column.Length, 0);
isFixedLengthStringType = MsSqlColumnHelper.IsFixedLengthString(msSqlColumnTypeEnum);
}
XmlNode property;
if (column == ItemKeyColumn)
{
XmlAttribute generateAttribute = doc.CreateAttribute("NeedToGenerate");
generateAttribute.Value = "true";
property = doc.CreateNode(XmlNodeType.Element, "KeyProperty", null);
property.Attributes.Append(generateAttribute);
}
else
{
property = doc.CreateNode(XmlNodeType.Element, "Property", null);
}
XmlAttribute readOnly = doc.CreateAttribute("ReadOnly");
readOnly.Value = Boolean.FalseString;
property.Attributes.Append(readOnly);
XmlNode cSharpProperty = doc.CreateNode(XmlNodeType.Element, "CSharp", null);
// make a comment element
XmlNode comment = doc.CreateNode(XmlNodeType.Element, "Comment", null);
property.AppendChild(comment);
// make a csharp element
XmlAttribute cSharpName = doc.CreateAttribute("CSharpName");
cSharpName.Value = CSharpTypeHelper.GetCSharpPropertyName(column.Name);
cSharpProperty.Attributes.Append(cSharpName);
XmlAttribute cSharpType = doc.CreateAttribute("CSharpType");
//CSharpTypeEnum cSharpTypeEnum = MsSqlColumnHelper.GetCSharpTypeEnum(column.Type);
CSharpTypeEnum cSharpTypeEnum = MsSqlColumnHelper.GetCSharpTypeEnum(column);
cSharpType.Value = CSharpTypeHelper.GetCSharpTypeName(cSharpTypeEnum);
cSharpProperty.Attributes.Append(cSharpType);
if (isFixedLengthStringType)
{
XmlAttribute cSharpLength = doc.CreateAttribute("Length");
if (length < Int32.MaxValue)
{
cSharpLength.Value = length.ToString();
cSharpProperty.Attributes.Append(cSharpLength);
}
}
if (isBlob)
{
XmlAttribute cSharpIsGZipped = doc.CreateAttribute("IsGZipped");
cSharpIsGZipped.Value = false.ToString();
cSharpProperty.Attributes.Append(cSharpIsGZipped);
XmlAttribute cSharpSeparateLoad = doc.CreateAttribute("NeedSeparateLoad");
cSharpSeparateLoad.Value = false.ToString();
cSharpProperty.Attributes.Append(cSharpSeparateLoad);
}
property.AppendChild(cSharpProperty);
// make a db element
XmlNode dbProperty = doc.CreateNode(XmlNodeType.Element, "Db", null);
XmlAttribute dbName = doc.CreateAttribute("DbName");
dbName.Value = column.Name;
dbProperty.Attributes.Append(dbName);
XmlAttribute dbType = doc.CreateAttribute("DbType");
dbType.Value = column.Type;
dbProperty.Attributes.Append(dbType);
XmlAttribute isNullable = doc.CreateAttribute("IsNullable");
isNullable.Value = column.IsNullable.ToString();
dbProperty.Attributes.Append(isNullable);
XmlAttribute dbLength = doc.CreateAttribute("Length");
if (length < Int32.MaxValue)
{
dbLength.Value = length.ToString();
}
else
{
dbLength.Value = "max";
}
dbProperty.Attributes.Append(dbLength);
property.AppendChild(dbProperty);
itemNode.AppendChild(property);
}
}
private void GenerateItemInfoBasicMethods(XmlNode item)
{
#region select method
XmlNode select = doc.CreateNode(XmlNodeType.Element, "SelectMethod", null);
XmlAttribute selectNeedToCreate = doc.CreateAttribute("NeedToCreate");
selectNeedToCreate.Value = true.ToString();
select.Attributes.Append(selectNeedToCreate);
XmlAttribute selectDalAccess = doc.CreateAttribute("DalAccess");
selectDalAccess.Value = true.ToString();
select.Attributes.Append(selectDalAccess);
XmlAttribute selectBllAccess = doc.CreateAttribute("BllAccess");
selectBllAccess.Value = true.ToString();
select.Attributes.Append(selectBllAccess);
item.AppendChild(select);
#endregion select method
#region insert method
XmlNode insert = doc.CreateNode(XmlNodeType.Element, "InsertMethod", null);
XmlAttribute insertNeedToCreate = doc.CreateAttribute("NeedToCreate");
insertNeedToCreate.Value = true.ToString();
insert.Attributes.Append(insertNeedToCreate);
XmlAttribute insertDalAccess = doc.CreateAttribute("DalAccess");
insertDalAccess.Value = true.ToString();
insert.Attributes.Append(insertDalAccess);
XmlAttribute insertBllAccess = doc.CreateAttribute("BllAccess");
insertBllAccess.Value = true.ToString();
insert.Attributes.Append(insertBllAccess);
item.AppendChild(insert);
#endregion insert method
#region update method
XmlNode update = doc.CreateNode(XmlNodeType.Element, "UpdateMethod", null);
XmlAttribute updateNeedToCreate = doc.CreateAttribute("NeedToCreate");
updateNeedToCreate.Value = true.ToString();
update.Attributes.Append(updateNeedToCreate);
XmlAttribute updateDalAccess = doc.CreateAttribute("DalAccess");
updateDalAccess.Value = true.ToString();
update.Attributes.Append(updateDalAccess);
XmlAttribute updateBllAccess = doc.CreateAttribute("BllAccess");
updateBllAccess.Value = true.ToString();
update.Attributes.Append(updateBllAccess);
item.AppendChild(update);
#endregion update method
#region delete method
XmlNode delete = doc.CreateNode(XmlNodeType.Element, "DeleteMethod", null);
XmlAttribute deleteNeedToCreate = doc.CreateAttribute("NeedToCreate");
deleteNeedToCreate.Value = true.ToString();
delete.Attributes.Append(deleteNeedToCreate);
XmlAttribute deleteDalAccess = doc.CreateAttribute("DalAccess");
deleteDalAccess.Value = true.ToString();
delete.Attributes.Append(deleteDalAccess);
XmlAttribute deleteBllAccess = doc.CreateAttribute("BllAccess");
deleteBllAccess.Value = true.ToString();
delete.Attributes.Append(deleteBllAccess);
item.AppendChild(delete);
#endregion delete method
}
#endregion
#region List item model declaration
private void GenerateListItemXmlModel(TableListItemModel view, IList<ColumnListItemModel> columns)
{
XmlNode item = doc.CreateNode(XmlNodeType.Element, "ListItemModel", null);
XmlAttribute viewName = doc.CreateAttribute("DbView");
viewName.Value = view.Name;
item.Attributes.Append(viewName);
XmlAttribute className = doc.CreateAttribute("ClassName");
className.Value = ListItemModel;
item.Attributes.Append(className);
XmlAttribute parent = doc.CreateAttribute("Parent");
parent.Value = String.Empty;
item.Attributes.Append(parent);
XmlNode node = doc.CreateNode(XmlNodeType.Element, "Comment", null);
item.AppendChild(node);
GenerateListItemPropertiesXmlModel(item, columns);
doc.DocumentElement.AppendChild(item);
GenerateListItemBasicMethods(view);
}
private void GenerateListItemPropertiesXmlModel(XmlNode itemNode, IList<ColumnListItemModel> columns)
{
foreach (ColumnListItemModel column in columns)
{
MsSqlColumnTypeEnum msSqlColumnTypeEnum = MsSqlColumnHelper.GetMsSqlColumnTypeEnum(column.Type);
int length = (int)Math.Round(MsSqlColumnHelper.GetLengthDivider(msSqlColumnTypeEnum) * column.Length, 0);
bool isFixedLengthStringType = MsSqlColumnHelper.IsFixedLengthString(msSqlColumnTypeEnum);
XmlNode property;
if (column == ListItemKeyColumn)
{
property = doc.CreateNode(XmlNodeType.Element, "KeyProperty", null);
}
else
{
property = doc.CreateNode(XmlNodeType.Element, "Property", null);
}
XmlNode cSharpProperty = doc.CreateNode(XmlNodeType.Element, "CSharp", null);
// make a comment element
XmlNode comment = doc.CreateNode(XmlNodeType.Element, "Comment", null);
property.AppendChild(comment);
// make a csharp element
XmlAttribute cSharpName = doc.CreateAttribute("CSharpName");
cSharpName.Value = CSharpTypeHelper.GetCSharpPropertyName(column.Name);
cSharpProperty.Attributes.Append(cSharpName);
XmlAttribute cSharpType = doc.CreateAttribute("CSharpType");
CSharpTypeEnum cSharpTypeEnum = MsSqlColumnHelper.GetCSharpTypeEnum(column);
cSharpType.Value = CSharpTypeHelper.GetCSharpTypeName(cSharpTypeEnum);
cSharpProperty.Attributes.Append(cSharpType);
if (isFixedLengthStringType)
{
XmlAttribute cSharpLength = doc.CreateAttribute("Length");
cSharpLength.Value = length.ToString();
cSharpProperty.Attributes.Append(cSharpLength);
}
property.AppendChild(cSharpProperty);
// make a db element
XmlNode dbProperty = doc.CreateNode(XmlNodeType.Element, "Db", null);
XmlAttribute dbName = doc.CreateAttribute("DbName");
dbName.Value = column.Name;
dbProperty.Attributes.Append(dbName);
XmlAttribute dbType = doc.CreateAttribute("DbType");
dbType.Value = column.Type;
dbProperty.Attributes.Append(dbType);
XmlAttribute isNullable = doc.CreateAttribute("IsNullable");
isNullable.Value = column.IsNullable.ToString();
dbProperty.Attributes.Append(isNullable);
XmlAttribute dbLength = doc.CreateAttribute("Length");
dbLength.Value = length.ToString();
dbProperty.Attributes.Append(dbLength);
property.AppendChild(dbProperty);
itemNode.AppendChild(property);
}
}
private void GenerateListItemBasicMethods(TableListItemModel view)
{
XmlNode item = doc.CreateNode(XmlNodeType.Element, "SelectMethod", null);
XmlAttribute name = doc.CreateAttribute("Name");
name.Value = String.Format("Get{0}", ServiceBuilderHelper.GetListServiceName(ListItemModel));
item.Attributes.Append(name);
XmlNode selectMethodComment = doc.CreateNode(XmlNodeType.Element, "Comment", null);
item.AppendChild(selectMethodComment);
// return
XmlNode returnNode = doc.CreateNode(XmlNodeType.Element, "Return", null);
XmlNode returnComment = doc.CreateNode(XmlNodeType.Element, "Comment", null);
returnNode.AppendChild(returnComment);
XmlAttribute returnType = doc.CreateAttribute("ReturnType");
returnType.Value = "IList";
returnNode.Attributes.Append(returnType);
XmlAttribute type = doc.CreateAttribute("Type");
type.Value = ListItemModel;
returnNode.Attributes.Append(type);
item.AppendChild(returnNode);
// sql query
XmlNode sqlNode = doc.CreateNode(XmlNodeType.Element, "Sql", null);
XmlNode query = doc.CreateNode(XmlNodeType.Element, "Query", null);
string sqlQuery = "SELECT * FROM " + view.Name;
XmlCDataSection queryCData = doc.CreateCDataSection(sqlQuery);
query.AppendChild(queryCData);
sqlNode.AppendChild(query);
item.AppendChild(sqlNode);
doc.DocumentElement.AppendChild(item);
}
#endregion
#endregion Helper Methods
}
}