Search in sources :

Example 41 with Query

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

the class DefaultMessageStream method getRecentPersonalMessages.

@Override
public List<Event> getRecentPersonalMessages(DocumentReference author, int limit, int offset) {
    List<Event> result = new ArrayList<Event>();
    try {
        Query q = this.qm.createQuery("where event.application = 'MessageStream' and event.type = 'personalMessage'" + " and event.user = :user order by event.date desc", Query.XWQL);
        q.bindValue("user", this.serializer.serialize(author));
        q.setLimit(limit > 0 ? limit : 30).setOffset(offset >= 0 ? offset : 0);
        result = this.stream.searchEvents(q);
    } catch (QueryException ex) {
        LOG.warn("Failed to search personal messages: {}", ex.getMessage());
    }
    return result;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) Event(org.xwiki.eventstream.Event)

Example 42 with Query

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

the class DefaultMessageStream method deleteMessage.

@Override
public void deleteMessage(String id) {
    Query q;
    try {
        q = this.qm.createQuery("where event.id = :id", Query.XWQL);
        q.bindValue("id", id);
        List<Event> events = this.stream.searchEvents(q);
        if (events == null || events.isEmpty()) {
            throw new IllegalArgumentException("This message does not exist");
        } else if (events.get(0).getUser().equals(this.bridge.getCurrentUserReference())) {
            this.stream.deleteEvent(events.get(0));
        } else {
            throw new IllegalArgumentException("You are not authorized to delete this message");
        }
    } catch (QueryException ex) {
        LOG.warn("Failed to delete message: {}", ex.getMessage());
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) Event(org.xwiki.eventstream.Event)

Example 43 with Query

use of org.xwiki.query.Query 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 44 with Query

use of org.xwiki.query.Query 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 45 with Query

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

the class DefaultModelBridge method getDocumentReferences.

@Override
public List<DocumentReference> getDocumentReferences(SpaceReference spaceReference) {
    try {
        // At the moment we don't have a way to retrieve only the direct children so we select all the descendants.
        // This means we select all the documents from the specified space and from all the nested spaces.
        String statement = "select distinct(doc.fullName) from XWikiDocument as doc " + "where doc.space = :space or doc.space like :spacePrefix escape '/'";
        Query query = this.queryManager.createQuery(statement, Query.HQL);
        query.setWiki(spaceReference.getWikiReference().getName());
        String localSpaceReference = this.localEntityReferenceSerializer.serialize(spaceReference);
        query.bindValue("space", localSpaceReference);
        String spacePrefix = LIKE_SPECIAL_CHARS.matcher(localSpaceReference).replaceAll("/$1");
        query.bindValue("spacePrefix", spacePrefix + ".%");
        List<DocumentReference> descendants = new ArrayList<>();
        for (Object fullName : query.execute()) {
            descendants.add(this.explicitDocumentReferenceResolver.resolve((String) fullName, spaceReference));
        }
        return descendants;
    } catch (Exception e) {
        this.logger.error("Failed to retrieve the documents from [{}].", spaceReference, e);
        return Collections.emptyList();
    }
}
Also used : Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

Query (org.xwiki.query.Query)129 DocumentReference (org.xwiki.model.reference.DocumentReference)41 Test (org.junit.Test)39 QueryException (org.xwiki.query.QueryException)36 ArrayList (java.util.ArrayList)29 XWikiException (com.xpn.xwiki.XWikiException)18 QueryFilter (org.xwiki.query.QueryFilter)18 QueryManager (org.xwiki.query.QueryManager)18 XWikiContext (com.xpn.xwiki.XWikiContext)15 HashMap (java.util.HashMap)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)12 Map (java.util.Map)11 List (java.util.List)9 Before (org.junit.Before)9 SecureQuery (org.xwiki.query.SecureQuery)9 XWiki (com.xpn.xwiki.XWiki)8 BaseObject (com.xpn.xwiki.objects.BaseObject)8 SQLQuery (org.hibernate.SQLQuery)8 WikiReference (org.xwiki.model.reference.WikiReference)8 WrappingQuery (org.xwiki.query.WrappingQuery)8