Search in sources :

Example 6 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore 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 7 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore 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 8 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore 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 9 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class AbstractDropNotNullDataMigration method getLiquibaseChangeLog.

@Override
public String getLiquibaseChangeLog() throws DataMigrationException {
    XWikiHibernateBaseStore store = getStore();
    Dialect dialect = store.getDialect();
    Configuration configuration = store.getConfiguration();
    Mapping mapping = configuration.buildMapping();
    PersistentClass pClass = configuration.getClassMapping(this.table.getName());
    Column column = ((Column) pClass.getProperty(this.property).getColumnIterator().next());
    String columnType = column.getSqlType(dialect, mapping);
    StringBuilder builder = new StringBuilder();
    builder.append("<changeSet author=\"xwiki\" id=\"R").append(this.getVersion().getVersion()).append("\">\n");
    builder.append("    <dropNotNullConstraint\n");
    builder.append("            columnDataType=\"").append(columnType).append('"').append('\n');
    builder.append("            columnName=\"").append(column.getName()).append('"').append('\n');
    builder.append("            tableName=\"").append(pClass.getTable().getName()).append("\"/>\n");
    builder.append("</changeSet>");
    return builder.toString();
}
Also used : Configuration(org.hibernate.cfg.Configuration) Column(org.hibernate.mapping.Column) Dialect(org.hibernate.dialect.Dialect) Mapping(org.hibernate.engine.Mapping) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 10 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class HibernateDataMigrationManager method getDBVersionFromDatabase.

@Override
public XWikiDBVersion getDBVersionFromDatabase() throws DataMigrationException {
    XWikiDBVersion ver = getDBVersionFromConfig();
    if (ver != null) {
        return ver;
    }
    final XWikiContext context = getXWikiContext();
    final XWikiHibernateBaseStore store = getStore();
    // Try retrieving a version from the database
    ver = store.failSafeExecuteRead(context, new HibernateCallback<XWikiDBVersion>() {

        @Override
        public XWikiDBVersion doInHibernate(Session session) throws HibernateException {
            // Retrieve the version from the database
            return (XWikiDBVersion) session.createCriteria(XWikiDBVersion.class).uniqueResult();
        }
    });
    // if it fails, return version 0 if there is some documents in the database, else null (empty db?)
    if (ver == null) {
        ver = store.failSafeExecuteRead(getXWikiContext(), new HibernateCallback<XWikiDBVersion>() {

            @Override
            public XWikiDBVersion doInHibernate(Session session) throws HibernateException {
                if (((Number) session.createCriteria(XWikiDocument.class).setProjection(Projections.rowCount()).uniqueResult()).longValue() > 0) {
                    return new XWikiDBVersion(0);
                }
                return null;
            }
        });
    }
    return ver;
}
Also used : XWikiDBVersion(com.xpn.xwiki.store.migration.XWikiDBVersion) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) HibernateCallback(com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback) Session(org.hibernate.Session)

Aggregations

XWikiHibernateBaseStore (com.xpn.xwiki.store.XWikiHibernateBaseStore)12 XWikiContext (com.xpn.xwiki.XWikiContext)10 Session (org.hibernate.Session)9 XWikiException (com.xpn.xwiki.XWikiException)7 HibernateException (org.hibernate.HibernateException)5 MailStoreException (org.xwiki.mail.MailStoreException)4 List (java.util.List)2 Query (org.hibernate.Query)2 Transaction (org.hibernate.Transaction)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 XWikiCacheStoreInterface (com.xpn.xwiki.store.XWikiCacheStoreInterface)1 HibernateCallback (com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 DataMigrationException (com.xpn.xwiki.store.migration.DataMigrationException)1 XWikiDBVersion (com.xpn.xwiki.store.migration.XWikiDBVersion)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Liquibase (liquibase.Liquibase)1