List of usage examples for org.hibernate Session getNamedQuery
org.hibernate.Query getNamedQuery(String queryName);
From source file:com.duroty.application.mail.manager.MailManager.java
License:Open Source License
/** * DOCUMENT ME!/*from www. jav a 2s. com*/ * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param label DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public int getCountMessages(Session hsession, String repositoryName, Label label) throws MailException { try { Query query = hsession.getNamedQuery("count-messages-by-label"); query.setInteger("label", label.getLabIdint()); query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); query.setString("folderSpam", this.folderSpam); query.setString("folderTrash", this.folderTrash); query.setString("folderDelete", FOLDER_DELETE); return ((Integer) query.uniqueResult()).intValue(); } catch (Exception e) { throw new MailException(e); } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.manager.MailManager.java
License:Open Source License
/** * DOCUMENT ME!/*from www . java 2 s . co m*/ * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param label 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, Label label, 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); Query hquery = hsession.getNamedQuery("messages-by-label"); String aux = hquery.getQueryString(); switch (order) { case ORDER_BY_IMPORTANT: if (orderType.equals("ASC")) { aux += " order by mes_flagged asc"; } else { aux += " order by mes_flagged desc"; } break; case ORDER_BY_ADDRESS: if (orderType.equals("ASC")) { aux += " order by mes_from asc"; } else { aux += " order by mes_from desc"; } break; case ORDER_BY_SIZE: if (orderType.equals("ASC")) { aux += " order by mes_size asc"; } else { aux += " order by mes_size desc"; } break; case ORDER_BY_SUBJECT: if (orderType.equals("ASC")) { aux += " order by mes_subject asc"; } else { aux += " order by mes_subject 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_UNREAD: if (orderType.equals("ASC")) { aux += " order by mes_recent asc"; } else { aux += " order by mes_recent desc"; } break; default: aux += " order by mes_date desc"; break; } SQLQuery h2query = hsession.createSQLQuery(aux); h2query.addEntity("i", Message.class); h2query.setInteger("label", label.getLabIdint()); h2query.setInteger("user", user.getUseIdint()); h2query.setString("folderTrash", this.folderTrash); h2query.setString("folderSpam", this.folderSpam); h2query.setString("folderDelete", FOLDER_DELETE); h2query.setFirstResult(page * messagesByPage); h2query.setMaxResults(messagesByPage); ScrollableResults scroll = h2query.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 buff = new StringBuffer(); while (it.hasNext()) { if (buff.length() > 0) { buff.append(", "); } LabMes labMes = (LabMes) it.next(); buff.append(labMes.getId().getLabel().getLabName()); } obj.setLabel(buff.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.MailManager.java
License:Open Source License
/** * DOCUMENT ME!// www .j ava 2 s . c o m * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public Counters getInfoCounters(Session hsession, String repositoryName) throws MailException { 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; }
From source file:com.duroty.application.mail.manager.MailManager.java
License:Open Source License
/** * DOCUMENT ME!//ww w . j a v a 2s. c om * * @param username DOCUMENT ME! * @param password DOCUMENT ME! * * @return DOCUMENT ME! * * @throws DMailException 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(); int uqs = value.intValue(); int pc = ((uqs * 25) / 100); return uqs + pc; } catch (Exception ex) { return 0; } finally { } }
From source file:com.duroty.application.mail.manager.PreferencesManager.java
License:Open Source License
/** * DOCUMENT ME!/* w w w.j av a2s . c o m*/ * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param idint DOCUMENT ME! * * @throws Exception DOCUMENT ME! */ public void deleteFilter(Session hsession, String repositoryName, Integer idint) throws Exception { try { Query query = hsession.getNamedQuery("delete-filter-by-idint"); query.setInteger("idint", idint); query.setString("username", repositoryName); query.executeUpdate(); 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 a 2s . 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 ava 2 s. co m * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param idint DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public FilterObj getFilter(Session hsession, String repositoryName, Integer idint) throws MailException { try { Query query = hsession.getNamedQuery("filter"); query.setInteger("idint", idint); query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); Filter filter = (Filter) query.uniqueResult(); 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()); return obj; } 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!//from w ww.j ava2 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.LuceneFiltersAnalysis.java
License:Open Source License
/** * DOCUMENT ME!/* www . j av a 2 s . c om*/ * * @param hsession DOCUMENT ME! * @param username DOCUMENT ME! * * @return DOCUMENT ME! */ private Vector getFilters(Session hsession, String username) { try { Vector filters = new Vector(); Criteria crit = hsession.createCriteria(Users.class); crit.add(Restrictions.eq("useUsername", username)); crit.add(Restrictions.eq("useActive", new Boolean(true))); Users user = (Users) crit.uniqueResult(); Query query = hsession.getNamedQuery("filter-by-user"); query.setInteger("user", user.getUseIdint()); ScrollableResults scroll = query.scroll(); while (scroll.next()) { filters.addElement(scroll.get(0)); } return filters; } catch (Exception ex) { DLog.log(DLog.ERROR, this.getClass(), ex); } finally { GeneralOperations.closeHibernateSession(hsession); } return null; }
From source file:com.duroty.task.IMAPServiceTask.java
License:Open Source License
/** * DOCUMENT ME!// w ww. ja va 2s.c om */ private void flush() { setInit(true); SessionFactory hfactory = null; Session hsession = null; javax.mail.Session msession = null; Store store = null; Folder rootFolder = null; Folder folder = null; try { hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory); hsession = hfactory.openSession(); msession = (javax.mail.Session) ctx.lookup(durotyMailFactory); store = msession.getStore("imap"); String imapHost = msession.getProperty("mail.imap.host"); Query query = hsession.getNamedQuery("users-mail"); query.setBoolean("active", true); query.setString("role", "mail"); ScrollableResults scroll = query.scroll(); while (scroll.next()) { Users user = (Users) scroll.get(0); String repositoryName = user.getUseUsername(); store.connect(imapHost, repositoryName, user.getUsePassword()); rootFolder = store.getDefaultFolder(); if ((rootFolder.getType() & Folder.HOLDS_FOLDERS) != 0) { folder = store.getFolder(this.imapInbox); Message[] messages = getMimeMessages(folder, new Flags(Flags.Flag.SEEN), false); MailUtilities.setFolderOpenAndReady(folder, Folder.READ_WRITE); if ((messages != null) && (messages.length > 0)) { for (int i = 0; i < messages.length; i++) { if (pool.size() >= poolSize) { DLog.log(DLog.WARN, this.getClass(), "PoolSize " + pool.size()); break; } MimeMessage mime = null; if (messages[i] instanceof MimeMessage) { mime = new MimeMessage((MimeMessage) messages[i]); String messageName = null; if (folder instanceof IMAPFolder) { messageName = String.valueOf(((IMAPFolder) folder).getUID(messages[i])); } else { messageName = String.valueOf(messages[i].getMessageNumber()); } messageName += this.imapInbox; boolean existMessage = existMessageName(hfactory.openSession(), user, messageName); if (existMessage) { messages[i].setFlag(Flags.Flag.SEEN, true); } else { String key = messageName + "--" + repositoryName; if (!poolContains(key)) { addPool(key); if (!isSpam(user, mime)) { Mailet mailet = new Mailet(this, messageName, repositoryName, mime); Thread thread = new Thread(mailet, key); thread.start(); //cal controlar si el borrem messages[i].setFlag(Flags.Flag.SEEN, true); } else { messages[i].setFlag(Flags.Flag.SEEN, true); messages[i].setFlag(Flags.Flag.DELETED, true); } } } Thread.sleep(100); } } } MailUtilities.setFolderClose(folder, true); folder = null; messages = null; folder = store.getFolder(this.imapSent); messages = getMimeMessages(folder, new Flags(Flags.Flag.SEEN), false); MailUtilities.setFolderOpenAndReady(folder, Folder.READ_WRITE); if (!folder.exists()) { folder.create(Folder.HOLDS_MESSAGES); } if (!folder.isSubscribed()) { folder.setSubscribed(true); } if ((messages != null) && (messages.length > 0)) { for (int i = 0; i < messages.length; i++) { if (pool.size() >= poolSize) { DLog.log(DLog.WARN, this.getClass(), "PoolSize " + pool.size()); break; } MimeMessage mime = null; if (messages[i] instanceof MimeMessage) { mime = new MimeMessage((MimeMessage) messages[i]); mime.addHeader("X-DBox", "SENT"); String messageName = null; if (folder instanceof IMAPFolder) { messageName = String.valueOf(((IMAPFolder) folder).getUID(messages[i])); } else { messageName = String.valueOf(messages[i].getMessageNumber()); } messageName += this.imapSent; boolean existMessage = existMessageName(hfactory.openSession(), user, messageName); if (existMessage) { messages[i].setFlag(Flags.Flag.SEEN, true); } else { String key = messageName + "--" + repositoryName; if (!poolContains(key)) { addPool(key); if (!isSpam(user, mime)) { Mailet mailet = new Mailet(this, messageName, repositoryName, mime); Thread thread = new Thread(mailet, key); thread.start(); //cal controlar si el borrem messages[i].setFlag(Flags.Flag.SEEN, true); } else { messages[i].setFlag(Flags.Flag.SEEN, true); messages[i].setFlag(Flags.Flag.DELETED, true); } } } Thread.sleep(100); } } } MailUtilities.setFolderClose(folder, true); folder = null; messages = null; folder = store.getFolder(this.imapDraft); messages = getMimeMessages(folder, new Flags(Flags.Flag.SEEN), false); MailUtilities.setFolderOpenAndReady(folder, Folder.READ_WRITE); if (!folder.exists()) { folder.create(Folder.HOLDS_MESSAGES); } if (!folder.isSubscribed()) { folder.setSubscribed(true); } if ((messages != null) && (messages.length > 0)) { for (int i = 0; i < messages.length; i++) { if (pool.size() >= poolSize) { DLog.log(DLog.WARN, this.getClass(), "PoolSize " + pool.size()); break; } MimeMessage mime = null; if (messages[i] instanceof MimeMessage) { mime = new MimeMessage((MimeMessage) messages[i]); mime.addHeader("X-DBox", "DRAFT"); String messageName = null; if (folder instanceof IMAPFolder) { messageName = String.valueOf(((IMAPFolder) folder).getUID(messages[i])); } else { messageName = String.valueOf(messages[i].getMessageNumber()); } messageName += this.imapDraft; boolean existMessage = existMessageName(hfactory.openSession(), user, messageName); if (existMessage) { messages[i].setFlag(Flags.Flag.SEEN, true); } else { String key = messageName + "--" + repositoryName; if (!poolContains(key)) { addPool(key); if (!isSpam(user, mime)) { Mailet mailet = new Mailet(this, messageName, repositoryName, mime); Thread thread = new Thread(mailet, key); thread.start(); //cal controlar si el borrem messages[i].setFlag(Flags.Flag.SEEN, true); } else { messages[i].setFlag(Flags.Flag.SEEN, true); messages[i].setFlag(Flags.Flag.DELETED, true); } } } Thread.sleep(100); } } } MailUtilities.setFolderClose(folder, true); folder = null; } MailUtilities.setFolderClose(rootFolder, true); MailUtilities.setStoreClose(store); } } catch (Exception e) { System.gc(); pool.clear(); StringWriter writer = new StringWriter(); e.printStackTrace(new PrintWriter(writer)); DLog.log(DLog.ERROR, this.getClass(), writer.toString()); } catch (OutOfMemoryError e) { System.gc(); pool.clear(); StringWriter writer = new StringWriter(); e.printStackTrace(new PrintWriter(writer)); DLog.log(DLog.ERROR, this.getClass(), writer.toString()); } catch (Throwable e) { System.gc(); pool.clear(); StringWriter writer = new StringWriter(); e.printStackTrace(new PrintWriter(writer)); DLog.log(DLog.ERROR, this.getClass(), writer.toString()); } finally { System.gc(); GeneralOperations.closeMailFolder(folder, false); GeneralOperations.closeMailFolder(rootFolder, false); GeneralOperations.closeMailStore(store); GeneralOperations.closeHibernateSession(hsession); setInit(false); } }