Example usage for org.hibernate Query setFirstResult

List of usage examples for org.hibernate Query setFirstResult

Introduction

In this page you can find the example usage for org.hibernate Query setFirstResult.

Prototype

@Override
    Query<R> setFirstResult(int startPosition);

Source Link

Usage

From source file:com.ineunet.knife.persist.dao.support.PaginationDaoSupport.java

License:Apache License

@Override
public List<T> list(QueryParameters queryParameters, int start, int rows, String orderBy) {
    Map<String, Object> paramValues = new HashMap<String, Object>();
    String jql = this.prepareQuery(queryParameters, paramValues);
    if (orderBy != null && orderBy.trim().length() != 0)
        jql += " order by o." + orderBy;
    Query query = this.createQuery(jql, paramValues);
    query.setFirstResult(start);
    query.setMaxResults(rows);//from www  .  ja v  a2s . co m
    @SuppressWarnings("unchecked")
    List<T> list = query.list();
    return list;
}

From source file:com.ineunet.knife.persist.dao.support.PaginationDaoSupport.java

License:Apache License

@Override
public List<T> list(QueryParameters queryParameters) {
    QueryParamParser param = new QueryParamParser(queryParameters);
    int start = param.getStart();
    String orderBy = param.getOrderBy();
    int rows = param.getRows();

    Map<String, Object> paramValues = new HashMap<String, Object>();
    String jql = this.prepareQuery(queryParameters, paramValues);
    if (orderBy != null && orderBy.trim().length() != 0)
        jql += " order by o." + orderBy;
    Query query = this.createQuery(jql, paramValues);
    query.setFirstResult(start);
    query.setMaxResults(rows);//from www.  j  a  va2 s  . com
    @SuppressWarnings("unchecked")
    List<T> list = query.list();
    return list;
}

From source file:com.iqtb.validacion.managedbean.MbCfdisEmitidos.java

