use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method exists.
/**
* Verifies if a wiki document exists
*/
@Override
public boolean exists(XWikiDocument doc, XWikiContext inputxcontext) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, true);
try {
boolean bTransaction = true;
MonitorPlugin monitor = Util.getMonitorPlugin(context);
try {
doc.setStore(this);
checkHibernate(context);
// Start monitoring timer
if (monitor != null) {
monitor.startTimer(HINT);
}
bTransaction = bTransaction && beginTransaction(null, context);
Session session = getSession(context);
String fullName = doc.getFullName();
String sql = "select doc.fullName from XWikiDocument as doc where doc.fullName=:fullName";
if (!doc.getLocale().equals(Locale.ROOT)) {
sql += " and doc.language=:language";
}
if (monitor != null) {
monitor.setTimerDesc(HINT, sql);
}
Query query = session.createQuery(sql);
query.setString("fullName", fullName);
if (!doc.getLocale().equals(Locale.ROOT)) {
query.setString("language", doc.getLocale().toString());
}
Iterator<String> it = query.list().iterator();
while (it.hasNext()) {
if (fullName.equals(it.next())) {
return true;
}
}
return false;
} catch (Exception e) {
Object[] args = { doc.getDocumentReferenceWithLocale() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_CHECK_EXISTS_DOC, "Exception while reading document {0}", e, args);
} finally {
// End monitoring timer
if (monitor != null) {
monitor.endTimer(HINT);
}
try {
if (bTransaction) {
endTransaction(context, false);
}
} catch (Exception e) {
}
}
} finally {
restoreExecutionXContext();
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method search.
@Override
public <T> List<T> search(String sql, int nb, int start, Object[][] whereParams, List<?> parameterValues, XWikiContext inputxcontext) throws XWikiException {
boolean bTransaction = true;
if (sql == null) {
return null;
}
XWikiContext context = getExecutionXContext(inputxcontext, true);
MonitorPlugin monitor = Util.getMonitorPlugin(context);
try {
// Start monitoring timer
if (monitor != null) {
monitor.startTimer(HINT);
}
checkHibernate(context);
bTransaction = beginTransaction(false, context);
Session session = getSession(context);
if (whereParams != null) {
sql += generateWhereStatement(whereParams);
}
Query query = session.createQuery(filterSQL(sql));
// Add values for provided HQL request containing "?" characters where to insert real
// values.
int parameterId = injectParameterListToQuery(0, query, parameterValues);
if (whereParams != null) {
for (Object[] whereParam : whereParams) {
query.setString(parameterId++, (String) whereParam[1]);
}
}
if (start != 0) {
query.setFirstResult(start);
}
if (nb != 0) {
query.setMaxResults(nb);
}
List<T> list = new ArrayList<T>();
list.addAll(query.list());
return list;
} catch (Exception e) {
Object[] args = { sql };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SEARCH, "Exception while searching documents with sql {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
}
restoreExecutionXContext();
// End monitoring timer
if (monitor != null) {
monitor.endTimer(HINT);
}
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method search.
public List search(Query query, int nb, int start, XWikiContext inputxcontext) throws XWikiException {
boolean bTransaction = true;
if (query == null) {
return null;
}
XWikiContext context = getExecutionXContext(inputxcontext, true);
MonitorPlugin monitor = Util.getMonitorPlugin(context);
try {
// Start monitoring timer
if (monitor != null) {
monitor.startTimer(HINT, query.getQueryString());
}
checkHibernate(context);
bTransaction = beginTransaction(false, context);
if (start != 0) {
query.setFirstResult(start);
}
if (nb != 0) {
query.setMaxResults(nb);
}
Iterator it = query.list().iterator();
List list = new ArrayList();
while (it.hasNext()) {
list.add(it.next());
}
if (bTransaction) {
// The session is closed here, too.
endTransaction(context, false, false);
bTransaction = false;
}
return list;
} catch (Exception e) {
Object[] args = { query.toString() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SEARCH, "Exception while searching documents with sql {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
}
restoreExecutionXContext();
// End monitoring timer
if (monitor != null) {
monitor.endTimer(HINT);
}
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method deleteWiki.
@Override
public void deleteWiki(String wikiName, XWikiContext inputxcontext) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, false);
boolean bTransaction = true;
String database = context.getWikiId();
Statement stmt = null;
try {
bTransaction = beginTransaction(context);
Session session = getSession(context);
Connection connection = session.connection();
stmt = connection.createStatement();
String schema = getSchemaFromWikiName(wikiName, context);
String escapedSchema = escapeSchema(schema, context);
executeDeleteWikiStatement(stmt, getDatabaseProductName(), escapedSchema);
endTransaction(context, true);
} catch (Exception e) {
Object[] args = { wikiName };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETE_DATABASE, "Exception while delete wiki database {0}", e, args);
} finally {
context.setWikiId(database);
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
}
try {
if (bTransaction) {
endTransaction(context, false);
}
} catch (Exception e) {
}
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method loadAttachmentList.
private void loadAttachmentList(XWikiDocument doc, XWikiContext context, boolean bTransaction) throws XWikiException {
try {
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(null, context);
}
Session session = getSession(context);
Query query = session.createQuery("from XWikiAttachment as attach where attach.docId=:docid");
query.setLong("docid", doc.getId());
@SuppressWarnings("unchecked") List<XWikiAttachment> list = query.list();
for (XWikiAttachment attachment : list) {
doc.setAttachment(attachment);
}
} catch (Exception e) {
this.logger.error("Failed to load attachments of document [{}]", doc.getDocumentReference(), e);
Object[] args = { doc.getDocumentReference() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SEARCHING_ATTACHMENT, "Exception while searching attachments for documents {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false);
}
} catch (Exception e) {
}
}
}
Aggregations