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");
}
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;
}
Aggregations