@PostConstruct
public void init() {
    lazyDataCfdis = new LazyDataModel<AuxCfdiEmitido>() {
        @Override/*from w w w.  j  av  a  2s.c  om*/
        public List<AuxCfdiEmitido> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                Map<String, String> filters) {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            listaCfdiE = new ArrayList<AuxCfdiEmitido>();
            listaCfdi = new ArrayList<Cfdis>();
            try {
                queryCount = "select count(idCfdis) ";
                query = "from Cfdis c, Sucursales s where c.idSucursal = s.idSucursal and s.idEmpresa = "
                        + empresa.getIdEmpresa();
                if (!filters.isEmpty()) {
                    Iterator it = filters.keySet().iterator();
                    while (it.hasNext()) {
                        filterProperty = (String) it.next();
                        filterValue = filters.get(filterProperty);
                        if (filterProperty.equals("folio")) {
                            query = query + " and c." + filterProperty + " = " + filterValue;
                        }
                        if (filterProperty.equals("serie") || filterProperty.equals("estado")) {
                            query = query + " and c." + filterProperty + " = '" + filterValue + "'";
                        }
                        if (filterProperty.equals("fecha") || filterProperty.equals("rfcEmisor")
                                || filterProperty.equals("rfcReceptor")) {
                            query = query + " and c." + filterProperty + " like '" + filterValue + "%'";
                        }
                        if (filterProperty.equals("rfcSocioComercial")) {
                            listaIdSocio = daoSociosComerciales
                                    .filtroSocioComercialByRFC(empresa.getIdEmpresa(), filterValue);
                            if (listaIdSocio != null) {
                                boolean isFirst = true;
                                for (Integer idSocio : listaIdSocio) {
                                    if (isFirst) {
                                        query = query + " and (c.idSocioComercial = " + idSocio;
                                        isFirst = false;
                                    } else {
                                        query = query + " or c.idSocioComercial = " + idSocio;
                                    }
                                }
                                query = query + ")";
                            }
                        }
                        if (filterProperty.equals("nombreSucursal")) {
                            listaidSucursal = daoSucursales.listaIdSucursalByNombre(empresa.getIdEmpresa(),
                                    filterValue);
                            if (listaidSucursal != null) {
                                boolean isFirst = true;
                                for (Integer idSucursal : listaidSucursal) {
                                    if (isFirst) {
                                        query = query + " and (c.idSucursal = " + idSucursal;
                                    } else {
                                        query = query + " or c.idSucursal = " + idSucursal;
                                    }
                                }
                                query = query + ")";
                            }
                        }
                    }
                }
                if (sortField != null && !sortField.isEmpty()) {
                    if (sortOrder.equals(SortOrder.ASCENDING)) {
                        query = query + " ORDER BY c." + sortField + " ASC";
                    } else {
                        query = query + " ORDER BY c." + sortField + " DESC";
                    }
                } else {
                    query = query + " ORDER BY c.fecha DESC";
                }
                queryCount = queryCount + query;
                Query result = session.createQuery("select c " + query);
                result.setFirstResult(first);
                result.setMaxResults(pageSize + first);
                this.setRowCount(((Long) session.createQuery(queryCount).uniqueResult()).intValue());
                listaCfdi = result.list();
                tx.commit();
                if (listaCfdi.isEmpty()) {
                    logger.info("No existen CFDI Emitidos para mostar");
                } else {
                    for (Cfdis cfdi : listaCfdi) {
                        cfdiE = new AuxCfdiEmitido();
                        cfdiE.setIdCfdis(cfdi.getIdCfdis());
                        cfdiE.setIdSucursal(cfdi.getIdSucursal());
                        cfdiE.setIdSocioComercial(cfdi.getIdSocioComercial());
                        cfdiE.setSerie(cfdi.getSerie());
                        cfdiE.setFolio(cfdi.getFolio());
                        cfdiE.setUuid(cfdi.getUuid());
                        cfdiE.setFecha(cfdi.getFecha());
                        cfdiE.setNumeroAprobacion(cfdi.getNumeroAprobacion());
                        cfdiE.setAnioAprobacion(cfdi.getAnioAprobacion());
                        cfdiE.setSubtotal(cfdi.getSubtotal());
                        cfdiE.setDescuento(cfdi.getDescuento());
                        cfdiE.setTotal(cfdi.getTotal());
                        cfdiE.setTipoMoneda(cfdi.getTipoMoneda());
                        cfdiE.setTipoCambio(cfdi.getTipoCambio());
                        cfdiE.setIva(cfdi.getIva());
                        cfdiE.setEstadoFiscal(cfdi.getEstadoFiscal());
                        cfdiE.setEstado(cfdi.getEstado());
                        cfdiE.setFechaCancelacion(cfdi.getFechaCancelacion());
                        cfdiE.setFechaModificacion(cfdi.getFechaModificacion());
                        cfdiE.setEstadoImpresion(cfdi.getEstadoImpresion());
                        cfdiE.setPedimento(cfdi.getPedimento());
                        cfdiE.setFechaPedimento(cfdi.getFechaPedimento());
                        cfdiE.setAduana(cfdi.getAduana());
                        cfdiE.setGenerationDate(cfdi.getGenerationDate());
                        cfdiE.setTicket(cfdi.getTicket());
                        cfdiE.setEstadoContable(cfdi.getEstadoContable());
                        cfdiE.setFechaVencimiento(cfdi.getFechaVencimiento());
                        cfdiE.setRfcEmisor(cfdi.getRfcEmisor());
                        cfdiE.setRfcReceptor(cfdi.getRfcReceptor());
                        cfdiE.setNombreSocioComercial(
                                daoSociosComerciales.getNombreSociobyIdSocio(cfdi.getIdSocioComercial()));
                        cfdiE.setNombreSucursal(daoSucursales.nombreSucursalByIdSucursal(cfdi.getIdSucursal()));
                        listaCfdiE.add(cfdiE);
                    }
                }
            } catch (HibernateException he) {
                descBitacora = "[CFDI_EMITIDO - init] HibernateException ERROR: " + he.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
                tx.rollback();
            } catch (Exception ex) {
                descBitacora = "[CFDI_EMITIDO - init] ERROR: " + ex.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
            } finally {
                if (session.isOpen()) {
                    session.clear();
                    session.close();
                }
            }
            return listaCfdiE;
        }
    };
}

