Example usage for org.hibernate Criteria scroll

List of usage examples for org.hibernate Criteria scroll

Introduction

In this page you can find the example usage for org.hibernate Criteria scroll.

Prototype

public ScrollableResults scroll() throws HibernateException;

Source Link

Document

Get the results as an instance of ScrollableResults .

Usage

From source file:com.duroty.application.mail.manager.PreferencesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/* w ww .  j  av  a 2s.  co  m*/
 *
 * @param hsession DOCUMENT ME!
 * @param token DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws ManagerException DOCUMENT ME!
 */
public Vector suggestContacts(Session hsession, String repositoryName, String token) throws Exception {
    Vector contacts = new Vector();

    try {
        Criteria crit = hsession.createCriteria(ContactList.class);
        crit.add(Restrictions.ilike("coliName", token, MatchMode.ANYWHERE));
        crit.add(Restrictions.eq("users", getUser(hsession, repositoryName)));
        crit.addOrder(Order.asc("coliName"));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            ContactList contactList = (ContactList) scroll.get(0);

            ContactObj obj = new ContactObj();
            obj.setIdint(contactList.getColiIdint());
            obj.setName(contactList.getColiName());
            obj.setEmail("GROUP");

            contacts.addElement(obj);
        }

        crit = hsession.createCriteria(Contact.class);
        crit.add(Restrictions.or(Restrictions.ilike("conName", token, MatchMode.ANYWHERE),
                Restrictions.ilike("conEmail", token, MatchMode.ANYWHERE)));
        crit.add(Restrictions.isNotNull("conSentDate"));
        crit.add(Restrictions.isNotNull("conReceivedDate"));
        crit.add(Restrictions.eq("users", getUser(hsession, repositoryName)));

        crit.add(Restrictions.eq("users", getUser(hsession, repositoryName)));
        crit.addOrder(Order.desc("conCount"));

        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());

            contacts.addElement(obj);
        }

        return contacts;
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.mail.manager.PreferencesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*  ww w.j a  va2 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 a v 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.duroty.application.mail.manager.PreferencesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from   ww  w  .j  a v a  2s.  co m*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param idints DOCUMENT ME!
 *
 * @throws Exception DOCUMENT ME!
 */
public void deleteContacts(Session hsession, String repositoryName, Integer[] idints) throws Exception {
    try {
        Users user = getUser(hsession, repositoryName);
        Criteria crit = hsession.createCriteria(Contact.class);
        crit.add(Restrictions.eq("users", user));
        crit.add(Restrictions.in("conIdint", idints));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Contact contact = (Contact) scroll.get(0);

            Criteria crit1 = hsession.createCriteria(Identity.class);
            crit1.add(Restrictions.eq("ideEmail", contact.getConEmail()));
            crit1.add(Restrictions.eq("ideActive", new Boolean(true)));

            ScrollableResults scroll2 = crit1.scroll();

            while (scroll2.next()) {
                Identity identity = (Identity) scroll2.get(0);
                Users buddy = identity.getUsers();

                if (buddy != null) {
                    Criteria auxCrit = hsession.createCriteria(BuddyList.class);
                    auxCrit.add(Restrictions.eq("usersByBuliOwnerIdint", user));
                    auxCrit.add(Restrictions.eq("usersByBuliBuddyIdint", buddy));

                    BuddyList buddyList = (BuddyList) auxCrit.uniqueResult();

                    if (buddyList != null) {
                        hsession.delete(buddyList);
                        hsession.flush();
                    }

                    auxCrit = hsession.createCriteria(BuddyList.class);
                    auxCrit.add(Restrictions.eq("usersByBuliOwnerIdint", buddy));
                    auxCrit.add(Restrictions.eq("usersByBuliBuddyIdint", user));

                    buddyList = (BuddyList) auxCrit.uniqueResult();

                    if (buddyList != null) {
                        hsession.delete(buddyList);
                        hsession.flush();
                    }
                }
            }

            hsession.delete(contact);
            hsession.flush();
        }
    } 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 va 2 s  .c o  m*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param idints DOCUMENT ME!
 *
 * @throws Exception DOCUMENT ME!
 */
