Example usage for org.hibernate ScrollableResults next

List of usage examples for org.hibernate ScrollableResults next

Introduction

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

Prototype

boolean next();

Source Link

Document

Advance to the next result.

Usage

From source file:com.duroty.application.admin.manager.AdminManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from w  w w  . ja v  a  2  s  .c  o m*/
 *
 * @param hsession DOCUMENT ME!
 * @param idint DOCUMENT ME!
 *
 * @throws AdminException DOCUMENT ME!
 */
public void deleteUsers(Session hsession, Integer[] idints) throws AdminException {
    try {
        Criteria crit = hsession.createCriteria(Users.class);
        crit.add(Restrictions.in("useIdint", idints));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Users users = (Users) scroll.get(0);

            if (users != null) {
                hsession.delete(users);
                hsession.flush();
            }
        }
    } catch (Exception ex) {
        throw new AdminException(ex);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.admin.manager.AdminManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from   w w  w. j a  va  2 s  . c  o m
 *
 * @param hsession DOCUMENT ME!
 * @param idint DOCUMENT ME!
 *
 * @throws AdminException DOCUMENT ME!
 */
public Vector roles(Session hsession) throws AdminException {
    Vector roles = new Vector();

    try {
        Criteria crit = hsession.createCriteria(Roles.class);
        crit.addOrder(Order.asc("rolName"));

        ScrollableResults scroll = crit.scroll();

        while (scroll.next()) {
            Roles role = (Roles) scroll.get(0);
            roles.addElement(new RoleObj(role.getRolIdint(), role.getRolName()));
        }

        return roles;
    } catch (Exception ex) {
        throw new AdminException(ex);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.chat.manager.ChatManager.java

License:Open Source License

/**
 * DOCUMENT ME!//  ww w .j av  a2 s .c o m
 *
 * @param hsession DOCUMENT ME!
 * @param username DOCUMENT ME!
 * @param away DOCUMENT ME!
 *
 * @throws ChatException DOCUMENT ME!
 */
public String ping(Session hsession, String username, int away) throws ChatException {
    try {
        Vector buddiesOnline = new Vector();
        Vector buddiesOffline = new Vector();
        Vector buddies = new Vector();
        Vector messages = new Vector();
        JSONObject json = new JSONObject();

        Users user = getUser(hsession, username);

        if (user.getUseIsOnline() >= 3) {
            user.setUseLastPing(new Date());
            hsession.update(user);
            hsession.flush();
        } else if (user.getUseLastState() > 0) {
            user.setUseIsOnline(user.getUseLastState());
            user.setUseLastPing(new Date());
            hsession.update(user);
            hsession.flush();
        } else {
            user.setUseIsOnline(0);
            user.setUseCustomMessage(null);
            user.setUseLastPing(new Date());
            hsession.update(user);
            hsession.flush();

            throw new NotLoggedInException();
        }

        json.put("state", user.getUseIsOnline());
        json.put("lastState", user.getUseLastState());
        json.put("awayMessage", user.getUseCustomMessage());

        Criteria crit2 = hsession.createCriteria(Conversations.class);
        crit2.add(Restrictions.eq("usersByConvRecipientIdint", user));

        ScrollableResults scroll2 = crit2.scroll();

        int numMessages = 0;

        while (scroll2.next()) {
            Conversations conversations = (Conversations) scroll2.get(0);
            messages.addElement(new ConversationsObj(conversations.getUsersByConvSenderIdint().getUseUsername(),
                    conversations.getConvMessage()));

            hsession.delete(conversations);

            numMessages++;
        }

        hsession.flush();

        json.put("numMessages", new Integer(numMessages));
        json.put("messages", messages);

        Criteria crit1 = hsession.createCriteria(BuddyList.class);
        crit1.add(Restrictions.eq("usersByBuliOwnerIdint", user));
        crit1.add(Restrictions.eq("buliActive", new Boolean(true)));
        crit1.addOrder(Order.desc("buliLastDate"));

        ScrollableResults scroll1 = crit1.scroll();

        while (scroll1.next()) {
            BuddyList buddyList = (BuddyList) scroll1.get(0);
            Users buddy = buddyList.getUsersByBuliBuddyIdint();

            if (buddy.isUseActive()) {
                String name = buddy.getUseName();

                if (StringUtils.isBlank(name)) {
                    name = buddy.getUseUsername();
                }

                if (buddy.getUseIsOnline() == 0) {
                    buddiesOffline
                            .addElement(new BuddyObj(name, buddy.getUseUsername(), buddy.getUseIsOnline()));
                } else {
                    buddiesOnline
                            .addElement(new BuddyObj(name, buddy.getUseUsername(), buddy.getUseIsOnline()));
                }
            }
        }

        if (!buddiesOnline.isEmpty()) {
            buddies.addAll(buddiesOnline);
        }

        if (!buddiesOffline.isEmpty()) {
            buddies.addAll(buddiesOffline);
        }

        json.put("buddy", buddies);

        return json.toString();
    } catch (Exception e) {
        throw new ChatException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from ww  w  .j  av  a2  s.com
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
public Vector getLabels(Session hsession, String repositoryName) throws FilesException {
    Vector labels = new Vector();

    try {
        Criteria criteria = hsession.createCriteria(Label.class);
        criteria.add(Restrictions.eq("users", getUser(hsession, repositoryName)));
        criteria.addOrder(Order.asc("labName"));

        ScrollableResults scroll = criteria.scroll();

        while (scroll.next()) {
            Label label = (Label) scroll.get(0);

            LabelObj obj = new LabelObj(label.getLabIdint(), label.getLabName());

            labels.addElement(obj);
        }

        return labels;
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from www .j  a va 2 s .c o  m*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param mids DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void applyLabel(Session hsession, String repositoryName, Integer label, Integer[] idints)
        throws FilesException {
    if ((idints == null) || (idints.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

    try {
        if ((label == null) || (label.intValue() <= 0)) {
            return;
        }

        Users user = getUser(hsession, repositoryName);

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

        Label hlabel = (Label) crit.uniqueResult();

        Query query = hsession.getNamedQuery("messages-by-attachments");
        query.setParameterList("idints", idints);
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            LabMes labMes = new LabMes(new LabMesId(message, hlabel));
            hsession.saveOrUpdate(labMes);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from w  w  w . ja  va2s . c  om*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param mids DOCUMENT ME!
 * @param flag DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void flagFiles(Session hsession, String repositoryName, Integer[] idints) throws FilesException {
    if ((idints == null) || (idints.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

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

        Query query = hsession.getNamedQuery("messages-by-attachments");
        query.setParameterList("idints", idints);
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            if (message.isMesFlagged()) {
                message.setMesFlagged(false);
            } else {
                message.setMesFlagged(true);
            }

            hsession.update(message);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!//w  ww. j a  va2 s  .co  m
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param idints DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void deleteFiles(Session hsession, String repositoryName, Integer[] idints) throws FilesException {
    if ((idints == null) || (idints.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

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

        Query query = hsession.getNamedQuery("messages-by-attachments");
        query.setParameterList("idints", idints);
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            message.setMesBox(this.folderTrash);

            hsession.update(message);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from w  w w  . java 2  s. c o m
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param idints DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void restoreFiles(Session hsession, String repositoryName, Integer[] idints) throws FilesException {
    if ((idints == null) || (idints.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

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

        Query query = hsession.getNamedQuery("messages-by-attachments");
        query.setParameterList("idints", idints);
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            message.setMesBox(this.folderFiles);

            hsession.update(message);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from   w ww. j a  v a2 s.  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 FilesException DOCUMENT ME!
 */
public Vector getFiles(Session hsession, String repositoryName, String folderName, int label, int page,
        int messagesByPage, int order, String orderType) throws FilesException {
    Vector files = 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);

        Query hquery = null;

        String[] folderNameList = new String[0];

        try {
            folderName = parseFolder(folderName);

            folderNameList = new String[] { folderName };

            if (folderName.equals(this.folderAll) || folderName.equals(this.folderHidden)) {
                folderNameList = new String[] { this.folderAll, this.folderDraft, this.folderHidden,
                        this.folderImportant, this.folderInbox, this.folderSent };
            }
        } catch (Exception ex) {
        }

        if ((folderNameList.length == 0) && (label <= 0)) {
            hquery = hsession.getNamedQuery("attachments");
        } else if ((folderNameList.length > 0) && (label <= 0)) {
            hquery = hsession.getNamedQuery("attachments-by-folder");
        } else if ((folderNameList.length == 0) && (label > 0)) {
            hquery = hsession.getNamedQuery("attachments-by-label");
        } else if ((folderNameList.length > 0) && (label > 0)) {
            hquery = hsession.getNamedQuery("attachments-by-folder-label");
        }

        String aux = hquery.getQueryString();

        switch (order) {
        case ORDER_BY_SIZE:

            if (orderType.equals("ASC")) {
                aux += " order by att_size asc";
            } else {
                aux += " order by att_size desc";
            }

            break;

        case ORDER_BY_DATE:

            if (orderType.equals("ASC")) {
                aux += " order by mes_date asc";
            } else {
                aux += " order by mes_date desc";
            }

            break;

        case ORDER_BY_TYPE:

            if (orderType.equals("ASC")) {
                aux += " order by att_content_type asc";
            } else {
                aux += " order by att_content_type desc";
            }

            break;

        default:

            if (!orderType.equals("ASC")) {
                aux += " order by att_name desc";
            } else {
                aux += " order by att_name asc";
            }

            break;
        }

        SQLQuery h2query = hsession.createSQLQuery(aux);

        if ((folderNameList.length == 0) && (label <= 0)) {
            h2query.setParameterList("no_boxes",
                    new String[] { this.folderTrash, this.folderChat, this.folderSpam, FOLDER_DELETE });
            h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint());
        } else if ((folderNameList.length > 0) && (label <= 0)) {
            h2query.setParameterList("boxes", folderNameList);
            h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint());
        } else if ((folderNameList.length == 0) && (label > 0)) {
            h2query.setInteger("label", label);
            h2query.setParameterList("no_boxes",
                    new String[] { this.folderTrash, this.folderChat, this.folderSpam, FOLDER_DELETE });
            h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint());
        } else if ((folderNameList.length > 0) && (label > 0)) {
            h2query.setInteger("label", label);
            h2query.setParameterList("boxes", folderNameList);
            h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint());
        }

        h2query.setFirstResult(page * messagesByPage);
        h2query.setMaxResults(messagesByPage);

        h2query.addEntity("testo", AttachmentWithDate.class);

        ScrollableResults scroll = h2query.scroll();

        while (scroll.next()) {
            AttachmentWithDate attachment = (AttachmentWithDate) scroll.get(0);

            AttachmentObj obj = new AttachmentObj();
            obj.setContentType(attachment.getAttContentType());

            Date date = attachment.getAttDate();

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

            obj.setDate(date);
            obj.setIdint(attachment.getAttIdint());
            obj.setName(attachment.getAttName());
            obj.setPart(attachment.getAttPart());

            int size = attachment.getAttSize();
            size /= 1024;

            if (size > 1024) {
                size /= 1024;
                obj.setSize(size + " MB");
            } else {
                obj.setSize(((size > 0) ? (size + "") : "<1") + " kB");
            }

            String extension = (String) this.extensions.get(attachment.getAttContentType());

            if (StringUtils.isBlank(extension)) {
                extension = "generic";
            }

            obj.setExtension(extension);

            Message message = attachment.getMessage();

            if (message.isMesFlagged()) {
                obj.setFlagged(true);
            } else {
                obj.setFlagged(false);
            }

            if (message.getLabMeses() != null) {
                Iterator it = message.getLabMeses().iterator();
                StringBuffer lab = new StringBuffer();

                while (it.hasNext()) {
                    if (lab.length() > 0) {
                        lab.append(", ");
                    }

                    LabMes labMes = (LabMes) it.next();
                    lab.append(labMes.getId().getLabel().getLabName());
                }

                if (lab.length() > 0) {
                    obj.setLabel(lab.toString());
                } else {
                }
            }

            obj.setBox(message.getMesBox());

            obj.setMid(message.getMesName());

            files.addElement(obj);
        }

        return files;
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*from  w  ww  . jav a2  s .co m*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public Counters getInfoCounters(Session hsession, String repositoryName) throws FilesException {
    Counters counters = new Counters();

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

        Query query = hsession.getNamedQuery("count-new-messages-by-folder");
        query.setString("folder", this.folderInbox);
        query.setInteger("user", user.getUseIdint());
        counters.setInbox(((Integer) query.uniqueResult()).intValue());

        query = hsession.getNamedQuery("count-new-messages-by-folder");
        query.setString("folder", this.folderSpam);
        query.setInteger("user", user.getUseIdint());
        counters.setSpam(((Integer) query.uniqueResult()).intValue());

        query = hsession.getNamedQuery("group-count-new-messages-by-label");
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Integer idint = (Integer) scroll.get(0);
            Integer count = (Integer) scroll.get(1);

            counters.addLabel(idint.intValue(), count.intValue());
        }

        counters.setQuota(getQuotaLayer(hsession, user));
    } catch (Exception ex) {
        return null;
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }

    return counters;
}