From source file:com.iqtb.validacion.managedbean.MbCfdisRecibidos.java

@PostConstruct
public void init() {
    cfdiDataList = new LazyDataModel<AuxCfdiRecibido>() {

        @Override/*w  w w .ja  v  a2 s .  c o m*/
        public List<AuxCfdiRecibido> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                Map<String, String> filters) {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            lista = new ArrayList<AuxCfdiRecibido>();
            rfcByIdSocio = null;
            try {
                queryCount = "select count(idCfdiRecibido) ";
                query = "from CfdisRecibidos where idEmpresa = " + empresa.getIdEmpresa()
                        + " and (estado != 'ELIMINADO' and estado != 'CANCELADO')";
                if (rolUsuario.getTipo().equals(RolTipo.SOCIO_COMERCIAL.name())) {
                    query = query + " and idSocioComercial = " + usuario.getIdSocioComercial();
                }

                if (!filters.isEmpty()) {
                    it = filters.keySet().iterator();
                    while (it.hasNext()) {
                        filterProperty = (String) it.next();
                        filterValue = filters.get(filterProperty);
                        if (filterProperty.equals("folio")) {
                            query = query + " and " + filterProperty + " = " + filterValue;
                        }
                        if (filterProperty.equals("serie") || filterProperty.equals("estado")) {
                            query = query + " and " + filterProperty + " = '" + filterValue + "'";
                        }
                        if (filterProperty.equals("fecha") || filterProperty.equals("fechaRecepcion")) {
                            query = query + " and " + filterProperty + " like '" + filterValue + "%'";
                        }
                        if (filterProperty.equals("rfcSocioComercial")) {
                            rfcByIdSocio = daoSociosComerciales
                                    .filtroSocioComercialByRFC(empresa.getIdEmpresa(), filterValue);
                            if (rfcByIdSocio != null) {
                                boolean isFirst = true;
                                for (Integer idSocio : rfcByIdSocio) {
                                    if (isFirst) {
                                        query = query + " and (idSocioComercial = " + idSocio;
                                        isFirst = false;
                                    } else {
                                        query = query + " or idSocioComercial = " + idSocio;
                                    }
                                }
                                query = query + ")";
                            }
                            rfcByIdSocio = null;
                        }
                        if (filterProperty.equals("nombreSocioComercial")) {
                            rfcByIdSocio = daoSociosComerciales
                                    .filtroSocioComercialByNombre(empresa.getIdEmpresa(), filterValue);
                            if (rfcByIdSocio != null) {
                                boolean isFirst = true;
                                for (Integer idSocio : rfcByIdSocio) {
                                    if (isFirst) {
                                        query = query + " and (idSocioComercial = " + idSocio;
                                        isFirst = false;
                                    } else {
                                        query = query + " or idSocioComercial = " + idSocio;
                                    }
                                }
                                query = query + ")";
                            }
                            rfcByIdSocio = null;
                        }
                    }
                }

                if (sortField != null && !sortField.isEmpty()) {
                    if (sortOrder.equals(SortOrder.ASCENDING)) {
                        query = query + " ORDER BY " + sortField + " ASC";
                    } else {
                        query = query + " ORDER BY " + sortField + " DESC";
                    }
                } else {
                    query = query + " ORDER BY fechaRecepcion DESC";
                }

                queryCount = queryCount + query;
                Query result = session.createQuery(
                        "select new CfdisRecibidos(idCfdiRecibido, idSocioComercial, serie, folio, fecha, fechaRecepcion, estado, tiposWarn) "
                                + query);
                result.setFirstResult(first);
                result.setMaxResults(pageSize + first);
                this.setRowCount(((Long) session.createQuery(queryCount).uniqueResult()).intValue());
                listaCFDIs = result.list();
                tx.commit();
                if (listaCFDIs == null || listaCFDIs.size() <= 0) {
                    logger.info("listaCfdisCriteria - No existen CfdisRecibidos para mostrar");
                    return lista;
                }
                for (CfdisRecibidos item : listaCFDIs) {
                    auxCfdiR = new AuxCfdiRecibido();
                    auxCfdiR.setIdCfdiRecibido(item.getIdCfdiRecibido());
                    auxCfdiR.setIdSocioComercial(item.getIdSocioComercial());
                    auxCfdiR.setSerie(item.getSerie());
                    auxCfdiR.setFolio(item.getFolio());
                    auxCfdiR.setFecha(item.getFecha());
                    auxCfdiR.setFechaRecepcion(item.getFechaRecepcion());
                    auxCfdiR.setEstado(item.getEstado());
                    auxCfdiR.setTiposWarn(item.getTiposWarn());
                    auxCfdiR.setRfcSocioComercial(
                            daoSociosComerciales.getRFCSociobyIdSocio(item.getIdSocioComercial()));
                    auxCfdiR.setNombreSocioComercial(
                            daoSociosComerciales.getNombreSociobyIdSocio(item.getIdSocioComercial()));
                    lista.add(auxCfdiR);
                }
            } catch (HibernateException he) {
                descBitacora = "[CFDI_RECIBIDO - init] " + usuario.getUserid()
                        + " cfdiDataList HibernateException ERROR " + he.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
                tx.rollback();
            } catch (Exception ex) {
                descBitacora = "[CFDI_RECIBIDO - init] " + usuario.getUserid() + " cfdiDataList ERROR "
                        + ex.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
            } finally {
                listaCFDIs = null;
                if (session.isOpen()) {
                    session.clear();
                    session.close();
                }
            }
            return lista;
        }
    };

    lazyCfdisEliminados = new LazyDataModel<AuxCfdiRecibido>() {

        @Override
        public List<AuxCfdiRecibido> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                Map<String, String> filters) {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            listaE = new ArrayList<AuxCfdiRecibido>();
            rfcByIdSocio = null;
            try {
                queryCount = "select count(idCfdiRecibido) ";
                query = "from CfdisRecibidos where idEmpresa = " + empresa.getIdEmpresa()
                        + " and (estado ='ELIMINADO' or estado ='CANCELADO')";
                if (rolUsuario.getTipo().equals(RolTipo.SOCIO_COMERCIAL.name())) {
                    query = query + " and idSocioComercial = " + usuario.getIdSocioComercial();
                }

                if (!filters.isEmpty()) {
                    it = filters.keySet().iterator();
                    while (it.hasNext()) {
                        filterProperty = (String) it.next();
                        filterValue = filters.get(filterProperty);
                        if (filterProperty.equals("folio")) {
                            query = query + " and " + filterProperty + " = " + filterValue;
                        }
                        if (filterProperty.equals("serie") || filterProperty.equals("estado")) {
                            query = query + " and " + filterProperty + " = '" + filterValue + "'";
                        }
                        if (filterProperty.equals("fecha") || filterProperty.equals("fechaRecepcion")) {
                            query = query + " and " + filterProperty + " like '" + filterValue + "%'";
                        }
                        if (filterProperty.equals("rfcSocioComercial")) {
                            rfcByIdSocio = daoSociosComerciales
                                    .filtroSocioComercialByRFC(empresa.getIdEmpresa(), filterValue);
                            if (rfcByIdSocio != null) {
                                boolean isFirst = true;
                                for (Integer idSocio : rfcByIdSocio) {
                                    if (isFirst) {
                                        query = query + " and (idSocioComercial = " + idSocio;
                                        isFirst = false;
                                    } else {
                                        query = query + " or idSocioComercial = " + idSocio;
                                    }
                                }
                                query = query + ")";
                            }
                            rfcByIdSocio = null;
                        }
                        if (filterProperty.equals("nombreSocioComercial")) {
                            rfcByIdSocio = daoSociosComerciales
                                    .filtroSocioComercialByNombre(empresa.getIdEmpresa(), filterValue);
                            if (rfcByIdSocio != null) {
                                boolean isFirst = true;
                                for (Integer idSocio : rfcByIdSocio) {
                                    if (isFirst) {
                                        query = query + " and (idSocioComercial = " + idSocio;
                                        isFirst = false;
                                    } else {
                                        query = query + " or idSocioComercial = " + idSocio;
                                    }
                                }
                                query = query + ")";
                            }
                            rfcByIdSocio = null;
                        }
                    }
                }

                if (sortField != null && !sortField.isEmpty()) {
                    if (sortOrder.equals(SortOrder.ASCENDING)) {
                        query = query + " ORDER BY " + sortField + " ASC";
                    } else {
                        query = query + " ORDER BY " + sortField + " DESC";
                    }
                } else {
                    query = query + " ORDER BY fechaRecepcion DESC";
                }

                queryCount = queryCount + query;
                Query result = session.createQuery(
                        "select new CfdisRecibidos(idCfdiRecibido, idSocioComercial, serie, folio, fecha, fechaRecepcion, estado, tiposWarn) "
                                + query);
                result.setFirstResult(first);
                result.setMaxResults(pageSize + first);
                this.setRowCount(((Long) session.createQuery(queryCount).uniqueResult()).intValue());
                listaCFDIsE = result.list();
                tx.commit();
                if (listaCFDIsE == null || listaCFDIsE.size() <= 0) {
                    logger.info("listaCfdisCriteriaE - No existen CfdisRecibidos para mostrar");
                    return listaE;
                }
                for (CfdisRecibidos item : listaCFDIsE) {
                    auxCfdiR = new AuxCfdiRecibido();
                    auxCfdiR.setIdCfdiRecibido(item.getIdCfdiRecibido());
                    auxCfdiR.setIdSocioComercial(item.getIdSocioComercial());
                    auxCfdiR.setSerie(item.getSerie());
                    auxCfdiR.setFolio(item.getFolio());
                    auxCfdiR.setFecha(item.getFecha());
                    auxCfdiR.setFechaRecepcion(item.getFechaRecepcion());
                    auxCfdiR.setEstado(item.getEstado());
                    auxCfdiR.setError(item.getError());
                    auxCfdiR.setRfcSocioComercial(
                            daoSociosComerciales.getRFCSociobyIdSocio(item.getIdSocioComercial()));
                    auxCfdiR.setNombreSocioComercial(
                            daoSociosComerciales.getNombreSociobyIdSocio(item.getIdSocioComercial()));
                    listaE.add(auxCfdiR);
                }
            } catch (HibernateException he) {
                descBitacora = "[CFDI_RECIBIDO - init] " + usuario.getUserid()
                        + " lazyCfdisEliminados HibernateException ERROR " + he.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
                tx.rollback();
            } catch (Exception ex) {
                descBitacora = "[CFDI_RECIBIDO - init] " + usuario.getUserid() + " lazyCfdisEliminados ERROR "
                        + ex.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
            } finally {
                listaCFDIsE = null;
                if (session.isOpen()) {
                    session.clear();
                    session.close();
                }
            }
            return listaE;
        }
    };
}

