Example usage for org.hibernate Query setMaxResults

List of usage examples for org.hibernate Query setMaxResults

Introduction

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

Prototype

@Override
    Query<R> setMaxResults(int maxResult);

Source Link

Usage

From source file:com.abiquo.server.core.infrastructure.network.IpPoolManagementDAO.java

License:Open Source License

public List<IpPoolManagement> findPublicIpsByVlan(final Integer datacenterId, final Integer vlanId,
        Integer startwith, final Integer limit, final String filter, final OrderByEnum orderByEnum,
        final Boolean descOrAsc, final Boolean all) {
    Query finalQuery = getSession().createQuery(
            BY_PUBLIC_VLAN + " " + defineAllFilter(all) + " " + defineOrderBy(orderByEnum, descOrAsc));
    finalQuery.setParameter("datacenter_id", datacenterId);
    finalQuery.setParameter("vlan_id", vlanId);
    finalQuery.setParameter("filterLike", filter == null || filter.isEmpty() ? "%" : "%" + filter + "%");

    // Check if the page requested is bigger than the last one
    Integer totalResults = finalQuery.list().size();

    if (limit != null) {
        finalQuery.setMaxResults(limit);
    }//  ww  w  .j a  v a2 s. co  m

    if (startwith >= totalResults) {
        startwith = totalResults - limit;
    }
    finalQuery.setFirstResult(startwith);
    finalQuery.setMaxResults(limit);

    PagedList<IpPoolManagement> ipList = new PagedList<IpPoolManagement>(finalQuery.list());
    ipList.setTotalResults(totalResults);
    ipList.setPageSize(limit);
    ipList.setCurrentElement(startwith);

    return ipList;
}

From source file:com.abiquo.server.core.infrastructure.network.IpPoolManagementDAO.java

License:Open Source License

public List<IpPoolManagement> findpublicIpsPurchasedByVirtualDatacenter(final Integer vdcId,
        final Boolean onlyAvailabe, Integer startwith, final Integer limit, final String filter,
        final OrderByEnum orderByEnum, final Boolean descOrAsc) {
    Query finalQuery = getSession().createQuery(BY_VDC_PURCHASED + " " + defineOnlyAvailableFilter(onlyAvailabe)
            + defineOrderBy(orderByEnum, descOrAsc));
    finalQuery.setParameter("vdc_id", vdcId);
    finalQuery.setParameter("filterLike", filter == null || filter.isEmpty() ? "%" : "%" + filter + "%");

    // Check if the page requested is bigger than the last one
    Integer totalResults = finalQuery.list().size();

    if (limit != null) {
        finalQuery.setMaxResults(limit);
    }//from www.  jav a2 s .  c  o  m

    if (startwith >= totalResults) {
        startwith = totalResults - limit;
    }
    finalQuery.setFirstResult(startwith);
    finalQuery.setMaxResults(limit);

    PagedList<IpPoolManagement> ipList = new PagedList<IpPoolManagement>(finalQuery.list());
    ipList.setTotalResults(totalResults);
    ipList.setPageSize(limit);
    ipList.setCurrentElement(startwith);

    return ipList;

}

From source file:com.abiquo.server.core.infrastructure.network.IpPoolManagementDAO.java

License:Open Source License

public List<IpPoolManagement> findpublicIpsToPurchaseByVirtualDatacenter(final Integer vdcId, Integer startwith,
        final Integer limit, final String filter, final OrderByEnum orderByEnum, final Boolean descOrAsc) {
    Query finalQuery = getSession()
            .createQuery(BY_VDC_TO_PURCHASE + " " + defineOrderBy(orderByEnum, descOrAsc));
    finalQuery.setParameter("vdc_id", vdcId);
    finalQuery.setParameter("filterLike", filter == null || filter.isEmpty() ? "%" : "%" + filter + "%");

    // Check if the page requested is bigger than the last one
    Integer totalResults = finalQuery.list().size();

    if (limit != null) {
        finalQuery.setMaxResults(limit);
    }//w  w w  . j a v a  2s. c o  m

    if (startwith >= totalResults) {
        startwith = totalResults - limit;
    }
    finalQuery.setFirstResult(startwith);
    finalQuery.setMaxResults(limit);

    PagedList<IpPoolManagement> ipList = new PagedList<IpPoolManagement>(finalQuery.list());
    ipList.setTotalResults(totalResults);
    ipList.setPageSize(limit);
    ipList.setCurrentElement(startwith);

    return ipList;
}

From source file:com.abiquo.server.core.infrastructure.RackDAO.java

License:Open Source License

