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