using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Harlinn.Oracle.DBTool.Types.Projects;
using Harlinn.Oracle.DBTool.Generators.Utils;
namespace Harlinn.Oracle.DBTool.Generators.Types
{
public class TypeGenerator : GeneratorBase
{
ProjectTabular tabular;
public TypeGenerator(ProjectTabular tabular)
{
this.tabular = tabular;
}
public override string GetFilename()
{
string className = tabular.GetDataTypeClassName();
string directory = GetDirectory();
string result = directory + "\\" + className + ".cs";
return result;
}
public override string GetDirectory()
{
string namespace_ = Project.Current.DataNamespace;
string directory = Project.Current.NamespaceToDirectory(namespace_);
return directory;
}
public void Generate()
{
string baseName = tabular.GetBaseName();
string commonNamespace = Project.Current.CommonNamespace;
string dataNamespace = Project.Current.DataNamespace;
string className = tabular.GetDataTypeClassName();
string abstract_ = "";
if (tabular is ProjectTable)
{
ProjectTable projectTable = (ProjectTable)tabular;
if (projectTable.IsTagTable)
{
abstract_ = "abstract ";
}
}
WriteLine("using System;");
WriteLine("using System.ComponentModel;");
WriteLine("using System.Collections.ObjectModel;");
WriteLine("using System.Runtime.Serialization;");
WriteLine();
WriteLine("using "+dataNamespace+".Utils;");
WriteLine();
WriteLine("namespace " + dataNamespace);
WriteLine("{");
WriteLine(" [DataContract(Namespace = Constants.Namespace)]");
WriteLine(" [Serializable]");
if (tabular is ProjectTable)
{
ProjectTable projectTable = (ProjectTable)tabular;
if (projectTable.IsTagTable)
{
List<ProjectTable> tagValueTables = Project.Current.GetProjectTagTypeList();
foreach (ProjectTable tagValueTable in tagValueTables)
{
string tagClassName = tagValueTable.BaseName.Replace("Value", "") + "ElementData";
WriteLine(" [KnownType(typeof(" + tagClassName + "))]");
}
}
}
WriteLine(" public " + abstract_ + "class " + className + " : ElementBase");
WriteLine(" {");
WriteLine();
List<ProjectTabularField> fields = tabular.GetFieldList();
if (tabular is ProjectTable)
{
ProjectTable projectTable = (ProjectTable)tabular;
if (projectTable.IsTagValue)
{
WriteLine(" private long tag;");
}
}
for(int i = 0; i < fields.Count; i++ )
{
ProjectTabularField field = fields[i];
string typeName = field.PropertyType;
string fieldName = field.PropertyFieldName;
WriteLine(" private " + typeName + " " + fieldName + ";");
}
if (tabular is ProjectTable)
{
ProjectTable projectTable = (ProjectTable)tabular;
ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
if (concurrencyField != null)
{
WriteLine(" private ElementState " + projectTable.ElementStatefieldName + ";");
WriteLine(" private " + className + " concurrencyConflictElement;");
}
}
WriteLine();
WriteLine(" public " + className + "( )");
WriteLine(" {");
WriteLine(" }");
WriteLine();
WriteLine();
string argumentDeclaration = ColumnHelper.GetArgumentDeclarationList(fields);
WriteLine(" public " + className + "( " + argumentDeclaration + " )");
WriteLine(" {");
for (int i = 0; i < fields.Count; i++)
{
ProjectTabularField field = fields[i];
string fieldName = field.PropertyFieldName;
WriteLine(" this." + fieldName + " = " + fieldName + ";");
}
WriteLine(" }");
WriteLine();
if (baseName == "Tag")
{
WriteLine(" public abstract TagType TagType");
WriteLine(" {");
WriteLine(" get;");
WriteLine(" }");
WriteLine();
string argumentDecl = ColumnHelper.GetArgumentDeclarationList(fields);
string arguments = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(fields);
//WriteLine(" public static TagElementData Create( long id, string name, long item, long type, string storage, long optimisticLock, string units, long? enumeration, string description, string comments )");
WriteLine(" public static TagElementData Create( " + argumentDecl + " )");
WriteLine(" {");
WriteLine(" TagElementData result = null;");
WriteLine(" TagType tagType = (TagType)type;");
WriteLine(" switch(tagType)");
WriteLine(" {");
List<ProjectTable> tagValueTables = Project.Current.GetProjectTagTypeList();
foreach (ProjectTable tagValueTable in tagValueTables)
{
string subClassName = tagValueTable.BaseName.Replace("Value", "") + "ElementData";
WriteLine(" case TagType." + tagValueTable.BaseName + ":");
//WriteLine(" result = new " + subClassName + "(id, name, item, type, storage, optimisticLock, units, enumeration, description, comments);");
WriteLine(" result = new " + subClassName + "(" + arguments + ");");
WriteLine(" break;");
}
WriteLine(" }");
WriteLine(" return result;");
WriteLine(" }");
WriteLine();
}
if (tabular is ProjectTable)
{
ProjectTable projectTable = (ProjectTable)tabular;
if (projectTable.IsTagValue)
{
WriteLine(" public " + className + "( long tag," + argumentDeclaration + " )");
WriteLine(" {");
WriteLine(" this.tag = tag;");
for (int i = 0; i < fields.Count; i++)
{
ProjectTabularField field = fields[i];
string fieldName = field.PropertyFieldName;
WriteLine(" this." + fieldName + " = " + fieldName + ";");
}
WriteLine(" }");
WriteLine();
}
}
WriteLine(" public override ElementType ElementType");
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" return ElementType." + tabular.GetBaseName()+";");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine( );
WriteLine(" public override void AssignTo(ElementBase destination)");
WriteLine(" {");
WriteLine(" " + className + " destinationElement = (" + className + ")destination;");
if (tabular is ProjectTable)
{
ProjectTable projectTable = (ProjectTable)tabular;
if (projectTable.IsTagValue)
{
WriteLine(" destinationElement.tag = this.tag;");
}
}
for (int i = 0; i < fields.Count; i++)
{
ProjectTabularField field = fields[i];
string propertyType = field.PropertyType;
string propertyName = field.PropertyName;
string propertyFieldName = field.PropertyFieldName;
if(propertyType == "byte[]")
{
WriteLine(" if(this."+propertyFieldName+" != null)");
WriteLine(" {");
WriteLine(" destinationElement." + propertyFieldName + " = (byte[])this." + propertyFieldName + ".Clone();");
WriteLine(" }");
WriteLine(" else");
WriteLine(" {");
WriteLine(" destinationElement." + propertyFieldName + " = null;");
WriteLine(" }");
}
else
{
WriteLine(" destinationElement." + propertyFieldName + " = this." + propertyFieldName + ";");
}
}
WriteLine(" }");
WriteLine();
WriteLine();
WriteLine(" public override int CompareTo(ElementBase other)");
WriteLine(" {");
WriteLine(" " + className + " otherElement = (" + className + ")other;");
for (int i = 0; i < fields.Count; i++)
{
ProjectTabularField field = fields[i];
string propertyType = field.PropertyType;
string propertyName = field.PropertyName;
string propertyFieldName = field.PropertyFieldName;
if (i == 0)
{
WriteLine(" int result = CompareHelper.Compare( otherElement." + propertyFieldName + " , this." + propertyFieldName + ");");
}
else
{
WriteLine(" result = CompareHelper.Compare( otherElement." + propertyFieldName + " , this." + propertyFieldName + ");");
}
if (i < (fields.Count - 1))
{
WriteLine(" if( result != 0)");
WriteLine(" {");
WriteLine(" return result;");
WriteLine(" }");
}
}
WriteLine(" return result;");
WriteLine(" }");
WriteLine();
WriteLine();
if (tabular is ProjectTable)
{
ProjectTable projectTable = (ProjectTable)tabular;
ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
if (concurrencyField != null)
{
WriteLine(" [DataMember(EmitDefaultValue=false)]");
WriteLine(" public ElementState " + projectTable.ElementStatePropertyName);
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" return " + projectTable.ElementStatefieldName + ";");
WriteLine(" }");
WriteLine(" set");
WriteLine(" {");
WriteLine(" this." + projectTable.ElementStatefieldName + " = value;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine(" [DataMember(EmitDefaultValue=false)]");
WriteLine(" public " + className + " ConcurrencyConflictElement");
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" return concurrencyConflictElement;");
WriteLine(" }");
WriteLine(" set");
WriteLine(" {");
WriteLine(" this.concurrencyConflictElement = value;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
}
if (projectTable.IsTagValue)
{
WriteLine(" [DataMember(EmitDefaultValue=false)]");
WriteLine(" public long Tag");
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" return tag;");
WriteLine(" }");
WriteLine(" set");
WriteLine(" {");
WriteLine(" this.tag = value;");
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 propertyFieldName = field.PropertyFieldName;
if (string.IsNullOrWhiteSpace(field.Comments) == false)
{
WriteLine(" [Description(\"" + field.Comments + "\")]");
}
if (string.IsNullOrWhiteSpace(field.DisplayName) == false)
{
WriteLine(" [DisplayName(\"" + field.DisplayName + "\")]");
}
WriteLine(" [DataMember(EmitDefaultValue=false)]");
WriteLine(" public " + propertyType + " " + propertyName);
WriteLine(" {");
WriteLine(" get");
WriteLine(" {");
WriteLine(" return " + propertyFieldName + ";");
WriteLine(" }");
WriteLine(" set");
WriteLine(" {");
WriteLine(" this." + propertyFieldName + " = value;");
WriteLine(" }");
WriteLine(" }");
WriteLine();
WriteLine();
}
WriteLine(" }");
WriteLine();
if (tabular is ProjectTable)
{
ProjectTable projectTable = (ProjectTable)tabular;
ProjectTabularPrimaryKey primaryKey = projectTable.GetPrimaryKey();
if ((primaryKey != null) && (projectTable.IsTagValue == false))
{
List<ProjectTabularField> primaryKeyFields = projectTable.GetPrimaryKeyFieldList();
if (primaryKeyFields.Count == 1)
{
ProjectTabularField primaryKeyField = primaryKeyFields[0];
WriteLine();
string propertyType = primaryKeyField.PropertyType;
string propertyName = primaryKeyField.PropertyName;
WriteLine(" public class Keyed"+baseName+"Collection : KeyedCollection<"+propertyType+","+className+">");
WriteLine(" {");
WriteLine(" protected override "+propertyType+" GetKeyForItem("+className+" item)");
WriteLine(" {");
WriteLine(" return item." + propertyName + ";");
WriteLine(" }");
WriteLine(" }");
}
}
WriteLine();
}
WriteLine("}");
}
}
public class MyKeyedCollection : KeyedCollection<string,Exception>
{
protected override string GetKeyForItem(Exception item)
{
throw new NotImplementedException();
}
}
}