use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class DefaultWikiDescriptorDocumentHelper method getAllXWikiServerClassDocumentNames.
@Override
public List<String> getAllXWikiServerClassDocumentNames() throws WikiManagerException {
WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
try {
Query query = this.queryManager.createQuery("from doc.object(XWiki.XWikiServerClass) as descriptor where doc.name like 'XWikiServer%' " + "and doc.fullName <> 'XWiki.XWikiServerClassTemplate'", Query.XWQL);
query.setWiki(wikiDescriptorManager.getMainWikiId());
query.addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "unique"));
return query.execute();
} catch (Exception e) {
throw new WikiManagerException("Failed to locate XWiki.XWikiServerClass documents", e);
}
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class HqlQueryExecutor method addEscapeLikeParametersFilter.
private void addEscapeLikeParametersFilter(Query query) {
if (!hasQueryParametersType(query)) {
return;
}
// Find the component class for the "escapeLikeParameters" filter
QueryFilter escapeFilter;
try {
escapeFilter = this.componentManagerProvider.get().getInstance(QueryFilter.class, ESCAPE_LIKE_PARAMETERS_FILTER);
} catch (ComponentLookupException e) {
// Shouldn't happen!
throw new RuntimeException(String.format("Failed to locate [%s] Query Filter", ESCAPE_LIKE_PARAMETERS_FILTER), e);
}
boolean found = false;
for (QueryFilter filter : query.getFilters()) {
if (escapeFilter.getClass().getName().equals(filter.getClass().getName())) {
found = true;
break;
}
}
if (!found) {
query.addFilter(escapeFilter);
}
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class HqlQueryExecutor method execute.
@Override
public <T> List<T> execute(final Query query) throws QueryException {
// Make sure the query is allowed in the current context
checkAllowed(query);
String oldDatabase = getContext().getWikiId();
try {
this.progress.startStep(query, "query.hql.progress.execute", "Execute HQL query [{}]", query);
if (query.getWiki() != null) {
getContext().setWikiId(query.getWiki());
}
return getStore().executeRead(getContext(), new HibernateCallback<List<T>>() {
@SuppressWarnings("unchecked")
@Override
public List<T> doInHibernate(Session session) {
org.hibernate.Query hquery = createHibernateQuery(session, query);
List<T> results = hquery.list();
if (query.getFilters() != null && !query.getFilters().isEmpty()) {
for (QueryFilter filter : query.getFilters()) {
results = filter.filterResults(results);
}
}
return results;
}
});
} catch (XWikiException e) {
throw new QueryException("Exception while executing query", query, e);
} finally {
getContext().setWikiId(oldDatabase);
this.progress.endStep(query);
}
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class SpacesResourceImpl method getSpaces.
@Override
public Spaces getSpaces(String wikiName, Integer start, Integer number) throws XWikiRestException {
Spaces spaces = objectFactory.createSpaces();
try {
List<String> spaceNames = queryManager.getNamedQuery("getSpaces").addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "hidden")).setOffset(start).setLimit(number).setWiki(wikiName).execute();
for (String spaceName : spaceNames) {
List<String> spaceList = Utils.getSpacesFromSpaceId(spaceName);
String homeId = Utils.getPageId(wikiName, spaceList, "WebHome");
Document home = null;
XWiki xwikiApi = Utils.getXWikiApi(componentManager);
if (xwikiApi.hasAccessLevel("view", homeId)) {
if (xwikiApi.exists(homeId)) {
home = Utils.getXWikiApi(componentManager).getDocument(homeId);
}
spaces.getSpaces().add(DomainObjectFactory.createSpace(objectFactory, uriInfo.getBaseUri(), wikiName, spaceList, home));
}
}
} catch (Exception e) {
throw new XWikiRestException(e);
}
return spaces;
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class PagesResourceImpl method getPages.
@Override
public Pages getPages(String wikiName, String spaceName, Integer start, Integer number, String parentFilterExpression, String order, Boolean withPrettyNames) throws XWikiRestException {
String database = Utils.getXWikiContext(componentManager).getWikiId();
List<String> spaces = parseSpaceSegments(spaceName);
String spaceId = Utils.getLocalSpaceId(spaces);
Pages pages = objectFactory.createPages();
try {
Utils.getXWikiContext(componentManager).setWikiId(wikiName);
Query query = ("date".equals(order)) ? queryManager.createQuery("select doc.name from Document doc where doc.space=:space and language='' order by doc.date desc", "xwql") : queryManager.getNamedQuery("getSpaceDocsName");
/* Use an explicit query to improve performance */
List<String> pageNames = query.addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "hidden")).bindValue("space", spaceId).setOffset(start).setLimit(number).execute();
Pattern parentFilter = null;
if (parentFilterExpression != null) {
if (parentFilterExpression.equals("null")) {
parentFilter = Pattern.compile("");
} else {
parentFilter = Pattern.compile(parentFilterExpression);
}
}
for (String pageName : pageNames) {
String pageFullName = Utils.getPageId(wikiName, spaces, pageName);
if (!Utils.getXWikiApi(componentManager).exists(pageFullName)) {
getLogger().warn("Page [{}] appears to be in space [{}] but no information is available.", pageName, spaceId);
} else {
Document doc = Utils.getXWikiApi(componentManager).getDocument(pageFullName);
/* We only add pages we have the right to access */
if (doc != null) {
boolean add = true;
Document parent = Utils.getParentDocument(doc, Utils.getXWikiApi(componentManager));
if (parentFilter != null) {
String parentId = "";
if (parent != null && !parent.isNew()) {
parentId = parent.getPrefixedFullName();
}
add = parentFilter.matcher(parentId).matches();
}
if (add) {
pages.getPageSummaries().add(DomainObjectFactory.createPageSummary(objectFactory, uriInfo.getBaseUri(), doc, Utils.getXWikiApi(componentManager), withPrettyNames));
}
}
}
}
} catch (Exception e) {
throw new XWikiRestException(e);
} finally {
Utils.getXWikiContext(componentManager).setWikiId(database);
}
return pages;
}
Aggregations