Search in sources :

Example 31 with QueryException

use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.

the class DefaultWikiCopier method copyDocuments.

@Override
public void copyDocuments(String fromWikiId, String toWikiId, boolean withHistory) throws WikiManagerException {
    XWikiContext context = xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    this.progress.pushLevelProgress(2, this);
    try {
        // Get documents
        this.progress.startStep(this, "Get documents to copy");
        Query query = queryManager.createQuery("select distinct doc.fullName from Document as doc", Query.XWQL);
        query.setWiki(fromWikiId);
        List<String> documentFullnames = query.execute();
        this.progress.endStep(this);
        // Copy documents
        this.progress.startStep(this, "Copy documents");
        this.progress.pushLevelProgress(documentFullnames.size(), this);
        WikiReference fromWikiReference = new WikiReference(fromWikiId);
        try {
            for (String documentFullName : documentFullnames) {
                this.progress.startStep(this);
                DocumentReference origDocReference = documentReferenceResolver.resolve(documentFullName, fromWikiReference);
                DocumentReference newDocReference = origDocReference.setWikiReference(new WikiReference(toWikiId));
                logger.info("Copying document [{}] to [{}].", origDocReference, newDocReference);
                xwiki.copyDocument(origDocReference, newDocReference, null, !withHistory, true, context);
                logger.info("Done copying document [{}] to [{}].", origDocReference, newDocReference);
                this.progress.endStep(this);
            }
        } finally {
            this.progress.popLevelProgress(this);
            this.progress.endStep(this);
        }
    } catch (QueryException e) {
        WikiManagerException thrownException = new WikiManagerException("Unable to get the list of wiki documents to copy.", e);
        logger.error(thrownException.getMessage(), thrownException);
        throw thrownException;
    } catch (XWikiException e) {
        WikiManagerException thrownException = new WikiManagerException("Failed to copy documents.", e);
        logger.error(thrownException.getMessage(), thrownException);
        throw thrownException;
    } finally {
        this.progress.popLevelProgress(this);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 32 with QueryException

use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.

the class WikiDescriptorMigratorTest method hibernateMigrateWhenQueryException.

@Test
public void hibernateMigrateWhenQueryException() throws Exception {
    List<String> documentList = new ArrayList<>();
    documentList.add("XWiki.XWikiServerSubwiki1");
    Exception exception = new QueryException("error in queryManager.createQuery()", null, null);
    when(queryManager.createQuery(any(), eq(Query.HQL))).thenThrow(exception);
    // Test
    mocker.getComponentUnderTest().hibernateMigrate();
    // Verify
    verify(mocker.getMockedLogger()).error("Failed to perform a query on the main wiki.", exception);
}
Also used : QueryException(org.xwiki.query.QueryException) ArrayList(java.util.ArrayList) XWikiException(com.xpn.xwiki.XWikiException) QueryException(org.xwiki.query.QueryException) Test(org.junit.Test)

Example 33 with QueryException

use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.

the class SolrQueryExecutor method execute.

@Override
public <T> List<T> execute(Query query) throws QueryException {
    this.progress.startStep(query, "query.solr.progress.execute", "Execute Solr query [{}]", query);
    this.progress.pushLevelProgress(3, query);
    try {
        this.progress.startStep(query, "query.solr.progress.execute.prepare", "Prepare");
        SolrInstance solrInstance = solrInstanceProvider.get();
        SolrQuery solrQuery = createSolrQuery(query);
        this.progress.startStep(query, "query.solr.progress.execute.execute", "Execute");
        QueryResponse response = solrInstance.query(solrQuery);
        this.progress.startStep(query, "query.solr.progress.execute.filter", "Filter");
        // Check access rights need to be checked before returning the response.
        // FIXME: this is not really the best way, mostly because at this point all grouping operations
        // have already been performed and any change on the result will not ensure that the grouping
        // information (facets, highlighting, maxScore, etc.) is still relevant.
        // A better way would be using a PostFilter as described in this article:
        // http://java.dzone.com/articles/custom-security-filtering-solr
        // Basically, we would be asking
        List<DocumentReference> usersToCheck = new ArrayList<>(2);
        if (query instanceof SecureQuery) {
            if (((SecureQuery) query).isCurrentUserChecked()) {
                usersToCheck.add(xcontextProvider.get().getUserReference());
            }
            if (((SecureQuery) query).isCurrentAuthorChecked()) {
                usersToCheck.add(xcontextProvider.get().getAuthorReference());
            }
        } else {
            usersToCheck.add(xcontextProvider.get().getUserReference());
            usersToCheck.add(xcontextProvider.get().getAuthorReference());
        }
        if (!usersToCheck.isEmpty()) {
            filterResponse(response, usersToCheck);
        }
        return (List<T>) Arrays.asList(response);
    } catch (Exception e) {
        throw new QueryException("Exception while executing query", query, e);
    } finally {
        this.progress.popLevelProgress(query);
        this.progress.endStep(query);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) List(java.util.List) SecureQuery(org.xwiki.query.SecureQuery) SolrQuery(org.apache.solr.client.solrj.SolrQuery) DocumentReference(org.xwiki.model.reference.DocumentReference) QueryException(org.xwiki.query.QueryException) SolrInstance(org.xwiki.search.solr.internal.api.SolrInstance)

Example 34 with QueryException

use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.

the class RecordableEventMigrator method hibernateMigrate.

@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
    String hql = "select event from ActivityEventImpl event where event.page LIKE concat(event.wiki, ':%')";
    try {
        List<ActivityEventImpl> events;
        do {
            Query query = queryManager.createQuery(hql, Query.HQL);
            query.setLimit(50);
            events = query.execute();
            for (ActivityEventImpl event : events) {
                fixEvent(event);
            }
        } while (!events.isEmpty());
    } catch (QueryException e) {
        throw new DataMigrationException("Failed to fix RecordableEvent problems.", e);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ActivityEventImpl(com.xpn.xwiki.plugin.activitystream.impl.ActivityEventImpl) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException)

Example 35 with QueryException

use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.

the class DefaultIconSetManager method getIconSet.

@Override
public IconSet getIconSet(String name) throws IconException {
    // Special case: the default icon theme
    if (DEFAULT_ICONSET_NAME.equals(name)) {
        return getDefaultIconSet();
    }
    // Get the icon set from the cache
    IconSet iconSet = iconSetCache.get(name, wikiDescriptorManager.getCurrentWikiId());
    // Load it if it is not loaded yet
    if (iconSet == null) {
        try {
            // Search by name
            String xwql = "FROM doc.object(IconThemesCode.IconThemeClass) obj WHERE obj.name = :name";
            Query query = queryManager.createQuery(xwql, Query.XWQL);
            query.bindValue("name", name);
            List<String> results = query.execute();
            if (results.isEmpty()) {
                return null;
            }
            // Get the first result
            String docName = results.get(0);
            DocumentReference docRef = documentReferenceResolver.resolve(docName);
            // Load the icon theme
            iconSet = iconSetLoader.loadIconSet(docRef);
            // Put it in the cache
            iconSetCache.put(docRef, iconSet);
            iconSetCache.put(name, wikiDescriptorManager.getCurrentWikiId(), iconSet);
        } catch (QueryException e) {
            throw new IconException(String.format("Failed to load the icon set [%s].", name), e);
        }
    }
    // Return the icon set
    return iconSet;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) DocumentReference(org.xwiki.model.reference.DocumentReference) IconSet(org.xwiki.icon.IconSet) IconException(org.xwiki.icon.IconException)

Aggregations

QueryException (org.xwiki.query.QueryException)57 Query (org.xwiki.query.Query)32 DocumentReference (org.xwiki.model.reference.DocumentReference)19 XWikiException (com.xpn.xwiki.XWikiException)18 ArrayList (java.util.ArrayList)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 BaseObject (com.xpn.xwiki.objects.BaseObject)8 Test (org.junit.Test)8 QueryFilter (org.xwiki.query.QueryFilter)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 HashMap (java.util.HashMap)6 XWikiRestException (org.xwiki.rest.XWikiRestException)6 Event (org.xwiki.eventstream.Event)5 WikiReference (org.xwiki.model.reference.WikiReference)5 Date (java.util.Date)4 List (java.util.List)4 IconException (org.xwiki.icon.IconException)4 QueryManager (org.xwiki.query.QueryManager)4 SecureQuery (org.xwiki.query.SecureQuery)4 XWiki (com.xpn.xwiki.XWiki)3