Search in sources :

Example 16 with QueryException

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

the class MessageStreamTest method testGetRecentPersonalMessagesWhenQueryFails.

@Test
public void testGetRecentPersonalMessagesWhenQueryFails() throws Exception {
    final Query mockQuery = getMockQuery();
    final QueryManager mockQueryManager = getComponentManager().getInstance(QueryManager.class);
    final EventStream mockEventStream = getComponentManager().getInstance(EventStream.class);
    final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    final EntityReferenceSerializer<String> mockSerializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
    getMockery().checking(new Expectations() {

        {
            allowing(mockBridge).getCurrentUserReference();
            will(returnValue(MessageStreamTest.this.currentUser));
            allowing(mockSerializer).serialize(MessageStreamTest.this.currentUser);
            will(returnValue("wiki:XWiki.JohnDoe"));
            exactly(1).of(mockQuery).setLimit(30);
            will(returnValue(mockQuery));
            exactly(1).of(mockQuery).setOffset(0);
            will(returnValue(mockQuery));
            allowing(mockQuery).bindValue(with(any(String.class)), with("wiki:XWiki.JohnDoe"));
            allowing(mockQueryManager).createQuery(with(aNonNull(String.class)), with(aNonNull(String.class)));
            will(returnValue(mockQuery));
            exactly(1).of(mockEventStream).searchEvents(with(mockQuery));
            will(throwException(new QueryException("", null, null)));
        }
    });
    List<Event> result = this.stream.getRecentPersonalMessages();
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isEmpty());
}
Also used : Expectations(org.jmock.Expectations) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) EventStream(org.xwiki.eventstream.EventStream) QueryManager(org.xwiki.query.QueryManager) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Event(org.xwiki.eventstream.Event) DefaultEvent(org.xwiki.eventstream.internal.DefaultEvent) Test(org.junit.Test)

Example 17 with QueryException

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

the class XWikiDocument method getChildrenReferences.

/**
 * Returns a list of references of all documents which list this document as their parent, in the current wiki.
 *
 * @param nb The number of results to return.
 * @param start The number of results to skip before we begin returning results.
 * @param context The {@link com.xpn.xwiki.XWikiContext context}.
 * @return the list of document references
 * @throws XWikiException If there's an error querying the database.
 * @since 2.2M2
 */
public List<DocumentReference> getChildrenReferences(int nb, int start, XWikiContext context) throws XWikiException {
    // Use cases:
    // - the parent document reference saved in the database matches the reference of this document, in its fully
    // serialized form (eg "wiki:space.page"). Note that this is normally not required since the wiki part
    // isn't saved in the database when it matches the current wiki.
    // - the parent document reference saved in the database matches the reference of this document, in its
    // serialized form without the wiki part (eg "space.page"). The reason we don't need to specify the wiki
    // part is because document parents saved in the database don't have the wiki part specified when it matches
    // the current wiki.
    // - the parent document reference saved in the database matches the page name part of this document's
    // reference (eg "page") and the parent document's space is the same as this document's space.
    List<DocumentReference> children = new ArrayList<DocumentReference>();
    try {
        Query query = getStore().getQueryManager().createQuery("select distinct doc.fullName from XWikiDocument doc where " + "doc.parent=:prefixedFullName or doc.parent=:fullName or (doc.parent=:name and doc.space=:space)", Query.XWQL);
        query.addFilter(Utils.getComponent(QueryFilter.class, "hidden"));
        query.bindValue("prefixedFullName", getDefaultEntityReferenceSerializer().serialize(getDocumentReference()));
        query.bindValue("fullName", LOCAL_REFERENCE_SERIALIZER.serialize(getDocumentReference()));
        query.bindValue("name", getDocumentReference().getName());
        query.bindValue("space", LOCAL_REFERENCE_SERIALIZER.serialize(getDocumentReference().getLastSpaceReference()));
        query.setLimit(nb).setOffset(start);
        List<String> queryResults = query.execute();
        WikiReference wikiReference = this.getDocumentReference().getWikiReference();
        for (String fullName : queryResults) {
            children.add(getCurrentDocumentReferenceResolver().resolve(fullName, wikiReference));
        }
    } catch (QueryException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, String.format("Failed to retrieve children for document [%s]", this.getDocumentReference()), e);
    }
    return children;
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) ToString(org.suigeneris.jrcs.util.ToString) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 18 with QueryException

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

