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