public void deleteGroups(Session hsession, String repositoryName, Integer[] idints) throws Exception {
    try {
        Criteria crit = hsession.createCriteria(ContactList.class);
        crit.add(Restrictions.eq("users", getUser(hsession, repositoryName)));
        crit.add(Restrictions.in("coliIdint", idints));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            hsession.delete(scroll.get(0));
            hsession.flush();
        }
    } 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 va 2s  . com*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param idint DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
public void setIdentityDefault(Session hsession, String repositoryName, int idint) throws MailException {
    try {
        Users user = getUser(hsession, repositoryName);

        Criteria crit = hsession.createCriteria(Identity.class);
        crit.add(Restrictions.not(Restrictions.eq("ideIdint", new Integer(idint))));
        crit.add(Restrictions.eq("users", user));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Identity identity = (Identity) scroll.get(0);
            identity.setIdeDefault(false);
            hsession.update(identity);
        }

        crit = hsession.createCriteria(Identity.class);
        crit.add(Restrictions.eq("ideIdint", new Integer(idint)));
        crit.add(Restrictions.eq("users", user));

        Identity identity = (Identity) crit.uniqueResult();

        if (identity != null) {
            identity.setIdeDefault(true);
            hsession.update(identity);
            hsession.flush();
        } else {
            throw new MailException("Error in default identity");
        }
    } catch (Exception e) {
        throw new MailException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.service.analyzer.HibernateBayesianAnalyzer.java

License:Open Source License

/**
 * Loads the token frequencies from the database.
 * @param conn The connection for accessing the database
 * @throws Exception/*from  w w  w  .ja  va  2 s . co m*/
 */
public void loadHamNSpam(String username) throws Exception {
    SessionFactory hfactory = null;
    Session hsession = null;

    try {
        hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory);
        hsession = hfactory.openSession();

        Users user = getUser(hsession, username);

        Criteria crit = hsession.createCriteria(BayesianHam.class);
        crit.add(Restrictions.eq("users", user));

        ScrollableResults scroll = crit.scroll();

        Map ham = getHamTokenCounts();

        while (scroll.next()) {
            BayesianHam bham = (BayesianHam) scroll.get(0);

            String token = bham.getHamToken();
            int count = bham.getHamOcurrences();

            // to reduce memory, use the token only if the count is > 1
            if (count > 1) {
                ham.put(token, new Integer(count));
            }
        }

        crit = hsession.createCriteria(BayesianSpam.class);
        crit.add(Restrictions.eq("users", user));

        scroll = crit.scroll();

        Map spam = getSpamTokenCounts();

        while (scroll.next()) {
            BayesianSpam bspam = (BayesianSpam) scroll.get(0);
            String token = bspam.getSpamToken();
            int count = bspam.getSpamOcurrences();

            // to reduce memory, use the token only if the count is > 1
            if (count > 1) {
                spam.put(token, new Integer(count));
            }
        }

        //Get the ham/spam message counts.
        crit = hsession.createCriteria(BayesianMessagecounts.class);
        crit.add(Restrictions.eq("users", user));

        BayesianMessagecounts bmc = (BayesianMessagecounts) crit.uniqueResult();

        if (bmc != null) {
            setHamMessageCount(bmc.getHamCount());
            setSpamMessageCount(bmc.getSpamCount());
        }
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.task.BookmarkServiceTask.java

License:Open Source License

/**
 * DOCUMENT ME!/*from   w  w  w.j  a v a2  s . c  o  m*/
 */
private void flush() {
    setInit(true);

    SessionFactory hfactory = null;
    Session hsession = null;

    try {
        hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory);
        hsession = hfactory.openSession();

        Criteria crit = hsession.createCriteria(Bookmark.class);

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            if (pool.size() >= poolSize) {
                break;
            }

            Bookmark bookmark = (Bookmark) scroll.get(0);

            Users user = bookmark.getUsers();

            String idint = NumberUtils.pad(Long.parseLong(String.valueOf(bookmark.getBooIdint())));

            String key = idint + "--" + user.getUseUsername();

            if (!poolContains(key)) {
                addPool(key);

                BookmarkObj bookmarkObj = new BookmarkObj();
                bookmarkObj.setIdint(idint);
                bookmarkObj.setUrl(bookmark.getBooUrl());

                Bookmarklet bookmarklet = new Bookmarklet(this, idint, user.getUseUsername(), bookmarkObj);
                Thread thread = new Thread(bookmarklet, key);
                thread.start();

                hsession.delete(bookmark);
                hsession.flush();
            }

            Thread.sleep(100);
        }
    } catch (Exception e) {
        pool.clear();
        DLog.log(DLog.ERROR, this.getClass(), e.getMessage());
        System.gc();
    } catch (OutOfMemoryError e) {
        pool.clear();
        DLog.log(DLog.ERROR, this.getClass(), e.getMessage());
        System.gc();
    } catch (Throwable e) {
        pool.clear();
        DLog.log(DLog.ERROR, this.getClass(), e.getMessage());
    } finally {
        GeneralOperations.closeHibernateSession(hsession);

        setInit(false);
    }
}