/**
 * Returns any machine that is in the rack in HALTED_FOR_SAVE. If howMany <= 0 an empty list is
 * returned.//from w w w.j a  v  a2 s.co  m
 * 
 * @param rackId rack.
 * @return Machine
 */
public List<Machine> getRandomMachinesToStartFromRack(final Integer rackId, final Integer howMany) {
    if (howMany <= 0) {
        return new ArrayList<Machine>();
    }
    Query q = getSession().createQuery(HQL_EMPTY_OFF_MACHINES_IN_RACK).setInteger("rackId", rackId);

    List<Machine> machines = q.setMaxResults(howMany).list();
    return machines;
}

From source file:com.abiquo.server.core.infrastructure.RackDAO.java

License:Open Source License

/**
 * Returns any machine that is in the rack in MANAGED. If howMany <= 0 an empty list is
 * returned./*from www . ja va2s  . co m*/
 * 
 * @param rackId rack.
 * @return Machine
 */
public List<Machine> getRandomMachinesToShutDownFromRack(final Integer rackId, final Integer howMany) {
    if (howMany <= 0) {
        return new ArrayList<Machine>();
    }
    Query q = getSession().createQuery(HQL_EMPTY_ON_MACHINES_IN_RACK).setInteger("rackId", rackId);

    List<Machine> machines = q.setMaxResults(howMany).list();
    return machines;
}

From source file:com.abiquo.server.core.infrastructure.storage.VolumeManagementDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<VolumeManagement> getVolumesByPool(final StoragePool sp, final FilterOptions filters) {
    // Check if the orderBy element is actually one of the available ones
    VolumeManagement.OrderByEnum orderByEnum = VolumeManagement.OrderByEnum
            .valueOf(filters.getOrderBy().toUpperCase());

    String orderBy = defineOrderBy(orderByEnum.getColumnHQL(), filters.getAsc());

    Query query = getSession().getNamedQuery(VolumeManagement.VOLUMES_BY_POOL);

    String req = query.getQueryString() + orderBy;
    // Add order filter to the query
    Query queryWithOrder = getSession().createQuery(req);
    queryWithOrder.setString("poolId", sp.getId());
    queryWithOrder.setString("filterLike",
            filters.getFilter().isEmpty() ? "%" : "%" + filters.getFilter() + "%");

    Integer size = queryWithOrder.list().size();

    queryWithOrder.setFirstResult(filters.getStartwith());
    queryWithOrder.setMaxResults(filters.getLimit());

    PagedList<VolumeManagement> volumesList = new PagedList<VolumeManagement>(queryWithOrder.list());
    volumesList.setTotalResults(size);/*from ww  w  .ja  v a 2  s . c  o m*/
    volumesList.setPageSize(filters.getLimit() > size ? size : filters.getLimit());
    volumesList.setCurrentElement(filters.getStartwith());

    return volumesList;
}

From source file:com.abiquo.server.core.infrastructure.storage.VolumeManagementDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<VolumeManagement> getVolumesByVirtualDatacenter(final VirtualDatacenter vdc,
        final FilterOptions filters) {
    // Check if the orderBy element is actually one of the available ones
    VolumeManagement.OrderByEnum orderByEnum = VolumeManagement.OrderByEnum
            .valueOf(filters.getOrderBy().toUpperCase());

    String orderBy = defineOrderBy(orderByEnum.getColumnHQL(), filters.getAsc());

    Query query = getSession().getNamedQuery(VolumeManagement.VOLUMES_BY_VDC);

    String req = query.getQueryString() + orderBy;
    // Add order filter to the query
    Query queryWithOrder = getSession().createQuery(req);
    queryWithOrder.setInteger("vdcId", vdc.getId());
    queryWithOrder.setString("filterLike",
            filters.getFilter().isEmpty() ? "%" : "%" + filters.getFilter() + "%");

    Integer size = queryWithOrder.list().size();

    queryWithOrder.setFirstResult(filters.getStartwith());
    queryWithOrder.setMaxResults(filters.getLimit());

    PagedList<VolumeManagement> volumesList = new PagedList<VolumeManagement>(queryWithOrder.list());
    volumesList.setTotalResults(size);//from  w w  w . j  a v a2  s  .c o m
    volumesList.setPageSize(filters.getLimit() > size ? size : filters.getLimit());
    volumesList.setCurrentElement(filters.getStartwith());

    return volumesList;
}

From source file:com.abiquo.server.core.infrastructure.storage.VolumeManagementDAO.java

License:Open Source License

