use of org.xwiki.query.QueryException 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.QueryException 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.QueryException in project xwiki-platform by xwiki.
the class TagQueryUtils method getDocumentsWithTag.
/**
* Get documents with the passed tags with the result depending on whether the caller decides to include
* hidden documents or not.
*
* @param tag a list of tags to match.
* @param includeHiddenDocuments if true then include hidden documents
* @param context XWiki context.
* @return list of docNames.
* @throws XWikiException if search query fails (possible failures: DB access problems, etc).
* @since 6.2M1
*/
public static List<String> getDocumentsWithTag(String tag, boolean includeHiddenDocuments, XWikiContext context) throws XWikiException {
List<String> results;
List<Object> parameters = new ArrayList<>();
parameters.add(TagPlugin.TAG_CLASS);
parameters.add(tag);
String hql = ", BaseObject as obj, DBStringListProperty as prop join prop.list item where obj.className=? and " + "obj.name=doc.fullName and obj.id=prop.id.id and prop.id.name='tags' and lower(item)=lower(?) order by " + "doc.fullName";
try {
Query query = context.getWiki().getStore().getQueryManager().createQuery(hql, Query.HQL);
query.bindValues(parameters);
query.addFilter(Utils.getComponent(QueryFilter.class, UniqueDocumentFilter.HINT));
if (!includeHiddenDocuments) {
query.addFilter(Utils.getComponent(QueryFilter.class, HiddenDocumentFilter.HINT));
}
results = query.execute();
} catch (QueryException e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, String.format("Failed to search for document with tag [%s]", tag), e);
}
return results;
}
use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.
the class WikiDescriptorMigrator method hibernateMigrate.
@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
String hql = "SELECT DISTINCT doc.fullName FROM XWikiDocument doc, BaseObject obj WHERE doc.fullName = obj.name" + " AND obj.className = :className AND doc.fullName <> :template AND doc.fullName NOT IN " + "(SELECT DISTINCT doc2.fullName FROM XWikiDocument doc2, BaseObject obj2, StringProperty propPrettyName" + " WHERE doc2.fullName = obj2.name AND obj2.className = :className" + " AND propPrettyName.id = obj2.id AND propPrettyName.name = :propertyName)";
try {
Query query = queryManager.createQuery(hql, Query.HQL);
query.bindValue("className", String.format("%s.%s", XWiki.SYSTEM_SPACE, XWikiServerClassDocumentInitializer.DOCUMENT_NAME));
query.bindValue("propertyName", XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME);
query.bindValue("template", "XWiki.XWikiServerClassTemplate");
List<String> results = query.execute();
for (String result : results) {
fixDocument(result);
}
} catch (QueryException e) {
logger.error("Failed to perform a query on the main wiki.", e);
}
}
use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.
the class DefaultWikiDescriptorDocumentHelper method findXWikiServerClassDocumentReference.
@Override
public DocumentReference findXWikiServerClassDocumentReference(String wikiAlias) throws WikiManagerException {
if (wikiAlias == null) {
throw new IllegalArgumentException("Wiki alias cannot be null");
}
WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
DocumentReference result = null;
try {
Query query = this.queryManager.createQuery("where doc.object(XWiki.XWikiServerClass).server = :wikiAlias and doc.name like 'XWikiServer%'", Query.XWQL);
query.bindValue("wikiAlias", wikiAlias);
query.setWiki(wikiDescriptorManager.getMainWikiId());
List<String> documentNames = query.execute();
// Resolve the document name into a references
if (documentNames != null && !documentNames.isEmpty()) {
result = documentReferenceResolver.resolve(documentNames.get(0));
}
} catch (QueryException e) {
throw new WikiManagerException(String.format("Failed to locate XWiki.XWikiServerClass document for wiki alias [%s]", wikiAlias), e);
}
return result;
}
Aggregations