Search in sources :

Example 11 with XWikiHibernateStore

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

the class DatabasePingDataProviderTest method provideData.

@Test
public void provideData() throws Exception {
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(execution.getContext()).thenReturn(executionContext);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    when(executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).thenReturn(xwikiContext);
    com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class);
    when(xwikiContext.getWiki()).thenReturn(xwiki);
    XWikiCacheStoreInterface cacheStore = mock(XWikiCacheStoreInterface.class);
    when(xwiki.getStore()).thenReturn(cacheStore);
    XWikiHibernateStore store = mock(XWikiHibernateStore.class);
    when(cacheStore.getStore()).thenReturn(store);
    DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    when(store.getDatabaseMetaData()).thenReturn(databaseMetaData);
    when(databaseMetaData.getDatabaseProductName()).thenReturn("HSQL Database Engine");
    when(databaseMetaData.getDatabaseProductVersion()).thenReturn("2.2.9");
    Map<String, Object> data = this.mocker.getComponentUnderTest().provideData();
    assertEquals(2, data.size());
    assertEquals("HSQL Database Engine", data.get("dbName"));
    assertEquals("2.2.9", data.get("dbVersion"));
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) XWikiCacheStoreInterface(com.xpn.xwiki.store.XWikiCacheStoreInterface) XWikiContext(com.xpn.xwiki.XWikiContext) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 12 with XWikiHibernateStore

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

the class ActivityStreamImpl method loadActivityEvent.

/**
 * @param event the event
 * @param bTransaction true if inside a transaction
 * @param context the XWiki Context
 * @return the event
 * @throws ActivityStreamException
 */
private ActivityEventImpl loadActivityEvent(ActivityEvent event, boolean bTransaction, XWikiContext context) throws ActivityStreamException {
    boolean bTransactionMutable = bTransaction;
    ActivityEventImpl act = null;
    String eventId = event.getEventId();
    if (useLocalStore()) {
        // load event from the local database
        XWikiHibernateStore hibstore = context.getWiki().getHibernateStore();
        try {
            if (bTransactionMutable) {
                hibstore.checkHibernate(context);
                bTransactionMutable = hibstore.beginTransaction(false, context);
            }
            Session session = hibstore.getSession(context);
            Query query = session.createQuery("select act.eventId from ActivityEventImpl as act where act.eventId = :eventId");
            query.setString("eventId", eventId);
            if (query.uniqueResult() != null) {
                act = new ActivityEventImpl();
                session.load(act, eventId);
            }
            if (bTransactionMutable) {
                hibstore.endTransaction(context, false, false);
            }
        } catch (Exception e) {
            throw new ActivityStreamException();
        } finally {
            try {
                if (bTransactionMutable) {
                    hibstore.endTransaction(context, false, false);
                }
            } catch (Exception e) {
            // Do nothing.
            }
        }
    } else if (useMainStore()) {
        // load event from the main database
        String oriDatabase = context.getWikiId();
        context.setWikiId(context.getMainXWiki());
        XWikiHibernateStore hibstore = context.getWiki().getHibernateStore();
        try {
            if (bTransactionMutable) {
                hibstore.checkHibernate(context);
                bTransactionMutable = hibstore.beginTransaction(false, context);
            }
            Session session = hibstore.getSession(context);
            Query query = session.createQuery("select act.eventId from ActivityEventImpl as act where act.eventId = :eventId");
            query.setString("eventId", eventId);
            if (query.uniqueResult() != null) {
                act = new ActivityEventImpl();
                session.load(act, eventId);
            }
            if (bTransactionMutable) {
                hibstore.endTransaction(context, false, false);
            }
        } catch (Exception e) {
            throw new ActivityStreamException();
        } finally {
            context.setWikiId(oriDatabase);
            try {
                if (bTransactionMutable) {
                    hibstore.endTransaction(context, false, false);
                }
            } catch (Exception e) {
            // Do nothing.
            }
        }
    }
    return act;
}
Also used : XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) Query(org.hibernate.Query) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) XWikiException(com.xpn.xwiki.XWikiException) MalformedURLException(java.net.MalformedURLException) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) Session(org.hibernate.Session)

Example 13 with XWikiHibernateStore

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

