Example usage for org.hibernate ScrollableResults get

List of usage examples for org.hibernate ScrollableResults get

Introduction

In this page you can find the example usage for org.hibernate ScrollableResults get.

Prototype

Object get(int i);

Source Link

Document

Get the ith object in the current row of results, without initializing any other results in the row.

Usage

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

License:Open Source License

/**
 * DOCUMENT ME!/*www. ja  v  a  2 s.  c om*/
 *
 * @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   w w  w .ja  va 2  s .c  om
 *
 * @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!//from  w w  w  . j  a  va 2  s  .  c o  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 ww .ja  v  a  2s  . c om*/
 *
 * @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!// w ww .j  a  va  2 s . c om
 *
 * @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!/*  ww w  . j a va2  s  .co  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!// w ww  .  j  a v  a2 s.  c  om
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
public Vector getFilters(Session hsession, String repositoryName) throws MailException {
    Vector filters = new Vector();

    try {
        Query query = hsession.getNamedQuery("filters");
        query.setInteger("user", getUser(hsession, repositoryName).getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Filter filter = (Filter) scroll.get(0);
            FilterObj obj = new FilterObj();
            Label label = filter.getLabel();

            obj.setArchive(filter.isFilArchive());
            obj.setDoesntHaveWords(filter.getFilDoesntHaveWords());
            obj.setForward(filter.getFilForwardTo());
            obj.setFrom(filter.getFilFrom());
            obj.setHasAttachment(filter.isFilHasAttacment());
            obj.setHasWords(filter.getFilHasWords());
            obj.setIdint(filter.getFilIdint());
            obj.setImportant(filter.isFilImportant());
            obj.setLabel(new LabelObj(label.getLabIdint(), label.getLabName()));
            obj.setOperator(!filter.isFilOrOperator());
            obj.setSubject(filter.getFilSubject());
            obj.setTo(filter.getFilTo());
            obj.setTrash(filter.isFilTrash());

            filters.addElement(obj);
        }

        return filters;
    } 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   ww  w  . j  a va 2  s .c  o m
 *
 * @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.application.mail.manager.SearchManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*ww w. ja  v  a  2  s  .  c  o  m*/
 *
 * @param username DOCUMENT ME!
 * @param password DOCUMENT ME!
 * @param label DOCUMENT ME!
 * @param box DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws DMailException DOCUMENT ME!
 */
private ChainedFilter getChainedFilter(Session hsession, String username, int label, String box)
        throws Exception {
    ChainedFilter filter = null;

    try {
        Users user = getUser(hsession, username);

        org.hibernate.Query query1 = null;
        org.hibernate.Query query2 = null;

        String[] boxes = null;

        if ((box != null) && box.equals("ALL")) {
            boxes = new String[] { this.folderInbox, this.folderSent, this.folderDraft, this.folderHidden,
                    this.folderBlog, this.folderFiles };
        } else if (box != null) {
            boxes = new String[] { box };
        } else {
            boxes = new String[] { this.folderInbox, this.folderSent, this.folderDraft, this.folderHidden,
                    this.folderBlog, this.folderFiles };
        }

        if ((boxes == null) && (label == 0)) {
            return null;
        } else if ((boxes != null) && (label == 0)) {
            query1 = hsession.getNamedQuery("count-messages-by-folder");
            query1.setParameterList("folder", boxes);
            query1.setInteger("user", user.getUseIdint());

            query2 = hsession.getNamedQuery("messages-by-folder");
            query2.setParameterList("folder", boxes);
            query2.setInteger("user", user.getUseIdint());
        } else if ((boxes == null) && (label > 0)) {
            query1 = hsession.getNamedQuery("count-messages-by-label");
            query1.setInteger("label", label);
            query1.setInteger("user", user.getUseIdint());
            query1.setString("folderTrash", this.folderTrash);
            query1.setString("folderSpam", this.folderSpam);
            query1.setString("folderDelete", FOLDER_DELETE);

            query2 = hsession.getNamedQuery("messages-by-label");
            query2.setInteger("label", label);
            query2.setInteger("user", user.getUseIdint());
            query2.setString("folderTrash", this.folderTrash);
            query2.setString("folderSpam", this.folderSpam);
            query2.setString("folderDelete", FOLDER_DELETE);
        } else {
            query1 = hsession.getNamedQuery("count-messages-by-folder-label");
            query1.setInteger("label", label);
            query1.setParameterList("folder", boxes);
            query1.setInteger("user", user.getUseIdint());
            query1.setString("folderTrash", this.folderTrash);
            query1.setString("folderSpam", this.folderSpam);
            query1.setString("folderDelete", FOLDER_DELETE);

            query2 = hsession.getNamedQuery("messages-by-folder-label");
            query2.setInteger("label", label);
            query2.setParameterList("folder", boxes);
            query2.setInteger("user", user.getUseIdint());
            query2.setString("folderTrash", this.folderTrash);
            query2.setString("folderSpam", this.folderSpam);
            query2.setString("folderDelete", FOLDER_DELETE);
        }

        Integer count = (Integer) query1.uniqueResult();

        if (count.intValue() > 0) {
            QueryFilter[] qFilters = new QueryFilter[count.intValue()];

            ScrollableResults scroll = query2.scroll();

            int i = 0;

            while (scroll.next()) {
                Message message = (Message) scroll.get(0);
                qFilters[i] = new QueryFilter(
                        new TermQuery(new Term(LuceneMessageConstants.Field_idint, message.getMesName())));
                i++;
            }

            filter = new ChainedFilter(qFilters);
        } else {
            QueryFilter[] qFilters = new QueryFilter[0];
            filter = new ChainedFilter(qFilters);
        }

        return filter;
    } finally {
    }
}

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/*  w  ww. j a  v  a 2s.c om*/
 */
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);
    }
}