From source file:com.iqtb.validacion.managedbean.MbContabilidadElectronica.java

@PostConstruct
public void init() {
    modelDataList = new LazyDataModel<ContabilidadElectronica>() {

        @Override/*from   w  w w  .  ja  v  a  2  s.c om*/
        public List<ContabilidadElectronica> load(int first, int pageSize, String sortField,
                SortOrder sortOrder, Map<String, String> filters) {

            listaContabilidad = new ArrayList<ContabilidadElectronica>();
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            try {
                queryCount = "select count(idContabilidad) ";
                query = "from ContabilidadElectronica where idEmpresa = " + empresa.getIdEmpresa();
                if (!filters.isEmpty()) {
                    it = filters.keySet().iterator();
                    while (it.hasNext()) {
                        filterProperty = (String) it.next();
                        filterValue = filters.get(filterProperty);
                        if (filterProperty.equals("anio")) {
                            query = query + " and " + filterProperty + " = " + filterValue;
                        }
                        if (filterProperty.equals("tipoArchivo") || filterProperty.equals("estado")
                                || filterProperty.equals("mes")) {
                            query = query + " and " + filterProperty + " = '" + filterValue + "'";
                        }
                        if (filterProperty.equals("fechaRecepcion") || filterProperty.equals("nombreRecibido")
                                || filterProperty.equals("nombreGenerado")) {
                            query = query + " and " + filterProperty + " like '" + filterValue + "%'";
                        }
                    }
                }
                if (sortField != null && !sortField.isEmpty()) {
                    if (sortOrder.equals(SortOrder.ASCENDING)) {
                        query = query + " ORDER BY " + sortField + " ASC";
                    } else {
                        query = query + " ORDER BY " + sortField + " DESC";
                    }
                } else {
                    query = query + " ORDER BY fechaRecepcion DESC";
                }

                queryCount = queryCount + query;
                Query result = session.createQuery(
                        "select new ContabilidadElectronica(idContabilidad, estado, tipoArchivo, nombreRecibido, nombreGenerado, fechaRecepcion, mes, anio) "
                                + query);
                result.setFirstResult(first);
                result.setMaxResults(pageSize + first);
                this.setRowCount(((Long) session.createQuery(queryCount).uniqueResult()).intValue());
                listaContabilidad = result.list();
                tx.commit();

            } catch (HibernateException he) {
                descBitacora = "[CONT_ELECTRONICA - init] " + usuario.getUserid() + " HibernateException ERROR "
                        + he.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
                tx.rollback();
            } finally {
                if (session.isOpen()) {
                    session.clear();
                    session.close();
                }
            }
            return listaContabilidad;
        }
    };
}

