// Metodos public StringBuilder GeraCodigoController(string _nomeNameSpaceProjeto, string _tabela, string _conexao, string _banco) { string strTabela = _tabela; objCodigo = new StringBuilder(); #region Inicial... objCodigo.AppendLine("using System;"); objCodigo.AppendLine("using System.Collections.Generic;"); objCodigo.AppendLine("using System.Collections;"); objCodigo.AppendLine("using System.Linq;"); objCodigo.AppendLine("using System.Text;"); objCodigo.AppendLine("using System.Web.Mvc;"); objCodigo.AppendLine("using DTO;"); objCodigo.AppendLine("using BLL;"); objCodigo.AppendLine(); objCodigo.AppendLine("/// <summary>"); objCodigo.AppendLine("/// Classe Controller gerada automática: " + strTabela); if (Environment.MachineName == "MARQUESNOTE-PC") { objCodigo.AppendLine("/// Criador: Marques Silva Fonseca"); } else { objCodigo.AppendLine("/// Criador: " + Environment.UserName); } objCodigo.AppendLine("/// Criada em " + strData); //objCodigo.AppendLine("/// Contato: [email protected]"); objCodigo.AppendLine("/// </summary>"); objCodigo.AppendLine("namespace " + _nomeNameSpaceProjeto + ".Controllers"); objCodigo.AppendLine("{"); string tabelaFormatada = strTabela.Replace("_", "").Replace("-", ""); string objtoBLLAtual = tabelaFormatada + "BO"; //TabelaBO string objetoDTOAtual = "Tab" + tabelaFormatada; //TabTabela System.Globalization.CultureInfo cultureinfo = System.Threading.Thread.CurrentThread.CurrentCulture; string tabelaMinusculo = tabelaFormatada.ToLower(); //tabela string tabelaMinusculoBO = cultureinfo.TextInfo.ToTitleCase(tabelaFormatada) + "BO"; //tabelaBO // Abre conexão com o banco objBanco = new Banco.Banco(_conexao); // Cria o objeto da classe Library objLib = new Library.Library(); objCodigo.AppendLine(tb + "[Authorize]"); objCodigo.AppendLine(tb + "public class " + tabelaFormatada + "Controller : Controller"); objCodigo.AppendLine(tb + "{"); if (strTabela.Substring(0, 2).ToLower() != "vw") { objCodigo.AppendLine(tb + tb + string.Format("//{0}<{1}> {2} = new {3}<{4}>();", "Vw" + objtoBLLAtual, "Tab" + "Vw" + tabelaFormatada, "Vw" + tabelaMinusculoBO, "Vw" + objtoBLLAtual, "Tab" + "Vw" + tabelaFormatada)); } objCodigo.AppendLine(tb + tb + string.Format("{0}<{1}> {2} = new {3}<{4}>();", objtoBLLAtual, objetoDTOAtual, tabelaMinusculoBO, objtoBLLAtual, objetoDTOAtual)); objCodigo.AppendLine(); #endregion #region Index objCodigo.AppendLine(tb + "// GET: " + tabelaFormatada + ""); objCodigo.AppendLine(tb + "public ActionResult Index()"); objCodigo.AppendLine(tb + "{"); //objCodigo.AppendLine(tb + tb + "ViewBag.Qtd = new DAL.Dao().ExecultarScript(\"Select count(*) from Modulo\");"); if (strTabela.Substring(0, 2).ToLower() != "vw") { objCodigo.AppendLine(tb + tb + "//IEnumerable<" + "Tab" + "Vw" + tabelaFormatada + "> listaRetornada = " + "Vw" + tabelaMinusculoBO + ".FindAllLista().AsEnumerable<" + "Tab" + "Vw" + tabelaFormatada + ">().OrderByDescending(M => M.DataCadastro);"); } objCodigo.AppendLine(tb + tb + "IEnumerable<" + objetoDTOAtual + "> listaRetornada = " + tabelaMinusculoBO + ".FindAllLista().AsEnumerable<" + objetoDTOAtual + ">().OrderByDescending(M => M.DataCadastro);"); objCodigo.AppendLine(tb + tb + "return View(listaRetornada);"); objCodigo.AppendLine(tb + "}"); objCodigo.AppendLine(); #endregion #region Details // Abre conexão objBanco = new Banco.Banco(_conexao); // Faz a leitura de todas as colunas da tabela objDr = objBanco.QueryConsulta("SELECT * FROM " + strTabela); nunrec = objDr.FieldCount; // objeto da classe Libary objLib = new Library.Library(); objCodigo.AppendLine(tb + "// GET: " + tabelaFormatada + "/Details/5"); if (nunrec > 0) { string tipoParametro = objLib.DefineTipo(objDr.GetDataTypeName(0).ToString()); if (tipoParametro == "Guid") { objCodigo.AppendLine(tb + "public ActionResult Details(string id)"); objCodigo.AppendLine(tb + "{"); //objCodigo.AppendLine(tb + tb + "System.Data.DataSet dsRetornado = " + tabelaMinusculoBO + ".FindBy_" + tabelaFormatada + "Id(new Guid(id));"); } else if (tipoParametro == "string") { objCodigo.AppendLine(tb + "public ActionResult Details(string id)"); objCodigo.AppendLine(tb + "{"); //objCodigo.AppendLine(tb + tb + "System.Data.DataSet dsRetornado = " + tabelaMinusculoBO + ".FindBy_" + tabelaFormatada + "Id(id);"); } else { objCodigo.AppendLine(tb + "public ActionResult Details(int id)"); objCodigo.AppendLine(tb + "{"); //objCodigo.AppendLine(tb + tb + "System.Data.DataSet dsRetornado = " + tabelaMinusculoBO + ".FindBy_" + tabelaFormatada + "Id(id);"); } } if (strTabela.Substring(0, 2).ToLower() != "vw") { objCodigo.AppendLine(tb + tb + "//List<" + "Tab" + "Vw" + tabelaFormatada + "> listaRetornada = " + "Vw" + tabelaMinusculoBO + ".FindAllLista(string.Format(\"" + objDr.GetName(0) + " = '{0}'\", id)).ToList();"); } objCodigo.AppendLine(tb + tb + "List<" + objetoDTOAtual + "> listaRetornada = " + tabelaMinusculoBO + ".FindAllLista(string.Format(\"" + objDr.GetName(0) + " = '{0}'\", id)).ToList();"); objCodigo.AppendLine(""); objCodigo.AppendLine(tb + tb + "if (listaRetornada.Count() > 0)"); objCodigo.AppendLine(tb + tb + "{"); objCodigo.AppendLine(tb + tb + " return View(listaRetornada[0]);"); objCodigo.AppendLine(tb + tb + "}"); objCodigo.AppendLine(tb + tb + "else"); objCodigo.AppendLine(tb + tb + "{"); objCodigo.AppendLine(tb + tb + " //Não retornou nenhum registro"); objCodigo.AppendLine(tb + tb + " return RedirectToAction(\"Index\");"); objCodigo.AppendLine(tb + tb + "}"); objCodigo.AppendLine(tb + "}"); objCodigo.AppendLine(""); // Fecha conexão objBanco.CloseConn(); objBanco = null; #endregion #region Create objCodigo.AppendLine(tb + "// GET: " + tabelaFormatada + "/Create"); objCodigo.AppendLine(tb + "public ActionResult Create()"); objCodigo.AppendLine(tb + "{"); List <string> ColunasRelacionadasForengKey = RetornaColunasRelacionadasForengKey(nomeTabela: strTabela, conexao: _conexao); List <string> ColunasReferenciadas = RetornaColunasReferenciadas(nomeTabela: strTabela, conexao: _conexao); List <string> TabelaReferenciada = RetornaTabelaReferenciadas(nomeTabela: strTabela, conexao: _conexao); for (int i = 0; i < ColunasRelacionadasForengKey.Count; i++) { objCodigo.AppendLine(tb + tb + "ViewBag." + ColunasRelacionadasForengKey[i] + " = new SelectList(new " + TabelaReferenciada[i] + "BO<Tab" + TabelaReferenciada[i] + ">().FindAllLista(), \"" + ColunasReferenciadas[0] + "\", \"Descricao\");"); } objCodigo.AppendLine(tb + tb + "return View();"); objCodigo.AppendLine(tb + "}"); objCodigo.AppendLine(); #endregion #region Create // Abre conexão objBanco = new Banco.Banco(_conexao); // Faz a leitura de todas as colunas da tabela objDr = objBanco.QueryConsulta("SELECT * FROM " + strTabela); nunrec = objDr.FieldCount; // objeto da classe Libary objLib = new Library.Library(); objCodigo.AppendLine(tb + "// POST: " + tabelaFormatada + "/Create"); objCodigo.AppendLine(tb + "[HttpPost]"); objCodigo.AppendLine(tb + "public ActionResult Create(" + objetoDTOAtual + " tab)"); objCodigo.AppendLine(tb + "{"); objCodigo.AppendLine(tb + tb + " try"); objCodigo.AppendLine(tb + tb + " {"); objCodigo.AppendLine(tb + tb + " #region Validações e regras específicas para essa operação"); objCodigo.AppendLine(tb + tb + " //if (string.IsNullOrEmpty(Convert.ToString(tab.Descricao)))"); objCodigo.AppendLine(tb + tb + " //{"); objCodigo.AppendLine(tb + tb + " // ModelState.AddModelError(\"Descricao\", \"Informe uma descrição!\");"); objCodigo.AppendLine(tb + tb + " //}"); objCodigo.AppendLine(tb + tb + " #endregion"); objCodigo.AppendLine(tb + tb + " "); objCodigo.AppendLine(tb + tb + " if (!ModelState.IsValid)"); objCodigo.AppendLine(tb + tb + " return Create();"); objCodigo.AppendLine(""); objCodigo.AppendLine(tb + tb + "tab." + objDr.GetName(0) + " = Guid.NewGuid();"); objCodigo.AppendLine(""); objCodigo.AppendLine(tb + tb + " #region modelo anterior"); objCodigo.AppendLine(tb + tb + tb + "//" + objetoDTOAtual + " " + tabelaMinusculo + " = new " + objetoDTOAtual + "();"); for (int i = 0; i < nunrec; i++) { //string colunaAtual = objDr.GetName(i).ToString(); //string tipo = objLib.DefineTipo(objDr.GetDataTypeName(i).ToString()); //colunaPrimaria = objDr.GetName(0).ToString(); //// lista as colunas //if (objDr.GetName(i).ToString() == objDr.GetName(0).ToString() && objLib.DefineTipo(objDr.GetDataTypeName(0).ToString()) == "Guid")//se for a 1º linha // objCodigo.AppendLine(tb + tb + tb + "" + tabelaMinusculo + "." + colunaAtual + " = Guid.NewGuid();"); //else // objCodigo.AppendLine(tb + tb + tb + "" + tabelaMinusculo + "." + colunaAtual + " = " + objLib.RetornaConvertCollectionsForm(colunaAtual, tipo) + ";"); colunaPrimaria = objDr.GetName(0); string tipo = objDr.GetDataTypeName(i); string colunaAtual = objDr.GetName(i).ToString(); // lista as colunas if (objDr.GetName(i) == objDr.GetName(0)) { //se for a 1º linha if (objLib.DefineTipo(objDr.GetDataTypeName(0).ToString()) == "Guid") { objCodigo.AppendLine(tb + tb + tb + "//" + tabelaMinusculo + "." + colunaAtual + " = Guid.NewGuid();"); } else if (objLib.DefineTipo(objDr.GetDataTypeName(0).ToString()) == "string") { objCodigo.AppendLine(tb + tb + tb + "//" + tabelaMinusculo + "." + colunaAtual + " = id;"); } else { objCodigo.AppendLine(tb + tb + tb + "//" + tabelaMinusculo + "." + colunaAtual + " = " + objLib.RetornaConvertCollectionsForm(colunaAtual, tipo) + ";"); } } else { objCodigo.AppendLine(tb + tb + tb + "//" + tabelaMinusculo + "." + colunaAtual + " = " + objLib.RetornaConvertCollectionsForm(colunaAtual, tipo) + ";"); } } objCodigo.AppendLine(tb + tb + " #endregion"); objCodigo.AppendLine(""); objCodigo.AppendLine(tb + tb + tb + " int retorno = " + tabelaMinusculoBO + ".Insert(tab);"); objCodigo.AppendLine(tb + tb + tb + " if (retorno == 1)"); objCodigo.AppendLine(tb + tb + tb + " return RedirectToAction(\"Details\", new { id = tab." + colunaPrimaria + " });"); objCodigo.AppendLine(tb + tb + tb + " else"); objCodigo.AppendLine(tb + tb + tb + " {"); objCodigo.AppendLine(tb + tb + tb + " //Ocorreu algum erro....."); objCodigo.AppendLine(tb + tb + tb + " ModelState.AddModelError(\"\", \"Ocorreu algum erro no procedimento atual. Se o problema persistir procure o administrador do sistema.\");"); objCodigo.AppendLine(tb + tb + tb + " return Create();"); objCodigo.AppendLine(tb + tb + tb + " }"); objCodigo.AppendLine(tb + tb + " }"); objCodigo.AppendLine(tb + tb + " catch (Exception ex)"); objCodigo.AppendLine(tb + tb + " {"); objCodigo.AppendLine(tb + tb + " ModelState.AddModelError(\"\", ex.Message);"); objCodigo.AppendLine(tb + tb + " return Create();"); objCodigo.AppendLine(tb + tb + " }"); objCodigo.AppendLine(tb + tb + "}"); objCodigo.AppendLine(); // Fecha conexão objBanco.CloseConn(); objBanco = null; #endregion #region Edit // Abre conexão objBanco = new Banco.Banco(_conexao); // Faz a leitura de todas as colunas da tabela objDr = objBanco.QueryConsulta("SELECT * FROM " + strTabela); nunrec = objDr.FieldCount; // objeto da classe Libary objLib = new Library.Library(); objCodigo.AppendLine(tb + "// GET: " + tabelaFormatada + "/Edit/5"); objCodigo.AppendLine(tb + "public ActionResult Edit(string id)"); objCodigo.AppendLine(tb + "{"); objCodigo.AppendLine(tb + tb + "List<" + objetoDTOAtual + "> listaRetornada = " + tabelaMinusculoBO + ".FindAllLista(string.Format(\"" + objDr.GetName(0) + " = '{0}'\", id)).ToList();"); objCodigo.AppendLine(""); objCodigo.AppendLine(tb + tb + "if (listaRetornada.Count() > 0)"); objCodigo.AppendLine(tb + tb + "{"); //List<string> ColunasRelacionadasForengKey = RetornaColunasRelacionadasForengKey(nomeTabela: strTabela, conexao: _conexao); List<string> ColunasRelacionadasForengKey = RetornaColunasRelacionadasForengKey(nomeTabela: strTabela, conexao: _conexao); //List<string> ColunasReferenciadas = RetornaColunasReferenciadas(nomeTabela: strTabela, conexao: _conexao); //List<string> TabelaReferenciada = RetornaTabelaReferenciadas(nomeTabela: strTabela, conexao: _conexao); for (int i = 0; i < ColunasRelacionadasForengKey.Count; i++) { objCodigo.AppendLine(tb + tb + "ViewBag." + ColunasRelacionadasForengKey[i] + " = new SelectList(new " + TabelaReferenciada[i] + "BO<Tab" + TabelaReferenciada[i] + ">().FindAllLista(), \"" + ColunasReferenciadas[0] + "\", \"Descricao\");"); } objCodigo.AppendLine(tb + tb + " return View(listaRetornada[0]);"); objCodigo.AppendLine(tb + tb + "}"); objCodigo.AppendLine(tb + tb + "else"); objCodigo.AppendLine(tb + tb + "{"); objCodigo.AppendLine(tb + tb + " //Não retornou nenhum registro"); objCodigo.AppendLine(tb + tb + " return RedirectToAction(\"Index\");"); objCodigo.AppendLine(tb + tb + "}"); objCodigo.AppendLine(tb + "}"); objCodigo.AppendLine(); // Fecha conexão objBanco.CloseConn(); objBanco = null; #endregion #region Edit // Abre conexão objBanco = new Banco.Banco(_conexao); // Faz a leitura de todas as colunas da tabela objDr = objBanco.QueryConsulta("SELECT * FROM " + strTabela); nunrec = objDr.FieldCount; // objeto da classe Libary objLib = new Library.Library(); objCodigo.AppendLine(tb + "// POST: " + tabelaFormatada + "/Edit/5"); objCodigo.AppendLine(tb + "[HttpPost]"); if (nunrec > 0) { string tipoParametro = objLib.DefineTipo(objDr.GetDataTypeName(0).ToString()); tipoParametro = tipoParametro == "Guid" || tipoParametro == "string" ? "string" : "int"; objCodigo.AppendLine(tb + "public ActionResult Edit(" + tipoParametro + " id, " + objetoDTOAtual + " tab)"); } objCodigo.AppendLine(tb + "{"); objCodigo.AppendLine(tb + tb + " try"); objCodigo.AppendLine(tb + tb + " {"); objCodigo.AppendLine(tb + tb + " #region Validações e regras específicas para essa operação"); objCodigo.AppendLine(tb + tb + " //if (string.IsNullOrEmpty(Convert.ToString(tab.Descricao)))"); objCodigo.AppendLine(tb + tb + " //{"); objCodigo.AppendLine(tb + tb + " // ModelState.AddModelError(\"Descricao\", \"Informe uma descrição!\");"); objCodigo.AppendLine(tb + tb + " //}"); objCodigo.AppendLine(tb + tb + " #endregion"); objCodigo.AppendLine(tb + tb + " "); objCodigo.AppendLine(tb + tb + " if (!ModelState.IsValid)"); objCodigo.AppendLine(tb + tb + " return Edit(id);"); objCodigo.AppendLine(""); objCodigo.AppendLine("tab." + objDr.GetName(0) + " = new Guid(id);"); objCodigo.AppendLine("#region modelo anterior"); objCodigo.AppendLine(tb + tb + tb + "//" + objetoDTOAtual + " " + tabelaMinusculo + " = new " + objetoDTOAtual + "();"); for (int i = 0; i < nunrec; i++) { colunaPrimaria = objDr.GetName(0); string tipo = objDr.GetDataTypeName(i); string colunaAtual = objDr.GetName(i).ToString(); // lista as colunas if (objDr.GetName(i) == objDr.GetName(0)) { //se for a 1º linha if (objLib.DefineTipo(objDr.GetDataTypeName(0).ToString()) == "Guid") { objCodigo.AppendLine(tb + tb + tb + "//" + tabelaMinusculo + "." + colunaAtual + " = new Guid(id);"); } else if (objLib.DefineTipo(objDr.GetDataTypeName(0).ToString()) == "string") { objCodigo.AppendLine(tb + tb + tb + "//" + tabelaMinusculo + "." + colunaAtual + " = id;"); } else { objCodigo.AppendLine(tb + tb + tb + "//" + tabelaMinusculo + "." + colunaAtual + " = " + objLib.RetornaConvertCollectionsForm(colunaAtual, tipo) + ";"); } } else { objCodigo.AppendLine(tb + tb + tb + "//" + tabelaMinusculo + "." + colunaAtual + " = " + objLib.RetornaConvertCollectionsForm(colunaAtual, tipo) + ";"); } } objCodigo.AppendLine("#endregion"); objCodigo.AppendLine(tb + tb + tb + " int retorno = " + tabelaMinusculoBO + ".Update(tab);"); objCodigo.AppendLine(tb + tb + tb + " if (retorno == 1)"); objCodigo.AppendLine(tb + tb + tb + " return RedirectToAction(\"Details\", new { id = tab." + colunaPrimaria + " });"); objCodigo.AppendLine(tb + tb + tb + " else"); objCodigo.AppendLine(tb + tb + tb + " {"); objCodigo.AppendLine(tb + tb + tb + " //Ocorreu algum erro....."); objCodigo.AppendLine(tb + tb + tb + " ModelState.AddModelError(\"\", \"Ocorreu algum erro no procedimento atual. Se o problema persistir procure o administrador do sistema.\");"); objCodigo.AppendLine(tb + tb + tb + " return Edit(id);"); objCodigo.AppendLine(tb + tb + tb + " }"); objCodigo.AppendLine(tb + tb + " }"); objCodigo.AppendLine(tb + tb + " catch (Exception ex)"); objCodigo.AppendLine(tb + tb + " {"); objCodigo.AppendLine(tb + tb + " ModelState.AddModelError(\"\", ex.Message);"); objCodigo.AppendLine(tb + tb + " return Edit(id);"); objCodigo.AppendLine(tb + tb + " }"); objCodigo.AppendLine(tb + "}"); objCodigo.AppendLine(); // Fecha conexão objBanco.CloseConn(); objBanco = null; #endregion #region Delete // Abre conexão objBanco = new Banco.Banco(_conexao); // Faz a leitura de todas as colunas da tabela objDr = objBanco.QueryConsulta("SELECT * FROM " + strTabela); nunrec = objDr.FieldCount; // objeto da classe Libary objLib = new Library.Library(); objCodigo.AppendLine(tb + "// GET: " + tabelaFormatada + "/Delete/5"); if (nunrec > 0) { string tipoParametro = objLib.DefineTipo(objDr.GetDataTypeName(0).ToString()); tipoParametro = tipoParametro == "Guid" || tipoParametro == "string" ? "string" : "int"; objCodigo.AppendLine(tb + "public ActionResult Delete(" + tipoParametro + " id)"); } objCodigo.AppendLine(tb + "{"); objCodigo.AppendLine(tb + tb + "try"); objCodigo.AppendLine(tb + tb + "{"); objCodigo.AppendLine(tb + tb + tb + "List<" + objetoDTOAtual + "> listaRetornada = this." + tabelaMinusculoBO + ".FindAllLista(string.Format(\"" + objDr.GetName(0) + " = '{0}'\", id)).Take(1).ToList();"); objCodigo.AppendLine(""); objCodigo.AppendLine(tb + tb + tb + "if (listaRetornada.Count > 0)"); objCodigo.AppendLine(tb + tb + tb + "{"); objCodigo.AppendLine(tb + tb + tb + tb + "int retorno = this." + tabelaMinusculoBO + ".Delete(listaRetornada[0]);"); objCodigo.AppendLine(tb + tb + tb + tb + "if (retorno == 0)"); objCodigo.AppendLine(tb + tb + tb + tb + "{"); objCodigo.AppendLine(tb + tb + tb + tb + tb + "//Ocorreu algum erro....."); objCodigo.AppendLine(tb + tb + tb + tb + tb + "return RedirectToAction(\"Index\");"); objCodigo.AppendLine(tb + tb + tb + tb + "}"); objCodigo.AppendLine(tb + tb + tb + "}"); objCodigo.AppendLine(tb + tb + tb + "//Não retornou nenhum registro"); objCodigo.AppendLine(tb + tb + tb + "return RedirectToAction(\"Index\");"); objCodigo.AppendLine(tb + tb + "}"); objCodigo.AppendLine(tb + tb + "catch"); objCodigo.AppendLine(tb + tb + "{"); objCodigo.AppendLine(tb + tb + tb + "return RedirectToAction(\"Index\");"); objCodigo.AppendLine(tb + tb + "}"); objCodigo.AppendLine(tb + "}"); objCodigo.AppendLine(); // Fecha conexão objBanco.CloseConn(); objBanco = null; #endregion objCodigo.AppendLine(tb + "}"); objCodigo.AppendLine("}"); return(objCodigo); }