use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class XWQLQueryExecutor method execute.
@Override
public <T> List<T> execute(Query query) throws QueryException {
EntityReference currentEntityReference = this.context.getCurrentEntityReference();
Query nativeQuery;
try {
this.progress.startStep(query, "query.xwql.progress.execute", "Execute XWQL query [{}]", query);
if (query.getWiki() != null) {
if (currentEntityReference.getType() == EntityType.WIKI) {
this.context.setCurrentEntityReference(new WikiReference(query.getWiki()));
} else {
this.context.setCurrentEntityReference(currentEntityReference.replaceParent(currentEntityReference.extractReference(EntityType.WIKI), new WikiReference(query.getWiki())));
}
}
nativeQuery = getQueryManager().createQuery(this.translator.translate(query.getStatement()), this.translator.getOutputLanguage());
nativeQuery.setLimit(query.getLimit());
nativeQuery.setOffset(query.getOffset());
nativeQuery.setWiki(query.getWiki());
if (query.getFilters() != null) {
for (QueryFilter filter : query.getFilters()) {
nativeQuery.addFilter(filter);
}
}
for (Entry<String, Object> e : query.getNamedParameters().entrySet()) {
nativeQuery.bindValue(e.getKey(), e.getValue());
}
for (Entry<Integer, Object> e : query.getPositionalParameters().entrySet()) {
nativeQuery.bindValue(e.getKey(), e.getValue());
}
if (nativeQuery instanceof SecureQuery && query instanceof SecureQuery) {
// No need to validate the HQL query for short XWQL queries
if (((SecureQuery) query).isCurrentAuthorChecked() && !isShortFormStatement(query.getStatement())) {
((SecureQuery) nativeQuery).checkCurrentAuthor(true);
}
// Let HQL module take care of that is supported
((SecureQuery) nativeQuery).checkCurrentUser(((SecureQuery) query).isCurrentUserChecked());
}
return nativeQuery.execute();
} catch (Exception e) {
if (e instanceof QueryException) {
throw (QueryException) e;
}
throw new QueryException("Exception while translating [" + query.getStatement() + "] XWQL query to the [" + this.translator.getOutputLanguage() + "] language", query, e);
} finally {
this.context.setCurrentEntityReference(currentEntityReference);
this.progress.endStep(query);
}
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class HqlQueryExecutor method filterQuery.
private Query filterQuery(Query query, String language) {
Query filteredQuery = query;
// If there are Query parameters of type QueryParameter then, for convenience, automatically add the
// "escapeLikeParameters" filter (if not already there)
addEscapeLikeParametersFilter(query);
if (query.getFilters() != null && !query.getFilters().isEmpty()) {
for (QueryFilter filter : query.getFilters()) {
// Step 1: For backward-compatibility reasons call #filterStatement() first
String filteredStatement = filter.filterStatement(filteredQuery.getStatement(), language);
// statement.
if (!filteredStatement.equals(filteredQuery.getStatement())) {
filteredQuery = new WrappingQuery(filteredQuery) {
@Override
public String getStatement() {
return filteredStatement;
}
};
}
// Step 2: Run #filterQuery()
filteredQuery = filter.filterQuery(filteredQuery);
}
}
return filteredQuery;
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class DefaultQueryTest method addSameFilterMultipleTimes.
@Test
public void addSameFilterMultipleTimes() {
Query query = new DefaultQuery("", Query.XWQL, null);
QueryFilter filter = new HiddenDocumentFilter();
query.addFilter(filter);
query.addFilter(filter);
// We're assuming filters are used with our Component manager, with @Singleton. To go further we'd need to add
// QueryFilter#getName() or QueryFilter#getID() in order to forbid the addition of multiple identical filters.
assertTrue(query.getFilters().size() == 1);
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class UsersClassPropertyValuesProviderTest method getValuesLocal.
@Test
public void getValuesLocal() throws Exception {
when(this.wikiUserManager.getUserScope(this.classReference.getWikiReference().getName())).thenReturn(UserScope.LOCAL_ONLY);
DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
when(this.allowedValuesQuery.execute()).thenReturn(Collections.singletonList(new Object[] { aliceReference, " Alice One " }));
when(this.xcontext.getWiki().getDocument(aliceReference, this.xcontext)).thenReturn(mock(XWikiDocument.class, "alice"));
QueryFilter documentFilter = this.mocker.getInstance(QueryFilter.class, "document");
QueryFilter viewableFilter = this.mocker.getInstance(QueryFilter.class, "viewable");
List<QueryFilter> filters = mock(List.class);
DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
when(this.usedValuesQuery.getFilters()).thenReturn(filters);
when(this.usedValuesQuery.execute()).thenReturn(Arrays.asList(new Object[] { bobReference, 17L }, new Object[] { aliceReference, 3L }));
EntityReferenceSerializer<String> compactSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compact");
when(compactSerializer.serialize(aliceReference, this.classReference.getWikiReference())).thenReturn("Users.Alice");
when(compactSerializer.serialize(bobReference, this.classReference.getWikiReference())).thenReturn("Users.Bob");
when(this.xcontext.getWiki().getURL(aliceReference, this.xcontext)).thenReturn("url/to/alice");
when(this.xcontext.getWiki().getURL(bobReference, this.xcontext)).thenReturn("url/to/bob");
XWikiDocument bobProfile = mock(XWikiDocument.class);
XWikiAttachment bobAvatar = mock(XWikiAttachment.class);
AttachmentReference bobAvatarReference = new AttachmentReference("somePhoto.png", bobReference);
when(this.xcontext.getWiki().getDocument(bobReference, this.xcontext)).thenReturn(bobProfile);
when(bobProfile.getStringValue("avatar")).thenReturn(bobAvatarReference.getName());
when(bobProfile.getAttachment("somePhoto.png")).thenReturn(bobAvatar);
when(bobAvatar.isImage(this.xcontext)).thenReturn(true);
when(bobAvatar.getReference()).thenReturn(bobAvatarReference);
when(this.xcontext.getWiki().getURL(bobAvatarReference, "download", "width=30&height=30&keepAspectRatio=true", null, this.xcontext)).thenReturn("url/to/bob/avatar");
PropertyValues values = this.mocker.getComponentUnderTest().getValues(this.propertyReference, 5, "foo");
assertEquals(2, values.getPropertyValues().size());
assertEquals("Users.Alice", values.getPropertyValues().get(0).getValue());
assertEquals("Alice One", values.getPropertyValues().get(0).getMetaData().get("label"));
assertEquals(3L, values.getPropertyValues().get(0).getMetaData().get("count"));
assertEquals("url/to/noavatar.png", values.getPropertyValues().get(0).getMetaData().get("icon"));
assertEquals("url/to/alice", values.getPropertyValues().get(0).getMetaData().get("url"));
assertEquals("Users.Bob", values.getPropertyValues().get(1).getValue());
assertEquals("Bob", values.getPropertyValues().get(1).getMetaData().get("label"));
assertEquals(17L, values.getPropertyValues().get(1).getMetaData().get("count"));
assertEquals("url/to/bob/avatar", values.getPropertyValues().get(1).getMetaData().get("icon"));
assertEquals("url/to/bob", values.getPropertyValues().get(1).getMetaData().get("url"));
verify(this.allowedValuesQuery, never()).setWiki(any(String.class));
verify(this.allowedValuesQuery, times(1)).execute();
verify(filters).clear();
verify(this.usedValuesQuery).addFilter(documentFilter);
verify(this.usedValuesQuery).addFilter(viewableFilter);
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class HqlQueryExecutorTest method createNamedNativeHibernateQuery.
@Test
@SuppressWarnings("unchecked")
public void createNamedNativeHibernateQuery() throws Exception {
DefaultQuery query = new DefaultQuery("queryName", this.executor);
Session session = mock(Session.class);
SQLQuery sqlQuery = mock(SQLQuery.class);
when(session.getNamedQuery(query.getStatement())).thenReturn(sqlQuery);
when(sqlQuery.getQueryString()).thenReturn("foo");
// Add a Query Filter to verify it's called and can change the statement
QueryFilter filter = mock(QueryFilter.class);
query.addFilter(filter);
when(filter.filterStatement("foo", "sql")).thenReturn("bar");
when(filter.filterQuery(any(Query.class))).then(returnsFirstArg());
SQLQuery finalQuery = mock(SQLQuery.class);
when(session.createSQLQuery("bar")).thenReturn(finalQuery);
NamedSQLQueryDefinition definition = mock(NamedSQLQueryDefinition.class);
when(definition.getResultSetRef()).thenReturn("someResultSet");
HibernateSessionFactory sessionFactory = this.mocker.getInstance(HibernateSessionFactory.class);
sessionFactory.getConfiguration().getNamedSQLQueries().put(query.getStatement(), definition);
assertSame(finalQuery, this.executor.createHibernateQuery(session, query));
verify(finalQuery).setResultSetMapping(definition.getResultSetRef());
}
Aggregations