List of usage examples for org.hibernate Criteria setFirstResult
public Criteria setFirstResult(int firstResult);
From source file:com.duroty.application.mail.manager.MailManager.java
License:Open Source License
/** * DOCUMENT ME!/*w w w. j a va2 s.c o m*/ * * @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!//from w w w . j a v a 2s . com * * @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 w w w.ja va 2 s. co m*/ * * @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!/*w w w. ja v a 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!// w w w. j av a2s .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.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); criteria.setMaxResults(last - first); return criteria.list(); }
From source file:com.emergya.persistenceGeo.dao.impl.MultiSirDatabaseGenericHibernateDAOImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w w w . j av a 2 s . co m public List<T> findAllFromTo(Integer first, Integer last) { Criteria criteria = getSession().createCriteria(persistentClass); criteria.setFirstResult(first); criteria.setMaxResults(last - first); return criteria.list(); }
From source file:com.emergya.persistenceGeo.dao.impl.MultiSirDatabaseGenericHibernateDAOImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from w ww . j a v a 2 s . c o m*/ public List<T> findOrdered(Integer first, Integer last, String orderField, boolean ascending) { Criteria criteria = getSession().createCriteria(persistentClass); criteria.setFirstResult(first); criteria.setMaxResults(last - first); if (ascending) { criteria.addOrder(Order.asc(orderField)); } else { criteria.addOrder(Order.desc(orderField)); } return criteria.list(); }
From source file:com.enonic.cms.store.dao.GroupEntityDao.java
License:Open Source License
@SuppressWarnings({ "unchecked" }) public List<GroupEntity> findByQuery(final GroupQuery spec) { return (List<GroupEntity>) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Criteria crit = session.createCriteria(GroupEntity.class).setCacheable(true); if (spec.getUserStoreKey() != null) { crit.add(Restrictions.eq("userStore.key", spec.getUserStoreKey().toInt())); } else if (spec.isGlobalOnly()) { crit.add(Restrictions.isNull("userStore.key")); }//from w w w .j av a 2 s .c o m if (!spec.isIncludeDeleted()) { crit.add(Restrictions.eq("deleted", 0)); } if (spec.getQuery() != null && spec.getQuery().length() > 0) { crit.add(Restrictions.ilike("name", spec.getQuery(), MatchMode.ANYWHERE)); } if (spec.getOrderBy() != null && !spec.getOrderBy().equals("")) { if (spec.isOrderAscending()) { crit.addOrder(Order.asc(spec.getOrderBy()).ignoreCase()); } else { crit.addOrder(Order.desc(spec.getOrderBy()).ignoreCase()); } } if (spec.getGroupTypes() != null) { Collection<GroupType> gt = new ArrayList<GroupType>(spec.getGroupTypes()); if (spec.isIncludeBuiltInGroups()) { if (!spec.isIncludeAnonymousGroups()) { gt.remove(GroupType.ANONYMOUS); } } else { gt.removeAll(GroupType.getBuiltInTypes()); } if (spec.isIncludeUserGroups()) { gt.add(GroupType.USER); } crit.add(Restrictions.in("type", GroupType.getIntegerValues(gt))); } else { Collection<GroupType> notGroupType = new ArrayList<GroupType>(); if (!spec.isIncludeBuiltInGroups()) { notGroupType.addAll(GroupType.getBuiltInTypes()); if (spec.isIncludeAnonymousGroups()) { notGroupType.remove(GroupType.ANONYMOUS); } } if (!spec.isIncludeUserGroups()) { notGroupType.add(GroupType.USER); } if (!spec.isIncludeAnonymousGroups() && !notGroupType.contains(GroupType.ANONYMOUS)) { notGroupType.add(GroupType.ANONYMOUS); } crit.add(Restrictions.not(Restrictions.in("type", GroupType.getIntegerValues(notGroupType)))); } crit.setFirstResult(spec.getIndex()); List list = crit.list(); if (spec.getCount() == null) { return list; } else { return list.subList(0, Math.min(spec.getCount(), list.size())); } } }); }
From source file:com.eryansky.common.orm.core.hibernate.support.HibernateSupportDao.java
License:Apache License
/** * ?Criteria,.// www. j av a2 s .c om * * @param c Hibernate Criteria * @param pageRequest ? * * @return {@link org.hibernate.Criteria} */ protected Criteria setPageRequestToCriteria(Criteria c, PageRequest pageRequest) { Assert.isTrue(pageRequest.getPageSize() > 0, "?0"); c.setFirstResult(pageRequest.getOffset()); c.setMaxResults(pageRequest.getPageSize()); if (pageRequest.isOrderBySetted()) { for (PageRequest.Sort sort : pageRequest.getSort()) { Order order = null; if (sort.getDir().equals(PageRequest.Sort.ASC)) { order = Order.asc(sort.getProperty()); } else { order = Order.desc(sort.getProperty()); } c.addOrder(order); } } return c; }