using System;
using System.Collections.Generic;
using System.ComponentModel;
using Harlinn.Oracle.DBTool.Types.Projects;
using Harlinn.Oracle.DBTool.Generators.Utils;
using Harlinn.Oracle.DBTool.Types.CSharp;
namespace Harlinn.Oracle.DBTool.Generators.Entities
{
public class EntityGenerator : GeneratorBase
{
ProjectTable projectTable;
public EntityGenerator(ProjectTable projectTable)
{
this.projectTable = projectTable;
}
public override string GetFilename()
{
string className = projectTable.GetEntityClassName();
string directory = GetDirectory();
string result = directory + "\\" + className + ".cs";
return result;
}
public override string GetDirectory()
{
string namespace_ = Project.Current.EntityNamespace;
string directory = Project.Current.NamespaceToDirectory(namespace_);
return directory;
}
public static void Generate(Project project)
{
List<ProjectTable> tables = project.GetProjectTableList();
foreach (ProjectTable table in tables)
{
if (table.IsTagValue == false)
{
EntityGenerator entityGenerator = new EntityGenerator(table);
using (entityGenerator)
{
entityGenerator.Generate();
entityGenerator.SaveToFile();
}
}
}
}
public void Generate()
{
ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
string baseName = projectTable.GetBaseName();
string commonNamespace = Project.Current.CommonNamespace;
string dataNamespace = Project.Current.DataNamespace;
string entityNamespace = Project.Current.EntityNamespace;
string className = projectTable.GetEntityClassName();
string dataClassName = projectTable.GetDataTypeClassName();
List<ProjectTabularField> fields = projectTable.GetFieldList();
string abstract_ = "";
if (projectTable.IsTagTable)
{
abstract_ = "abstract ";
}
WriteLine("using System;");
WriteLine("using System.Collections.Generic;");
WriteLine("using System.ComponentModel;");
WriteLine("using System.Windows.Forms;");
WriteLine("using System.Runtime.Serialization;");
WriteLine("using System.Threading;");
WriteLine("using System.Reflection;");
WriteLine();
WriteLine("using " + commonNamespace + ";");
WriteLine("using " + dataNamespace + ";");
WriteLine();
WriteLine("namespace " + entityNamespace );
WriteLine("{");
WriteLine(" [Serializable]");
WriteLine(" public " + abstract_ + "partial class " + className + " : EntityBase");
WriteLine(" {");
WriteLine();
WriteLine(" private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);");
WriteLine();
WriteLine(" private static void LogException(Exception exc, MethodBase method)");
WriteLine(" {");
WriteLine(" " + commonNamespace + ".Logger.LogException(sfLog, exc, method);");
WriteLine(" }");
WriteLine();
WriteLine(" public event On" + baseName + "EntityChangedDelegate OnChangedEvent;");
WriteLine(" public event On" + baseName + "EntityDeletedDelegate OnDeletedEvent;");
WriteLine();
WriteLine(" public " + className + "(EntityContext entityContext)");
WriteLine(" : base(entityContext)");
WriteLine(" {");
WriteLine();
WriteLine(" }");
WriteLine(" public " + className + "(EntityContext entityContext, " + dataClassName + " data)");
WriteLine(" : base(entityContext, data)");
WriteLine(" {");
WriteLine();
WriteLine(" }");
WriteLine();
/*WriteLine(" ~" + className + "( )");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" Disposing(false);");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" }");
WriteLine(" }");
WriteLine();*/
WriteLine(" protected override void Disposing(bool disposing)");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" EntityContext constext = Context;");
WriteLine(" if ((constext != null) && (!IsDisposed))");
WriteLine(" {");
WriteLine(" constext.Unregister(this);");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" }");
WriteLine(" base.Disposing(disposing);");
WriteLine(" }");
WriteLine();
WriteLine();
WriteLine(" private " + dataClassName + " Data");
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" return (" + dataClassName + ")_Data;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine(" protected override ElementBase CloneData(ElementBase element)");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + dataClassName + " result = (" + dataClassName + ")CreateData();");
WriteLine(" if (element != null)");
WriteLine(" {");
WriteLine(" " + dataClassName + " elementData = (" + dataClassName + ")element;");
WriteLine(" elementData.AssignTo(result);");
if (concurrencyField != null)
{
WriteLine(" result." + projectTable.ElementStatePropertyName + " = elementData."+projectTable.ElementStatePropertyName+";");
}
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
if (projectTable.IsTagTable == false)
{
WriteLine(" protected override ElementBase CreateData()");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + dataClassName + " elementData = new " + dataClassName + "();");
if (concurrencyField != null)
{
WriteLine(" elementData." + projectTable.ElementStatePropertyName + " = ElementState.New;");
}
WriteLine(" return elementData;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
}
WriteLine(" protected override ElementBase SaveData()");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + dataClassName + " result = null;");
WriteLine(" " + dataClassName + " data = Data;");
WriteLine(" if (data != null)");
WriteLine(" {");
if (concurrencyField != null)
{
WriteLine(" if (data." + projectTable.ElementStatePropertyName + " == ElementState.New)");
WriteLine(" {");
WriteLine(" result = Context._Insert"+baseName+"(data);");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" result = Context._Update" + baseName + "(data);");
WriteLine(" }");
}
else
{
WriteLine(" if ( data.Id == 0 )");
WriteLine(" {");
WriteLine(" result = Context._Insert" + baseName + "(data);");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" result = Context._Update" + baseName + "(data);");
WriteLine(" }");
}
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
if (projectTable.IsTagTable)
{
WriteLine(" public TagType TagType");
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" if (EnterReadLock())");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + dataClassName + " data = Data;");
WriteLine(" if (data != null)");
WriteLine(" {");
WriteLine(" return data.TagType;");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" return default(TagType);");
WriteLine(" }");
WriteLine(" }");
WriteLine(" finally");
WriteLine(" {");
WriteLine(" ExitReadLock();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" throw new TimeoutException();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine();
}
for (int i = 0; i < fields.Count; i++)
{
ProjectTabularField field = fields[i];
string propertyType = field.PropertyType;
string propertyName = field.PropertyName;
string dataClassPropertyName = propertyName;
string defaultValue = TypeHelper.GetDefaultPropertyValue(field);
if (field.IsReference)
{
propertyName += "Id";
}
else
{
if (string.IsNullOrWhiteSpace(field.Comments) == false)
{
WriteLine(" [Description(\"" + field.Comments + "\")]");
}
if (string.IsNullOrWhiteSpace(field.DisplayName) == false)
{
WriteLine(" [DisplayName(\"" + field.DisplayName + "\")]");
}
}
WriteLine(" public " + propertyType + " " + propertyName);
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" if (EnterReadLock())");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" "+dataClassName+" data = Data;");
WriteLine(" if (data != null)");
WriteLine(" {");
WriteLine(" return data." + dataClassPropertyName + ";");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" return " + defaultValue + ";");
WriteLine(" }");
WriteLine(" }");
WriteLine(" finally");
WriteLine(" {");
WriteLine(" ExitReadLock();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" throw new TimeoutException();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" set");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" if (EnterWriteLock())");
WriteLine(" {");
WriteLine(" bool changed = false;");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + dataClassName + " data = Data;");
WriteLine(" if (data."+dataClassPropertyName+" == value)");
WriteLine(" return;");
WriteLine(" data."+dataClassPropertyName+" = value;");
WriteLine(" changed = true;");
WriteLine(" }");
WriteLine(" finally");
WriteLine(" {");
WriteLine(" ExitWriteLock();");
WriteLine(" }");
WriteLine(" if (changed)");
WriteLine(" {");
WriteLine(" OnPropertyChanged(\"" + propertyName + "\");");
WriteLine(" }");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" throw new TimeoutException();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine();
if (field.IsReference)
{
ProjectTable referencedTable = field.GetReferencedTable();
if (referencedTable != null)
{
ProjectTabularPrimaryKey referencedPrimaryKey = referencedTable.GetPrimaryKey();
List<ProjectTabularField> referencedPrimaryKeyFields = referencedPrimaryKey.GetFieldList();
string referencedMethodName = MethodNamesHelper.GetServiceGetByMethodName(referencedPrimaryKey);
//string argDecl = ColumnHelper.GetArgumentDeclarationList(referencedPrimaryKeyFields);
//string args = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(referencedPrimaryKeyFields);
string referencedEntityClassName = referencedTable.EntityClassName;
string propertyFieldName = field.PropertyFieldName;
propertyName = field.PropertyName;
WriteLine(" private WeakReference " + propertyFieldName + "Reference;");
if (string.IsNullOrWhiteSpace(field.Comments) == false)
{
WriteLine(" [Description(\"" + field.Comments + "\")]");
}
if (string.IsNullOrWhiteSpace(field.DisplayName) == false)
{
WriteLine(" [DisplayName(\"" + field.DisplayName + "\")]");
}
WriteLine(" public " + referencedEntityClassName + " " + propertyName);
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" long id = 0;");
WriteLine(" if (EnterReadLock())");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + dataClassName + " data = Data;");
if (TypeHelper.GetIsCSharpNullableType(field))
{
WriteLine(" if ((data != null)&&(data." + dataClassPropertyName + ".HasValue))");
WriteLine(" {");
WriteLine(" id = data." + dataClassPropertyName + ".Value;");
WriteLine(" if( " + propertyFieldName + "Reference != null )");
WriteLine(" {");
WriteLine(" " + referencedEntityClassName + " " + propertyFieldName + " = " + propertyFieldName + "Reference.Target as " + referencedEntityClassName + ";");
WriteLine(" if( " + propertyFieldName + " != null )");
WriteLine(" {");
WriteLine(" if( " + propertyFieldName + ".Id == id )");
WriteLine(" {");
WriteLine(" return " + propertyFieldName + ";");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
}
else
{
WriteLine(" if (data != null)");
WriteLine(" {");
WriteLine(" id = data." + dataClassPropertyName + ";");
WriteLine(" if( " + propertyFieldName + "Reference != null )");
WriteLine(" {");
WriteLine(" " + referencedEntityClassName + " " + propertyFieldName + " = " + propertyFieldName + "Reference.Target as " + referencedEntityClassName + ";");
WriteLine(" if( " + propertyFieldName + " != null )");
WriteLine(" {");
WriteLine(" if( " + propertyFieldName + ".Id == id )");
WriteLine(" {");
WriteLine(" return " + propertyFieldName + ";");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
}
WriteLine(" }");
WriteLine(" finally");
WriteLine(" {");
WriteLine(" ExitReadLock();");
WriteLine(" }");
WriteLine(" if( id != 0 )");
WriteLine(" {");
WriteLine(" EntityContext context = Context;");
WriteLine(" if( context != null )");
WriteLine(" {");
WriteLine(" " + referencedEntityClassName + " result = context." + referencedMethodName + "( id );");
WriteLine(" if (EnterWriteLock())");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + propertyFieldName + "Reference = new WeakReference( result );");
WriteLine(" }");
WriteLine(" finally");
WriteLine(" {");
WriteLine(" ExitWriteLock();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" throw new TimeoutException();");
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" return null;");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" throw new TimeoutException();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" set");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" if (EnterWriteLock())");
WriteLine(" {");
WriteLine(" bool changed = false;");
WriteLine(" try");
WriteLine(" {");
WriteLine(" long id = 0;");
WriteLine(" if( value != null )");
WriteLine(" {");
WriteLine(" id = value.Id;");
WriteLine(" }");
WriteLine(" " + dataClassName + " data = Data;");
WriteLine(" if (data." + dataClassPropertyName + " == id)");
WriteLine(" return;");
WriteLine(" data." + dataClassPropertyName + " = id;");
WriteLine(" changed = true;");
WriteLine(" }");
WriteLine(" finally");
WriteLine(" {");
WriteLine(" ExitWriteLock();");
WriteLine(" }");
WriteLine(" if (changed)");
WriteLine(" {");
WriteLine(" OnPropertyChanged(\"" + propertyName + "\");");
WriteLine(" }");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" throw new TimeoutException();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine();
}
}
}
WriteLine();
List<ProjectTabularReference> referencesToTable = projectTable.GetReferencesToTable();
foreach (ProjectTabularReference referenceToTable in referencesToTable)
{
ProjectTabular referencingTable = referenceToTable.GetTabular();
string referencingEntityName = referencingTable.GetEntityClassName();
string referencedMethodName = MethodNamesHelper.GetServiceGetByMethodName(referenceToTable);
List<ProjectTabularField> referenceToTableFields = referenceToTable.GetFieldList();
if ((referencingTable is ProjectTable) && (((ProjectTable)referencingTable).IsManyToMany))
{
string prefix = referenceToTableFields[0].PropertyName;
//WriteLine(" public List<" + referencingEntityName + "> " + prefix + referencingTable.GetBaseName() + "s");
WriteLine(" public " + referencingEntityName + "List " + prefix + referencingTable.GetBaseName() + "s");
}
else
{
string prefix = string.Empty;
if (referencingEntityName == "QueueConnectionEntity")
{
prefix = referenceToTableFields[0].PropertyName;
}
string propertyName = prefix + referencingTable.GetBaseName();
if (propertyName.EndsWith("s") == false)
{
propertyName += "s";
}
//WriteLine(" public List<" + referencingEntityName + "> " + referencingTable.GetBaseName() + "s");
WriteLine(" public " + referencingEntityName + "List " + propertyName );
}
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" long id = 0;");
WriteLine(" if (EnterReadLock())");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + dataClassName + " data = Data;");
WriteLine(" if (data != null)");
WriteLine(" {");
WriteLine(" id = data.Id;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" finally");
WriteLine(" {");
WriteLine(" ExitReadLock();");
WriteLine(" }");
WriteLine(" if( id != 0 )");
WriteLine(" {");
WriteLine(" EntityContext context = Context;");
WriteLine(" if( context != null )");
WriteLine(" {");
WriteLine(" " + referencingEntityName + "List result = context." + referencedMethodName + "( id );");
WriteLine(" return result;");
/*WriteLine(" List<" + referencingEntityName + "> elements = context." + referencedMethodName + "( id );");
WriteLine(" if ( elements != null )");
WriteLine(" {");
WriteLine(" " + referencingEntityName + "List result = new " + referencingEntityName + "List( elements );");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" " + referencingEntityName + "List result = new " + referencingEntityName + "List( );");
WriteLine(" return result;");
WriteLine(" }");*/
WriteLine(" }");
WriteLine(" }");
WriteLine(" return null;");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" throw new TimeoutException();");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine();
}
WriteLine();
WriteLine();
WriteLine(" public void HandleChanged(On" + baseName + "ChangedEventArgs eventArgs )");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" if (EnterWriteLock())");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" SetDataOnChangeNotification( eventArgs." + baseName + ");");
WriteLine(" }");
WriteLine(" finally");
WriteLine(" {");
WriteLine(" ExitWriteLock();");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine(" if (OnChangedEvent != null)");
WriteLine(" {");
WriteLine(" On" + baseName + "EntityChangedEventArgs ea = new On" + baseName + "EntityChangedEventArgs(eventArgs.ClientId, this);");
WriteLine(" OnChangedEvent(this, ea);");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod() );");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine();
WriteLine(" public void HandleDeleted(On" + baseName + "DeletedEventArgs eventArgs )");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" if (OnDeletedEvent != null)");
WriteLine(" {");
WriteLine(" On" + baseName + "EntityDeletedEventArgs ea = new On" + baseName + "EntityDeletedEventArgs(eventArgs.ClientId, eventArgs.Id);");
WriteLine(" OnDeletedEvent(this, ea);");
WriteLine(" }");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod() );");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine();
WriteLine(" }");
WriteLine();
WriteLine();
WriteLine();
WriteLine(" public class " + baseName + "EntityCache");
WriteLine(" {");
WriteLine(" private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);");
WriteLine(" private static void LogException(Exception exc, MethodBase method)");
WriteLine(" {");
WriteLine(" Logger.LogException(sfLog, exc, method);");
WriteLine(" }");
WriteLine(" private WeakReference contextReference;");
WriteLine(" private Dictionary<long, WeakReference> elements;");
WriteLine(" public " + baseName + "EntityCache(EntityContext entityContext)");
WriteLine(" {");
WriteLine(" if (entityContext == null)");
WriteLine(" {");
WriteLine(" throw new ArgumentNullException(\"entityContext\");");
WriteLine(" }");
WriteLine(" contextReference = new WeakReference(entityContext);");
WriteLine(" elements = new Dictionary<long, WeakReference>();");
WriteLine(" }");
WriteLine(" public EntityContext Context");
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" EntityContext result = (EntityContext)contextReference.Target;");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" public " + className + " GetEntityFromCache(long id)");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + className + " result = null;");
WriteLine(" WeakReference entityReference = null;");
WriteLine(" if (elements.TryGetValue(id, out entityReference))");
WriteLine(" {");
WriteLine(" result = (" + className + ")entityReference.Target;");
WriteLine(" if (result == null)");
WriteLine(" {");
WriteLine(" elements.Remove(id);");
WriteLine(" }");
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
ProjectTabularPrimaryKey primaryKey = projectTable.GetPrimaryKey();
List<ProjectTabularField> primaryKeyFields = primaryKey.GetFieldList();
string methodName = MethodNamesHelper.GetServiceGetByMethodName(primaryKey);
string argDecl = ColumnHelper.GetArgumentDeclarationList(primaryKeyFields);
string args = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(primaryKeyFields);
if (projectTable.IsTagTable == false)
{
WriteLine(" public " + className + " GetEntity(" + argDecl + ")");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + className + " result = GetEntityFromCache(" + args + ");");
WriteLine(" if (result == null)");
WriteLine(" {");
WriteLine(" EntityContext context = Context;");
WriteLine(" if (context != null)");
WriteLine(" {");
WriteLine(" " + dataClassName + " data = context._" + methodName + "(" + args + ");");
WriteLine(" if (data != null)");
WriteLine(" {");
WriteLine(" " + className + " entity = new " + className + "(context, data);");
WriteLine(" elements.Add(id, new WeakReference(entity));");
WriteLine(" result = entity;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
args = ColumnHelper.GetCommaSeparatedPropertyNameList("data", primaryKeyFields);
WriteLine(" public " + className + " GetEntity(" + dataClassName + " data )");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + className + " result = GetEntityFromCache(" + args + ");");
WriteLine(" if (result == null)");
WriteLine(" {");
WriteLine(" EntityContext context = Context;");
WriteLine(" if (context != null)");
WriteLine(" {");
WriteLine(" " + className + " entity = new " + className + "(context, data);");
WriteLine(" elements.Add(" + args + ", new WeakReference(entity));");
WriteLine(" result = entity;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
}
else
{
GenerateGetCachedTagMethods();
}
WriteLine(" public bool RemoveEntity(long id)");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" return elements.Remove(id);");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine();
WriteLine(" }");
GenerateBindingList();
WriteLine("}");
WriteLine();
}
public void GenerateGetCachedTagMethods()
{
List<ProjectTable> tagValueTables = Project.Current.GetProjectTagTypeList();
foreach (ProjectTable tagValueTable in tagValueTables)
{
GenerateGetCachedTagMethod(tagValueTable);
}
string className = projectTable.GetEntityClassName();
string dataBaseClassName = projectTable.GetDataTypeClassName();
ProjectTabularPrimaryKey primaryKey = projectTable.GetPrimaryKey();
List<ProjectTabularField> primaryKeyFields = primaryKey.GetFieldList();
string args = ColumnHelper.GetCommaSeparatedPropertyNameList("data", primaryKeyFields);
WriteLine(" public " + className + " GetEntity(" + dataBaseClassName + " data )");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + className + " result = GetEntityFromCache(" + args + ");");
WriteLine(" if (result == null)");
WriteLine(" {");
WriteLine(" EntityContext context = Context;");
WriteLine(" if (context != null)");
WriteLine(" {");
WriteLine(" switch(data.Type)");
WriteLine(" {");
for (int i = 0; i < tagValueTables.Count; i++)
{
ProjectTable tagValueTable = tagValueTables[i];
string entityClassName = tagValueTable.BaseName.Replace("Value", "") + "Entity";
string dataClassName = tagValueTable.BaseName.Replace("Value", "") + "ElementData";
WriteLine(" case " + tagValueTable.ElementTypeId + ":");
WriteLine(" {");
WriteLine(" " + entityClassName + " entity = new " + entityClassName + "(context, (" + dataClassName + ")data);");
WriteLine(" elements.Add(" + args + ", new WeakReference(entity));");
WriteLine(" result = entity;");
WriteLine(" }");
WriteLine(" break;");
}
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine(" public " + className + " GetEntity( long id )");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + className + " result = GetEntityFromCache( id );");
WriteLine(" if (result == null)");
WriteLine(" {");
WriteLine(" EntityContext context = Context;");
WriteLine(" if (context != null)");
WriteLine(" {");
WriteLine(" TagElementData data = context._GetTagById(id);");
WriteLine(" if( data != null )");
WriteLine(" {");
WriteLine(" switch(data.Type)");
WriteLine(" {");
for (int i = 0; i < tagValueTables.Count; i++)
{
ProjectTable tagValueTable = tagValueTables[i];
string entityClassName = tagValueTable.BaseName.Replace("Value", "") + "Entity";
string dataClassName = tagValueTable.BaseName.Replace("Value", "") + "ElementData";
WriteLine(" case " + tagValueTable.ElementTypeId + ":");
WriteLine(" {");
WriteLine(" " + entityClassName + " entity = new " + entityClassName + "(context, (" + dataClassName + ")data);");
WriteLine(" elements.Add(" + args + ", new WeakReference(entity));");
WriteLine(" result = entity;");
WriteLine(" }");
WriteLine(" break;");
}
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
}
private void GenerateGetCachedTagMethod(ProjectTable tagValueTable)
{
ProjectTabularPrimaryKey primaryKey = projectTable.GetPrimaryKey();
List<ProjectTabularField> primaryKeyFields = primaryKey.GetFieldList();
string baseName = tagValueTable.BaseName.Replace("Value", "");
string entityClassName = tagValueTable.BaseName.Replace("Value", "") + "Entity";
string dataClassName = tagValueTable.BaseName.Replace("Value", "") + "ElementData";
string args = ColumnHelper.GetCommaSeparatedPropertyNameList("data", primaryKeyFields);
WriteLine(" public " + entityClassName + " Get" + baseName + "Entity(" + dataClassName + " data )");
WriteLine(" {");
WriteLine(" try");
WriteLine(" {");
WriteLine(" " + entityClassName + " result = (" + entityClassName + ")GetEntityFromCache(" + args + ");");
WriteLine(" if (result == null)");
WriteLine(" {");
WriteLine(" EntityContext context = Context;");
WriteLine(" if (context != null)");
WriteLine(" {");
WriteLine(" " + entityClassName + " entity = new " + entityClassName + "(context, data);");
WriteLine(" elements.Add(" + args + ", new WeakReference(entity));");
WriteLine(" result = entity;");
WriteLine(" }");
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" catch (Exception exc)");
WriteLine(" {");
WriteLine(" LogException(exc, MethodBase.GetCurrentMethod());");
WriteLine(" throw;");
WriteLine(" }");
WriteLine(" }");
}
private void GenerateBindingList()
{
if (projectTable.IsTagValue == false)
{
string commonNamespace = Project.Current.CommonNamespace;
string baseName = projectTable.GetBaseName();
string entityClassName = projectTable.GetEntityClassName();
WriteLine();
WriteLine(" [Serializable]");
WriteLine(" public class " + baseName + "EntityList : BindingList<" + entityClassName + ">, ITypedList");
WriteLine(" {");
WriteLine(" private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);");
WriteLine();
WriteLine(" private static void LogException(Exception exc, MethodBase method)");
WriteLine(" {");
WriteLine(" " + commonNamespace + ".Logger.LogException(sfLog, exc, method);");
WriteLine(" }");
WriteLine();
WriteLine(" public " + baseName + "EntityList( )");
WriteLine(" {");
WriteLine(" }");
WriteLine();
WriteLine(" public " + baseName + "EntityList( IList<" + entityClassName + "> entities )");
WriteLine(" : base( entities )");
WriteLine(" {");
WriteLine(" }");
WriteLine();
WriteLine();
WriteLine(" [NonSerialized()]");
WriteLine(" private PropertyDescriptorCollection propertyDescriptorCollection;");
WriteLine(" public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)");
WriteLine(" {");
WriteLine(" if (listAccessors != null && listAccessors.Length > 0)");
WriteLine(" {");
WriteLine(" PropertyDescriptorCollection result = ListBindingHelper.GetListItemProperties(listAccessors[0].PropertyType);");
WriteLine(" return result;");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" if (propertyDescriptorCollection == null)");
WriteLine(" {");
WriteLine(" propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(" + entityClassName + "), new Attribute[] { new BrowsableAttribute(true) });");
WriteLine(" }");
WriteLine(" return propertyDescriptorCollection;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine(" public string GetListName(PropertyDescriptor[] listAccessors)");
WriteLine(" {");
WriteLine(" string result = typeof(" + entityClassName + ").Name;");
WriteLine(" return result;");
WriteLine(" }");
WriteLine();
WriteLine();
WriteLine(" }");
WriteLine();
WriteLine();
}
}
}
}