Search in sources :

Example 1 with HibernateCallback

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

XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiHibernateBaseStore (com.xpn.xwiki.store.XWikiHibernateBaseStore)1 HibernateCallback (com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback)1 XWikiDBVersion (com.xpn.xwiki.store.migration.XWikiDBVersion)1 Session (org.hibernate.Session)1