From source file:com.iqtb.validacion.managedbean.MbOrdenCompra.java

@PostConstruct
public void init() {
    lazyDataOrden = new LazyDataModel<AuxOrdenCompra>() {
        @Override/*from  ww  w .ja v a 2  s .c  om*/
        public List<AuxOrdenCompra> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                Map<String, String> filters) {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            listaAuxOrden = new ArrayList<AuxOrdenCompra>();
            listaOrden = new ArrayList<OrdenesCompra>();
            try {
                queryCount = "select count(idOrdenCompra) ";
                query = "from OrdenesCompra where idEmpresa = " + empresa.getIdEmpresa();
                if (!filters.isEmpty()) {
                    Iterator it = filters.keySet().iterator();
                    while (it.hasNext()) {
                        filterProperty = (String) it.next();
                        filterValue = filters.get(filterProperty);
                        if (filterProperty.equals("numeroOc")) {
                            query = query + " and " + filterProperty + " = '" + filterValue + "'";
                        }
                        if (filterProperty.equals("rfcVendedor") || filterProperty.equals("total")
                                || filterProperty.equals("tipoCambio") || filterProperty.equals("tipoMoneda")) {
                            query = query + " and " + filterProperty + " like '" + filterValue + "%'";
                        }
                        if (filterProperty.equals("rfcSocioComercial")) {
                            listaIdSocio = daoSociosComerciales
                                    .filtroSocioComercialByRFC(empresa.getIdEmpresa(), filterValue);
                            for (Integer idSocio : listaIdSocio) {
                                query = query + " and idSocioComercial = " + idSocio;
                            }
                        }
                    }
                }
                if (sortField != null && !sortField.isEmpty()) {
                    if (sortOrder.equals(SortOrder.ASCENDING)) {
                        query = query + " ORDER BY " + sortField + " ASC";
                    } else {
                        query = query + " ORDER BY " + sortField + " DESC";
                    }
                } else {
                    query = query + " ORDER BY idOrdenCompra DESC";
                }
                queryCount = queryCount + query;
                Query result = session.createQuery(query);
                result.setMaxResults(pageSize + first);
                result.setFirstResult(first);
                this.setRowCount(((Long) session.createQuery(queryCount).uniqueResult()).intValue());
                listaOrden = result.list();
                tx.commit();
                if (listaOrden.isEmpty()) {
                    logger.info("No existen Ordenes de Compra para mostar");
                } else {
                    for (OrdenesCompra ordenCompra : listaOrden) {
                        auxOrdenCompra = new AuxOrdenCompra();
                        auxOrdenCompra.setIdOrdenCompra(ordenCompra.getIdOrdenCompra());
                        auxOrdenCompra.setIdEmpresa(ordenCompra.getIdEmpresa());
                        auxOrdenCompra.setIdSocioComercial(ordenCompra.getIdSocioComercial());
                        auxOrdenCompra.setNumeroOc(ordenCompra.getNumeroOc());
                        auxOrdenCompra.setTotal(ordenCompra.getTotal());
                        auxOrdenCompra.setTipoMoneda(ordenCompra.getTipoMoneda());
                        auxOrdenCompra.setTipoCambio(ordenCompra.getTipoCambio());
                        auxOrdenCompra.setRfcVendedor(ordenCompra.getRfcVendedor());
                        auxOrdenCompra.setRfcSocioComercial(
                                daoSociosComerciales.getRFCSociobyIdSocio(ordenCompra.getIdSocioComercial()));
                        listaAuxOrden.add(auxOrdenCompra);
                    }
                }
            } catch (HibernateException he) {
                descBitacora = "[ORDEN_COMPRA - init] HibernateException ERROR: " + he.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
                tx.rollback();
            } catch (Exception ex) {
                descBitacora = "[ORDEN_COMPRA - init] ERROR: " + ex.getMessage();
                registrarBitacora(usuario.getIdUsuario(), null, empresa.getIdEmpresa(), descBitacora,
                        BitacoraTipo.ERROR.name());
                logger.error(descBitacora);
            } finally {
                if (session.isOpen()) {
                    session.clear();
                    session.close();
                }
            }
            return listaAuxOrden;
        }
    };
}

