Exemple #1
0
        public string BuscarByDescricao(string campoId, string campoDescr, string tipoDAO,
                                        string nomeMetodo, string parametros, string descricao, string recuperarValores)
        {
            try
            {
                campoId    = Decode(campoId);
                campoDescr = Decode(campoDescr);
                tipoDAO    = Decode(tipoDAO);
                parametros = recuperarValores != "true" ? Decode(parametros) : parametros;

                var flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;

                Type DAO = Type.GetType(tipoDAO + (tipoDAO.Contains("Glass.Data") ? ", Glass.Data" :
                                                   tipoDAO.Contains("WebGlass.Business") ? ", WebGlass.Business" : ""));

                Type tipoModel = tipoDAO.Contains("DAL") ? DAO.GetMethod("GetElementByPrimaryKey", flags, null, new Type[] { typeof(uint) }, null).ReturnType : null;

                bool isCampoDescrInTable = false;

                if (tipoDAO.Contains("DAL") && tipoModel != null && String.IsNullOrEmpty(parametros))
                {
                    PropertyInfo propDescr = tipoModel.GetProperty(campoDescr, flags);

                    PersistencePropertyAttribute[] descrAtr = propDescr.GetCustomAttributes(typeof(PersistencePropertyAttribute), true) as PersistencePropertyAttribute[];

                    if (descrAtr.Length > 0 && descrAtr[0].Direction != DirectionParameter.InputOptional && descrAtr[0].Direction != DirectionParameter.InputOptionalOutput &&
                        descrAtr[0].Direction != DirectionParameter.InputOptionalOutputOnlyInsert)
                    {
                        isCampoDescrInTable = true;
                    }
                }

                if (isCampoDescrInTable)
                {
                    object instance = DAO.GetProperty("Instance", flags).GetValue(null, null);

                    string nomeTabela = DAO.GetMethod("GetTableName", flags).Invoke(instance, null) as string;

                    if (String.IsNullOrEmpty(nomeTabela))
                    {
                        return("Erro|Tabela não encontrada.");
                    }

                    string sql = "select concat(Cast(" + GetDbName(tipoModel, campoId) + " as char), '|', Cast(" +
                                 GetDbName(tipoModel, campoDescr) + " as char)) from " + nomeTabela + " where " + GetDbName(tipoModel, campoDescr) + "=?descr";

                    string retorno = DAO.GetMethod("ExecuteScalar", flags, null, new Type[] { typeof(string), typeof(GDA.GDAParameter[]) }, null).MakeGenericMethod(typeof(string)).
                                     Invoke(instance, new object[] { sql, new GDAParameter[] { new GDAParameter("?descr", descricao) } }) as string;

                    if (retorno == null)
                    {
                        throw new Exception("Item não encontrado.");
                    }

                    string[] dados = retorno.Split('|');
                    return("Ok|" + dados[0] + "|" + dados[1]);
                }
                else
                {
                    nomeMetodo = Decode(nomeMetodo).Split(',')[1];

                    var      metodos         = DAO.GetMethods(flags);
                    string[] dadosParametros = parametros.Split('|');

                    MethodInfo metodo = metodos.Where(x => x.Name == nomeMetodo).
                                        Where(x => x.GetParameters().Length == (parametros != String.Empty ? dadosParametros.Length : 0)).
                                        FirstOrDefault();

                    tipoModel = metodo.ReturnType;

                    ParameterInfo[] pi    = metodo.GetParameters();
                    object[]        param = new object[pi.Length];

                    for (int j = 0; j < param.Length; j++)
                    {
                        for (int k = 0; k < dadosParametros.Length; k++)
                        {
                            string[] dados = dadosParametros[k].Split(':');
                            if (pi[j].Name == dados[0])
                            {
                                param[j] = Conversoes.ConverteValor(pi[j].ParameterType, dados[1]);
                                break;
                            }
                        }
                    }

                    object instance = metodo.IsStatic ? null :
                                      DAO.GetProperty("Instance", flags).GetValue(null, null);

                    if (!metodo.IsStatic && instance == null)
                    {
                        try { instance = Activator.CreateInstance(DAO); }
                        catch { }
                    }

                    object retorno = metodo.Invoke(instance, param);

                    PropertyInfo id = null, descr = null;
                    object       i, d;

                    if (typeof(IEnumerable).IsAssignableFrom(tipoModel))
                    {
                        foreach (object o in (IEnumerable)retorno)
                        {
                            if (id == null)
                            {
                                id    = o.GetType().GetProperty(campoId);
                                descr = o.GetType().GetProperty(campoDescr);
                            }

                            i = id.GetValue(o, null);
                            d = descr.GetValue(o, null);

                            string comp = d is string || d is ValueType || d != null?d.ToString() : null;

                            if (String.Equals(Formatacoes.TrataTextoComparacaoSelPopUp(comp).Replace("  ", "").Replace(" ", "").Replace("-", "").ToLower(),
                                              Formatacoes.TrataTextoComparacaoSelPopUp(descricao).Replace("  ", "").Replace(" ", "").Replace("-", "").ToLower(),
                                              StringComparison.InvariantCultureIgnoreCase))
                            {
                                return("Ok|" + i + "|" + d);
                            }
                        }

                        return("Erro|Item não encontrado.");
                    }
                    else
                    {
                        id    = retorno.GetType().GetProperty(campoId);
                        descr = retorno.GetType().GetProperty(campoDescr);

                        i = id.GetValue(retorno, null);
                        d = descr.GetValue(retorno, null);

                        string comp = d is string || d is ValueType || d != null?d.ToString() : null;

                        if (String.Equals(Formatacoes.TrataTextoComparacaoSelPopUp(comp).Replace("  ", "").Replace(" ", "").Replace("-", "").ToLower(),
                                          Formatacoes.TrataTextoComparacaoSelPopUp(descricao).Replace("  ", "").Replace(" ", "").Replace("-", "").ToLower(),
                                          StringComparison.InvariantCultureIgnoreCase))
                        {
                            return("Ok|" + i + "|" + d);
                        }
                        else
                        {
                            return("Erro|Item não encontrado.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return("Erro|" + Glass.MensagemAlerta.FormatErrorMsg("", ex));
            }
        }