use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class DefaultUsersQueryBuilder method build.
@Override
public Query build(UsersClass usersClass) throws QueryException {
String statement = new StringBuilder("select doc.fullName as userReference,").append(" firstName.value||' '||lastName.value as userName ").append("from XWikiDocument doc, BaseObject obj, StringProperty firstName, StringProperty lastName ").append("where doc.fullName = obj.name and obj.className = 'XWiki.XWikiUsers'").append(" and obj.id = firstName.id.id and firstName.id.name = 'first_name'").append(" and obj.id = lastName.id.id and lastName.id.name = 'last_name'").append(" and doc.space = 'XWiki' ").append("order by lower(firstName.value), firstName.value, lower(lastName.value), lastName.value").toString();
Query query = this.queryManager.createQuery(statement, Query.HQL);
// Resolve the document full name from the first column into a DocumentReference (the user profile reference).
query.addFilter(this.documentFilter);
// Remove the users whose profile page is not viewable by the current user.
query.addFilter(this.viewableFilter);
query.setWiki(usersClass.getOwnerDocument().getDocumentReference().getWikiReference().getName());
return query;
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class ImplicitlyAllowedValuesDBListQueryBuilder method build.
/**
* {@inheritDoc} The query is constructed according to the following rules:
* <ul>
* <li>If no class name, id or value fields are selected, return a query that returns no rows.</li>
* <li>If only the class name is provided, select all document names which have an object of that type.</li>
* <li>If only one of id and value is provided, select just one column.</li>
* <li>If id = value, select just one column.</li>
* <li>If no class name is provided, assume the fields are document properties.</li>
* </ul>
* If there are two columns selected, use the first one as the stored value and the second one as the displayed
* value.
*
* @see QueryBuilder#build(java.lang.Object)
*/
@Override
public Query build(DBListClass dbListClass) throws QueryException {
String className = StringUtils.defaultString(dbListClass.getClassname());
String idField = StringUtils.defaultString(dbListClass.getIdField());
String valueField = StringUtils.defaultString(dbListClass.getValueField());
boolean hasClassName = !StringUtils.isBlank(className);
boolean hasIdField = !StringUtils.isBlank(idField);
boolean hasValueField = !StringUtils.isBlank(valueField);
// Initialize with default query (that returns no rows).
String statement = "select doc.name from XWikiDocument doc where 1 = 0";
Map<String, Object> parameters = new HashMap<>();
if (hasIdField || hasValueField) {
statement = getStatementWhenIdValueFieldsAreSpecified(className, idField, valueField, hasClassName, hasIdField, hasValueField, parameters);
} else if (hasClassName) {
statement = "select distinct doc.fullName from XWikiDocument as doc, BaseObject as obj" + " where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName";
parameters.put(CLASS_NAME, className);
parameters.put(TEMPLATE_NAME, getTemplateName(className));
}
Query query = this.queryManager.createQuery(statement, Query.HQL);
query.setWiki(dbListClass.getOwnerDocument().getDocumentReference().getWikiReference().getName());
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
query.bindValue(entry.getKey(), entry.getValue());
}
query.addFilter(this.viewableValueFilter);
return query;
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class UsedValuesListQueryBuilder method build.
@Override
public Query build(ListClass listClass) throws QueryException {
String statement = String.format("select %1$s, count(*) as unfilterable0 " + "from BaseObject as obj, %2$s " + "where obj.className = :className and obj.name <> :templateName" + " and prop.id.id = obj.id and prop.id.name = :propertyName " + "group by %1$s " + "order by count(*) desc", getSelectColumnAndFromTable(listClass));
Query query = this.queryManager.createQuery(statement, Query.HQL);
bindParameterValues(query, listClass);
query.addFilter(new ViewableValueFilter(listClass));
query.setWiki(listClass.getReference().extractReference(EntityType.WIKI).getName());
return query;
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class UsedValuesListQueryBuilder method canView.
private boolean canView(ListClass listClass, String value, long count) {
// We can't check all the documents where this value occurs so we check just one of them, chosen randomly.
long offset = ThreadLocalRandom.current().nextLong(count);
String statement = String.format("select obj.name from BaseObject as obj, %2$s " + "where obj.className = :className and obj.name <> :templateName " + "and prop.id.id = obj.id and prop.id.name = :propertyName and %1$s = :propertyValue", getSelectColumnAndFromTable(listClass));
try {
Query query = this.queryManager.createQuery(statement, Query.HQL);
bindParameterValues(query, listClass);
query.bindValue("propertyValue", value);
query.setWiki(listClass.getReference().extractReference(EntityType.WIKI).getName());
query.setOffset((int) offset).setLimit(1);
List<?> results = query.execute();
if (results.size() > 0) {
DocumentReference documentReference = this.documentReferenceResolver.resolve((String) results.get(0));
if (this.authorization.hasAccess(Right.VIEW, documentReference)) {
return true;
}
}
} catch (QueryException e) {
this.logger.warn("Failed to check if the list value is viewable. Root cause is [{}]." + " Continue assuming the value is not viewable.", ExceptionUtils.getRootCauseMessage(e));
}
return false;
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class XWikiDocumentMockitoTest method getChildrenReferences.
@Test
public void getChildrenReferences() throws Exception {
Query query = mock(Query.class);
when(this.oldcore.getQueryManager().createQuery(any(), eq(Query.XWQL))).thenReturn(query);
QueryFilter hiddenFilter = this.oldcore.getMocker().registerMockComponent(QueryFilter.class, "hidden");
when(query.setLimit(7)).thenReturn(query);
List<String> result = Arrays.asList("X.y", "A.b");
when(query.<String>execute()).thenReturn(result);
List<DocumentReference> childrenReferences = document.getChildrenReferences(7, 3, this.oldcore.getXWikiContext());
verify(query).addFilter(hiddenFilter);
verify(query).setLimit(7);
verify(query).setOffset(3);
Assert.assertEquals(2, childrenReferences.size());
Assert.assertEquals(new DocumentReference("wiki", "X", "y"), childrenReferences.get(0));
Assert.assertEquals(new DocumentReference("wiki", "A", "b"), childrenReferences.get(1));
}
Aggregations