List of usage examples for org.hibernate Criteria setMaxResults
public Criteria setMaxResults(int maxResults);
From source file:com.denimgroup.threadfix.data.dao.hibernate.HibernateVulnerabilitySearchDao.java
License:Mozilla Public License
private Criteria performLookupSetup(VulnerabilitySearchParameters parameters) { assert parameters != null; Criteria criteria = VulnerabilitySearchCriteriaConstructor .getCriteriaWithRestrictions(sessionFactory.getCurrentSession(), parameters); if (parameters.getNumberVulnerabilities() != null) { criteria.setMaxResults(parameters.getNumberVulnerabilities()); if (parameters.getPage() != null) criteria.setFirstResult(parameters.getNumberVulnerabilities() * (parameters.getPage() - 1)); }/* w w w . ja v a 2 s . c o m*/ return criteria; }
From source file:com.duroty.application.admin.manager.AdminManager.java
License:Open Source License
/** * DOCUMENT ME!//from w w w. j a v a 2s . c om * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param token DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public SearchUsersObj allUsers(Session hsession, int page, int byPage) throws Exception { SearchUsersObj searchUsersObj = new SearchUsersObj(); Vector users = new Vector(); try { Criteria crit = hsession.createCriteria(Users.class); searchUsersObj.setHits(crit.list().size()); crit = hsession.createCriteria(Users.class); crit.setFirstResult(page * byPage); crit.setMaxResults(byPage); crit.addOrder(Order.asc("useName")); ScrollableResults scroll = crit.scroll(); while (scroll.next()) { Users user = (Users) scroll.get(0); UserObj obj = new UserObj(); obj.setActive(user.isUseActive()); obj.setEmail(user.getUseEmail()); Iterator it = user.getMailPreferenceses().iterator(); MailPreferences mailPreferences = (MailPreferences) it.next(); obj.setHtmlMessages(mailPreferences.isMaprHtmlMessage()); obj.setIdint(user.getUseIdint()); obj.setLanguage(user.getUseLanguage()); obj.setMessagesByPage(mailPreferences.getMaprMessagesByPage()); obj.setName(user.getUseName()); obj.setPassword(user.getUsePassword()); obj.setQuotaSize(mailPreferences.getMaprQuotaSize()); obj.setRegisterDate(user.getUseRegisterDate()); Integer[] roles = new Integer[user.getUserRoles().size()]; if (roles.length > 0) { it = user.getUserRoles().iterator(); int i = 0; while (it.hasNext()) { UserRole userRole = (UserRole) it.next(); roles[i] = new Integer(userRole.getId().getRoles().getRolIdint()); i++; } } obj.setRoles(roles); obj.setSignature(mailPreferences.getMaprSignature()); if (mailPreferences.getMaprSpamTolerance() == 100) { obj.setSpamTolerance(true); } else { obj.setSpamTolerance(false); } obj.setUsername(user.getUseUsername()); obj.setVacationBody(mailPreferences.getMaprVacationBody()); obj.setVacationSubject(mailPreferences.getMaprVacationSubject()); obj.setVactionActive(mailPreferences.isMaprVacationActive()); int usedQuotaSize = getUsedQuotaSize(hsession, user); usedQuotaSize /= 1024; if (usedQuotaSize > 1024) { usedQuotaSize /= 1024; obj.setUsedQuota(usedQuotaSize + " MB"); } else { obj.setUsedQuota(((usedQuotaSize > 0) ? (usedQuotaSize + "") : "<1") + " kB"); } users.addElement(obj); } searchUsersObj.setUsers(users); return searchUsersObj; } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.admin.manager.AdminManager.java
License:Open Source License
/** * DOCUMENT ME!//from www. ja v a 2 s.co m * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param token DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public SearchUsersObj suggestUsers(Session hsession, String token, int page, int byPage) throws Exception { SearchUsersObj searchUsersObj = new SearchUsersObj(); Vector users = new Vector(); try { Criteria crit = hsession.createCriteria(Users.class); crit.add(Restrictions.or(Restrictions.ilike("useName", token, MatchMode.ANYWHERE), Restrictions.ilike("useEmail", token, MatchMode.ANYWHERE))); searchUsersObj.setHits(crit.list().size()); crit = hsession.createCriteria(Users.class); crit.add(Restrictions.or(Restrictions.ilike("useName", token, MatchMode.ANYWHERE), Restrictions.ilike("useEmail", token, MatchMode.ANYWHERE))); crit.setFirstResult(page * byPage); crit.setMaxResults(byPage); crit.addOrder(Order.asc("useName")); ScrollableResults scroll = crit.scroll(); while (scroll.next()) { Users user = (Users) scroll.get(0); UserObj obj = new UserObj(); obj.setActive(user.isUseActive()); obj.setEmail(user.getUseEmail()); Iterator it = user.getMailPreferenceses().iterator(); MailPreferences mailPreferences = (MailPreferences) it.next(); obj.setHtmlMessages(mailPreferences.isMaprHtmlMessage()); obj.setIdint(user.getUseIdint()); obj.setLanguage(user.getUseLanguage()); obj.setMessagesByPage(mailPreferences.getMaprMessagesByPage()); obj.setName(user.getUseName()); obj.setPassword(user.getUsePassword()); obj.setQuotaSize(mailPreferences.getMaprQuotaSize()); obj.setRegisterDate(user.getUseRegisterDate()); Integer[] roles = new Integer[user.getUserRoles().size()]; if (roles.length > 0) { it = user.getUserRoles().iterator(); int i = 0; while (it.hasNext()) { UserRole userRole = (UserRole) it.next(); roles[i] = new Integer(userRole.getId().getRoles().getRolIdint()); i++; } } obj.setRoles(roles); obj.setSignature(mailPreferences.getMaprSignature()); if (mailPreferences.getMaprSpamTolerance() == 100) { obj.setSpamTolerance(true); } else { obj.setSpamTolerance(false); } obj.setUsername(user.getUseUsername()); obj.setVacationBody(mailPreferences.getMaprVacationBody()); obj.setVacationSubject(mailPreferences.getMaprVacationSubject()); obj.setVactionActive(mailPreferences.isMaprVacationActive()); int usedQuotaSize = getUsedQuotaSize(hsession, user); usedQuotaSize /= 1024; if (usedQuotaSize > 1024) { usedQuotaSize /= 1024; obj.setUsedQuota(usedQuotaSize + " MB"); } else { obj.setUsedQuota(((usedQuotaSize > 0) ? (usedQuotaSize + "") : "<1") + " kB"); } users.addElement(obj); } searchUsersObj.setUsers(users); return searchUsersObj; } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.manager.MailManager.java
License:Open Source License
/** * DOCUMENT ME!/*from w ww . ja va2s.c om*/ * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param folderName DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public Vector getMessages(Session hsession, String repositoryName, String folderName, int page, int messagesByPage, int order, String orderType) throws MailException { Vector messages = new Vector(); try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); Criteria crit = hsession.createCriteria(Message.class); crit.add(Restrictions.eq("users", user)); if (folderName.equals(this.folderAll) || folderName.equals(this.folderHidden)) { crit.add(Restrictions.not(Restrictions.in("mesBox", new String[] { this.folderTrash, this.folderSpam, this.folderChat, FOLDER_DELETE }))); } else if (folderName.equals(this.folderImportant)) { crit.add(Restrictions.eq("mesFlagged", new Boolean(true))); } else { crit.add(Restrictions.eq("mesBox", folderName)); } switch (order) { case ORDER_BY_IMPORTANT: if (orderType.equals("ASC")) { crit.addOrder(Order.asc("mesFlagged")); } else { crit.addOrder(Order.desc("mesFlagged")); } break; case ORDER_BY_ADDRESS: if (orderType.equals("ASC")) { crit.addOrder(Order.asc("mesFrom")); } else { crit.addOrder(Order.desc("mesFrom")); } break; case ORDER_BY_SIZE: if (orderType.equals("ASC")) { crit.addOrder(Order.asc("mesSize")); } else { crit.addOrder(Order.desc("mesSize")); } break; case ORDER_BY_SUBJECT: if (orderType.equals("ASC")) { crit.addOrder(Order.asc("mesSubject")); } else { crit.addOrder(Order.desc("mesSubject")); } break; case ORDER_BY_DATE: if (orderType.equals("ASC")) { crit.addOrder(Order.asc("mesDate")); } else { crit.addOrder(Order.desc("mesDate")); } break; case ORDER_BY_UNREAD: if (orderType.equals("ASC")) { crit.addOrder(Order.asc("mesRecent")); } else { crit.addOrder(Order.desc("mesRecent")); } break; default: crit.addOrder(Order.desc("mesDate")); break; } crit.setFirstResult(page * messagesByPage); crit.setMaxResults(messagesByPage); ScrollableResults scroll = crit.scroll(); while (scroll.next()) { Message message = (Message) scroll.get(0); MessageObj obj = new MessageObj(message.getMesName()); obj.setBox(message.getMesBox()); obj.setFrom(message.getMesFrom()); if ((message.getAttachments() != null) && (message.getAttachments().size() > 0)) { obj.setHasAttachment(true); } else { obj.setHasAttachment(false); } int size = message.getMesSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } if (message.getMesBox().equals(folderSent)) { try { obj.setEmail(message.getMesTo()); } catch (Exception e) { obj.setEmail("unknown to"); } } else { obj.setEmail(message.getMesFrom()); } Date date = message.getMesDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if (calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); if (message.getLabMeses() != null) { Iterator it = message.getLabMeses().iterator(); StringBuffer label = new StringBuffer(); while (it.hasNext()) { if (label.length() > 0) { label.append(", "); } LabMes labMes = (LabMes) it.next(); label.append(labMes.getId().getLabel().getLabName()); } obj.setLabel(label.toString()); } try { if (StringUtils.isBlank(message.getMesSubject())) { obj.setSubject("(no subject)"); } else { obj.setSubject(message.getMesSubject()); } } catch (Exception ex) { obj.setSubject("(no subject)"); } if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.isMesRecent()) { obj.setRecent(true); } else { obj.setRecent(false); } String priority = "normal"; if (MessageUtilities.isHighPriority(message.getMesHeaders())) { priority = "high"; } else if (MessageUtilities.isLowPriority(message.getMesHeaders())) { priority = "low"; } obj.setPriority(priority); messages.addElement(obj); } return messages; } catch (Exception e) { throw new MailException(e); } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.manager.PreferencesManager.java
License:Open Source License
/** * DOCUMENT ME!// w ww .j a va2s .co m * * @param hsession DOCUMENT ME! * @param token DOCUMENT ME! * * @return DOCUMENT ME! * * @throws ManagerException DOCUMENT ME! */ public SearchContactsObj searchContacts(Session hsession, String repositoryName, String token, int page, int byPage, int order, String extra) throws Exception { SearchContactsObj sobj = new SearchContactsObj(); Vector contacts = new Vector(); try { Criteria crit = hsession.createCriteria(Contact.class); crit.add(Restrictions.or(Restrictions.ilike("conName", token, MatchMode.ANYWHERE), Restrictions.ilike("conEmail", token, MatchMode.ANYWHERE))); crit.add(Restrictions.eq("users", getUser(hsession, repositoryName))); crit.add(Restrictions.isNotNull("conSentDate")); crit.add(Restrictions.isNotNull("conReceivedDate")); int hits = crit.list().size(); sobj.setHits(hits); crit = hsession.createCriteria(Contact.class); crit.add(Restrictions.or(Restrictions.ilike("conName", token, MatchMode.ANYWHERE), Restrictions.ilike("conEmail", token, MatchMode.ANYWHERE))); crit.add(Restrictions.eq("users", getUser(hsession, repositoryName))); crit.add(Restrictions.isNotNull("conSentDate")); crit.add(Restrictions.isNotNull("conReceivedDate")); switch (order) { case ORDER_BY_EMAIL: crit.addOrder(Order.asc("conEmail")); break; case ORDER_BY_FREQUENCY_SENT: crit.addOrder(Order.asc("conSentDate")); break; case ORDER_BY_FREQUENCY_RECEIVED: crit.addOrder(Order.asc("conReceivedDate")); break; default: crit.addOrder(Order.asc("conSentDate")); break; } if (byPage > 0) { crit.setFirstResult(page * byPage); crit.setMaxResults(byPage); } ScrollableResults scroll = crit.scroll(); while (scroll.next()) { Contact contact = (Contact) scroll.get(0); ContactObj obj = new ContactObj(); obj.setIdint(contact.getConIdint()); obj.setName(contact.getConName()); obj.setEmail(contact.getConEmail()); if (!StringUtils.isBlank(contact.getConDescription())) { obj.setDescription(contact.getConDescription().replaceAll("\n", "\\\\n") .replaceAll("\r", "\\\\r").replaceAll("'", "''")); } contacts.addElement(obj); } sobj.setContacts(contacts); return sobj; } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.manager.PreferencesManager.java
License:Open Source License
/** * DOCUMENT ME!/*from ww w . ja v a 2s . com*/ * * @param hsession DOCUMENT ME! * @param token DOCUMENT ME! * * @return DOCUMENT ME! * * @throws ManagerException DOCUMENT ME! */ public SearchGroupsObj searchGroups(Session hsession, String repositoryName, String token, int page, int byPage, int order, String extra) throws Exception { SearchGroupsObj sobj = new SearchGroupsObj(); Vector groups = new Vector(); try { Criteria crit = hsession.createCriteria(ContactList.class); crit.add(Restrictions.or(Restrictions.ilike("coliName", token, MatchMode.ANYWHERE), Restrictions.ilike("coliName", token, MatchMode.ANYWHERE))); crit.add(Restrictions.eq("users", getUser(hsession, repositoryName))); int hits = crit.list().size(); sobj.setHits(hits); crit = hsession.createCriteria(ContactList.class); crit.add(Restrictions.or(Restrictions.ilike("coliName", token, MatchMode.ANYWHERE), Restrictions.ilike("coliName", token, MatchMode.ANYWHERE))); crit.add(Restrictions.eq("users", getUser(hsession, repositoryName))); crit.addOrder(Order.asc("coliName")); if (byPage > 0) { crit.setFirstResult(page * byPage); crit.setMaxResults(byPage); } ScrollableResults scroll = crit.scroll(); while (scroll.next()) { ContactList contactList = (ContactList) scroll.get(0); ContactListObj obj = new ContactListObj(); obj.setIdint(contactList.getColiIdint()); obj.setName(contactList.getColiName()); Set set = contactList.getConColis(); StringBuffer emails = new StringBuffer(); if (set != null) { Iterator it = set.iterator(); while (it.hasNext()) { ConColi conColi = (ConColi) it.next(); Contact contact = conColi.getId().getContact(); if (emails.length() > 0) { emails.append(", "); } String name = ""; if (!StringUtils.isBlank(contact.getConName())) { name = contact.getConName() + " "; } emails.append(name + "<" + contact.getConEmail() + ">"); } if (emails.length() > 0) { obj.setEmails(emails.toString() + ", "); } } groups.addElement(obj); } sobj.setGroups(groups); return sobj; } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.manager.PreferencesManager.java
License:Open Source License
/** * DOCUMENT ME!//from ww w. j a va 2 s . co m * * @param hsession DOCUMENT ME! * @param token DOCUMENT ME! * * @return DOCUMENT ME! * * @throws ManagerException DOCUMENT ME! */ public SearchContactsObj getContacts(Session hsession, String repositoryName, int page, int byPage, int order, String extra) throws Exception { Vector contacts = new Vector(); SearchContactsObj sobj = new SearchContactsObj(); try { Criteria crit = hsession.createCriteria(Contact.class); crit.add(Restrictions.isNotNull("conSentDate")); crit.add(Restrictions.isNotNull("conReceivedDate")); crit.add(Restrictions.eq("users", getUser(hsession, repositoryName))); int hits = crit.list().size(); sobj.setHits(hits); crit = hsession.createCriteria(Contact.class); crit.add(Restrictions.isNotNull("conSentDate")); crit.add(Restrictions.isNotNull("conReceivedDate")); crit.add(Restrictions.eq("users", getUser(hsession, repositoryName))); crit.setFirstResult(page * byPage); crit.setMaxResults(byPage); switch (order) { case ORDER_BY_EMAIL: crit.addOrder(Order.asc("conEmail")); break; case ORDER_BY_FREQUENCY_SENT: crit.addOrder(Order.desc("conCount")); break; case ORDER_BY_FREQUENCY_RECEIVED: crit.addOrder(Order.desc("conCount")); break; default: crit.addOrder(Order.desc("conCount")); break; } ScrollableResults scroll = crit.scroll(); while (scroll.next()) { Contact contact = (Contact) scroll.get(0); ContactObj obj = new ContactObj(); obj.setIdint(contact.getConIdint()); obj.setName(contact.getConName()); obj.setEmail(contact.getConEmail()); if (!StringUtils.isBlank(contact.getConDescription())) { obj.setDescription(contact.getConDescription().replaceAll("\n", "\\\\n") .replaceAll("\r", "\\\\r").replaceAll("'", "''")); } contacts.addElement(obj); } sobj.setContacts(contacts); return sobj; } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.manager.PreferencesManager.java
License:Open Source License
/** * DOCUMENT ME!//from www .ja va2s . co m * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param page DOCUMENT ME! * @param byPage DOCUMENT ME! * @param order DOCUMENT ME! * @param extra DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public SearchGroupsObj getGroups(Session hsession, String repositoryName, int page, int byPage, int order, String extra) throws Exception { Vector contacts = new Vector(); SearchGroupsObj sobj = new SearchGroupsObj(); try { Criteria crit = hsession.createCriteria(ContactList.class); crit.add(Restrictions.eq("users", getUser(hsession, repositoryName))); int hits = crit.list().size(); sobj.setHits(hits); crit = hsession.createCriteria(ContactList.class); crit.add(Restrictions.eq("users", getUser(hsession, repositoryName))); crit.addOrder(Order.asc("coliName")); if (byPage > 0) { crit.setFirstResult(page * byPage); crit.setMaxResults(byPage); } ScrollableResults scroll = crit.scroll(); while (scroll.next()) { ContactList contactList = (ContactList) scroll.get(0); ContactListObj obj = new ContactListObj(); obj.setIdint(contactList.getColiIdint()); obj.setName(contactList.getColiName()); Set set = contactList.getConColis(); StringBuffer emails = new StringBuffer(); if (set != null) { Iterator it = set.iterator(); while (it.hasNext()) { ConColi conColi = (ConColi) it.next(); Contact contact = conColi.getId().getContact(); if (emails.length() > 0) { emails.append(", "); } String name = ""; if (!StringUtils.isBlank(contact.getConName())) { name = contact.getConName().replaceAll("'", "\\\\'") + " "; } emails.append(name + "<" + contact.getConEmail() + ">"); } if (emails.length() > 0) { obj.setEmails(emails.toString() + ", "); } } contacts.addElement(obj); } sobj.setGroups(contacts); return sobj; } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.eharmony.matching.seeking.executor.hibernate.HibernateQueryExecutor.java
License:Apache License
protected <T, R> Criteria getCriteria(Query<T, R> query) { final Criterion translated = queryTranslator.translate(query); if (log.isDebugEnabled()) { log.debug(translated);//from w w w . ja v a 2 s .c om } final Criteria criteria = getSession().createCriteria(query.getEntityClass()); criteria.add(translated); final List<Order> orders = queryTranslator.translateOrder(query).get(); for (final Order order : orders) { criteria.addOrder(order); } final Projection projection = queryTranslator.translateProjection(query); if (projection != null) { criteria.setProjection(projection); } if (query.getMaxResults() != null) { criteria.setMaxResults(query.getMaxResults()); } return criteria; }
From source file:com.emergya.persistenceGeo.dao.impl.GenericHibernateDAOImpl.java
License:Open Source License
@SuppressWarnings("unchecked") public List<T> findAllFromTo(Integer first, Integer last) { Criteria criteria = getSession().createCriteria(persistentClass); criteria.setFirstResult(first);/* w w w . j ava 2s . c o m*/ criteria.setMaxResults(last - first); return criteria.list(); }