use of org.xwiki.query.Query 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.Query 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());
}
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class MessageStreamTest method testGetRecentPersonalMessagesWhenQueryFails.
@Test
public void testGetRecentPersonalMessagesWhenQueryFails() throws Exception {
final Query mockQuery = getMockQuery();
final QueryManager mockQueryManager = getComponentManager().getInstance(QueryManager.class);
final EventStream mockEventStream = getComponentManager().getInstance(EventStream.class);
final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
final EntityReferenceSerializer<String> mockSerializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
getMockery().checking(new Expectations() {
{
allowing(mockBridge).getCurrentUserReference();
will(returnValue(MessageStreamTest.this.currentUser));
allowing(mockSerializer).serialize(MessageStreamTest.this.currentUser);
will(returnValue("wiki:XWiki.JohnDoe"));
exactly(1).of(mockQuery).setLimit(30);
will(returnValue(mockQuery));
exactly(1).of(mockQuery).setOffset(0);
will(returnValue(mockQuery));
allowing(mockQuery).bindValue(with(any(String.class)), with("wiki:XWiki.JohnDoe"));
allowing(mockQueryManager).createQuery(with(aNonNull(String.class)), with(aNonNull(String.class)));
will(returnValue(mockQuery));
exactly(1).of(mockEventStream).searchEvents(with(mockQuery));
will(throwException(new QueryException("", null, null)));
}
});
List<Event> result = this.stream.getRecentPersonalMessages();
Assert.assertNotNull(result);
Assert.assertTrue(result.isEmpty());
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class XWikiDocument method getChildrenReferences.
/**
* Returns a list of references of all documents which list this document as their parent, in the current wiki.
*
* @param nb The number of results to return.
* @param start The number of results to skip before we begin returning results.
* @param context The {@link com.xpn.xwiki.XWikiContext context}.
* @return the list of document references
* @throws XWikiException If there's an error querying the database.
* @since 2.2M2
*/
public List<DocumentReference> getChildrenReferences(int nb, int start, XWikiContext context) throws XWikiException {
// Use cases:
// - the parent document reference saved in the database matches the reference of this document, in its fully
// serialized form (eg "wiki:space.page"). Note that this is normally not required since the wiki part
// isn't saved in the database when it matches the current wiki.
// - the parent document reference saved in the database matches the reference of this document, in its
// serialized form without the wiki part (eg "space.page"). The reason we don't need to specify the wiki
// part is because document parents saved in the database don't have the wiki part specified when it matches
// the current wiki.
// - the parent document reference saved in the database matches the page name part of this document's
// reference (eg "page") and the parent document's space is the same as this document's space.
List<DocumentReference> children = new ArrayList<DocumentReference>();
try {
Query query = getStore().getQueryManager().createQuery("select distinct doc.fullName from XWikiDocument doc where " + "doc.parent=:prefixedFullName or doc.parent=:fullName or (doc.parent=:name and doc.space=:space)", Query.XWQL);
query.addFilter(Utils.getComponent(QueryFilter.class, "hidden"));
query.bindValue("prefixedFullName", getDefaultEntityReferenceSerializer().serialize(getDocumentReference()));
query.bindValue("fullName", LOCAL_REFERENCE_SERIALIZER.serialize(getDocumentReference()));
query.bindValue("name", getDocumentReference().getName());
query.bindValue("space", LOCAL_REFERENCE_SERIALIZER.serialize(getDocumentReference().getLastSpaceReference()));
query.setLimit(nb).setOffset(start);
List<String> queryResults = query.execute();
WikiReference wikiReference = this.getDocumentReference().getWikiReference();
for (String fullName : queryResults) {
children.add(getCurrentDocumentReferenceResolver().resolve(fullName, wikiReference));
}
} catch (QueryException e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, String.format("Failed to retrieve children for document [%s]", this.getDocumentReference()), e);
}
return children;
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class DefaultModelBridge method getDocumentReferences.
@Override
public List<DocumentReference> getDocumentReferences(SpaceReference spaceReference) {
try {
// At the moment we don't have a way to retrieve only the direct children so we select all the descendants.
// This means we select all the documents from the specified space and from all the nested spaces.
String statement = "select distinct(doc.fullName) from XWikiDocument as doc " + "where doc.space = :space or doc.space like :spacePrefix escape '/'";
Query query = this.queryManager.createQuery(statement, Query.HQL);
query.setWiki(spaceReference.getWikiReference().getName());
String localSpaceReference = this.localEntityReferenceSerializer.serialize(spaceReference);
query.bindValue("space", localSpaceReference);
String spacePrefix = LIKE_SPECIAL_CHARS.matcher(localSpaceReference).replaceAll("/$1");
query.bindValue("spacePrefix", spacePrefix + ".%");
List<DocumentReference> descendants = new ArrayList<>();
for (Object fullName : query.execute()) {
descendants.add(this.explicitDocumentReferenceResolver.resolve((String) fullName, spaceReference));
}
return descendants;
} catch (Exception e) {
this.logger.error("Failed to retrieve the documents from [{}].", spaceReference, e);
return Collections.emptyList();
}
}
Aggregations