Search in sources :

Example 1 with QueryFilter

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);
    }
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) SecureQuery(org.xwiki.query.SecureQuery) EntityReference(org.xwiki.model.reference.EntityReference) WikiReference(org.xwiki.model.reference.WikiReference) SecureQuery(org.xwiki.query.SecureQuery) QueryException(org.xwiki.query.QueryException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 2 with QueryFilter

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;
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SecureQuery(org.xwiki.query.SecureQuery) WrappingQuery(org.xwiki.query.WrappingQuery)

Example 3 with QueryFilter

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);
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) Query(org.xwiki.query.Query) Test(org.junit.Test)

Example 4 with QueryFilter

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);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryFilter(org.xwiki.query.QueryFilter) PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 5 with QueryFilter

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());
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) NamedSQLQueryDefinition(org.hibernate.engine.NamedSQLQueryDefinition) DefaultQuery(org.xwiki.query.internal.DefaultQuery) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) DefaultQuery(org.xwiki.query.internal.DefaultQuery) WrappingQuery(org.xwiki.query.WrappingQuery) HibernateSessionFactory(com.xpn.xwiki.store.hibernate.HibernateSessionFactory) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

QueryFilter (org.xwiki.query.QueryFilter)20 Query (org.xwiki.query.Query)15 Test (org.junit.Test)8 QueryException (org.xwiki.query.QueryException)7 SQLQuery (org.hibernate.SQLQuery)5 WrappingQuery (org.xwiki.query.WrappingQuery)5 XWikiException (com.xpn.xwiki.XWikiException)4 Session (org.hibernate.Session)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 SecureQuery (org.xwiki.query.SecureQuery)4 QueryManager (org.xwiki.query.QueryManager)3 DefaultQuery (org.xwiki.query.internal.DefaultQuery)3 Document (com.xpn.xwiki.api.Document)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Pair (org.apache.commons.lang3.tuple.Pair)2 XWikiRestException (org.xwiki.rest.XWikiRestException)2