From source file:com.duroty.task.ChatConversationsTask.java

License:Open Source License

/**
 * DOCUMENT ME!/* w ww  .  ja v a 2s  .  com*/
 *
 * @param dirUsers DOCUMENT ME!
 *
 * @throws Exception DOCUMENT ME!
 */
public void flush() throws Exception {
    SessionFactory hfactory = null;
    Session hsession = null;

    try {
        hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory);
        hsession = hfactory.openSession();

        Calendar cal = new GregorianCalendar();

        //int hour = cal.get(Calendar.HOUR_OF_DAY);
        //int minute = cal.get(Calendar.MINUTE);
        //int second = cal.get(Calendar.SECOND);
        Calendar cal1 = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE) - 1,
                cal.get(Calendar.SECOND));
        Date date = new Date(cal1.getTimeInMillis());

        Criteria crit = hsession.createCriteria(Conversations.class);
        crit.add(Restrictions.le("convStamp", date));
        crit.addOrder(Order.asc("convStamp"));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            hsession.delete(scroll.get(0));
            hsession.flush();
        }
    } catch (Exception e) {
        System.gc();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } catch (OutOfMemoryError e) {
        System.gc();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } catch (Throwable e) {
        System.gc();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } finally {
        GeneralOperations.closeHibernateSession(hsession);

        setInit(false);

        System.gc();
    }
}

From source file:com.duroty.task.ChatTask.java

License:Open Source License

/**
 * DOCUMENT ME!//from   w w w  .  j a v a 2  s.com
 *
 * @param dirUsers DOCUMENT ME!
 *
 * @throws Exception DOCUMENT ME!
 */
public void flush() throws Exception {
    SessionFactory hfactory = null;
    Session hsession = null;

    try {
        hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory);
        hsession = hfactory.openSession();

        Calendar cal = new GregorianCalendar();

        //int hour = cal.get(Calendar.HOUR_OF_DAY);
        //int minute = cal.get(Calendar.MINUTE);
        //int second = cal.get(Calendar.SECOND);
        Calendar cal1 = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE) - 5,
                cal.get(Calendar.SECOND));
        Date date = new Date(cal1.getTimeInMillis());

        Criteria crit = hsession.createCriteria(Users.class);
        crit.add(Restrictions.le("useLastPing", date));
        crit.add(Restrictions.not(Restrictions.eq("useIsOnline", new Integer(0))));
        crit.add(Restrictions.isNotNull("useIsOnline"));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Users user = (Users) scroll.get(0);
            user.setUseIsOnline(0);
            hsession.update(user);
            hsession.flush();
        }

        /*cal1 = new GregorianCalendar(cal.get(Calendar.YEAR),
            cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
            cal.get(Calendar.HOUR_OF_DAY),
            cal.get(Calendar.MINUTE) - 1, cal.get(Calendar.SECOND));
        date = new Date(cal1.getTimeInMillis());
                
        crit = hsession.createCriteria(Conversations.class);
        crit.add(Restrictions.le("convStamp", date));
                
        scroll = crit.scroll();
                
        while (scroll.next()) {
            Conversations conv = (Conversations) scroll.get(0);
        hsession.update(conv);
        hsession.flush();
        }*/
    } catch (Exception e) {
        System.gc();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } catch (OutOfMemoryError e) {
        System.gc();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } catch (Throwable e) {
        System.gc();

        StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        DLog.log(DLog.ERROR, this.getClass(), writer.toString());
    } finally {
        GeneralOperations.closeHibernateSession(hsession);

        setInit(false);

        System.gc();
    }
}