Example usage for org.hibernate Criteria setFirstResult

List of usage examples for org.hibernate Criteria setFirstResult

Introduction

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

Prototype

public Criteria setFirstResult(int firstResult);

Source Link

Document

Set the first result to be retrieved.

Usage

From source file:br.com.webbudget.domain.model.repository.entries.ContactRepository.java

License:Open Source License

/**
 *
 * @param filter//from  ww w  .  j  a v  a 2  s  . c o  m
 * @param blocked
 * @param pageRequest
 * @return
 */
@Override
public Page<Contact> listLazilyByFilter(String filter, Boolean blocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (filter != null) {
        criteria.add(Restrictions.or(Restrictions.ilike("name", "%" + filter + "%"),
                Restrictions.ilike("email", "%" + filter + "%"), Restrictions.eq("document", filter),
                Restrictions.ilike("city", "%" + filter + "%")));
    }

    if (blocked != null) {
        criteria.add(Restrictions.eq("blocked", blocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.CostCenterRepository.java

License:Open Source License

/**
 * //from   ww w .j a v a  2 s .c  om
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<CostCenter> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.MovementClassRepository.java

License:Open Source License

/**
 * //from w w  w.j  a v a 2s  . co m
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<MovementClass> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.VehicleRepository.java

License:Open Source License

/**
 *
 * @param isBlocked//from  w  w  w .  j a va  2 s .  c om
 * @param pageRequest
 * @return
 */
@Override
public Page<Vehicle> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.WalletRepository.java

License:Open Source License

/**
 * /*  www.ja v a2 s  .  com*/
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<Wallet> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.financial.FixedMovementRepository.java

License:Open Source License

/**
 *
 * @param filter/*from   w w  w .jav a2s .  co  m*/
 * @param pageRequest
 * @return
 */
@Override
public Page<FixedMovement> listByFilter(String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    final List<Criterion> criterions = new ArrayList<>();

    // filtramos
    if (filter != null && !filter.isEmpty()) {

        criterions.add(Restrictions.ilike("description", "%" + filter + "%"));
        criterions.add(Restrictions.ilike("identification", "%" + filter + "%"));

        // se conseguir castar para bigdecimal trata como um filtro
        try {
            criterions.add(Restrictions.eq("value", new BigDecimal(filter)));
        } catch (NumberFormatException ex) {
        }
    }

    criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.financial.LaunchRepository.java

License:Open Source License

/**
 * /* w ww  . j  av a2s  . c om*/
 * @param fixedMovement
 * @param pageRequest
 * @return 
 */
@Override
public Page<Launch> listByFixedMovement(FixedMovement fixedMovement, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    criteria.createAlias("fixedMovement", "fm");
    criteria.add(Restrictions.eq("fm.id", fixedMovement.getId()));

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.financial.MovementRepository.java

License:Open Source License

/**
 *
 * @param filter/*  ww  w.j  a v a2s  .c  om*/
 * @param pageRequest
 * @return
 */
@Override
public Page<Movement> listByFilter(MovementFilter filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    final List<Criterion> criterions = new ArrayList<>();

    criteria.createAlias("contact", "co", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("apportionments", "ap");
    criteria.createAlias("ap.movementClass", "mc");
    criteria.createAlias("ap.costCenter", "cc");
    criteria.createAlias("financialPeriod", "fp");

    // montramos os criterios de filtragem geral
    if (filter.hasCriteria()) {
        criterions.add(Restrictions.eq("code", filter.getCriteria()));
        criterions.add(Restrictions.ilike("description", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("mc.name", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("cc.name", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("co.name", "%" + filter.getCriteria() + "%"));

        // se conseguir castar para bigdecimal trata como um filtro
        try {
            criterions.add(Restrictions.eq("value", filter.criteriaToBigDecimal()));
        } catch (ParseException ex) {
        }
    }

    criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));
    criteria.add(Restrictions.and(filter.getCustomFilters()));

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    // aplica o multisort dos campos
    if (pageRequest.isMultiSort()) {
        pageRequest.getMultiSortFields().stream().forEach(field -> {
            if (field.getDirection() == SortDirection.ASC) {
                criteria.addOrder(Order.asc(field.getSortField()));
            } else if (field.getDirection() == SortDirection.DESC) {
                criteria.addOrder(Order.desc(field.getSortField()));
            }
        });
    } else if (pageRequest.getSortDirection() == SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.logbook.RefuelingRepository.java

License:Open Source License

/**
 *
 * @param filter//from  w  w w .  j  a  v a 2 s.  c o m
 * @param pageRequest
 * @return
 */
@Override
public Page<Refueling> listLazily(String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (StringUtils.isNotBlank(filter)) {
        // TODO colocar os filtros aqui
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.miscellany.FinancialPeriodRepository.java

License:Open Source License

/**
 * //from   ww  w  .java  2 s .c  o m
 * @param isClosed
 * @param pageRequest
 * @return 
 */
@Override
public Page<FinancialPeriod> listByStatusLazily(Boolean isClosed, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isClosed != null) {
        criteria.add(Restrictions.eq("closed", isClosed));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}