public EntityCodeGenerator(EntityModel model, GeneratorConfig config, string projectJson) { var kdiff3Paths = new[] { config.KDiff3Path }; this.model = model; CodeFileHelper.Kdiff3Path = kdiff3Paths.FirstOrDefault(File.Exists); CodeFileHelper.TSCPath = config.TSCPath ?? "tsc"; this.rootDir = Path.GetDirectoryName(projectJson); this.config = config; this.serverTypings = Path.Combine(rootDir, "Modules/Common/Imports/ServerTypings/".Replace('/', Path.DirectorySeparatorChar)); if (!Directory.Exists(serverTypings)) this.serverTypings = Path.Combine(rootDir, "Imports/ServerTypings/".Replace('/', Path.DirectorySeparatorChar)); }
public EntityCodeGenerator(EntityModel model, GeneratorConfig config, string csproj) { var kdiff3Paths = new[] { config.KDiff3Path }; this.model = model; CodeFileHelper.Kdiff3Path = kdiff3Paths.FirstOrDefault(File.Exists); CodeFileHelper.TSCPath = config.TSCPath ?? "tsc"; this.rootDir = Path.GetDirectoryName(csproj); this.config = config; this.serverTypings = Path.Combine(rootDir, "Modules/Common/Imports/ServerTypings/".Replace('/', Path.DirectorySeparatorChar)); if (!Directory.Exists(serverTypings)) { this.serverTypings = Path.Combine(rootDir, "Imports/ServerTypings/".Replace('/', Path.DirectorySeparatorChar)); } }
public EntityCodeGenerator(EntityModel model, GeneratorConfig config) { var kdiff3Paths = new[] { config.KDiff3Path, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "KDiff3\\kdiff3.exe"), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "KDiff3\\kdiff3.exe"), }; this.model = model; CodeFileHelper.Kdiff3Path = kdiff3Paths.FirstOrDefault(File.Exists); if (config.TFSIntegration) CodeFileHelper.SetupTFSIntegration(config.TFPath); CodeFileHelper.SetupTSCPath(config.TSCPath); siteWebProj = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.WebProjectFile)); siteWebPath = Path.GetDirectoryName(siteWebProj); this.serverTypings = Path.Combine(siteWebPath, "Modules/Common/Imports/ServerTypings/".Replace('/', Path.DirectorySeparatorChar)); if (!Directory.Exists(serverTypings)) this.serverTypings = Path.Combine(siteWebPath, "Imports/ServerTypings/".Replace('/', Path.DirectorySeparatorChar)); if (!string.IsNullOrEmpty(config.ScriptProjectFile)) { scriptProject = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.ScriptProjectFile)); scriptPath = Path.GetDirectoryName(scriptProject); if (!File.Exists(scriptProject)) { scriptProject = null; scriptPath = null; } } this.config = config; }
public static EntityModel GenerateModel(IDbConnection connection, string tableSchema, string table, string module, string connectionKey, string entityClass, string permission, GeneratorConfig config) { var model = new EntityModel(); model.Module = module; if (connection.GetDialect().ServerType.StartsWith("MySql", StringComparison.OrdinalIgnoreCase)) { model.Schema = null; } else { model.Schema = tableSchema; } model.Permission = permission; model.ConnectionKey = connectionKey; model.RootNamespace = config.RootNamespace; var className = entityClass ?? ClassNameFromTableName(table); model.ClassName = className; model.RowClassName = className + "Row"; model.Title = Inflector.Inflector.Titleize(className); model.Tablename = table; model.Fields = new List <EntityField>(); model.Joins = new List <EntityJoin>(); model.Instance = true; var schemaProvider = SchemaHelper.GetSchemaProvider(connection.GetDialect().ServerType); var fields = schemaProvider.GetFieldInfos(connection, tableSchema, table).ToList(); if (!fields.Any(x => x.IsPrimaryKey)) { var primaryKeys = new HashSet <string>(schemaProvider.GetPrimaryKeyFields(connection, tableSchema, table)); foreach (var field in fields) { field.IsPrimaryKey = primaryKeys.Contains(field.FieldName); } } if (!fields.Any(x => x.IsIdentity)) { var identities = new HashSet <string>(schemaProvider.GetIdentityFields(connection, tableSchema, table)); foreach (var field in fields) { field.IsIdentity = identities.Contains(field.FieldName); } } var foreigns = schemaProvider.GetForeignKeys(connection, tableSchema, table) .ToLookup(x => x.FKName) .Where(x => x.Count() == 1) .SelectMany(x => x) .ToList(); foreach (var field in fields) { var fk = foreigns.FirstOrDefault(x => x.FKColumn == field.FieldName); if (fk != null) { field.PKSchema = fk.PKSchema; field.PKTable = fk.PKTable; field.PKColumn = fk.PKColumn; } } var prefix = DeterminePrefixLength(fields, x => x.FieldName); model.FieldPrefix = fields.First().FieldName.Substring(0, prefix); var identity = fields.FirstOrDefault(f => f.IsIdentity == true); if (identity == null) { identity = fields.FirstOrDefault(f => f.IsPrimaryKey == true); } if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } else { identity = fields.FirstOrDefault(f => f.IsPrimaryKey == true) ?? fields.FirstOrDefault(); if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } } string baseRowMatch = null; HashSet <string> baseRowFieldset = null; List <string> baseRowFieldList = new List <string>(); foreach (var k in config.BaseRowClasses ?? new List <GeneratorConfig.BaseRowClass>()) { var b = k.ClassName; var f = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var fl = new List <string>(); bool skip = false; foreach (var s in k.Fields ?? new List <string>()) { string n = s.TrimToNull(); if (n == null || !fields.Any(z => z.FieldName.Substring(prefix) == n)) { skip = true; break; } f.Add(n); fl.Add(n); } if (skip) { continue; } if (baseRowFieldset == null || f.Count > baseRowFieldset.Count) { baseRowFieldset = f; baseRowFieldList = fl; baseRowMatch = b; } } var removeForeignFields = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var s in config.RemoveForeignFields ?? new List <string>()) { string n = s.TrimToNull(); if (n != null) { removeForeignFields.Add(n); } } if (baseRowFieldset != null && baseRowFieldset.Count > 0) { model.RowBaseClass = baseRowMatch; model.FieldsBaseClass = baseRowMatch + "Fields"; model.RowBaseFields = new List <EntityField>(); fields = fields.Where(f => { if (baseRowFieldset.Contains(f.FieldName.Substring(prefix))) { var ef = ToEntityField(f, prefix); ef.Flags = null; model.RowBaseFields.Add(ef); return(false); } return(true); }).ToList(); } else { model.RowBaseClass = "Row"; model.RowBaseFields = new List <EntityField>(); model.FieldsBaseClass = "RowFieldsBase"; } var fieldByIdent = new Dictionary <string, EntityField>(StringComparer.OrdinalIgnoreCase); foreach (var field in fields) { var f = ToEntityField(field, prefix); if (f.Ident == model.IdField) { f.ColAttributes = "EditLink, DisplayName(\"Db.Shared.RecordId\"), AlignRight"; } int i = 0; string ident = f.Ident; while (fieldByIdent.ContainsKey(ident)) { ident = f.Ident + ++i; } f.Ident = ident; fieldByIdent[ident] = f; if (f.Name == className && f.FieldType == "String") { model.NameField = f.Name; f.ColAttributes = f.ColAttributes ?? "EditLink"; } var foreign = foreigns.Find((k) => k.FKColumn.Equals(field.FieldName, StringComparison.OrdinalIgnoreCase)); if (foreign != null) { if (f.Title.EndsWith(" Id") && f.Title.Length > 3) { f.Title = f.Title.SafeSubstring(0, f.Title.Length - 3); } f.PKSchema = foreign.PKSchema; f.PKTable = foreign.PKTable; f.PKColumn = foreign.PKColumn; var frgfld = schemaProvider.GetFieldInfos(connection, foreign.PKSchema, foreign.PKTable).ToList(); int frgPrefix = RowGenerator.DeterminePrefixLength(frgfld, z => z.FieldName); var j = new EntityJoin(); j.Fields = new List <EntityField>(); j.Name = GenerateVariableName(f.Name.Substring(prefix)); if (j.Name.EndsWith("Id") || j.Name.EndsWith("ID")) { j.Name = j.Name.Substring(0, j.Name.Length - 2); } f.ForeignJoinAlias = j.Name; j.SourceField = f.Ident; frgfld = frgfld.Where(y => !removeForeignFields.Contains(y.FieldName)).ToList(); foreach (var frg in frgfld) { if (frg.FieldName.Equals(foreign.PKColumn, StringComparison.OrdinalIgnoreCase)) { continue; } var k = ToEntityField(frg, frgPrefix); k.Flags = null; k.Title = Inflector.Inflector.Titleize(JU(j.Name, frg.FieldName.Substring(frgPrefix))); k.Ident = JI(j.Name, k.Ident); i = 0; ident = k.Ident; while (fieldByIdent.ContainsKey(ident)) { ident = k.Ident + ++i; } k.Ident = ident; fieldByIdent[ident] = k; var atk = new List <string>(); atk.Add("DisplayName(\"" + k.Title + "\")"); k.Expression = "j" + j.Name + ".[" + k.Name + "]"; atk.Add("Expression(\"" + k.Expression + "\")"); k.Attributes = String.Join(", ", atk); if (f.TextualField == null && k.FieldType == "String") { f.TextualField = k.Ident; } j.Fields.Add(k); } model.Joins.Add(j); } model.Fields.Add(f); } if (model.NameField == null) { var fld = model.Fields.FirstOrDefault(z => z.FieldType == "String"); if (fld != null) { model.NameField = fld.Ident; fld.ColAttributes = fld.ColAttributes ?? "EditLink"; } } foreach (var x in model.Fields) { var attrs = new List <string>(); attrs.Add("DisplayName(\"" + x.Title + "\")"); if (x.Ident != x.Name) { attrs.Add("Column(\"" + x.Name + "\")"); } if ((x.Size ?? 0) > 0) { attrs.Add("Size(" + x.Size + ")"); } if (x.Scale > 0) { attrs.Add("Scale(" + x.Scale + ")"); } if (!String.IsNullOrEmpty(x.Flags)) { attrs.Add(x.Flags); } if (!String.IsNullOrEmpty(x.PKTable)) { attrs.Add("ForeignKey(\"" + (string.IsNullOrEmpty(x.PKSchema) ? x.PKTable : ("[" + x.PKSchema + "].[" + x.PKTable + "]")) + "\", \"" + x.PKColumn + "\")"); attrs.Add("LeftJoin(\"j" + x.ForeignJoinAlias + "\")"); } if (model.NameField == x.Ident) { attrs.Add("QuickSearch"); } if (x.TextualField != null) { attrs.Add("TextualField(\"" + x.TextualField + "\")"); } x.Attributes = String.Join(", ", attrs.ToArray()); } return(model); }
private static DialogAttributes processAdvancedTips_Dialog(Serenity.CodeGenerator.EntityField x, ref EntityModel m) { //DialogAttributes m.DialogAttributes = new DialogAttributes(); //m.DialogAttributes.Attrs01 = new List<string>(); //m.DialogAttributes.Attrs02 = new List<string>(); //m.DialogAttributes.AttrsConstructor = new List<string>(); //m.DialogAttributes.AttrsValidacao = new List<string>(); //m.DialogAttributes.AttrsConfirmSave = new List<string>(); #region // *** DIALOG PARA CONFIRMAR SE VAI SALVAR OU NÃO *** //m.DialogAttributes.AttrsConfirmSave.Add(" // *** DIALOG PARA CONFIRMAR SE VAI SALVAR OU NÃO ***"); //m.DialogAttributes.AttrsConfirmSave.Add(" " + (m.RootNamespace) + "DialogUtils.pendingChangesConfirmation(this.element, () => this.getSaveState() != this.loadedState);"); //m.DialogAttributes.AttrsConfirmSave.Add(" }"); //m.DialogAttributes.AttrsConfirmSave.Add(""); //m.DialogAttributes.AttrsConfirmSave.Add("//INICIO - PROCESSA O CONFIRM SAVE"); //m.DialogAttributes.AttrsConfirmSave.Add(" getSaveState() {"); //m.DialogAttributes.AttrsConfirmSave.Add(" try { return $.toJSON(this.getSaveEntity()); }"); //m.DialogAttributes.AttrsConfirmSave.Add(" catch (e) { return null; }"); //m.DialogAttributes.AttrsConfirmSave.Add(" }"); //m.DialogAttributes.AttrsConfirmSave.Add(""); //m.DialogAttributes.AttrsConfirmSave.Add(" loadResponse(data) {"); //m.DialogAttributes.AttrsConfirmSave.Add(" super.loadResponse(data);"); //m.DialogAttributes.AttrsConfirmSave.Add(" this.loadedState = this.getSaveState();"); //m.DialogAttributes.AttrsConfirmSave.Add(" }"); //m.DialogAttributes.AttrsConfirmSave.Add("//FIM - PROCESSA O CONFIRM SAVE"); #endregion #region MyRegion if ((x.DataType == "DateTime") && ((x.Ident.ToUpper().Contains("ATUALIZACAO")) || (x.Ident.ToUpper().Contains("UPDATE")))) { m.DialogAttributes.Attrs01.Add("this.form." + x.Ident + ".valueAsDate = new Date();"); } if ((x.DataType == "Boolean") && ((x.Ident.ToUpper().Contains("ATIVO")))) { m.DialogAttributes.Attrs02.Add("//if (this.form." + x.Ident + ".value == true)"); m.DialogAttributes.Attrs02.Add("//{"); m.DialogAttributes.Attrs02.Add("// this.form.ALGUMCAMPO.getGridField().toggle(true);"); m.DialogAttributes.Attrs02.Add("//}"); } if ((x.DataType == "Boolean") && ((x.Ident.ToUpper().Contains("ATIVO")))) { m.DialogAttributes.AttrsConstructor.Add("//// *** INICIO - CHECKBOX CHANGE - " + x.Ident + " ***"); m.DialogAttributes.AttrsConstructor.Add("//this.form." + x.Ident + ".change(e => {"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// if (this.form." + x.Ident + ".value == true) {"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// var isChecked = false;"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// Q.confirm(\"Confirma a seleção ?\", "); m.DialogAttributes.AttrsConstructor.Add("// () => {"); m.DialogAttributes.AttrsConstructor.Add("// isChecked = true;"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// //var texto = this.form.ALGUMCAMPO.getGridField().find('.caption').prop('outerHTML').split('Nome').join('TEXTO NOVO');"); m.DialogAttributes.AttrsConstructor.Add("// //this.form.ALGUMCAMPO.getGridField().find('.caption').prop('outerHTML', texto);"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// this.form." + x.Ident + ".value = isChecked;"); m.DialogAttributes.AttrsConstructor.Add("// this.form.ALGUMCAMPO.getGridField().toggle(true);"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// this.form.ALGUMCAMPO.value = null;"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// }"); m.DialogAttributes.AttrsConstructor.Add("// );"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// this.form." + x.Ident + ".value = isChecked;"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// }"); m.DialogAttributes.AttrsConstructor.Add("// else {"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// var isChecked = true;"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// Q.confirm(\"Confirma a exclusão ?\\nOs dados existentes serão descartados.\", "); m.DialogAttributes.AttrsConstructor.Add("// () => {"); m.DialogAttributes.AttrsConstructor.Add("// isChecked = false;"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// this.form." + x.Ident + ".value = isChecked;"); m.DialogAttributes.AttrsConstructor.Add("// this.form.ALGUMCAMPO.getGridField().toggle(false);"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// }"); m.DialogAttributes.AttrsConstructor.Add("// );"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// this.form." + x.Ident + ".value = isChecked;"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add("// }"); m.DialogAttributes.AttrsConstructor.Add("//});"); m.DialogAttributes.AttrsConstructor.Add("//// *** FIM - CHECKBOX CHANGE - NotaFiscalTerceiro ***"); m.DialogAttributes.AttrsConstructor.Add("//"); m.DialogAttributes.AttrsConstructor.Add(""); m.DialogAttributes.AttrsConstructor.Add(""); } if ((x.DataType == "String") && ((x.Ident.ToUpper().Contains("CNPJ")))) { m.DialogAttributes.AttrsValidacao.Add(m.RootNamespace + ".addValidationRule_CNPJ(this.form." + x.Ident + ");"); } if ((x.DataType == "String") && ((x.Ident.ToUpper().Contains("CPF")))) { m.DialogAttributes.AttrsValidacao.Add(m.RootNamespace + ".addValidationRule_CPF(this.form." + x.Ident + ");"); } #endregion return(m.DialogAttributes); }
public static EntityModel GenerateModel(IDbConnection connection, string tableSchema, string table, string module, string connectionKey, string entityClass, string permission, GeneratorConfig config) { var model = new EntityModel(); model.Module = module; if (connection.GetDialect().ServerType.StartsWith("MySql", StringComparison.OrdinalIgnoreCase)) { model.Schema = null; } else { model.Schema = tableSchema; } model.Permission = permission; model.ConnectionKey = connectionKey; model.RootNamespace = config.RootNamespace; var className = entityClass ?? ClassNameFromTableName(table); model.ClassName = className; model.RowClassName = className + "Row"; model.Title = Inflector.Inflector.Titleize(className); model.Tablename = table; model.Fields = new List <EntityField>(); model.Joins = new List <EntityJoin>(); model.Instance = true; model.DialogAttributes = new DialogAttributes(); /*ROLEMBERG FILHO*/ model.DialogAttributes.Attrs01 = new List <string>(); /*ROLEMBERG FILHO*/ model.DialogAttributes.Attrs02 = new List <string>(); /*ROLEMBERG FILHO*/ model.DialogAttributes.Attrs03 = new List <string>(); /*ROLEMBERG FILHO*/ model.DialogAttributes.AttrsConstructor = new List <string>(); /*ROLEMBERG FILHO*/ model.DialogAttributes.AttrsValidacao = new List <string>(); /*ROLEMBERG FILHO*/ //model.DialogAttributes.AttrsConfirmSave = new List<string>(); /*ROLEMBERG FILHO*/ //model.DialogAttributes.Attrs03 = processAdvancedTips_Model(ref model); /*ROLEMBERG FILHO*/ processAdvancedTips_Model(ref model); /*ROLEMBERG FILHO*/ var schemaProvider = SchemaHelper.GetSchemaProvider(connection.GetDialect().ServerType); var fields = schemaProvider.GetFieldInfos(connection, tableSchema, table).ToList(); if (!fields.Any(x => x.IsPrimaryKey)) { var primaryKeys = new HashSet <string>(schemaProvider.GetPrimaryKeyFields(connection, tableSchema, table)); foreach (var field in fields) { field.IsPrimaryKey = primaryKeys.Contains(field.FieldName); } } if (!fields.Any(x => x.IsIdentity)) { var identities = new HashSet <string>(schemaProvider.GetIdentityFields(connection, tableSchema, table)); foreach (var field in fields) { field.IsIdentity = identities.Contains(field.FieldName); } } var foreigns = schemaProvider.GetForeignKeys(connection, tableSchema, table) .ToLookup(x => x.FKName) .Where(x => x.Count() == 1) .SelectMany(x => x) .ToList(); foreach (var field in fields) { var fk = foreigns.FirstOrDefault(x => x.FKColumn == field.FieldName); if (fk != null) { field.PKSchema = fk.PKSchema; field.PKTable = fk.PKTable; field.PKColumn = fk.PKColumn; } } var prefix = DeterminePrefixLength(fields, x => x.FieldName); model.FieldPrefix = fields.First().FieldName.Substring(0, prefix); var identity = fields.FirstOrDefault(f => f.IsIdentity == true); if (identity == null) { identity = fields.FirstOrDefault(f => f.IsPrimaryKey == true); } if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } else { identity = fields.FirstOrDefault(f => f.IsPrimaryKey == true) ?? fields.FirstOrDefault(); if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } } string baseRowMatch = null; HashSet <string> baseRowFieldset = null; List <string> baseRowFieldList = new List <string>(); foreach (var k in config.BaseRowClasses ?? new List <GeneratorConfig.BaseRowClass>()) { var b = k.ClassName; var f = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var fl = new List <string>(); bool skip = false; foreach (var s in k.Fields ?? new List <string>()) { string n = s.TrimToNull(); if (n == null || !fields.Any(z => z.FieldName.Substring(prefix) == n)) { skip = true; break; } f.Add(n); fl.Add(n); } if (skip) { continue; } if (baseRowFieldset == null || f.Count > baseRowFieldset.Count) { baseRowFieldset = f; baseRowFieldList = fl; baseRowMatch = b; } } var removeForeignFields = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var s in config.RemoveForeignFields ?? new List <string>()) { string n = s.TrimToNull(); if (n != null) { removeForeignFields.Add(n); } } if (baseRowFieldset != null && baseRowFieldset.Count > 0) { model.RowBaseClass = baseRowMatch; model.FieldsBaseClass = baseRowMatch + "Fields"; model.RowBaseFields = new List <EntityField>(); fields = fields.Where(f => { if (baseRowFieldset.Contains(f.FieldName.Substring(prefix))) { var ef = ToEntityField(f, prefix, config); ef.Flags = null; model.RowBaseFields.Add(ef); return(false); } return(true); }).ToList(); } else { model.RowBaseClass = "Row"; model.RowBaseFields = new List <EntityField>(); model.FieldsBaseClass = "RowFieldsBase"; } var fieldByIdent = new Dictionary <string, EntityField>(StringComparer.OrdinalIgnoreCase); foreach (var field in fields) { var f = ToEntityField(field, prefix, config); if (f.Ident == model.IdField) { f.ColAttributes = "EditLink, DisplayName(\"Db.Shared.RecordId\"), AlignRight"; } int i = 0; string ident = f.Ident; while (fieldByIdent.ContainsKey(ident)) { ident = f.Ident + ++i; } f.Ident = ident; fieldByIdent[ident] = f; if (f.Name == className && f.FieldType == "String") { model.NameField = f.Name; f.ColAttributes = f.ColAttributes ?? "EditLink"; } //ROLEMBERG FILHO - processa advanced tips para COLUNAS f.ColAttributes += processAdvancedTips_Columns(f); //ROLEMBERG FILHO - processa advanced tips para COLUNAS var foreign = foreigns.Find((k) => k.FKColumn.Equals(field.FieldName, StringComparison.OrdinalIgnoreCase)); if (foreign != null) { if (f.Title.EndsWith(" Id") && f.Title.Length > 3) { f.Title = f.Title.SafeSubstring(0, f.Title.Length - 3); } f.PKSchema = foreign.PKSchema; f.PKTable = foreign.PKTable; f.PKColumn = foreign.PKColumn; var frgfld = schemaProvider.GetFieldInfos(connection, foreign.PKSchema, foreign.PKTable).ToList(); int frgPrefix = RowGenerator.DeterminePrefixLength(frgfld, z => z.FieldName); var j = new EntityJoin(); j.Fields = new List <EntityField>(); j.Name = GenerateVariableName(f.Name.Substring(prefix)); if (j.Name.EndsWith("Id") || j.Name.EndsWith("ID")) { j.Name = j.Name.Substring(0, j.Name.Length - 2); } f.ForeignJoinAlias = j.Name; j.SourceField = f.Ident; frgfld = frgfld.Where(y => !removeForeignFields.Contains(y.FieldName)).ToList(); foreach (var frg in frgfld) { if (frg.FieldName.Equals(foreign.PKColumn, StringComparison.OrdinalIgnoreCase)) { continue; } var k = ToEntityField(frg, frgPrefix, config); k.Flags = null; //ROLEMBERG FILHO - trata o Tilte de acordo com as regras de AcertaPalavras //k.Title = Inflector.Inflector.Titleize(JU(j.Name, frg.FieldName.Substring(frgPrefix))); k.Title = config.ReplaceStringinDisplayName ? ToolsHeper.AcertaPalavra(Inflector.Inflector.Titleize(JU(j.Name, frg.FieldName.Substring(frgPrefix)))) : Inflector.Inflector.Titleize(JU(j.Name, frg.FieldName.Substring(frgPrefix))); //ROLEMBERG FILHO - trata o Tilte de acordo com as regras de AcertaPalavras k.Ident = JI(j.Name, k.Ident); i = 0; ident = k.Ident; while (fieldByIdent.ContainsKey(ident)) { ident = k.Ident + ++i; } k.Ident = ident; fieldByIdent[ident] = k; var atk = new List <string>(); atk.Add("DisplayName(\"" + k.Title + "\")"); k.Expression = "j" + j.Name + ".[" + k.Name + "]"; atk.Add("Expression(\"" + k.Expression + "\")"); k.Attributes = String.Join(", ", atk); if (f.TextualField == null && k.FieldType == "String") { f.TextualField = k.Ident; } j.Fields.Add(k); } model.Joins.Add(j); } model.Fields.Add(f); } if (model.NameField == null) { var fld = model.Fields.FirstOrDefault(z => z.FieldType == "String"); if (fld != null) { model.NameField = fld.Ident; fld.ColAttributes = fld.ColAttributes ?? "EditLink"; } } foreach (var x in model.Fields) { var attrs = new List <string>(); //ROLEMBERG FILHO - lookup Editor Form var attrsLookupEditorForm = new List <string>(); //ROLEMBERG FILHO - lookup Editor Form attrs.Add("DisplayName(\"" + x.Title + "\")"); if (x.Ident != x.Name) { attrs.Add("Column(\"" + x.Name + "\")"); } if ((x.Size ?? 0) > 0) { attrs.Add("Size(" + x.Size + ")"); } if (x.Scale > 0) { attrs.Add("Scale(" + x.Scale + ")"); } if (!String.IsNullOrEmpty(x.Flags)) { attrs.Add(x.Flags); } if (!String.IsNullOrEmpty(x.PKTable)) { attrs.Add("ForeignKey(\"" + (string.IsNullOrEmpty(x.PKSchema) ? x.PKTable : ("[" + x.PKSchema + "].[" + x.PKTable + "]")) + "\", \"" + x.PKColumn + "\")"); attrs.Add("LeftJoin(\"j" + x.ForeignJoinAlias + "\")"); //ROLEMBERG FILHO - trata o LOOKUPEDITOR attrsLookupEditorForm.Add("LookupEditor(typeof(" + model.Module + ".Entities." + Serenity.CodeGenerator.RowGenerator.ClassNameFromTableName(x.PKTable) + "Row), InplaceAdd = true)"); //ROLEMBERG FILHO - trata o LOOKUPEDITOR } if (model.NameField == x.Ident) { attrs.Add("QuickSearch"); } if (x.TextualField != null) { attrs.Add("TextualField(\"" + x.TextualField + "\")"); } //ROLEMBERG FILHO - trata o PLACEHOLDER e ADVANCED TIPS //if (config.FieldDescriptionasPlaceholder) //{ if (!string.IsNullOrEmpty(x.FieldDescription)) { if (x.DataType == "Boolean") { attrs.Add("Hint(\"" + x.FieldDescription + "\")"); } else { attrs.Add("Placeholder(\"" + x.FieldDescription + "\")"); } } //} //if (config.GenerateRowswithAdvancedTips) //{ string attr = processAdvancedTips(x); if (!string.IsNullOrEmpty(attr)) { attrs.Add(attr); } //} //ROLEMBERG FILHO - trata o PLACEHOLDER e ADVANCED TIPS x.Attributes = String.Join(", ", attrs.ToArray()); x.AttrsLookupEditorForm = String.Join(", ", attrsLookupEditorForm.ToArray()); x.AttrsFileUpload = processAdvancedTips_Image_File(x, model.Tablename); x.FormAttributes = processAdvancedTips_Forms(x); //x.DialogAttributes = processAdvancedTips_Dialog(x, model.RootNamespace); processAdvancedTips_Dialog(x, ref model); } return(model); }
public static EntityModel GenerateModel(IDbConnection connection, string tableSchema, string table, string module, string connectionKey, string entityClass, string permission, GeneratorConfig config) { var model = new EntityModel(); model.Module = module; if (connection.GetDialect().ServerType.StartsWith("MySql", StringComparison.OrdinalIgnoreCase)) { model.Schema = null; } else { model.Schema = tableSchema; } model.Permission = permission; model.ConnectionKey = connectionKey; model.RootNamespace = config.RootNamespace; var className = entityClass ?? ClassNameFromTableName(table); model.ClassName = className; model.RowClassName = className + "Row"; model.Title = Inflector.Inflector.Titleize(className); model.Tablename = table; model.Fields = new List <EntityField>(); model.Joins = new List <EntityJoin>(); model.Instance = true; var schemaProvider = SchemaHelper.GetSchemaProvider(connection.GetDialect().ServerType); var fields = schemaProvider.GetFieldInfos(connection, tableSchema, table).ToList(); if (!fields.Any(x => x.IsPrimaryKey)) { var primaryKeys = new HashSet <string>(schemaProvider.GetPrimaryKeyFields(connection, tableSchema, table)); foreach (var field in fields) { field.IsPrimaryKey = primaryKeys.Contains(field.FieldName); } } if (!fields.Any(x => x.IsIdentity)) { var identities = new HashSet <string>(schemaProvider.GetIdentityFields(connection, tableSchema, table)); foreach (var field in fields) { field.IsIdentity = identities.Contains(field.FieldName); } } var foreigns = schemaProvider.GetForeignKeys(connection, tableSchema, table) .ToLookup(x => x.FKName) .Where(x => x.Count() == 1) .SelectMany(x => x) .ToList(); foreach (var field in fields) { var fk = foreigns.FirstOrDefault(x => x.FKColumn == field.FieldName); if (fk != null) { field.PKSchema = fk.PKSchema; field.PKTable = fk.PKTable; field.PKColumn = fk.PKColumn; } } var prefix = DeterminePrefixLength(fields, x => x.FieldName); model.FieldPrefix = fields.First().FieldName.Substring(0, prefix); var identity = fields.FirstOrDefault(f => f.IsIdentity == true); if (identity == null) { identity = fields.FirstOrDefault(f => f.IsPrimaryKey == true); } if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } else { identity = fields.FirstOrDefault(f => f.IsPrimaryKey == true); if (identity != null) { model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix)); } } string baseRowMatch = null; HashSet <string> baseRowFieldset = null; List <string> baseRowFieldList = new List <string>(); foreach (var k in config.BaseRowClasses ?? new List <GeneratorConfig.BaseRowClass>()) { var b = k.ClassName; var f = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var fl = new List <string>(); bool skip = false; foreach (var s in k.Fields ?? new List <string>()) { string n = s.TrimToNull(); if (n == null || !fields.Any(z => z.FieldName.Substring(prefix) == n)) { skip = true; break; } f.Add(n); fl.Add(n); } if (skip) { continue; } if (baseRowFieldset == null || f.Count > baseRowFieldset.Count) { baseRowFieldset = f; baseRowFieldList = fl; baseRowMatch = b; } } var removeForeignFields = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var s in config.RemoveForeignFields ?? new List <string>()) { string n = s.TrimToNull(); if (n != null) { removeForeignFields.Add(n); } } if (baseRowFieldset != null && baseRowFieldset.Count > 0) { model.RowBaseClass = baseRowMatch; model.FieldsBaseClass = baseRowMatch + "Fields"; model.RowBaseFields = new List <EntityField>(); fields = fields.Where(f => { if (baseRowFieldset.Contains(f.FieldName.Substring(prefix))) { var ef = ToEntityField(f, prefix); ef.Flags = null; model.RowBaseFields.Add(ef); return(false); } return(true); }).ToList(); } else { model.RowBaseClass = "Row"; model.RowBaseFields = new List <EntityField>(); model.FieldsBaseClass = "RowFieldsBase"; } foreach (var field in fields) { var f = ToEntityField(field, prefix); if (f.Name == className && f.FieldType == "String") { model.NameField = f.Name; } var foreign = foreigns.Find((k) => k.FKColumn.Equals(field.FieldName, StringComparison.OrdinalIgnoreCase)); if (foreign != null) { if (f.Title.EndsWith(" Id") && f.Title.Length > 3) { f.Title = f.Title.SafeSubstring(0, f.Title.Length - 3); } f.PKSchema = foreign.PKSchema; f.PKTable = foreign.PKTable; f.PKColumn = foreign.PKColumn; var frgfld = schemaProvider.GetFieldInfos(connection, foreign.PKSchema, foreign.PKTable).ToList(); int frgPrefix = RowGenerator.DeterminePrefixLength(frgfld, z => z.FieldName); var j = new EntityJoin(); j.Fields = new List <EntityField>(); j.Name = GenerateVariableName(f.Name.Substring(prefix)); if (j.Name.EndsWith("Id") || j.Name.EndsWith("ID")) { j.Name = j.Name.Substring(0, j.Name.Length - 2); } f.ForeignJoinAlias = j.Name; j.SourceField = f.Ident; frgfld = frgfld.Where(y => !removeForeignFields.Contains(y.FieldName)).ToList(); foreach (var frg in frgfld) { if (frg.FieldName.Equals(foreign.PKColumn, StringComparison.OrdinalIgnoreCase)) { continue; } var k = ToEntityField(frg, frgPrefix); k.Flags = null; k.Title = Inflector.Inflector.Titleize(JU(j.Name, frg.FieldName.Substring(frgPrefix))); if (f.TextualField == null && k.FieldType == "String") { f.TextualField = JI(j.Name, k.Ident); } j.Fields.Add(k); } model.Joins.Add(j); } model.Fields.Add(f); } if (model.NameField == null) { var fld = model.Fields.FirstOrDefault(z => z.FieldType == "String"); if (fld != null) { model.NameField = fld.Ident; } } return(model); }