Search in sources :

Example 76 with XWikiException

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();
    }
}
Also used : Query(org.hibernate.Query) MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 77 with XWikiException

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);
        }
    }
}
Also used : Query(org.hibernate.Query) MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 78 with XWikiException

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);
        }
    }
}
Also used : MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) List(java.util.List) ArrayList(java.util.ArrayList) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException)

Example 79 with XWikiException

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) {
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 80 with XWikiException

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) {
        }
    }
}
Also used : Query(org.hibernate.Query) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)442 XWikiContext (com.xpn.xwiki.XWikiContext)156 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)147 DocumentReference (org.xwiki.model.reference.DocumentReference)98 BaseObject (com.xpn.xwiki.objects.BaseObject)88 IOException (java.io.IOException)57 QueryException (org.xwiki.query.QueryException)57 ArrayList (java.util.ArrayList)56 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)51 XWiki (com.xpn.xwiki.XWiki)48 XWikiRestException (org.xwiki.rest.XWikiRestException)44 Session (org.hibernate.Session)42 Document (com.xpn.xwiki.api.Document)38 InitializationException (org.xwiki.component.phase.InitializationException)36 WebApplicationException (javax.ws.rs.WebApplicationException)32 SQLException (java.sql.SQLException)31 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)30 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)29 UnexpectedException (org.xwiki.store.UnexpectedException)29 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)25