Example usage for javax.persistence Query setMaxResults

List of usage examples for javax.persistence Query setMaxResults

Introduction

In this page you can find the example usage for javax.persistence Query setMaxResults.

Prototype

Query setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:io.apiman.manager.api.jpa.JpaStorage.java

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getApiVersions(java.lang.String, java.lang.String)
 *///from  w w  w. j  a v  a  2  s  .  co  m
@Override
public List<ApiVersionSummaryBean> getApiVersions(String orgId, String apiId) throws StorageException {
    beginTx();
    try {
        EntityManager entityManager = getActiveEntityManager();
        String jpql = "SELECT v " + "  FROM ApiVersionBean v" + "  JOIN v.api s" + "  JOIN s.organization o"
                + " WHERE o.id = :orgId" + "  AND s.id = :apiId" + " ORDER BY v.createdOn DESC";
        Query query = entityManager.createQuery(jpql);
        query.setMaxResults(500);
        query.setParameter("orgId", orgId);
        query.setParameter("apiId", apiId);

        List<ApiVersionBean> apiVersions = query.getResultList();
        List<ApiVersionSummaryBean> rval = new ArrayList<>(apiVersions.size());
        for (ApiVersionBean apiVersion : apiVersions) {
            ApiVersionSummaryBean svsb = new ApiVersionSummaryBean();
            svsb.setOrganizationId(apiVersion.getApi().getOrganization().getId());
            svsb.setOrganizationName(apiVersion.getApi().getOrganization().getName());
            svsb.setId(apiVersion.getApi().getId());
            svsb.setName(apiVersion.getApi().getName());
            svsb.setDescription(apiVersion.getApi().getDescription());
            svsb.setVersion(apiVersion.getVersion());
            svsb.setStatus(apiVersion.getStatus());
            svsb.setPublicAPI(apiVersion.isPublicAPI());
            rval.add(svsb);
        }
        return rval;
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        throw new StorageException(t);
    } finally {
        rollbackTx();
    }
}

From source file:io.apiman.manager.api.jpa.JpaStorage.java

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getClientVersions(java.lang.String, java.lang.String)
 *///w ww  .ja  va2  s. com
