Search in sources :

Example 41 with QueryException

use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.

the class XWikiGroupServiceImpl method listMemberForGroup.

@Override
@Deprecated
public List<String> listMemberForGroup(String group, XWikiContext context) throws XWikiException {
    List<String> list = new ArrayList<String>();
    try {
        if (group == null) {
            try {
                return context.getWiki().getStore().getQueryManager().getNamedQuery("getAllUsers").execute();
            } catch (QueryException ex) {
                throw new XWikiException(0, 0, ex.getMessage(), ex);
            }
        } else {
            String gshortname = Util.getName(group, context);
            XWikiDocument docgroup = context.getWiki().getDocument(gshortname, context);
            List<BaseObject> groups = docgroup.getXObjects(GROUPCLASS_REFERENCE);
            if (groups != null) {
                for (BaseObject bobj : groups) {
                    if (bobj != null) {
                        String member = bobj.getStringValue(FIELD_XWIKIGROUPS_MEMBER);
                        if (StringUtils.isNotEmpty(member)) {
                            list.add(member);
                        }
                    }
                }
            }
            return list;
        }
    } catch (XWikiException e) {
        LOGGER.error("Failed to get group document", e);
    }
    return null;
}
Also used : QueryException(org.xwiki.query.QueryException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 42 with QueryException

use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.

the class XWikiGroupServiceImpl method getAllGroupsReferencesForMember.

@Override
public Collection<DocumentReference> getAllGroupsReferencesForMember(DocumentReference memberReference, int limit, int offset, XWikiContext context) throws XWikiException {
    Collection<DocumentReference> groupReferences = null;
    String prefixedFullName = this.entityReferenceSerializer.serialize(memberReference);
    String key = context.getWikiId() + "/" + prefixedFullName;
    synchronized (key) {
        if (this.memberGroupsCache == null) {
            initCache(context);
        }
        // TODO: add cache support for customized limit/offset ?
        boolean supportCache = limit <= 0 && offset <= 0;
        if (supportCache) {
            groupReferences = this.memberGroupsCache.get(key);
        }
        if (groupReferences == null) {
            List<String> groupNames;
            try {
                Query query;
                if (memberReference.getWikiReference().getName().equals(context.getWikiId()) || (memberReference.getLastSpaceReference().getName().equals("XWiki") && memberReference.getName().equals(XWikiRightService.GUEST_USER))) {
                    query = context.getWiki().getStore().getQueryManager().getNamedQuery("listGroupsForUser").bindValue("username", prefixedFullName).bindValue("shortname", this.localWikiEntityReferenceSerializer.serialize(memberReference)).bindValue("veryshortname", memberReference.getName());
                } else {
                    query = context.getWiki().getStore().getQueryManager().getNamedQuery("listGroupsForUserInOtherWiki").bindValue("prefixedmembername", prefixedFullName);
                }
                query.setOffset(offset);
                query.setLimit(limit);
                groupNames = query.execute();
            } catch (QueryException ex) {
                throw new XWikiException(0, 0, ex.getMessage(), ex);
            }
            groupReferences = new HashSet<DocumentReference>(groupNames.size());
            for (String groupName : groupNames) {
                groupReferences.add(this.currentMixedDocumentReferenceResolver.resolve(groupName));
            }
            // itself are part of it.
            if (isAllGroupImplicit(context) && memberReference.getWikiReference().getName().equals(context.getWikiId()) && !memberReference.getName().equals(XWikiRightService.GUEST_USER)) {
                DocumentReference currentXWikiAllGroup = new DocumentReference(context.getWikiId(), "XWiki", XWikiRightService.ALLGROUP_GROUP);
                if (!currentXWikiAllGroup.equals(memberReference)) {
                    groupReferences.add(currentXWikiAllGroup);
                }
            }
            if (supportCache) {
                this.memberGroupsCache.set(key, groupReferences);
            }
        }
    }
    return groupReferences;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 43 with QueryException

use of org.xwiki.query.QueryException 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)

Example 44 with QueryException

use of org.xwiki.query.QueryException 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);
    }
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SecureQuery(org.xwiki.query.SecureQuery) List(java.util.List) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 45 with QueryException

use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.

the class PageHistoryResourceImpl method getPageHistory.

@Override
public History getPageHistory(String wikiName, String spaceName, String pageName, Integer start, Integer number, String order, Boolean withPrettyNames) throws XWikiRestException {
    List<String> spaces = parseSpaceSegments(spaceName);
    String spaceId = Utils.getLocalSpaceId(spaces);
    History history = new History();
    try {
        // Note that the query is made to work with Oracle which treats empty strings as null.
        String query = String.format("select doc.space, doc.name, rcs.id, rcs.date, rcs.author, rcs.comment" + " from XWikiRCSNodeInfo as rcs, XWikiDocument as doc where rcs.id.docId = doc.id and" + " doc.space = :space and doc.name = :name and (doc.language = '' or doc.language is null)" + " order by rcs.date %s, rcs.id.version1 %s, rcs.id.version2 %s", order, order, order);
        List<Object> queryResult = null;
        queryResult = queryManager.createQuery(query, Query.XWQL).bindValue("space", spaceId).bindValue("name", pageName).setLimit(number).setOffset(start).setWiki(wikiName).execute();
        for (Object object : queryResult) {
            Object[] fields = (Object[]) object;
            XWikiRCSNodeId nodeId = (XWikiRCSNodeId) fields[2];
            Timestamp timestamp = (Timestamp) fields[3];
            Date modified = new Date(timestamp.getTime());
            String modifier = (String) fields[4];
            String comment = (String) fields[5];
            HistorySummary historySummary = DomainObjectFactory.createHistorySummary(objectFactory, uriInfo.getBaseUri(), wikiName, spaces, pageName, null, nodeId.getVersion(), modifier, modified, comment, Utils.getXWikiApi(componentManager), withPrettyNames);
            history.getHistorySummaries().add(historySummary);
        }
    } catch (QueryException e) {
        throw new XWikiRestException(e);
    }
    return history;
}
Also used : QueryException(org.xwiki.query.QueryException) XWikiRestException(org.xwiki.rest.XWikiRestException) HistorySummary(org.xwiki.rest.model.jaxb.HistorySummary) XWikiRCSNodeId(com.xpn.xwiki.doc.rcs.XWikiRCSNodeId) History(org.xwiki.rest.model.jaxb.History) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Aggregations

QueryException (org.xwiki.query.QueryException)57 Query (org.xwiki.query.Query)32 DocumentReference (org.xwiki.model.reference.DocumentReference)19 XWikiException (com.xpn.xwiki.XWikiException)18 ArrayList (java.util.ArrayList)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 BaseObject (com.xpn.xwiki.objects.BaseObject)8 Test (org.junit.Test)8 QueryFilter (org.xwiki.query.QueryFilter)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 HashMap (java.util.HashMap)6 XWikiRestException (org.xwiki.rest.XWikiRestException)6 Event (org.xwiki.eventstream.Event)5 WikiReference (org.xwiki.model.reference.WikiReference)5 Date (java.util.Date)4 List (java.util.List)4 IconException (org.xwiki.icon.IconException)4 QueryManager (org.xwiki.query.QueryManager)4 SecureQuery (org.xwiki.query.SecureQuery)4 XWiki (com.xpn.xwiki.XWiki)3