From source file:com.iqtb.validacion.managedbean.MbOrdenCompra.java

public void dataLazyCfdi() {
    lazyDataCfdi = new LazyDataModel<CfdisRecibidos>() {
        @Override//ww w.  j  av a  2s.  c o  m
        public List<CfdisRecibidos> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                Map<String, String> filters) {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            listaCFDIs = new ArrayList<CfdisRecibidos>();
            try {
                queryCount = "select count(idCfdiRecibido) ";
                query = "from CfdisRecibidos where idEmpresa = " + empresa.getIdEmpresa()
                        + " and idOrdenCompra = " + auxOrdenCompra.getIdOrdenCompra();
                if (!filters.isEmpty()) {
                    Iterator it = filters.keySet().iterator();
                    while (it.hasNext()) {
                        filterProperty = (String) it.next();
                        filterValue = filters.get(filterProperty);
                        if (filterProperty.equals("serie") || filterProperty.equals("estado")) {
                            query = query + " and " + filterProperty + " = '" + filterValue + "'";
                        }
                        if (filterProperty.equals("folio")) { //|| filterProperty.equals("total")
                            query = query + " and " + filterProperty + " = " + filterValue;
                        }
                        if (filterProperty.equals("fecha") || filterProperty.equals("fechaRecepcion")
                                || filterProperty.equals("total")) {
                            query = query + " and " + filterProperty + " like '" + filterValue + "%'";
                        }
                    }
                }
                if (sortField != null && !sortField.isEmpty()) {
                    if (sortOrder.equals(SortOrder.ASCENDING)) {
                        query = query + " ORDER BY " + sortField + " ASC";
                    } else {
                        query = query + " ORDER BY " + sortField + " DESC";
                    }
                } else {
                    query = query + " ORDER BY fechaRecepcion DESC";
                }
                queryCount = queryCount + query;
                Query result = session.createQuery(
                        "select new CfdisRecibidos(idCfdiRecibido, idSocioComercial, serie, folio, fecha, total, fechaRecepcion, estado, tiposWarn)"
                                + query);
                this.setRowCount(((Long) session.createQuery(queryCount).uniqueResult()).intValue());
                result.setFirstResult(first);
                result.setMaxResults(pageSize + first);
                listaCFDIs = result.list();
            } catch (HibernateException e) {
                logger.error(usuario.getUserid() + " dataLazyCfdi ERROR " + e);
            } finally {
                if (session.isOpen()) {
                    session.clear();
                    session.close();
                }
            }
            return listaCFDIs;
        }
    };
}

