/// <summary> /// Retorna o número de registros na coleção de objetos(tabela de banco de dados) do tipo do objeto passado /// por parâmetro. /// </summary> /// <param name="obj"></param> /// <returns></returns> public int GetObjectCount(IPersistente obj) { try { Type objectType = obj.GetType(); int count = -1; count = (int)Session.CreateQuery("select count(*) from " + objectType.ToString()).UniqueResult(); return(count); } catch (NonUniqueResultException nurex) { throw new DaoException("Erro ao buscar contagem", nurex); } catch (ADOException adoex) { throw new DaoException("Erro ao buscar contagem: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro ao buscar contagem", hex); } catch (Exception ex) { throw new DaoException("Erro ao buscar contagem", ex); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="listaAscDesc"></param> /// <returns></returns> public System.Collections.ICollection BuscarPorExemplo(IPersistente obj, ArrayList listaAscDesc) { ICollection items = null; try { ICriteria criteria = GetCriteria(obj.GetType(), 0, 0, listaAscDesc, false); Example queryExample = Example.Create(obj).EnableLike(MatchMode.Anywhere) .IgnoreCase().ExcludeNulls().ExcludeZeroes(); criteria.Add(queryExample); items = criteria.List(); return(items); } catch (InstantiationException ie) { throw new DaoException("Erro ao buscar itens: " + ie.Message, ie); } catch (ADOException adoex) { throw new DaoException("Erro ao buscar itens: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro ao buscar itens: " + hex.Message, hex); } catch (Exception ex) { throw new DaoException("Erro ao buscar itens: " + ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="atributosExcluidos"></param> /// <param name="tamanhoPagina"></param> /// <param name="numeroPagina"></param> /// <param name="listaAscDesc"></param> /// <returns></returns> public System.Collections.ICollection BuscarPorExemploPaginavel(IPersistente obj, string[] atributosExcluidos, int tamanhoPagina, int numeroPagina, ArrayList listaAscDesc) { ICollection items = null; try { ICriteria criteria = GetCriteria(obj.GetType(), tamanhoPagina, numeroPagina, listaAscDesc, true); Example queryExample = Example.Create(obj).EnableLike(MatchMode.Anywhere) .IgnoreCase().ExcludeNulls().ExcludeZeroes(); foreach (string atributo in atributosExcluidos) { queryExample.ExcludeProperty(atributo); } criteria.Add(queryExample); items = criteria.List(); return(items); } catch (InstantiationException ie) { throw new DaoException("Erro ao buscar itens: " + ie.Message, ie); } catch (ADOException adoex) { throw new DaoException("Erro ao buscar itens: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro ao buscar itens: " + hex.Message, hex); } catch (Exception ex) { throw new DaoException("Erro ao buscar itens: " + ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="tamanhoPagina"></param> /// <param name="numeroPagina"></param> /// <param name="listaAscDesc"></param> /// <returns></returns> public System.Collections.ICollection BuscarTodosPaginavel(IPersistente obj, int tamanhoPagina, int numeroPagina, ArrayList listaAscDesc) { ICollection items = null; try { ICriteria criteria = GetCriteria(obj.GetType(), tamanhoPagina, numeroPagina, listaAscDesc, true); items = criteria.List(); return(items); } catch (InstantiationException ie) { throw new DaoException("Erro ao buscar itens: " + ie.Message, ie); } catch (ADOException adoex) { throw new DaoException("Erro ao buscar itens: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro ao buscar itens: " + hex.Message, hex); } catch (Exception ex) { throw new DaoException("Erro ao buscar itens: " + ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="parametro"></param> /// <returns></returns> public System.Collections.ICollection BuscarTodos(IPersistente obj, ParametroOrder parametro) { ICollection items = null; try { ArrayList listaAscDesc = new ArrayList(); listaAscDesc.Add(parametro); ICriteria criteria = GetCriteria(obj.GetType(), 0, 0, listaAscDesc, false); items = criteria.List(); return(items); } catch (InstantiationException ie) { throw new DaoException("Erro ao buscar itens: " + ie.Message, ie); } catch (ADOException adoex) { throw new DaoException("Erro ao buscar itens: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro ao buscar itens: " + hex.Message, hex); } catch (Exception ex) { throw new DaoException("Erro ao buscar itens: " + ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> public System.Collections.ICollection BuscarTodos(IPersistente obj) { ICollection items = null; try { Type tipo = obj.GetType(); ICriteria criteria = Session.CreateCriteria(tipo); items = criteria.List(); return(items); } catch (InstantiationException ie) { throw new DaoException("Erro ao buscar itens: " + ie.Message, ie); } catch (ADOException adoex) { throw new DaoException("Erro ao buscar itens: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro ao buscar itens: " + hex.Message, hex); } catch (Exception ex) { throw new DaoException("Erro ao buscar itens: " + ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="atributosExcluidos"></param> /// <returns></returns> public System.Collections.ICollection BuscarPorExemplo(IPersistente obj, string[] atributosExcluidos) { ICollection items = null; try { Type tipo = obj.GetType(); Example queryExample = Example.Create(obj).EnableLike(MatchMode.Anywhere) .IgnoreCase().ExcludeNulls().ExcludeZeroes(); foreach (string atributo in atributosExcluidos) { queryExample.ExcludeProperty(atributo); } ICriteria criteria = Session.CreateCriteria(tipo).Add(queryExample); items = criteria.List(); return(items); } catch (InstantiationException ie) { throw new DaoException("Erro ao buscar itens: " + ie.Message, ie); } catch (ADOException adoex) { throw new DaoException("Erro ao buscar itens: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro ao buscar itens: " + hex.Message, hex); } catch (Exception ex) { throw new DaoException("Erro ao buscar itens: " + ex.Message, ex); } }
/// <summary> /// Método para buscar objeto por chave primária. Passa-se um objeto qualquer com o tipo que se deseja carregar e o objeto que representa chave(pode ser int, string, ou mesmo objeto que representa chave composta como PessoaPk) /// </summary> /// <param name="obj">Objeto qualquer do tipo que se deseja carregar</param> /// <param name="objetoId">chave primária do objeto específico que se deseja carregar</param> /// <returns>Objeto específico com chave primária especificada</returns> public IPersistente BuscarPorChavePrimaria(IPersistente obj, Object objetoId) { try { IPersistente objetoPersistido = (IPersistente)Session.Get(obj.GetType(), objetoId); return(objetoPersistido); } catch (InstantiationException ie) { throw new DaoException("Erro recuperando item " + objetoId + ". Mensagem de erro original: " + ie.Message, ie); } catch (ADOException adoex) { throw new DaoException("Erro recuperando item " + objetoId + ". Mensagem de erro original: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro recuperando item " + objetoId + ". Mensagem de erro original: " + hex.Message, hex); } catch (Exception ex) { throw new DaoException("Erro recuperando item " + objetoId, ex); } }
/// <summary> /// Retorna o número de registros na coleção de objetos(tabela de banco de dados) do tipo do objeto passado /// por parâmetro. /// </summary> /// <param name="obj"></param> /// <returns></returns> public int GetObjectCountPorExemplo(IPersistente obj) { try { Type objectType = obj.GetType(); int count = -1; String filtro = "select count(*) from " + objectType.Name.ToString() + " " + objectType.Name.ToString().ToLower() + " "; String nomeClasse = objectType.Name.ToString(); MethodInfo metodoGet = null; PropertyInfo[] propriedades = objectType.GetProperties(); foreach (PropertyInfo propriedade in propriedades) { //acessador metodoGet = propriedade.GetGetMethod(); //Nome da propriedade String nomePropriedade = propriedade.Name; //Nome do tipo da propriedade String tipoPropriedade = propriedade.PropertyType.Name; String valorPropriedade; if (!nomePropriedade.Equals("Id")) { if (tipoPropriedade.Equals("String") || tipoPropriedade.Equals("string")) { //captura o valor da propriedade ou, caso esteja nulo, captura nulo mas sem lançar exceção. valorPropriedade = propriedade.GetValue(obj, null) as String; if (!String.IsNullOrEmpty(valorPropriedade)) { if (!filtro.Contains("where")) { filtro += "where (upper(" + nomeClasse.ToLower() + "." + nomePropriedade + ") like '%" + valorPropriedade.ToUpper() + "%') "; } else { filtro += "and (upper(" + nomeClasse.ToLower() + "." + nomePropriedade + ") like '%" + valorPropriedade.ToUpper() + "%') "; } } } if (tipoPropriedade.Equals("Int32") || tipoPropriedade.Equals("int") || tipoPropriedade.Equals("Int64")) { valorPropriedade = ((Object)propriedade.GetValue(obj, null)).ToString(); if (!String.IsNullOrEmpty(valorPropriedade) && !valorPropriedade.Equals("0")) { if (!filtro.Contains("where")) { filtro += "where (" + nomeClasse.ToLower() + "." + nomePropriedade + " = " + valorPropriedade + ") "; } else { filtro += "and (" + nomeClasse.ToLower() + "." + nomePropriedade + " = " + valorPropriedade + ") "; } } } if (tipoPropriedade.Equals("Double") || tipoPropriedade.Equals("double")) { valorPropriedade = ((Object)propriedade.GetValue(obj, null)).ToString(); if (!String.IsNullOrEmpty(valorPropriedade)) { if (!filtro.Contains("where")) { filtro += "where (" + nomeClasse.ToLower() + "." + nomePropriedade + " = " + valorPropriedade + ") "; } else { filtro += "and (" + nomeClasse.ToLower() + "." + nomePropriedade + " = " + valorPropriedade + ") "; } } } } } count = (int)Session.CreateQuery(filtro).UniqueResult(); return(count); } catch (NonUniqueResultException nurex) { throw new DaoException("Erro ao buscar contagem", nurex); } catch (ADOException adoex) { throw new DaoException("Erro ao buscar contagem: " + adoex.Message, adoex); } catch (HibernateException hex) { throw new DaoException("Erro ao buscar contagem", hex); } catch (Exception ex) { throw new DaoException("Erro ao buscar contagem", ex); } }