Example usage for org.hibernate Session getNamedQuery

List of usage examples for org.hibernate Session getNamedQuery

Introduction

In this page you can find the example usage for org.hibernate Session getNamedQuery.

Prototype

org.hibernate.Query getNamedQuery(String queryName);

Source Link

Document

Create a Query instance for the named query.

Usage

From source file:com.dgc.DAO.GenericDao.java

public List<E> executeNamedQuery(String namedQuery, HashMap<String, Object> parameters) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from  w  w w.j a  va  2  s  .c  om*/
    Query query = session.getNamedQuery(namedQuery);
    for (String parameter : parameters.keySet()) {
        if (parameters.get(parameter) instanceof List<?>) {
            query.setParameterList(parameter, (Collection<?>) parameters.get(parameter));
        } else {
            query.setParameter(parameter, parameters.get(parameter));
        }
    }
    @SuppressWarnings("unchecked")
    ArrayList<E> result = new ArrayList<E>((List<E>) query.list());
    session.getTransaction().commit();
    return result;
}

From source file:com.dumbweb.projectfutsal.dao.impl.TimingDAOImpl.java

@Override
public List<Timing> getAllTimings() {

    Session session = sessionFactory.openSession();
    return session.getNamedQuery("Timing.findAll").list();

}

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

License:Open Source License

/**
 * DOCUMENT ME!//from   w  w  w  . ja v  a  2s. c  o m
 *
 * @param hsession DOCUMENT ME!
 * @param user DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
protected int getUsedQuotaSize(org.hibernate.Session hsession, Users user) throws MailException {
    try {
        Query query = hsession.getNamedQuery("used-quota-size");
        query.setInteger("user", new Integer(user.getUseIdint()));

        Integer value = (Integer) query.uniqueResult();

        return value.intValue();
    } catch (Exception ex) {
        return 0;
    } finally {
    }
}

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

License:Open Source License

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

    try {
        Query query = hsession.getNamedQuery("delete-labels-by-mid");
        query.setParameterList("mids", mids);
        query.setString("username", repositoryName);

        query.executeUpdate();

        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 www.  j  a  v  a  2s . 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   www .j av  a  2 s  .  co  m
 *
 * @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 w w  .j  a v  a2 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!//  w  w  w .ja v  a  2s  .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 w w  .  j a va 2 s .  co  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 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  . ja va2s .co m*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param folderName DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public int getCountFiles(Session hsession, String repositoryName, String folderName, int label)
        throws FilesException {
    try {
        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("count-attachments");
            hquery.setParameterList("no_boxes",
                    new String[] { this.folderTrash, this.folderChat, this.folderSpam, FOLDER_DELETE });
            hquery.setInteger("user", getUser(hsession, repositoryName).getUseIdint());
        } else if ((folderNameList.length > 0) && (label <= 0)) {
            hquery = hsession.getNamedQuery("count-attachments-by-folder");
            hquery.setParameterList("boxes", folderNameList);
            hquery.setInteger("user", getUser(hsession, repositoryName).getUseIdint());
        } else if ((folderNameList.length == 0) && (label > 0)) {
            hquery = hsession.getNamedQuery("count-attachments-by-label");
            hquery.setInteger("label", label);
            hquery.setParameterList("no_boxes",
                    new String[] { this.folderTrash, this.folderChat, this.folderSpam, FOLDER_DELETE });
            hquery.setInteger("user", getUser(hsession, repositoryName).getUseIdint());
        } else if ((folderNameList.length > 0) && (label > 0)) {
            hquery = hsession.getNamedQuery("count-attachments-by-folder-label");
            hquery.setInteger("label", label);
            hquery.setParameterList("boxes", folderNameList);
            hquery.setInteger("user", getUser(hsession, repositoryName).getUseIdint());
        }

        return ((Integer) hquery.uniqueResult()).intValue();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}