Search in sources :

Example 1 with QueryParameter

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

the class DefaultQuery method bindValue.

@Override
public QueryParameter bindValue(String var) {
    QueryParameter parameter = new DefaultQueryParameter(this);
    bindValue(var, parameter);
    return parameter;
}
Also used : QueryParameter(org.xwiki.query.QueryParameter)

Example 2 with QueryParameter

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

the class DBListClassPropertyValuesProviderTest method getValuesMixedWithUsed.

@Test
public void getValuesMixedWithUsed() throws Exception {
    ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
    DocumentReference authorReference = this.dbListClass.getOwnerDocument().getAuthorReference();
    PropertyValues values = new PropertyValues();
    PropertyValue red = new PropertyValue();
    red.setValue("red");
    red.setMetaData(new HashMap<String, Object>());
    red.getMetaData().put("label", "Red");
    values.getPropertyValues().add(red);
    when(this.authorExecutor.call(any(), eq(authorReference))).thenReturn(values);
    Query query = mock(Query.class);
    QueryParameter queryParameter = mock(QueryParameter.class);
    when(this.usedValuesQueryBuilder.build(dbListClass)).thenReturn(query);
    when(query.bindValue("text")).thenReturn(queryParameter);
    when(queryParameter.anyChars()).thenReturn(queryParameter);
    when(queryParameter.literal("bar")).thenReturn(queryParameter);
    when(query.execute()).thenReturn(Arrays.asList(new Object[] { "blue", 21L }, new Object[] { "red", 17L }));
    assertSame(values, this.mocker.getComponentUnderTest().getValues(propertyReference, 3, "bar"));
    verify(query).setLimit(2);
    verify(query).addFilter(this.mocker.getInstance(QueryFilter.class, "text"));
    verify(queryParameter, times(2)).anyChars();
    assertEquals(2, values.getPropertyValues().size());
    assertEquals("red", values.getPropertyValues().get(0).getValue());
    assertEquals(17L, values.getPropertyValues().get(0).getMetaData().get("count"));
    assertEquals("blue", values.getPropertyValues().get(1).getValue());
    assertEquals(21L, values.getPropertyValues().get(1).getMetaData().get("count"));
}
Also used : QueryParameter(org.xwiki.query.QueryParameter) QueryFilter(org.xwiki.query.QueryFilter) PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) Query(org.xwiki.query.Query) PropertyValue(org.xwiki.rest.model.jaxb.PropertyValue) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 3 with QueryParameter

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

the class ExportAction method resolvePagesToExport.

private Collection<DocumentReference> resolvePagesToExport(String[] pages, XWikiContext context) throws XWikiException {
    List<DocumentReference> pageList = new ArrayList<>();
    if (pages == null || pages.length == 0) {
        pageList.add(context.getDoc().getDocumentReference());
    } else {
        Map<String, Object[]> wikiQueries = new HashMap<>();
        for (int i = 0; i < pages.length; ++i) {
            String pattern = pages[i];
            String wikiName;
            if (pattern.contains(":")) {
                int index = pattern.indexOf(':');
                wikiName = pattern.substring(0, index);
                pattern = pattern.substring(index + 1);
            } else {
                wikiName = context.getWikiId();
            }
            StringBuffer where;
            List<QueryParameter> params;
            if (!wikiQueries.containsKey(wikiName)) {
                Object[] query = new Object[2];
                query[0] = where = new StringBuffer("where ");
                query[1] = params = new ArrayList<>();
                wikiQueries.put(wikiName, query);
            } else {
                Object[] query = wikiQueries.get(wikiName);
                where = (StringBuffer) query[0];
                params = (List<QueryParameter>) query[1];
            }
            if (i > 0) {
                where.append(" or ");
            }
            where.append("doc.fullName like ?");
            params.add(new DefaultQueryParameter(null).like(pattern));
        }
        DocumentReferenceResolver<String> resolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "current");
        QueryManager queryManager = Utils.getComponent(QueryManager.class);
        String database = context.getWikiId();
        try {
            for (Map.Entry<String, Object[]> entry : wikiQueries.entrySet()) {
                String wikiName = entry.getKey();
                Object[] query = entry.getValue();
                String where = query[0].toString();
                List<Object> params = (List<Object>) query[1];
                Query dbQuery = queryManager.createQuery(where, Query.HQL);
                List<String> docsNames = dbQuery.setWiki(wikiName).bindValues(params).execute();
                for (String docName : docsNames) {
                    String pageReference = wikiName + XWikiDocument.DB_SPACE_SEP + docName;
                    if (context.getWiki().getRightService().hasAccessLevel("view", context.getUser(), pageReference, context)) {
                        pageList.add(resolver.resolve(pageReference));
                    }
                }
            }
        } catch (QueryException e) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_EXPORT, "Failed to resolve pages to export", e);
        } finally {
            context.setWikiId(database);
        }
    }
    return pageList;
}
Also used : DefaultQueryParameter(org.xwiki.query.internal.DefaultQueryParameter) QueryParameter(org.xwiki.query.QueryParameter) DefaultQueryParameter(org.xwiki.query.internal.DefaultQueryParameter) Query(org.xwiki.query.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryException(org.xwiki.query.QueryException) QueryManager(org.xwiki.query.QueryManager) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with QueryParameter

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

the class AbstractListClassPropertyValuesProviderTest method configure.

public void configure() throws Exception {
    Provider<XWikiContext> xcontextProvider = getMocker().getInstance(XWikiContext.TYPE_PROVIDER);
    XWiki xwiki = mock(XWiki.class);
    BaseClass xclass = mock(BaseClass.class);
    DocumentReference authorReference = new DocumentReference("wiki", "Users", "Alice");
    when(xcontextProvider.get()).thenReturn(this.xcontext);
    when(this.xcontext.getWiki()).thenReturn(xwiki);
    when(this.classDocument.getXClass()).thenReturn(xclass);
    when(this.classDocument.getDocumentReference()).thenReturn(this.classReference);
    when(this.classDocument.getAuthorReference()).thenReturn(authorReference);
    QueryParameter queryParameter = mock(QueryParameter.class);
    when(this.allowedValuesQuery.bindValue("text")).thenReturn(queryParameter);
    when(this.usedValuesQuery.bindValue("text")).thenReturn(queryParameter);
    when(queryParameter.anyChars()).thenReturn(queryParameter);
    when(queryParameter.literal("foo")).thenReturn(queryParameter);
}
Also used : QueryParameter(org.xwiki.query.QueryParameter) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

QueryParameter (org.xwiki.query.QueryParameter)4 DocumentReference (org.xwiki.model.reference.DocumentReference)3 Query (org.xwiki.query.Query)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1 ClassPropertyReference (org.xwiki.model.reference.ClassPropertyReference)1 QueryException (org.xwiki.query.QueryException)1 QueryFilter (org.xwiki.query.QueryFilter)1 QueryManager (org.xwiki.query.QueryManager)1 DefaultQueryParameter (org.xwiki.query.internal.DefaultQueryParameter)1 PropertyValue (org.xwiki.rest.model.jaxb.PropertyValue)1 PropertyValues (org.xwiki.rest.model.jaxb.PropertyValues)1