/// <summary> /// Builds the Domain model code which includes : /// 1. Entity. /// 2. ActiveRecord /// 3. Service /// 4. Repository /// 5. Settings /// 6. Validator /// </summary> /// <param name="ctx"></param> /// <returns></returns> public virtual BoolMessageItem <ModelContainer> Process(ModelContext ctx) { string codeTemplatePath = ctx.AllModels.Settings.ModelCodeLocationTemplate; this.Context = ctx; Dictionary <string, CodeFile> fileMap = CodeBuilderUtils.GetFiles(ctx, string.Empty, codeTemplatePath); ctx.AllModels.Iterate(m => ctx.CanProcess(m, (model) => model.GenerateCode), currentModel => { var modelChain = ctx.AllModels.InheritanceFor(currentModel.Name); fileMap = CodeFileHelper.GetFilteredDomainModelFiles(currentModel, fileMap); string generatedCodePath = (string)ctx.AllModels.Settings.ModelCodeLocation; generatedCodePath += "/" + currentModel.Name; IDictionary <string, string> subs = new Dictionary <string, string>(); BuildSubstitutions(ctx, currentModel, modelChain, subs); foreach (string fileName in fileMap.Keys) { string templateFile = codeTemplatePath + "/" + fileName; string generated = generatedCodePath + "/" + fileMap[fileName].QualifiedName; CodeFileHelper.Write(templateFile, generated, generatedCodePath, subs); } }); return(new BoolMessageItem <ModelContainer>(ctx.AllModels, true, string.Empty)); }
/// <summary> /// Builds the Domain model code which includes : /// 1. Entity. /// 2. ActiveRecord /// 3. Service /// 4. Repository /// 5. Settings /// 6. Validator /// </summary> /// <param name="ctx"></param> /// <returns></returns> public virtual BoolMessageItem <ModelContainer> Process(ModelContext ctx) { string codeTemplatePath = ctx.AllModels.Settings.ModelCodeLocationTemplate; Dictionary <string, CodeFile> fileMap = CodeBuilderUtils.GetFiles(ctx, string.Empty, codeTemplatePath); foreach (Model currentModel in ctx.AllModels.AllModels) { // Pre condition. if (currentModel.GenerateCode) { // Create the database table for all the models. List <Model> modelChain = ModelUtils.GetModelInheritancePath(ctx, currentModel.Name, true); fileMap = CodeFileHelper.GetFilteredDomainModelFiles(currentModel, fileMap); string generatedCodePath = (string)ctx.AllModels.Settings.ModelCodeLocation; generatedCodePath += "/" + currentModel.Name; IDictionary <string, string> subs = new Dictionary <string, string>(); BuildSubstitutions(ctx, currentModel, modelChain, subs); foreach (string fileName in fileMap.Keys) { string templateFile = codeTemplatePath + "/" + fileName; string generated = generatedCodePath + "/" + fileMap[fileName].QualifiedName; CodeFileHelper.Write(templateFile, generated, generatedCodePath, subs); } } } return(new BoolMessageItem <ModelContainer>(ctx.AllModels, true, string.Empty)); }
/// <summary> /// Creates the models in the database. /// </summary> /// <param name="ctx"></param> /// <returns></returns> public BoolMessageItem <ModelContainer> Process(ModelContext ctx) { var bufferTables = new StringBuilder(); var bufferProcs = new StringBuilder(); var bufferBoth = new StringBuilder(); var bufferDrop = new StringBuilder(); DBSchema schema = new DBSchema(_conn); ctx.AllModels.Iterate(m => ctx.CanProcess(m, (model) => model.GenerateTable), currentModel => { // Create the database table for all the models. List <Model> modelInheritanceChain = ctx.AllModels.InheritanceFor(currentModel.Name); Validate(ctx, currentModel); // Create table schema for model & create in database. DataTable modelTable = ConvertModelChainToTable(currentModel, modelInheritanceChain, ctx); string sqlTableSchema = string.Empty, sqlProcs = string.Empty, sqlModel = string.Empty; string sqlDrop = string.Empty, sqlDropProcs = string.Empty, sqlDropTable = string.Empty; // Build sql for // 1. create table // 2. create procs // 3. create table & procs // 4. drop procs & table sqlDropTable = schema.GetDropTable(currentModel.TableName, true); var sqlDropTableNoGo = schema.GetDropTable(currentModel.TableName, false); sqlTableSchema = BuildTableSchema(ctx, modelInheritanceChain, currentModel, true); var sqlTableSchemaNoGo = BuildTableSchema(ctx, modelInheritanceChain, currentModel, false); List <string> sqlTable = new List <string>() { sqlDropTableNoGo, sqlTableSchemaNoGo }; List <string> procNames = CodeFileHelper.GetProcNames(ctx.AllModels.Settings.ModelDbStoredProcTemplates); BoolMessageItem <List <string> > storedProcSql = CreateStoredProcs(ctx, currentModel, true); BoolMessageItem <List <string> > storedProcSqlNoGo = CreateStoredProcs(ctx, currentModel, false); if (storedProcSql.Success) { storedProcSql.Item.ForEach(proc => sqlProcs += proc + Environment.NewLine); } procNames.ForEach(procName => sqlDropProcs += schema.GetDropProc(currentModel.TableName, procName) + Environment.NewLine); sqlModel = sqlDropTable + Environment.NewLine + sqlTableSchema + Environment.NewLine + sqlProcs; sqlDrop = sqlDropProcs + sqlDropTable + Environment.NewLine; // Create in the database. ExecuteSqlInDb(sqlTable, ctx, currentModel); ExecuteSqlInDb(storedProcSqlNoGo.Item, ctx, currentModel); // Create model install file containing both the table/procs sql. CreateInstallSqlFile(ctx, currentModel, sqlModel); bufferTables.Append(sqlTable + Environment.NewLine); bufferProcs.Append(sqlProcs + Environment.NewLine + Environment.NewLine); bufferBoth.Append(sqlModel + Environment.NewLine); bufferDrop.Append(sqlDrop + Environment.NewLine); }); // Create the files. string installPath = ctx.AllModels.Settings.ModelInstallLocation; Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_tables.sql", bufferTables.ToString())); Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_tables.sql", bufferTables.ToString())); Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_procs.sql", bufferProcs.ToString())); Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_all.sql", bufferBoth.ToString())); Try.CatchLog(() => File.WriteAllText(installPath + "_uninstall_models.sql", bufferDrop.ToString())); return(null); }