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