namespace QueryAnalyzer
{
public partial class PDM : Form
{
public PDM()
{
InitializeComponent();
}
private void btnPDM_Click(object sender, EventArgs e)
{
bool AddRelations = false;
StringBuilder sb = new StringBuilder();
sb.Append(HTMLConstantsForPDM.HEADER);
int SchemaCounter = 0;
List<string> lstSchema = Database.GetAllSchemas();
Dictionary<string, List<Classes.Table>> hashTable = Database.GetAllTablesNew();
Dictionary<string, List<Classes.Column>> hashColumn = Database.GetAllColumnsforPDM();
Dictionary<string, List<Classes.Relations>> hashRelations = Database.GetAllRelations();
// this Function Merge 4 different Schema to AIL Schema may be their was some error in schema name
SchemaCounter = MergeModulesInAIL(sb, SchemaCounter, lstSchema, hashTable, hashColumn, hashRelations, AddRelations);
// Logic is quite Simple Create Module Definition i-e represented by, Schema, List Entities in it and then all Attributes Entities Wise and then Relations in one Schema
foreach (string Schema in lstSchema)
{
SchemaCounter++;
CreateModuleDefinition(sb, SchemaCounter, Schema);
foreach (Classes.Table table in hashTable[Schema])
{
CreateEntityRow(sb, table);
}
sb.Append(HTMLConstantsForPDM.ENTITIES_AfterModule); // ->
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CreateAttributesModuleDefinition(sb, SchemaCounter, Schema);
foreach (Classes.Table table in hashTable[Schema])
{
CreateEntityDefinition(sb, table);
foreach (Classes.Column column in hashColumn[Schema + table.TableName])
{
CreateAttributeRow(sb, column);
}
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterEntity); // 9
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (AddRelations)
{
if (hashRelations.ContainsKey(Schema))
{
CreateRelationsDefinition(sb, SchemaCounter, Schema);
foreach (Classes.Relations relations in hashRelations[Schema])
{
CreateRelationRow(sb, relations);
}
//sb.Append(HTMLConstantsForPDM.RELATIONS_AfterModule); // 7
}
}
}
sb.Append(HTMLConstantsForPDM.FOOTER);
if (sfd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(sfd.FileName))
{
sw.Write(sb.ToString());
}
}
}
#region MergeModulesInAIL
private static int MergeModulesInAIL(StringBuilder sb, int SchemaCounter, List<string> lstSchema, Dictionary<string, List<Classes.Table>> hashTable, Dictionary<string, List<Classes.Column>> hashColumn, Dictionary<string, List<Classes.Relations>> hashRelations, bool AddRelations)
{
string ModuleAIL = "AIL";
SchemaCounter++;
string establishment = lstSchema[0];
lstSchema.RemoveAt(0);
string firms = lstSchema[0];
lstSchema.RemoveAt(0);
string ngo = lstSchema[0];
lstSchema.RemoveAt(0);
string socities = lstSchema[0];
lstSchema.RemoveAt(0);
// Get All the Tables of AIL_ESTABLISHMENT , AIL_FIRMS, AIL_NGO AND AIL_SOCITIES
List<Classes.Table> TableList = new List<Table>();
TableList.AddRange(hashTable[establishment]);
TableList.AddRange(hashTable[firms]);
TableList.AddRange(hashTable[ngo]);
TableList.AddRange(hashTable[socities]);
// Get All the Relations of AIL_ESTABLISHMENT , AIL_FIRMS, AIL_NGO AND AIL_SOCITIES
List<Classes.Relations> RelationList;
if (AddRelations)
{
RelationList = new List<Relations>();
RelationList.AddRange(hashRelations[establishment]);
RelationList.AddRange(hashRelations[firms]);
RelationList.AddRange(hashRelations[ngo]);
RelationList.AddRange(hashRelations[socities]);
}
// Get All the Columns of AIL_ESTABLISHMENT , AIL_FIRMS, AIL_NGO AND AIL_SOCITIES
List<Classes.Column> ColumnList = new List<Column>();
foreach (Classes.Table table in TableList)
{
ColumnList.AddRange(hashColumn[table.Schema + table.TableName]);
}
CreateModuleDefinition(sb, SchemaCounter, ModuleAIL);
foreach (Classes.Table table in TableList)
{
CreateEntityRow(sb, table);
}
sb.Append(HTMLConstantsForPDM.ENTITIES_AfterModule); // ->
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CreateAttributesModuleDefinition(sb, SchemaCounter, ModuleAIL);
foreach (Classes.Table table in TableList)
{
CreateEntityDefinition(sb, table);
foreach (Classes.Column column in ColumnList)
{
// Generate 5.2 Identified Attributes
if (table.TableName == column.TableName && table.Schema == column.Schema)
{
CreateAttributeRow(sb, column);
}
}
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterEntity); // 9
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (AddRelations)
{
//if (RelationList.Count > 0)
//{
// CreateRelationsDefinition(sb, SchemaCounter, ModuleAIL);
// foreach (Classes.Relations relations in RelationList)
// {
// CreateRelationRow(sb, relations);
// }
// //sb.Append(HTMLConstantsForPDM.RELATIONS_AfterModule); // 7
//}
}
return SchemaCounter;
}
#endregion MergeModulesInAIL
#region Relation Row
private static void CreateRelationRow(StringBuilder sb, Classes.Relations relations)
{
// 5.3 Identified Relations
//sb.Append(HTMLConstantsForPDM.RELATIONS_BeforeEntitiesName); // 4
//sb.Append(relations.EntityRelation); // ->
//sb.Append(HTMLConstantsForPDM.RELATIONS_BeforeRelationName); // 5
//sb.Append(relations.RelationName); // ->
//sb.Append(HTMLConstantsForPDM.RELATIONS_AfterRelationName); // 6
}
#endregion Relation Row
#region Create Attribute Row
private static void CreateAttributeRow(StringBuilder sb, Classes.Column column)
{
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeAttributeName); // 6
sb.Append(column.ColumnName); // ->
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeDataType); // 7
sb.Append(column.DataType); // ->
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeAttributeType); // 8
sb.Append(column.Key); // ->
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterAttributeType); // 9
}
#endregion Attribute Row
#region Attribute Module Definition
private static void CreateAttributesModuleDefinition(StringBuilder sb, int SchemaCounter, string SchemaName)
{
// Attributes
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeNumber); // 1
sb.Append(SchemaCounter); // ->
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterNumber_BeforeModuleName); // 2
sb.Append(SchemaName); // ->
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterNumber_AfterModuleName); // 3
}
#endregion Attribute Module Definition
#region Create Entity Row
private static void CreateEntityRow(StringBuilder sb, Classes.Table table)
{
// Generate 5.1 Identified Entities
sb.Append(HTMLConstantsForPDM.ENTITIES_BeforeEntityName); // 4
sb.Append(table.TableName); // ->
sb.Append(HTMLConstantsForPDM.ENTITIES_AfterEntityName); // 5
//sb.Append(table.EntityNumber); // ->
//sb.Append(HTMLConstantsForPDM.ENTITIES_AfterEntityNumber); // 6
//sb.Append(table.Type); // ->
//sb.Append(HTMLConstantsForPDM.ENTITIES_AfterEntityType); // 7
}
#endregion Create Entity Row
#region Create Relations Definition
private static void CreateRelationsDefinition(StringBuilder sb, int SchemaCounter, string SchemaName)
{
// Relations
//sb.Append(HTMLConstantsForPDM.RELATIONS_BeforeNumber); // 1
//sb.Append(SchemaCounter); // ->
//sb.Append(HTMLConstantsForPDM.RELATIONS_AfterNumber_BeforeModuleName); // 2
//sb.Append(SchemaName); // ->
//sb.Append(HTMLConstantsForPDM.RELATIONS_AfterModuleName); // 3
}
#endregion Create Relations Definition
#region Create Entity Definition
private static void CreateEntityDefinition(StringBuilder sb, Classes.Table table)
{
// Generate 5.2 Identified Attributes
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_BeforeEntityName); // 4
sb.Append(table.TableName); // ->
sb.Append(HTMLConstantsForPDM.ATTRIBUTES_AfterEntityName); // 5
}
#endregion Create Entity Definition
#region Create Module Definition
private static void CreateModuleDefinition(StringBuilder sb, int SchemaCounter, string ModuleName)
{
// Generate Document for each module
// 5. ARMS Module
// Schama
sb.Append(HTMLConstantsForPDM.MODULE_BeforeNumber); // 1
sb.Append(SchemaCounter); // ->
sb.Append(HTMLConstantsForPDM.MODULE_AfterNumber_BeforeModuleName); // 2
sb.Append(ModuleName); // ->
sb.Append(HTMLConstantsForPDM.MODULE_AfterNumber_AfterModuleName_BeforeDescModuleName); // 3
sb.Append(ModuleName); // ->
sb.Append(HTMLConstantsForPDM.MODULE_AfterNumber_AfterModuleName_AfterDescModuleName); // 4
// Entities
sb.Append(HTMLConstantsForPDM.ENTITIES_BeforeNumber); // 1
sb.Append(SchemaCounter); // ->
sb.Append(HTMLConstantsForPDM.ENTITIES_AfterNumber_BeforeModuleName); // 2
sb.Append(ModuleName); // ->
sb.Append(HTMLConstantsForPDM.ENTITIES_AfterModuleName); // 3
}
#endregion Create Module Definition
}
}