the class TagQueryUtils method getDocumentsWithTag.

/**
 * Get documents with the passed tags with the result depending on whether the caller decides to include
 * hidden documents or not.
 *
 * @param tag a list of tags to match.
 * @param includeHiddenDocuments if true then include hidden documents
 * @param context XWiki context.
 * @return list of docNames.
 * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
 * @since 6.2M1
 */
public static List<String> getDocumentsWithTag(String tag, boolean includeHiddenDocuments, XWikiContext context) throws XWikiException {
    List<String> results;
    List<Object> parameters = new ArrayList<>();
    parameters.add(TagPlugin.TAG_CLASS);
    parameters.add(tag);
    String hql = ", BaseObject as obj, DBStringListProperty as prop join prop.list item where obj.className=? and " + "obj.name=doc.fullName and obj.id=prop.id.id and prop.id.name='tags' and lower(item)=lower(?) order by " + "doc.fullName";
    try {
        Query query = context.getWiki().getStore().getQueryManager().createQuery(hql, Query.HQL);
        query.bindValues(parameters);
        query.addFilter(Utils.getComponent(QueryFilter.class, UniqueDocumentFilter.HINT));
        if (!includeHiddenDocuments) {
            query.addFilter(Utils.getComponent(QueryFilter.class, HiddenDocumentFilter.HINT));
        }
        results = query.execute();
    } catch (QueryException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, String.format("Failed to search for document with tag [%s]", tag), e);
    }
    return results;
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) XWikiException(com.xpn.xwiki.XWikiException)

Example 19 with QueryException

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

the class WikiDescriptorMigrator method hibernateMigrate.

@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
    String hql = "SELECT DISTINCT doc.fullName FROM XWikiDocument doc, BaseObject obj WHERE doc.fullName = obj.name" + " AND obj.className = :className AND doc.fullName <> :template AND doc.fullName NOT IN " + "(SELECT DISTINCT doc2.fullName FROM XWikiDocument doc2, BaseObject obj2, StringProperty propPrettyName" + " WHERE doc2.fullName = obj2.name AND obj2.className = :className" + " AND propPrettyName.id = obj2.id AND propPrettyName.name = :propertyName)";
    try {
        Query query = queryManager.createQuery(hql, Query.HQL);
        query.bindValue("className", String.format("%s.%s", XWiki.SYSTEM_SPACE, XWikiServerClassDocumentInitializer.DOCUMENT_NAME));
        query.bindValue("propertyName", XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME);
        query.bindValue("template", "XWiki.XWikiServerClassTemplate");
        List<String> results = query.execute();
        for (String result : results) {
            fixDocument(result);
        }
    } catch (QueryException e) {
        logger.error("Failed to perform a query on the main wiki.", e);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query)

Example 20 with QueryException

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

the class DefaultWikiDescriptorDocumentHelper method findXWikiServerClassDocumentReference.

@Override
public DocumentReference findXWikiServerClassDocumentReference(String wikiAlias) throws WikiManagerException {
    if (wikiAlias == null) {
        throw new IllegalArgumentException("Wiki alias cannot be null");
    }
    WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
    DocumentReference result = null;
    try {
        Query query = this.queryManager.createQuery("where doc.object(XWiki.XWikiServerClass).server = :wikiAlias and doc.name like 'XWikiServer%'", Query.XWQL);
        query.bindValue("wikiAlias", wikiAlias);
        query.setWiki(wikiDescriptorManager.getMainWikiId());
        List<String> documentNames = query.execute();
        // Resolve the document name into a references
        if (documentNames != null && !documentNames.isEmpty()) {
            result = documentReferenceResolver.resolve(documentNames.get(0));
        }
    } catch (QueryException e) {
        throw new WikiManagerException(String.format("Failed to locate XWiki.XWikiServerClass document for wiki alias [%s]", wikiAlias), e);
    }
    return result;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) DocumentReference(org.xwiki.model.reference.DocumentReference)

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