@Override
public List<ClientVersionSummaryBean> getClientVersions(String orgId, String clientId) throws StorageException {
    beginTx();
    try {
        EntityManager entityManager = getActiveEntityManager();
        String jpql = "SELECT v" + "  FROM ClientVersionBean v" + "  JOIN v.client a"
                + "  JOIN a.organization o" + " WHERE o.id = :orgId" + "   AND a.id = :clientId"
                + " ORDER BY v.createdOn DESC";
        Query query = entityManager.createQuery(jpql);
        query.setMaxResults(500);
        query.setParameter("orgId", orgId);
        query.setParameter("clientId", clientId);
        List<ClientVersionBean> clientVersions = query.getResultList();
        List<ClientVersionSummaryBean> rval = new ArrayList<>();
        for (ClientVersionBean clientVersion : clientVersions) {
            ClientVersionSummaryBean avsb = new ClientVersionSummaryBean();
            avsb.setOrganizationId(clientVersion.getClient().getOrganization().getId());
            avsb.setOrganizationName(clientVersion.getClient().getOrganization().getName());
            avsb.setId(clientVersion.getClient().getId());
            avsb.setName(clientVersion.getClient().getName());
            avsb.setDescription(clientVersion.getClient().getDescription());
            avsb.setVersion(clientVersion.getVersion());
            avsb.setApiKey(clientVersion.getApikey());
            avsb.setStatus(clientVersion.getStatus());

            rval.add(avsb);
        }
        return rval;
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        throw new StorageException(t);
    } finally {
        rollbackTx();
    }
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

public <T extends EntityBaseStandard, E extends AbstractFiltroJpa> List<T> findFilter(Class<T> classObj,
        E filtro) throws Exception {
    List<T> returnValue = null;

    EntityManagerFactory emf = null;//from w w w.  j  av a  2 s . c  o  m
    EntityManager em = null;
    Iterator i = null;
    Query q = null;
    try {
        /* Istanzia l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        StringBuilder hqlQuery = new StringBuilder();

        hqlQuery.append("select t from " + classObj.getName() + " t ");

        hqlQuery.append(" WHERE ");
        if (filtro != null && StringUtils.isNotEmpty(filtro.getSQLWhere())) {
            hqlQuery.append(filtro.getSQLWhere());
        }

        if (filtro != null) {
            hqlQuery.append(filtro.getSQLSort());
        }

        /* Crea la query */
        logger.debug(hqlQuery.toString());
        q = em.createQuery(hqlQuery.toString());

        /*
         * Se il numero di elementi e' diverso da 0 specifico quanti e da
         * dove cominciare
         */
        if (filtro != null && filtro.getRighePerPagina() > 0) {
            q.setMaxResults(filtro.getRighePerPagina());
            if (filtro.getPagina() > 0) {
                q.setFirstResult((filtro.getPagina() - 1) * filtro.getRighePerPagina());

            }
        }

        if (filtro != null && filtro.getListaParametri() != null) {
            for (Entry<String, Object> parametro : filtro.getListaParametri().entrySet()) {
                q.setParameter(parametro.getKey(), parametro.getValue());
            }
        }

        /* Calcolo la collezione di elementi desiderati */
        returnValue = q.getResultList();

    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
    }
    return returnValue;
}

From source file:io.apiman.manager.api.jpa.JpaStorage.java

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getPlansInOrgs(java.util.Set)
 *//*from  w  w  w .j a  v a2s. c  om*/
@Override
public List<PlanSummaryBean> getPlansInOrgs(Set<String> orgIds) throws StorageException {
    List<PlanSummaryBean> rval = new ArrayList<>();
    if (orgIds == null || orgIds.isEmpty()) {
        return rval;
    }
    beginTx();
    try {
        EntityManager entityManager = getActiveEntityManager();
        String jpql = "SELECT p FROM PlanBean p JOIN p.organization o WHERE o.id IN :orgs ORDER BY p.id ASC";
        Query query = entityManager.createQuery(jpql);
        query.setParameter("orgs", orgIds);
        query.setMaxResults(500);

        List<PlanBean> qr = query.getResultList();
        for (PlanBean bean : qr) {
            PlanSummaryBean summary = new PlanSummaryBean();
            summary.setId(bean.getId());
            summary.setName(bean.getName());
            summary.setDescription(bean.getDescription());
            OrganizationBean org = bean.getOrganization();
            summary.setOrganizationId(org.getId());
            summary.setOrganizationName(org.getName());
            rval.add(summary);
        }
        return rval;
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        throw new StorageException(t);
    } finally {
        rollbackTx();
    }
}

From source file:org.medici.bia.dao.user.UserDAOJpaImpl.java

/**
 * {@inheritDoc}/*w  w  w .  ja  va2 s .  co  m*/
 */
@Override
public Page findUsers(User user, Set<Authority> authorities, boolean searchAuthorities,
        PaginationFilter paginationFilter) {
    boolean authFilter = authorities != null && authorities.size() > 0;
    Page page = new Page(paginationFilter);
    Query query = null;
    String jpql = "FROM User ";
    String condition = getConditionOnUser(authFilter ? "user" : null, user);

    if (authFilter) {
        jpql += "AS user, UserRole AS role ";
    }

    if (!StringUtils.equals(condition, "")) {
        jpql = jpql + "WHERE " + condition;
    }

    if (authFilter) {
        if (!jpql.contains("WHERE")) {
            jpql += "WHERE ";
        } else {
            jpql += " AND ";
        }
        jpql += "user = role.user AND ";
        if (searchAuthorities) {
            jpql += "role.userAuthority.authority IN (:authorities)";
        } else {
            jpql += "NOT EXISTS (SELECT r FROM UserRole AS r WHERE r.user = user AND r.userAuthority.authority IN (:authorities))";
        }
    }

    if (paginationFilter.getTotal() == null) {
        String countQuery = (authFilter ? "SELECT COUNT(user) " : "SELECT COUNT(*) ") + jpql;
        query = getEntityManager().createQuery(countQuery);
        if (authFilter) {
            query.setParameter("authorities", authorities);
        }
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery((authFilter ? "SELECT DISTINCT user " : "") + jpql
            + getOrderByQuery(paginationFilter.getSortingCriterias()));

    if (authFilter) {
        query.setParameter("authorities", authorities);
    }

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

From source file:io.apiman.manager.api.jpa.JpaStorage.java

@Override
public Iterator<ClientVersionBean> getAllClientVersions(OrganizationBean organizationBean, ClientStatus status,
        int lim) throws StorageException {
    String jpql = "SELECT v " + " FROM ClientVersionBean v " + " JOIN v.client c " + " JOIN c.organization o "
            + "WHERE o.id = :orgId ";

    if (status != null) {
        jpql += String.format(" AND v.status = '%s' ", status.name());
    }/*  www  . ja  v  a  2s  .  c om*/

    Query query = getActiveEntityManager().createQuery(jpql);
    query.setParameter("orgId", organizationBean.getId());
    if (lim > 0) {
        query.setMaxResults(lim);
    }
    return super.getAll(ClientVersionBean.class, query);
}

From source file:io.apiman.manager.api.jpa.JpaStorage.java

@Override
public Iterator<PlanVersionBean> getAllPlanVersions(OrganizationBean organizationBean, PlanStatus status,
        int lim) throws StorageException {
    String jpql = "SELECT v " + "  FROM PlanVersionBean v " + "  JOIN v.plan p " + "  JOIN p.organization o "
            + " WHERE o.id = :orgId ";

    if (status != null) {
        jpql += String.format(" AND v.status = '%s' ", status.name());
    }//from  w  ww  .j  a v  a2  s .  c  o  m

    Query query = getActiveEntityManager().createQuery(jpql);
    query.setParameter("orgId", organizationBean.getId());
    if (lim > 0) {
        query.setMaxResults(lim);
    }
    return super.getAll(PlanVersionBean.class, query);
}

From source file:io.apiman.manager.api.jpa.JpaStorage.java

@Override
public Iterator<ApiVersionBean> getAllApiVersions(OrganizationBean organizationBean, ApiStatus status, int lim)
        throws StorageException {
    String jpql = "SELECT v " + "  FROM ApiVersionBean v " + "  JOIN v.api a" + "  JOIN a.organization o "
            + " WHERE o.id = :orgId ";

    if (status != null) {
        jpql += String.format(" AND v.status = '%s' ", status.name());
    }/*from ww  w  .j  a v  a  2s . c  o  m*/

    Query query = getActiveEntityManager().createQuery(jpql);
    query.setParameter("orgId", organizationBean.getId());

    if (lim > 0) {
        query.setMaxResults(lim);
    }
    return super.getAll(ApiVersionBean.class, query);
}

From source file:io.apiman.manager.api.jpa.JpaStorage.java

@Override
public Iterator<ContractBean> getAllContracts(OrganizationBean organizationBean, int lim)
        throws StorageException {
    String jpql = " SELECT contractBean " + " FROM ContractBean contractBean "
    // Api//from w  ww  .  j a va  2  s  . c om
            + " JOIN contractBean.api apiVersion " + " JOIN apiVersion.api api "
            + " JOIN api.organization apiOrg "
            // Client
            + " JOIN contractBean.client clientVersion " + " JOIN clientVersion.client client "
            + " JOIN api.organization clientOrg "
            // Check API status
            + " WHERE (apiOrg.id = :orgId AND apiVersion.status = :apiStatus)"
            // Check In-Org ClientApp status
            + " OR (clientOrg.id = :orgId AND clientVersion.status = :clientStatus)";

    Query query = getActiveEntityManager().createQuery(jpql);
    query.setParameter("orgId", organizationBean.getId());
    query.setParameter("clientStatus", ClientStatus.Registered);
    query.setParameter("apiStatus", ApiStatus.Published);

    if (lim > 0) {
        query.setMaxResults(lim);
    }
    return super.getAll(ContractBean.class, query);
}

From source file:io.apiman.manager.api.jpa.JpaStorage.java

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getContracts(java.lang.String, java.lang.String, java.lang.String, int, int)
 *//*w w w . ja  v a  2 s . co  m*/
@Override
public List<ContractSummaryBean> getContracts(String organizationId, String apiId, String version, int page,
        int pageSize) throws StorageException {
    int start = (page - 1) * pageSize;
    beginTx();
    try {
        EntityManager entityManager = getActiveEntityManager();
        String jpql = "SELECT c from ContractBean c " + "  JOIN c.api apiv " + "  JOIN apiv.api api "
                + "  JOIN c.client clientv " + "  JOIN clientv.client client " + "  JOIN api.organization sorg"
                + "  JOIN client.organization aorg" + " WHERE api.id = :apiId " + "   AND sorg.id = :orgId "
                + "   AND apiv.version = :version " + " ORDER BY sorg.id, api.id ASC";
        Query query = entityManager.createQuery(jpql);
        query.setParameter("orgId", organizationId);
        query.setParameter("apiId", apiId);
        query.setParameter("version", version);
        query.setFirstResult(start);
        query.setMaxResults(pageSize);
        List<ContractBean> contracts = query.getResultList();
        List<ContractSummaryBean> rval = new ArrayList<>(contracts.size());
        for (ContractBean contractBean : contracts) {
            ClientBean client = contractBean.getClient().getClient();
            ApiBean api = contractBean.getApi().getApi();
            PlanBean plan = contractBean.getPlan().getPlan();

            OrganizationBean clientOrg = entityManager.find(OrganizationBean.class,
                    client.getOrganization().getId());
            OrganizationBean apiOrg = entityManager.find(OrganizationBean.class, api.getOrganization().getId());

            ContractSummaryBean csb = new ContractSummaryBean();
            csb.setClientId(client.getId());
            csb.setClientOrganizationId(client.getOrganization().getId());
            csb.setClientOrganizationName(clientOrg.getName());
            csb.setClientName(client.getName());
            csb.setClientVersion(contractBean.getClient().getVersion());
            csb.setContractId(contractBean.getId());
            csb.setCreatedOn(contractBean.getCreatedOn());
            csb.setPlanId(plan.getId());
            csb.setPlanName(plan.getName());
            csb.setPlanVersion(contractBean.getPlan().getVersion());
            csb.setApiDescription(api.getDescription());
            csb.setApiId(api.getId());
            csb.setApiName(api.getName());
            csb.setApiOrganizationId(apiOrg.getId());
            csb.setApiOrganizationName(apiOrg.getName());
            csb.setApiVersion(contractBean.getApi().getVersion());

            rval.add(csb);
        }
        return rval;
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        throw new StorageException(t);
    } finally {
        rollbackTx();
    }
}