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);
}
}
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);
}
}
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);
}
}
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();
}
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;
}
Aggregations