Example usage for javax.persistence TypedQuery setMaxResults

List of usage examples for javax.persistence TypedQuery setMaxResults

Introduction

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

Prototype

TypedQuery<X> setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:org.openmeetings.app.data.conference.Roommanagement.java

/**
 * gets a list of all availible rooms// w w w . j  av  a 2 s  .  c  o m
 * 
 * @param user_level
 * @param start
 * @param max
 * @param orderby
 * @param asc
 * @return
 */
public List<Rooms> getRoomsInternatl(int start, int max, String orderby, boolean asc) {
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Rooms> cq = cb.createQuery(Rooms.class);
        Root<Rooms> c = cq.from(Rooms.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Rooms> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Rooms> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getRooms ] ", ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.conference.Roommanagement.java

public List<Rooms> getRoomsInternatlbyType(int start, int max, String orderby, boolean asc,
        String externalRoomType) {
    try {//from   w  ww  . j a  v a 2 s . c  o m
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Rooms> cq = cb.createQuery(Rooms.class);
        Root<Rooms> c = cq.from(Rooms.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        Predicate subCondition = cb.equal(c.get("externalRoomType"), externalRoomType);
        cq.where(condition, subCondition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Rooms> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Rooms> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getRooms ] ", ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.conference.Roommanagement.java

/**
 * gets a list of all availible rooms//from   www.ja  v  a2s  . c om
 * 
 * @param user_level
 * @param start
 * @param max
 * @param orderby
 * @param asc
 * @return
 */
public List<Rooms> getRoomsInternatlByHQL(int start, int max, String orderby, boolean asc, String search) {
    try {

        String hql = "select c from Rooms c " + "where c.deleted <> 'true' " + "AND c.name LIKE :search ";

        if (search.length() == 0) {
            search = "%";
        } else {
            search = "%" + search + "%";
        }
        if (orderby != null) {
            hql += " ORDER BY " + (orderby.startsWith("c.") ? "" : "c.") + orderby;

            if (asc) {
                hql += " ASC";
            } else {
                hql += " DESC";
            }
        }
        TypedQuery<Rooms> query = em.createQuery(hql, Rooms.class);
        query.setParameter("search", search);
        query.setFirstResult(start);
        query.setMaxResults(max);

        return query.getResultList();

    } catch (Exception ex2) {
        log.error("[getRooms ] ", ex2);
    }
    return null;
}

From source file:ca.uhn.fhir.jpa.dao.BaseHapiFhirDao.java

protected TagList getTags(Class<? extends IBaseResource> theResourceType, IIdType theResourceId) {
    String resourceName = null;//  www .ja  va  2s  .co  m
    if (theResourceType != null) {
        resourceName = toResourceName(theResourceType);
        if (theResourceId != null && theResourceId.hasVersionIdPart()) {
            IFhirResourceDao<? extends IBaseResource> dao = getDao(theResourceType);
            BaseHasResource entity = dao.readEntity(theResourceId);
            TagList retVal = new TagList();
            for (BaseTag next : entity.getTags()) {
                retVal.add(next.getTag().toTag());
            }
            return retVal;
        }
    }

    Set<Long> tagIds = new HashSet<Long>();
    findMatchingTagIds(resourceName, theResourceId, tagIds, ResourceTag.class);
    findMatchingTagIds(resourceName, theResourceId, tagIds, ResourceHistoryTag.class);
    if (tagIds.isEmpty()) {
        return new TagList();
    }
    {
        CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
        CriteriaQuery<TagDefinition> cq = builder.createQuery(TagDefinition.class);
        Root<TagDefinition> from = cq.from(TagDefinition.class);
        cq.where(from.get("myId").in(tagIds));
        cq.orderBy(builder.asc(from.get("mySystem")), builder.asc(from.get("myCode")));
        TypedQuery<TagDefinition> q = myEntityManager.createQuery(cq);
        q.setMaxResults(getConfig().getHardTagListLimit());

        TagList retVal = new TagList();
        for (TagDefinition next : q.getResultList()) {
            retVal.add(next.toTag());
        }

        return retVal;
    }
}

From source file:org.openmeetings.app.data.conference.Roommanagement.java

/**
 * // w  w  w  . j a  v a 2s . c  om
 * @param organisation_id
 * @param start
 * @param max
 * @param orderby
 * @param asc
 * @return
 */
private List<Rooms_Organisation> getRoomsOrganisationByOrganisationId(long organisation_id, int start, int max,
        String orderby, boolean asc) {
    try {
        String hql = "select c from Rooms_Organisation as c "
                + "where c.organisation.organisation_id = :organisation_id " + "AND c.deleted <> :deleted";
        if (orderby.startsWith("c.")) {
            hql += "ORDER BY " + orderby;
        } else {
            hql += "ORDER BY " + "c." + orderby;
        }
        if (asc) {
            hql += " ASC";
        } else {
            hql += " DESC";
        }

        TypedQuery<Rooms_Organisation> q = em.createQuery(hql, Rooms_Organisation.class);

        q.setParameter("organisation_id", organisation_id);
        q.setParameter("deleted", "true");
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Rooms_Organisation> ll = q.getResultList();

        return ll;
    } catch (Exception ex2) {
        log.error("[getRoomsByOrganisation] ", ex2);
    }
    return null;
}

From source file:com.hp.triclops.repository.VehicleRepositoryDAO.java

/**
 * /* w w  w  . j a v a2s .c  om*/
 * @param id ?null""??
 * @param vin ?null""??
 * @param vendor ?null""??
 * @param model ?null""??
 * @param t_flag 0 ? 1  ??
 * @param displacement ?null""??
 * @param license_plate ?null""??
 * @param start_date ?null??
 * @param end_date ??null??
 * @param orderByProperty ?? Vehicle?,vin
 * @param ascOrDesc ??? "ASC""DESC"  ???,ASC
 * @param pageSize ??? 0
 * @param currentPage ??? 0
 * @param oid id
 * @return  ????Page
 */
public Page findVehiclesByKeys(Integer id, String vin, String vendor, String model, Integer t_flag,
        String displacement, String license_plate, Date start_date, Date end_date, String orderByProperty,
        String ascOrDesc, Integer pageSize, Integer currentPage, Integer oid) {
    String jpql = "select v FROM Vehicle v";
    String jpql_count = "";
    id = (id == null) ? -1 : id;
    vin = (vin == null) ? "" : EscapeStringUtil.toEscape(vin);
    vendor = (vendor == null) ? "" : vendor;
    model = (model == null) ? "" : model;
    displacement = (displacement == null) ? "" : displacement;
    license_plate = (license_plate == null) ? "" : EscapeStringUtil.toEscape(license_plate);
    orderByProperty = (orderByProperty == null) ? "vin" : orderByProperty;
    ascOrDesc = (ascOrDesc == null) ? "ASC" : ascOrDesc;
    t_flag = (t_flag == null) ? -1 : t_flag;
    pageSize = (pageSize == null) ? 10 : pageSize;
    pageSize = (pageSize <= 0) ? 10 : pageSize;
    currentPage = (currentPage == null) ? 1 : currentPage;
    currentPage = (currentPage <= 0) ? 1 : currentPage;
    if (oid != null && oid >= 0) {
        jpql = jpql + " join v.organizationSet O where O.id =:oid";
    } else {
        jpql = jpql + " where 1=1";
    }
    if (id >= 0) {
        jpql = jpql + " And v.id =:id";
    }
    if (!vin.equals("")) {
        jpql = jpql + " And v.vin = :vin";
    }
    if (!vendor.equals("")) {
        jpql = jpql + " And v.vendor = :vendor";
    }
    if (!model.equals("")) {
        jpql = jpql + " And v.model = :model";
    }
    if (!displacement.equals("")) {
        jpql = jpql + " And v.displacement = :displacement";
    }
    if (!license_plate.equals("")) {
        jpql = jpql + " And v.license_plate = :license_plate";
    }
    if (t_flag == 0 || t_flag == 1) {
        jpql = jpql + " And v.t_flag =:t_flag";
    }
    if (start_date != null) {
        jpql = jpql + " And v.product_date >= :start_date";
    }
    if (end_date != null) {
        jpql = jpql + " And v.product_date <= :end_date";
    }
    jpql = jpql + " Order by v." + orderByProperty + " " + ascOrDesc;
    jpql_count = jpql;

    TypedQuery query = em.createQuery(jpql, Vehicle.class);
    TypedQuery queryCount = em.createQuery(jpql_count, Vehicle.class);

    if (oid != null && oid >= 0) {
        query.setParameter("oid", oid);
        queryCount.setParameter("oid", oid);
    }
    if (id >= 0) {
        query.setParameter("id", id);
        queryCount.setParameter("id", id);
    }
    if (!vin.equals("")) {
        query.setParameter("vin", vin);
        queryCount.setParameter("vin", vin);
    }
    if (!vendor.equals("")) {
        query.setParameter("vendor", vendor);
        queryCount.setParameter("vendor", vendor);
    }
    if (!model.equals("")) {
        query.setParameter("model", model);
        queryCount.setParameter("model", model);
    }
    if (!displacement.equals("")) {
        query.setParameter("displacement", displacement);
        queryCount.setParameter("displacement", displacement);
    }
    if (!license_plate.equals("")) {
        query.setParameter("license_plate", license_plate);
        queryCount.setParameter("license_plate", license_plate);
    }
    if (t_flag == 0 || t_flag == 1) {
        query.setParameter("t_flag", t_flag);
        queryCount.setParameter("t_flag", t_flag);
    }
    if (start_date != null) {
        query.setParameter("start_date", start_date);
        queryCount.setParameter("start_date", start_date);
    }
    if (end_date != null) {
        query.setParameter("end_date", end_date);
        queryCount.setParameter("end_date", end_date);
    }
    query.setFirstResult((currentPage - 1) * pageSize);
    query.setMaxResults(pageSize);
    List items = query.getResultList();
    Long count = (long) queryCount.getResultList().size();
    return new Page(currentPage, pageSize, count, items);
}

From source file:org.openmeetings.app.data.user.Usermanagement.java

/**
 * query for a list of users// ww  w .j a v  a 2  s. c o m
 * 
 * @param users_id
 * @param user_level
 * @param start
 * @param max
 * @param orderby
 * @return
 */
public SearchResult<Users> getUsersList(long user_level, int start, int max, String orderby, boolean asc) {
    try {
        if (authLevelManagement.checkAdminLevel(user_level)) {
            SearchResult<Users> sresult = new SearchResult<Users>();
            sresult.setObjectName(Users.class.getName());
            sresult.setRecords(usersDao.selectMaxFromUsers());

            // get all users
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Users> cq = cb.createQuery(Users.class);
            Root<Users> c = cq.from(Users.class);
            Predicate condition = cb.equal(c.get("deleted"), "false");
            cq.where(condition);
            cq.distinct(asc);
            if (asc) {
                cq.orderBy(cb.asc(c.get(orderby)));
            } else {
                cq.orderBy(cb.desc(c.get(orderby)));
            }
            TypedQuery<Users> q = em.createQuery(cq);
            q.setFirstResult(start);
            q.setMaxResults(max);
            List<Users> ll = q.getResultList();
            sresult.setResult(ll);
            return sresult;
        }
    } catch (Exception ex2) {
        log.error("[getUsersList] " + ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.user.Usermanagement.java

/**
 * suche eines Bentzers/*  w  ww. jav  a2  s  . com*/
 * 
 * @param user_level
 * @param searchstring
 * @param max
 * @param start
 * @return
 */
public List<Users> searchUser(long user_level, String searchcriteria, String searchstring, int max, int start,
        String orderby, boolean asc) {
    if (authLevelManagement.checkAdminLevel(user_level)) {
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Users> cq = cb.createQuery(Users.class);
            Root<Users> c = cq.from(Users.class);
            Expression<String> literal = cb.literal("%" + searchstring + "%");
            // crit.add(Restrictions.ilike(searchcriteria, "%" +
            // searchstring + "%"));
            Path<String> path = c.get(searchcriteria);
            Predicate predicate = cb.like(path, literal);
            Predicate condition = cb.notEqual(c.get("deleted"), "true");
            cq.where(condition, predicate);
            cq.distinct(asc);
            if (asc) {
                cq.orderBy(cb.asc(c.get(orderby)));
            } else {
                cq.orderBy(cb.desc(c.get(orderby)));
            }
            TypedQuery<Users> q = em.createQuery(cq);
            q.setFirstResult(start);
            q.setMaxResults(max);
            List<Users> contactsZ = q.getResultList();
            return contactsZ;
        } catch (Exception ex2) {
            log.error("searchUser", ex2);
        }
    }
    return null;
}

From source file:gov.osti.services.Metadata.java

/**
 * Send a POC email notification on SUBMISSION/APPROVAL of DOE CODE records.
 *
 * @param md the METADATA to send notification for
 *//*from   w ww. ja  va  2 s . c  om*/
private static void sendPOCNotification(DOECodeMetadata md) {
    // if HOST or MD or PROJECT MANAGER NAME isn't set, cannot send
    if (StringUtils.isEmpty(EMAIL_HOST) || null == md || StringUtils.isEmpty(PM_NAME))
        return;

    Long codeId = md.getCodeId();
    String siteCode = md.getSiteOwnershipCode();
    Status workflowStatus = md.getWorkflowStatus();

    // if SITE OWNERSHIP isn't set, cannot send
    if (StringUtils.isEmpty(siteCode))
        return;

    // only applicable to APPROVED records
    if (!Status.Approved.equals(workflowStatus))
        return;

    // get the SITE information
    Site site = SiteServices.findSiteBySiteCode(siteCode);
    if (null == site) {
        log.warn("Unable to locate SITE information for SITE CODE: " + siteCode);
        return;
    }

    // lookup previous Snapshot status info for item
    EntityManager em = DoeServletContextListener.createEntityManager();
    TypedQuery<MetadataSnapshot> querySnapshot = em
            .createNamedQuery("MetadataSnapshot.findByCodeIdLastNotStatus", MetadataSnapshot.class)
            .setParameter("status", DOECodeMetadata.Status.Approved).setParameter("codeId", codeId);

    String lastApprovalFor = "submitted/announced";
    List<MetadataSnapshot> results = querySnapshot.setMaxResults(1).getResultList();
    for (MetadataSnapshot ms : results) {
        lastApprovalFor = ms.getSnapshotKey().getSnapshotStatus().toString().toLowerCase();
    }

    List<String> emails = site.getPocEmails();

    // if POC is setup
    if (emails != null && !emails.isEmpty()) {
        try {
            HtmlEmail email = new HtmlEmail();
            email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8);
            email.setHostName(EMAIL_HOST);

            String lab = site.getLab();
            lab = lab.isEmpty() ? siteCode : lab;

            String softwareTitle = md.getSoftwareTitle().replaceAll("^\\h+|\\h+$", "");

            email.setFrom(EMAIL_FROM);
            email.setSubject("POC Notification -- " + workflowStatus + " -- DOE CODE ID: " + codeId + ", "
                    + softwareTitle);

            for (String pocEmail : emails)
                email.addTo(pocEmail);

            // if email is provided, BCC the Project Manager
            if (!StringUtils.isEmpty(PM_EMAIL))
                email.addBcc(PM_EMAIL, PM_NAME);

            StringBuilder msg = new StringBuilder();

            msg.append("<html>");
            msg.append("Dear Sir or Madam:");

            String biblioLink = SITE_URL + "/biblio/" + codeId;

            msg.append("<p>As a point of contact for ").append(lab)
                    .append(", we wanted to inform you that a software project, titled ").append(softwareTitle)
                    .append(", associated with your organization was ").append(lastApprovalFor)
                    .append(" to DOE CODE and assigned DOE CODE ID: ").append(codeId)
                    .append(".  This project record is discoverable in <a href=\"").append(SITE_URL)
                    .append("\">DOE CODE</a>, e.g. searching by the project title or DOE CODE ID #, and can be found here: <a href=\"")
                    .append(biblioLink).append("\">").append(biblioLink).append("</a></p>");

            msg.append(
                    "<p>If you have any questions, please do not hesitate to <a href=\"mailto:doecode@osti.gov\">Contact Us</a>.</p>");
            msg.append("<p>Sincerely,</p>");
            msg.append("<p>").append(PM_NAME).append("<br/>Product Manager for DOE CODE<br/>USDOE/OSTI</p>");

            msg.append("</html>");

            email.setHtmlMsg(msg.toString());

            email.send();
        } catch (EmailException e) {
            log.error("Unable to send POC notification to " + Arrays.toString(emails.toArray()) + " for #"
                    + md.getCodeId());
            log.error("Message: " + e.getMessage());
        }
    }
}

From source file:gov.osti.services.Metadata.java

/**
 * Send an email notification on APPROVAL of DOE CODE records.
 *
 * @param md the METADATA to send notification for
 *//*from  ww  w  .  j  a v a  2 s .com*/
private static void sendApprovalNotification(DOECodeMetadata md) {
    HtmlEmail email = new HtmlEmail();
    email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8);
    email.setHostName(EMAIL_HOST);

    // if HOST or record OWNER or PROJECT MANAGER NAME isn't set, cannot send
    if (StringUtils.isEmpty(EMAIL_HOST) || null == md || StringUtils.isEmpty(md.getOwner())
            || StringUtils.isEmpty(PM_NAME))
        return;
    // only has meaning for APPROVED records
    if (!Status.Approved.equals(md.getWorkflowStatus()))
        return;

    try {
        // get the OWNER information
        User owner = UserServices.findUserByEmail(md.getOwner());
        if (null == owner) {
            log.warn("Unable to locate USER information for Code ID: " + md.getCodeId());
            return;
        }

        Long codeId = md.getCodeId();

        // lookup previous Snapshot status info for item
        EntityManager em = DoeServletContextListener.createEntityManager();
        TypedQuery<MetadataSnapshot> querySnapshot = em
                .createNamedQuery("MetadataSnapshot.findByCodeIdLastNotStatus", MetadataSnapshot.class)
                .setParameter("status", DOECodeMetadata.Status.Approved).setParameter("codeId", codeId);

        String lastApprovalFor = "submitted/announced";
        List<MetadataSnapshot> results = querySnapshot.setMaxResults(1).getResultList();
        for (MetadataSnapshot ms : results) {
            lastApprovalFor = ms.getSnapshotKey().getSnapshotStatus().toString().toLowerCase();
        }

        String softwareTitle = md.getSoftwareTitle().replaceAll("^\\h+|\\h+$", "");

        email.setFrom(EMAIL_FROM);
        email.setSubject("Approved -- DOE CODE ID: " + codeId + ", " + softwareTitle);
        email.addTo(md.getOwner());

        // if email is provided, BCC the Project Manager
        if (!StringUtils.isEmpty(PM_EMAIL))
            email.addBcc(PM_EMAIL, PM_NAME);

        StringBuilder msg = new StringBuilder();

        msg.append("<html>");
        msg.append("Dear ").append(owner.getFirstName()).append(" ").append(owner.getLastName()).append(":");

        msg.append("<P>Thank you -- your ").append(lastApprovalFor).append(" project, DOE CODE ID: <a href=\"")
                .append(SITE_URL).append("/biblio/").append(codeId).append("\">").append(codeId)
                .append("</a>, has been approved.  It is now <a href=\"").append(SITE_URL)
                .append("\">searchable</a> in DOE CODE by, for example, title or CODE ID #.</P>");

        // OMIT the following for BUSINESS TYPE software, or last ANNOUNCED software
        if (!DOECodeMetadata.Type.B.equals(md.getSoftwareType())
                && !lastApprovalFor.equalsIgnoreCase("announced")) {
            msg.append(
                    "<P>You may need to continue editing your project to announce it to the Department of Energy ")
                    .append("to ensure announcement and dissemination in accordance with DOE statutory responsibilities. For more information please see ")
                    .append("<a href=\"").append(SITE_URL)
                    .append("/faq#what-does-it-mean-to-announce\">What does it mean to announce scientific code to DOE CODE?</a></P>");
        }
        msg.append(
                "<P>If you have questions such as What are the benefits of getting a DOI for code or software?, see the ")
                .append("<a href=\"").append(SITE_URL).append("/faq\">DOE CODE FAQs</a>.</P>");
        msg.append(
                "<P>If we can be of assistance, please do not hesitate to <a href=\"mailto:doecode@osti.gov\">Contact Us</a>.</P>");
        msg.append("<P>Sincerely,</P>");
        msg.append("<P>").append(PM_NAME).append("<BR/>Product Manager for DOE CODE<BR/>USDOE/OSTI</P>");

        msg.append("</html>");

        email.setHtmlMsg(msg.toString());

        email.send();
    } catch (EmailException e) {
        log.error("Unable to send APPROVAL notification for #" + md.getCodeId());
        log.error("Message: " + e.getMessage());
    }
}