Search in sources :

Example 11 with MailStoreException

use of org.xwiki.mail.MailStoreException in project xwiki-platform by xwiki.

the class DatabaseMailStatusStore method save.

@Override
public void save(final MailStatus status, final Map<String, Object> parameters) throws MailStoreException {
    XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore;
    XWikiContext xwikiContext = this.contextProvider.get();
    // Save in the main wiki
    String currentWiki = xwikiContext.getWikiId();
    xwikiContext.setWikiId(xwikiContext.getMainXWiki());
    try {
        // Delete any previous state of the message
        delete(status.getMessageId(), parameters);
        store.executeWrite(xwikiContext, new XWikiHibernateBaseStore.HibernateCallback<Object>() {

            @Override
            public Object doInHibernate(Session session) throws HibernateException, XWikiException {
                session.save(status);
                return null;
            }
        });
        // Log the save for debugging purpose
        this.logger.debug("Saved mail status [{}]", status);
    } catch (Exception e) {
        throw new MailStoreException(String.format("Failed to save mail status [%s] to the database.", status), e);
    } finally {
        xwikiContext.setWikiId(currentWiki);
    }
}
Also used : MailStoreException(org.xwiki.mail.MailStoreException) HibernateException(org.hibernate.HibernateException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException) MailStoreException(org.xwiki.mail.MailStoreException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session)

Example 12 with MailStoreException

use of org.xwiki.mail.MailStoreException in project xwiki-platform by xwiki.

the class DatabaseMailStatusStore method delete.

@Override
public void delete(final String uniqueMessageId, Map<String, Object> parameters) throws MailStoreException {
    XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore;
    XWikiContext xwikiContext = this.contextProvider.get();
    // Delete from the main wiki
    String currentWiki = xwikiContext.getWikiId();
    xwikiContext.setWikiId(xwikiContext.getMainXWiki());
    try {
        store.executeWrite(xwikiContext, new XWikiHibernateBaseStore.HibernateCallback<Object>() {

            @Override
            public Object doInHibernate(Session session) throws HibernateException, XWikiException {
                // Delete the message
                String queryString = String.format("delete from %s where mail_id=:id", MailStatus.class.getName());
                session.createQuery(queryString).setParameter(ID_PARAMETER_NAME, uniqueMessageId).executeUpdate();
                return null;
            }
        });
    } catch (Exception e) {
        throw new MailStoreException(String.format("Failed to delete mail status (message id [%s]) " + "from the database.", uniqueMessageId), e);
    } finally {
        xwikiContext.setWikiId(currentWiki);
    }
}
Also used : MailStoreException(org.xwiki.mail.MailStoreException) HibernateException(org.hibernate.HibernateException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException) MailStoreException(org.xwiki.mail.MailStoreException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session)

Example 13 with MailStoreException

use of org.xwiki.mail.MailStoreException in project xwiki-platform by xwiki.

the class DatabaseMailStatusStore method count.

@Override
public long count(final Map<String, Object> filterMap) throws MailStoreException {
    XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore;
    final XWikiContext xwikiContext = this.contextProvider.get();
    // Count in the main wiki
    String currentWiki = xwikiContext.getWikiId();
    xwikiContext.setWikiId(xwikiContext.getMainXWiki());
    // Compute the Query string based on the passed filter map
    final String queryString = computeCountQueryString(filterMap);
    try {
        Long count = store.executeRead(xwikiContext, new XWikiHibernateBaseStore.HibernateCallback<Long>() {

            @Override
            public Long doInHibernate(Session session) throws HibernateException, XWikiException {
                Query query = session.createQuery(queryString);
                query.setProperties(filterMap);
                return (Long) query.uniqueResult();
            }
        });
        return count;
    } catch (Exception e) {
        throw new MailStoreException(String.format("Failed to count mail statuses matching the filter [%s] from the database.", filterMap), e);
    } finally {
        xwikiContext.setWikiId(currentWiki);
    }
}
Also used : MailStoreException(org.xwiki.mail.MailStoreException) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException) MailStoreException(org.xwiki.mail.MailStoreException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session)