From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java

License:Open Source License

/**
 * QL //from   ww w  . j a  va 2  s .  c  om
 * 
 * @param page
 * @param qlString
 * @param parameter
 * @return
 */
@SuppressWarnings("unchecked")
public <E> Page<E> find(Page<E> page, String qlString, Object... parameter) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countQlString = "select count(*) " + removeSelect(removeOrders(qlString));
        // page.setCount(Long.valueOf(createQuery(countQlString,
        // parameter).uniqueResult().toString()));
        Query query = createQuery(countQlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String ql = qlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        ql += " order by " + page.getOrderBy();
    }
    Query query = createQuery(ql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    page.setList(query.list());
    return page;
}

From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java

License:Open Source License

/**
 * SQL /*from ww  w.  ja va 2 s  .  c o  m*/
 * 
 * @param page
 * @param sqlString
 * @param resultClass
 * @param parameter
 * @return
 */
@SuppressWarnings("unchecked")
public <E> Page<E> findBySql(Page<E> page, String sqlString, Class<?> resultClass, Object... parameter) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countSqlString = "select count(*) " + removeSelect(removeOrders(sqlString));
        // page.setCount(Long.valueOf(createSqlQuery(countSqlString,
        // parameter).uniqueResult().toString()));
        Query query = createSqlQuery(countSqlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String sql = sqlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        sql += " order by " + page.getOrderBy();
    }
    SQLQuery query = createSqlQuery(sql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    setResultTransformer(query, resultClass);
    page.setList(query.list());
    return page;
}

