use of org.xwiki.query.internal.DefaultQueryParameter 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;
}
Aggregations