public List<VolumeManagement> getVolumesByEnterprise(final Integer id, final FilterOptions filters) {
    // Check if the orderBy element is actually one of the available ones
    VolumeManagement.OrderByEnum orderByEnum = VolumeManagement.OrderByEnum
            .valueOf(filters.getOrderBy().toUpperCase());

    Query query = getSession().createSQLQuery(SQL_VOLUME_MANAGEMENT_GET_VOLUMES_FROM_ENTERPRISE
            + defineOrderBy(orderByEnum.getColumnSQL(), filters.getAsc()));
    query.setParameter("idEnterprise", id);
    query.setParameter("filterLike", filters.getFilter().isEmpty() ? "%" : "%" + filters.getFilter() + "%");

    Integer size = getSQLQueryResults(getSession(), query, VolumeManagement.class, 0).size();

    query.setFirstResult(filters.getStartwith());
    query.setMaxResults(filters.getLimit());
    PagedList<VolumeManagement> volumes = new PagedList<VolumeManagement>(
            getSQLQueryResults(getSession(), query, VolumeManagement.class, 0));
    volumes.setTotalResults(size);/*from   w  w  w  .j av a2  s .  c o m*/
    volumes.setPageSize(filters.getLimit() > size ? size : filters.getLimit());
    volumes.setCurrentElement(filters.getStartwith());

    return volumes;

}

From source file:com.abiquo.server.core.infrastructure.storage.VolumeManagementDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<VolumeManagement> getAvailableVolumes(final VirtualDatacenter vdc, final FilterOptions filters) {
    // Check if the orderBy element is actually one of the available ones
    VolumeManagement.OrderByEnum orderByEnum = VolumeManagement.OrderByEnum
            .valueOf(filters.getOrderBy().toUpperCase());

    String orderBy = defineOrderBy(orderByEnum.getColumnHQL(), filters.getAsc());

    Query query = getSession().getNamedQuery(VolumeManagement.VOLUMES_AVAILABLES);

    String req = query.getQueryString() + orderBy;
    // Add order filter to the query
    Query queryWithOrder = getSession().createQuery(req);
    queryWithOrder.setInteger("vdcId", vdc.getId());
    queryWithOrder.setString("filterLike",
            filters.getFilter().isEmpty() ? "%" : "%" + filters.getFilter() + "%");

    Integer size = queryWithOrder.list().size();

    // Limit 0 means no size filter
    if (filters.getLimit() == 0) {
        filters.setLimit(size);//from  w  w w.j a va 2  s.  c  om
        filters.setStartwith(0);
    }

    queryWithOrder.setFirstResult(filters.getStartwith());
    queryWithOrder.setMaxResults(filters.getLimit());

    PagedList<VolumeManagement> volumesList = new PagedList<VolumeManagement>(queryWithOrder.list());
    volumesList.setTotalResults(size);
    volumesList.setPageSize(filters.getLimit() > size ? size : filters.getLimit());
    volumesList.setCurrentElement(filters.getStartwith());

    return volumesList;
}

From source file:com.abiquo.server.core.infrastructure.storage.VolumeManagementDAO.java

License:Open Source License

public List<VolumeManagement> getVolumesByVirtualMachine(final VirtualMachine vm, final FilterOptions filters) {
    if (filters != null) {
        // Check if the orderBy element is actually one of the available ones
        VolumeManagement.OrderByEnum orderByEnum = VolumeManagement.OrderByEnum
                .valueOf(filters.getOrderBy().toUpperCase());

        String orderBy = defineOrderBy(orderByEnum.getColumnHQL(), filters.getAsc());

        Query query = getSession().getNamedQuery(VolumeManagement.VOLUMES_ATTACHED_TO_VM);

        String req = query.getQueryString() + orderBy;
        // Add order filter to the query
        Query queryWithOrder = getSession().createQuery(req);
        queryWithOrder.setInteger("vmId", vm.getId());
        queryWithOrder.setParameter("state", VolumeState.ATTACHED);
        queryWithOrder.setString("filterLike",
                filters.getFilter().isEmpty() ? "%" : "%" + filters.getFilter() + "%");

        Integer size = queryWithOrder.list().size();

        queryWithOrder.setFirstResult(filters.getStartwith());
        queryWithOrder.setMaxResults(filters.getLimit());

        PagedList<VolumeManagement> volumesList = new PagedList<VolumeManagement>(queryWithOrder.list());
        volumesList.setTotalResults(size);
        volumesList.setPageSize(filters.getLimit() > size ? size : filters.getLimit());
        volumesList.setCurrentElement(filters.getStartwith());

        return volumesList;
    } else {/* w  ww  .  j  av a2 s  . c  o  m*/
        Criteria criteria = createCriteria(sameVirtualMachine(vm), sameState(VolumeState.ATTACHED));
        return getResultList(criteria);
    }
}