the class DefaultEventStatusManager method saveEventStatusInStore.

private void saveEventStatusInStore(ActivityEventStatus eventStatus) throws ActivityStreamException {
    XWikiContext context = contextProvider.get();
    XWikiHibernateStore hibernateStore = context.getWiki().getHibernateStore();
    try {
        hibernateStore.beginTransaction(context);
        Session session = hibernateStore.getSession(context);
        session.save(eventStatus);
        hibernateStore.endTransaction(context, true);
    } catch (XWikiException e) {
        hibernateStore.endTransaction(context, false);
        throw new ActivityStreamException(e);
    }
}
Also used : XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) XWikiContext(com.xpn.xwiki.XWikiContext) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 14 with XWikiHibernateStore

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

the class RefererStatsStoreItem method storeInternal.

@Override
public void storeInternal(List<XWikiStatsStoreItem> stats) {
    RefererStatsStoreItem lastItem = (RefererStatsStoreItem) stats.get(stats.size() - 1);
    XWikiHibernateStore store = this.context.getWiki().getHibernateStore();
    if (store == null) {
        return;
    }
    RefererStats refererStat = new RefererStats(lastItem.name, lastItem.referer, lastItem.periodDate, lastItem.periodType);
    // Load old statistics object from database
    try {
        // TODO Fix use of deprecated call.
        store.loadXWikiCollection(refererStat, this.context, true);
    } catch (XWikiException e) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Failed to load referer statictics object [" + getId() + "]");
        }
    }
    // Increment counters
    refererStat.setIntValue("pageViews", refererStat.getPageViews() + stats.size());
    // Re-save statistics object
    try {
        // TODO Fix use of deprecated call.
        store.saveXWikiCollection(refererStat, this.context, true);
    } catch (XWikiException e) {
        LOGGER.error("Failed to save referer statictics object [" + getId() + "]");
    }
}
Also used : RefererStats(com.xpn.xwiki.stats.impl.RefererStats) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) XWikiException(com.xpn.xwiki.XWikiException)

Example 15 with XWikiHibernateStore

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

the class VisitStatsStoreItem method storeInternal.

@Override
public void storeInternal(List<XWikiStatsStoreItem> stats) {
    VisitStatsStoreItem firstItem = (VisitStatsStoreItem) stats.get(0);
    VisitStats oldVisitStats = firstItem.visitStats.getOldObject();
    VisitStatsStoreItem lastItem = (VisitStatsStoreItem) stats.get(stats.size() - 1);
    VisitStats newVisitStats = lastItem.visitStats;
    XWikiHibernateStore store = this.context.getWiki().getHibernateStore();
    if (store == null) {
        return;
    }
    try {
        // other one because the ID info have changed
        if (oldVisitStats != null) {
            try {
                // TODO Fix use of deprecated call.
                store.deleteXWikiCollection(oldVisitStats, this.context, true, true);
            } catch (Exception e) {
                if (LOGGER.isWarnEnabled()) {
                    LOGGER.error("Failed to delete old visit statistics object from database [{}]", getId(), e);
                }
            }
        }
        // TODO Fix use of deprecated call.
        store.saveXWikiCollection(newVisitStats, this.context, true);
    } catch (XWikiException e) {
        LOGGER.error("Failed to save visit statistics object [{}]", getId(), e);
    }
}
Also used : XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) VisitStats(com.xpn.xwiki.stats.impl.VisitStats) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)18 XWikiException (com.xpn.xwiki.XWikiException)15 ArrayList (java.util.ArrayList)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 Session (org.hibernate.Session)6 DocumentStats (com.xpn.xwiki.stats.impl.DocumentStats)4 ActivityStreamException (com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException)3 RefererStats (com.xpn.xwiki.stats.impl.RefererStats)2 VisitStats (com.xpn.xwiki.stats.impl.VisitStats)2 DataMigrationException (com.xpn.xwiki.store.migration.DataMigrationException)2 MalformedURLException (java.net.MalformedURLException)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 Test (org.junit.Test)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 XWikiRCSNodeInfo (com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 BaseObjectReference (com.xpn.xwiki.objects.BaseObjectReference)1 XWikiCacheStoreInterface (com.xpn.xwiki.store.XWikiCacheStoreInterface)1