Search in sources :

Example 11 with QueryException

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

the class WikiPagesResourceImpl method getPages.

@Override
public Pages getPages(String wikiName, Integer start, String name, String space, String author, Integer number) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    Pages pages = objectFactory.createPages();
    /* This try is just needed for executing the finally clause. */
    try {
        Map<String, String> filters = new HashMap<String, String>();
        if (!name.equals("")) {
            filters.put("name", name);
        }
        if (!space.equals("")) {
            filters.put("space", Utils.getLocalSpaceId(parseSpaceSegments(space)));
        }
        if (!author.equals("")) {
            filters.put("author", author);
        }
        /* Build the query */
        Formatter f = new Formatter();
        f.format("select doc from XWikiDocument as doc");
        if (filters.keySet().size() > 0) {
            f.format(" where (");
            int i = 0;
            for (String param : filters.keySet()) {
                if (param.equals("name")) {
                    f.format(" upper(doc.fullName) like :name ");
                }
                if (param.equals("space")) {
                    f.format(" upper(doc.space) like :space ");
                }
                if (param.equals("author")) {
                    f.format(" upper(doc.contentAuthor) like :author ");
                }
                i++;
                if (i < filters.keySet().size()) {
                    f.format(" and ");
                }
            }
            f.format(")");
        }
        String queryString = f.toString();
        /* Execute the query by filling the parameters */
        List<Object> queryResult = null;
        try {
            Query query = queryManager.createQuery(queryString, Query.XWQL).setLimit(number).setOffset(start);
            for (String param : filters.keySet()) {
                query.bindValue(param, String.format("%%%s%%", filters.get(param).toUpperCase()));
            }
            queryResult = query.execute();
        } catch (QueryException e) {
            throw new XWikiRestException(e);
        }
        /* Get the results and populate the returned representation */
        for (Object object : queryResult) {
            XWikiDocument xwikiDocument = (XWikiDocument) object;
            xwikiDocument.setDatabase(wikiName);
            Document doc = new Document(xwikiDocument, Utils.getXWikiContext(componentManager));
            /*
                 * We manufacture page summaries in place because we don't have all the data for calling the
                 * DomainObjectFactory method (doing so would require to retrieve an actual Document)
                 */
            PageSummary pageSummary = objectFactory.createPageSummary();
            pageSummary.setId(doc.getPrefixedFullName());
            pageSummary.setFullName(doc.getFullName());
            pageSummary.setWiki(wikiName);
            pageSummary.setSpace(doc.getSpace());
            pageSummary.setName(doc.getName());
            pageSummary.setTitle(doc.getTitle());
            pageSummary.setParent(doc.getParent());
            URL absoluteUrl = Utils.getXWikiContext(componentManager).getURLFactory().createExternalURL(doc.getSpace(), doc.getName(), "view", null, null, Utils.getXWikiContext(componentManager));
            pageSummary.setXwikiAbsoluteUrl(absoluteUrl.toString());
            pageSummary.setXwikiRelativeUrl(Utils.getXWikiContext(componentManager).getURLFactory().getURL(absoluteUrl, Utils.getXWikiContext(componentManager)));
            String pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName()).toString();
            Link pageLink = objectFactory.createLink();
            pageLink.setHref(pageUri);
            pageLink.setRel(Relations.PAGE);
            pageSummary.getLinks().add(pageLink);
            pages.getPageSummaries().add(pageSummary);
        }
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
    return pages;
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) Query(org.xwiki.query.Query) HashMap(java.util.HashMap) Formatter(java.util.Formatter) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) URL(java.net.URL) Pages(org.xwiki.rest.model.jaxb.Pages) QueryException(org.xwiki.query.QueryException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PageSummary(org.xwiki.rest.model.jaxb.PageSummary) Link(org.xwiki.rest.model.jaxb.Link)

Example 12 with QueryException

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

the class DefaultIconSetManager method getIconSetNames.

@Override
public List<String> getIconSetNames() throws IconException {
    try {
        String xwql = "SELECT obj.name FROM Document doc, doc.object(IconThemesCode.IconThemeClass) obj " + "ORDER BY obj.name";
        Query query = queryManager.createQuery(xwql, Query.XWQL);
        return query.execute();
    } catch (QueryException e) {
        throw new IconException("Failed to get the name of all icon sets.", e);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) IconException(org.xwiki.icon.IconException)

Example 13 with QueryException

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

the class DefaultIconSetManagerTest method getIconSetWhenException.

@Test
public void getIconSetWhenException() throws Exception {
    // Mocks
    Exception exception = new QueryException("exception in the query", null, null);
    when(queryManager.createQuery(any(), any())).thenThrow(exception);
    // Test
    Exception caughtException = null;
    try {
        mocker.getComponentUnderTest().getIconSet("silk");
    } catch (IconException e) {
        caughtException = e;
    }
    assertNotNull(caughtException);
    assertEquals(exception, caughtException.getCause());
    assertEquals("Failed to load the icon set [silk].", caughtException.getMessage());
}
Also used : QueryException(org.xwiki.query.QueryException) QueryException(org.xwiki.query.QueryException) IconException(org.xwiki.icon.IconException) MalformedURLException(java.net.MalformedURLException) IconException(org.xwiki.icon.IconException) Test(org.junit.Test)

Example 14 with QueryException

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

the class DefaultMessageStream method getRecentPersonalMessages.

@Override
public List<Event> getRecentPersonalMessages(DocumentReference author, int limit, int offset) {
    List<Event> result = new ArrayList<Event>();
    try {
        Query q = this.qm.createQuery("where event.application = 'MessageStream' and event.type = 'personalMessage'" + " and event.user = :user order by event.date desc", Query.XWQL);
        q.bindValue("user", this.serializer.serialize(author));
        q.setLimit(limit > 0 ? limit : 30).setOffset(offset >= 0 ? offset : 0);
        result = this.stream.searchEvents(q);
    } catch (QueryException ex) {
        LOG.warn("Failed to search personal messages: {}", ex.getMessage());
    }
    return result;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) Event(org.xwiki.eventstream.Event)

Example 15 with QueryException

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

the class DefaultMessageStream method deleteMessage.

@Override
public void deleteMessage(String id) {
    Query q;
    try {
        q = this.qm.createQuery("where event.id = :id", Query.XWQL);
        q.bindValue("id", id);
        List<Event> events = this.stream.searchEvents(q);
        if (events == null || events.isEmpty()) {
            throw new IllegalArgumentException("This message does not exist");
        } else if (events.get(0).getUser().equals(this.bridge.getCurrentUserReference())) {
            this.stream.deleteEvent(events.get(0));
        } else {
            throw new IllegalArgumentException("You are not authorized to delete this message");
        }
    } catch (QueryException ex) {
        LOG.warn("Failed to delete message: {}", ex.getMessage());
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) Event(org.xwiki.eventstream.Event)

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