<%#NAMESPACE System.IO, System.Text, System.Text.RegularExpressions, System.Globalization %><%
// $Id: CSharp_IBatis_BusinessObject.csgen,v 1.3 2005/12/15 23:57:34 morciuch Exp $
public class GeneratedTemplate : DotNetScriptTemplate
{
private ArrayList _selectedTables;
private ArrayList _selectedViews;
private string _dbName;
private string _tableName;
private string _className;
private string _exportPath;
private string _exportPathXml;
private string _fileName;
private string _nameSpace;
private string _assemblyName;
private string _prefix;
private bool _createClassFiles;
private bool _createXmlFiles;
private bool _createReadOnly;
private bool _generateEqualsHashCode;
private bool _castDecimalsAsReal;
public GeneratedTemplate( ZeusContext context ) : base( context ) {}
public override void Render()
{
_dbName = input["chooseDatabase"].ToString();
_selectedTables = input["chooseTables"] as ArrayList;
_selectedViews = input["chooseViews"] as ArrayList;
_exportPath = input["outputPath"].ToString();
_exportPathXml = input["outputPathXML"].ToString();
_nameSpace = input["classNamespace"].ToString();
_assemblyName = input["assemblyName"].ToString();
_prefix = input["memberPrefix"].ToString();
_createClassFiles = (bool)input["chkClass"];
_createXmlFiles = (bool)input["chkMapping"];
_createReadOnly = (bool)input["chkReadOnly"];
_generateEqualsHashCode = (bool)input["chkEqualsHashCode"];
_castDecimalsAsReal = (bool)input["chkCastAsReal"];
foreach( string _newTable in _selectedTables )
{
ITable _workingTable = MyMeta.Databases[_dbName].Tables[_newTable];
_tableName = _workingTable.Alias.Replace( " ", "" );
_className = ToPascalCase( _tableName );
if( _createClassFiles )
{
GenerateClassFile( _workingTable.Columns );
}
if( _createXmlFiles )
{
GenerateMappingFile( _workingTable.Columns );
}
}
foreach( string _newView in _selectedViews )
{
IView _workingView = MyMeta.Databases[_dbName].Views[_newView];
_tableName = _workingView.Alias.Replace( " ", "" );
_className = ToPascalCase( _tableName );
if( _createClassFiles )
{
GenerateClassFile( _workingView.Columns );
}
if( _createXmlFiles )
{
GenerateMappingFile( _workingView.Columns );
}
}
}
private void GenerateClassFile( IColumns Columns )
{%>/*
insert license info here
*/
using System;
namespace <%= _nameSpace %>
{
/// <summary>
/// Generated by MyGeneration using the IBatis Object Mapping template
/// </summary>
[Serializable]
public sealed class <%= _className %>
{
<% BuildPrivateMembers( Columns ); %>
<% BuildDefaultConstructor( Columns ); %>
<% BuildPublicAccessors( Columns ); %>
<% BuildPublicFunctions( Columns ); %>
<% if( _generateEqualsHashCode ) BuildEqualsHashCodeOverrides( Columns ); %>
}
}
<%
_fileName = _className + ".cs";
output.save( Path.Combine( _exportPath, _fileName ), false );
output.clear();
}
private void GenerateMappingFile( IColumns Columns )
{
BuildXMLDefinition( Columns );
_fileName = _className + ".xml";
output.save( Path.Combine( _exportPathXml, _fileName ), false );
output.clear();
}
private void BuildDefaultConstructor( IColumns Columns )
{
%>#region Default ( Empty ) Class Constuctor
/// <summary>
/// default constructor
/// </summary>
public <%= _className %>()
{<%
foreach( IColumn field in Columns )
{
string fieldName = ColumnToMemberVariable( field );
//string fieldType = ( field.IsInForeignKey && !field.IsInPrimaryKey ? ToPascalCase( field.ForeignKeys[0].PrimaryTable.Alias.Replace( " ", "" ) ) : ColumnToIBatisType( field ) );
string fieldType = ColumnToIBatisType( field );
if( fieldType.EndsWith( "[]" ) )
{%>
<%= fieldName %> = new <%= fieldType %>{}; <%
}
else
{
switch( fieldType )
{
case "string":%>
<%= fieldName %> = null; <%
break;
case "DateTime":%>
<%= fieldName %> = new DateTime(); <%
break;
case "bool":%>
<%= fieldName %> = false; <%
break;
case "decimal":
case "float":
case "short":
case "int":
case "long":%>
<%= fieldName %> = 0; <%
break;
default:%>
<%= fieldName %> = new <%= fieldType %>(); <%
break;
}
}
}%>
}
#endregion // End of Default ( Empty ) Class Constuctor<%
}
private void BuildRequiredConstructor( IColumns Columns )
{
if( CountRequiredFields( Columns ) > 0 && CountNullableFields( Columns ) < Columns.Count )
{
%>#region Required Fields Only Constructor
/// <summary>
/// required (not null) fields only constructor
/// </summary>
public <%= _className %>(<%
bool first = true;
foreach( IColumn col in Columns )
{
if( !col.IsNullable )
{
if( !first ) output.write( ", " );
output.write( ColumnToIBatisType( col ) + " " + ColumnToArgumentName( col ) );
first = false;
}
}
%>)
{<%
foreach( IColumn col in Columns )
{
if( !col.IsNullable )
{
%>
<%= ColumnToMemberVariable( col ) %> = <%= ColumnToArgumentName( col ) %>; <%
}
else
{
switch( ColumnToIBatisType( col ) )
{
default:%>
<%= ColumnToMemberVariable( col ) %> = null; <%
break;
case "string":%>
<%= ColumnToMemberVariable( col ) %> = null; <%
break;
case "DateTime":%>
<%= ColumnToMemberVariable( col ) %> = new DateTime(); <%
break;
case "bool":%>
<%= ColumnToMemberVariable( col ) %> = false; <%
break;
case "decimal":
case "float":
case "short":
case "int":
case "long":%>
<%= ColumnToMemberVariable( col ) %> = 0; <%
break;
}
}
}
%>
}
#endregion // End Required Fields Only Constructor<%
}
}
private void BuildFullConstructor( IColumns Columns )
{
%>#region Full Constructor
/// <summary>
/// full constructor
/// </summary>
public <%= _className %>(<%
bool first = true;
foreach( IColumn field in Columns )
{
if( !first ) output.write( ", " );
//output.write( ( field.IsInForeignKey && !field.IsInPrimaryKey ? ToPascalCase( field.ForeignKeys[0].PrimaryTable.Alias.Replace( " ", "" ) ) : ColumnToIBatisType( field ) ) + " " + ColumnToArgumentName( field ) );
output.write( ColumnToIBatisType( field ) + " " + ColumnToArgumentName( field ) );
first = false;
}%>)
{<%
foreach( IColumn col in Columns )
{
%>
<%= ColumnToMemberVariable( col ) %> = <%= ColumnToArgumentName( col ) %>; <%
}
%>
}
#endregion // End Full Constructor<%
}
private void BuildEqualsHashCodeOverrides( IColumns Columns )
{
%>
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
<%= _className %> castObj = (<%= _className %>)obj; <%
if( CountUniqueFields( Columns ) == 0 )
{%>
return castObj.GetHashCode() == this.GetHashCode()<%
}
else
{%>
return ( castObj != null )<%
foreach( IColumn c in Columns )
{
if( c.IsInPrimaryKey )
{
%> &&
( this.<%= ColumnToMemberVariable( c ) %> == castObj.<%= ColumnToPropertyName( c ) %> )<%
}
}
} %>;
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
<% if( CountUniqueFields( Columns ) == 0 )
{
%>return this.GetType().FullName.GetHashCode();
<%
}
els