Search in sources :

Example 21 with QueryManager

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

the class CreateActionTest method setUp.

@Before
public void setUp() throws Exception {
    context = oldcore.getXWikiContext();
    Utils.setComponentManager(oldcore.getMocker());
    QueryManager mockSecureQueryManager = oldcore.getMocker().registerMockComponent((Type) QueryManager.class, "secure");
    mockTemplateProvidersQuery = mock(Query.class);
    when(mockSecureQueryManager.createQuery(any(), any())).thenReturn(mockTemplateProvidersQuery);
    when(mockTemplateProvidersQuery.execute()).thenReturn(Collections.emptyList());
    when(oldcore.getMockContextualAuthorizationManager().hasAccess(any(Right.class), any(EntityReference.class))).thenReturn(true);
    Provider<DocumentReference> mockDocumentReferenceProvider = oldcore.getMocker().registerMockComponent(DocumentReference.TYPE_PROVIDER);
    when(mockDocumentReferenceProvider.get()).thenReturn(new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome"));
    mockURLFactory = mock(XWikiURLFactory.class);
    context.setURLFactory(mockURLFactory);
    action = new CreateAction();
    mockRequest = mock(XWikiRequest.class);
    context.setRequest(mockRequest);
    mockResponse = mock(XWikiResponse.class);
    context.setResponse(mockResponse);
    when(mockRequest.get("type")).thenReturn("plain");
}
Also used : Query(org.xwiki.query.Query) QueryManager(org.xwiki.query.QueryManager) Right(org.xwiki.security.authorization.Right) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 22 with QueryManager

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

the class ScriptQuery method count.

/**
 * Allow to retrieve the total count of items for the given query instead of the actual results. This method will
 * only work for queries selecting document full names, see {@link CountDocumentFilter} for more information.
 *
 * @return the total number of results for this query.
 */
public long count() {
    long result = -1;
    try {
        // Create a copy of the wrapped query.
        QueryManager queryManager = (QueryManager) this.componentManager.getInstance(QueryManager.class);
        Query countQuery = queryManager.createQuery(getStatement(), getLanguage());
        countQuery.setWiki(getWiki());
        for (Map.Entry<Integer, Object> entry : getPositionalParameters().entrySet()) {
            countQuery.bindValue(entry.getKey(), entry.getValue());
        }
        for (Map.Entry<String, Object> entry : getNamedParameters().entrySet()) {
            countQuery.bindValue(entry.getKey(), entry.getValue());
        }
        for (QueryFilter filter : getFilters()) {
            countQuery.addFilter(filter);
        }
        // Add the count filter to it.
        countQuery.addFilter(this.componentManager.<QueryFilter>getInstance(QueryFilter.class, "count"));
        // Execute and retrieve the count result.
        List<Long> results = countQuery.execute();
        result = results.get(0);
    } catch (Exception e) {
        LOGGER.warn("Failed to create count query for query [{}]", getStatement());
        e.printStackTrace();
    }
    return result;
}
Also used : Query(org.xwiki.query.Query) SecureQuery(org.xwiki.query.SecureQuery) QueryException(org.xwiki.query.QueryException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) QueryFilter(org.xwiki.query.QueryFilter) QueryManager(org.xwiki.query.QueryManager) Map(java.util.Map)

Aggregations

QueryManager (org.xwiki.query.QueryManager)22 Query (org.xwiki.query.Query)18 DocumentReference (org.xwiki.model.reference.DocumentReference)11 Before (org.junit.Before)7 Test (org.junit.Test)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWiki (com.xpn.xwiki.XWiki)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 XWikiException (com.xpn.xwiki.XWikiException)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 Map (java.util.Map)4 BaseObject (com.xpn.xwiki.objects.BaseObject)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 QueryException (org.xwiki.query.QueryException)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Pair (org.apache.commons.lang3.tuple.Pair)2 Expectations (org.jmock.Expectations)2 ConfigurationSource (org.xwiki.configuration.ConfigurationSource)2