Example 14 with MailStoreException

use of org.xwiki.mail.MailStoreException in project xwiki-platform by xwiki.

the class DatabaseMailStatusStore method load.

@Override
public List<MailStatus> load(final Map<String, Object> filterMap, final int offset, final int count, String sortField, boolean sortAscending) throws MailStoreException {
    XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore;
    final XWikiContext xwikiContext = this.contextProvider.get();
    // Load from the main wiki
    String currentWiki = xwikiContext.getWikiId();
    xwikiContext.setWikiId(xwikiContext.getMainXWiki());
    // Compute the Query string based on the passed filter map
    final String queryString = computeSelectQueryString(filterMap, sortField, sortAscending);
    // Log query and parameters
    logQuery(queryString, filterMap);
    try {
        List<MailStatus> mailStatuses = store.executeRead(xwikiContext, new XWikiHibernateBaseStore.HibernateCallback<List<MailStatus>>() {

            @Override
            public List<MailStatus> doInHibernate(Session session) throws HibernateException, XWikiException {
                Query query = session.createQuery(queryString);
                if (offset > 0) {
                    query.setFirstResult(offset);
                }
                if (count > 0) {
                    query.setMaxResults(count);
                }
                query.setProperties(filterMap);
                List<MailStatus> queryResult = (List<MailStatus>) query.list();
                return queryResult;
            }
        });
        // Log loaded statuses
        if (this.logger.isDebugEnabled()) {
            for (MailStatus mailStatus : mailStatuses) {
                this.logger.debug("Loaded mail status [{}]", mailStatus);
            }
        }
        return mailStatuses;
    } catch (Exception e) {
        throw new MailStoreException(String.format("Failed to load mail statuses matching the filter [%s] from the database.", filterMap), e);
    } finally {
        xwikiContext.setWikiId(currentWiki);
    }
}
Also used : MailStoreException(org.xwiki.mail.MailStoreException) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) MailStoreException(org.xwiki.mail.MailStoreException) HibernateException(org.hibernate.HibernateException) List(java.util.List) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) MailStatus(org.xwiki.mail.MailStatus) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 15 with MailStoreException

use of org.xwiki.mail.MailStoreException in project xwiki-platform by xwiki.

the class FileSystemMailContentStore method delete.

@Override
public void delete(String batchId, String uniqueMessageId) throws MailStoreException {
    File messageFile = null;
    try {
        messageFile = getMessageFile(batchId, uniqueMessageId);
        messageFile.delete();
        // Also remove the directory. Note that it'll succeed only the directory is empty which is what we want.
        getBatchDirectory(batchId).delete();
    } catch (Exception e) {
        throw new MailStoreException(String.format("Failed to delete message (id [%s], batch id [%s]) file [%s]", uniqueMessageId, batchId, messageFile), e);
    }
}
Also used : MailStoreException(org.xwiki.mail.MailStoreException) File(java.io.File) MailStoreException(org.xwiki.mail.MailStoreException) InitializationException(org.xwiki.component.phase.InitializationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

MailStoreException (org.xwiki.mail.MailStoreException)16 MailStatus (org.xwiki.mail.MailStatus)8 Test (org.junit.Test)7 MailContentStore (org.xwiki.mail.MailContentStore)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 XWikiException (com.xpn.xwiki.XWikiException)4 XWikiHibernateBaseStore (com.xpn.xwiki.store.XWikiHibernateBaseStore)4 HibernateException (org.hibernate.HibernateException)4 Session (org.hibernate.Session)4 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)4 MailListener (org.xwiki.mail.MailListener)4 MailStatusStore (org.xwiki.mail.MailStatusStore)4 File (java.io.File)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 InitializationException (org.xwiki.component.phase.InitializationException)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 Session (javax.mail.Session)2 MimeMessage (javax.mail.internet.MimeMessage)2 Query (org.hibernate.Query)2