From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public <E> Page<E> findBySqlString(Page<E> page, String sqlString, Parameter parameter, Class[] resultClass,
        Class returnVO) {/*from w w  w .  j a va 2s  .c  om*/
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String select = StringHelper.isNull(page.getCountDistinctColumn()) ? "select count(*) "
                : "select count(distinct " + page.getCountDistinctColumn() + ") ";
        String countSqlString = select + removeSelect(removeOrders(sqlString));
        // page.setCount(Long.valueOf(createSqlQuery(countSqlString,
        // parameter).uniqueResult().toString()));
        Query query = createSqlQuery(countSqlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    String sql = sqlString;
    // group by
    if (StringUtils.isNotBlank(page.getGroupBy())) {
        sql += " group by " + page.getGroupBy();
    }
    // order by
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        sql += " order by " + page.getOrderBy();
    }
    SQLQuery query = createSqlQuery(sql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }

    boolean isMutilReturn = false;
    if (resultClass != null && resultClass.length == 1)
        setResultTransformer(query, resultClass[0]);
    else if (resultClass != null && resultClass.length > 1) {
        setResultTransformer(query, resultClass);
        isMutilReturn = true;
    }
    List l = query.list();

    page.setList(query.list());

    if (isMutilReturn)
        setResultReturnVO(returnVO, page);

    return page;
}