Search in sources :

Example 1 with DefaultQueryParameter

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

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 Query (org.xwiki.query.Query)1 QueryException (org.xwiki.query.QueryException)1 QueryManager (org.xwiki.query.QueryManager)1 QueryParameter (org.xwiki.query.QueryParameter)1 DefaultQueryParameter (org.xwiki.query.internal.DefaultQueryParameter)1