use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class DefaultModelBridgeTest method getDocumentReferences.
@Test
public void getDocumentReferences() throws Exception {
SpaceReference spaceReference = new SpaceReference("wiki", "Space");
Query query = mock(Query.class);
QueryManager queryManager = this.mocker.getInstance(QueryManager.class);
when(queryManager.createQuery(any(), any())).thenReturn(query);
EntityReferenceSerializer<String> localEntityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
when(localEntityReferenceSerializer.serialize(spaceReference)).thenReturn("Space");
when(query.execute()).thenReturn(Arrays.<Object>asList("Page"));
DocumentReferenceResolver<String> explicitDocumentReferenceResolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "explicit");
DocumentReference documentReference = new DocumentReference("Page", spaceReference);
when(explicitDocumentReferenceResolver.resolve("Page", spaceReference)).thenReturn(documentReference);
assertEquals(Arrays.asList(documentReference), this.mocker.getComponentUnderTest().getDocumentReferences(spaceReference));
verify(query).setWiki(spaceReference.getWikiReference().getName());
verify(query).bindValue("space", "Space");
verify(query).bindValue("spacePrefix", "Space.%");
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class DBListClassPropertyValuesProviderTest method getValuesMixedWithUsed.
@Test
public void getValuesMixedWithUsed() throws Exception {
ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
DocumentReference authorReference = this.dbListClass.getOwnerDocument().getAuthorReference();
PropertyValues values = new PropertyValues();
PropertyValue red = new PropertyValue();
red.setValue("red");
red.setMetaData(new HashMap<String, Object>());
red.getMetaData().put("label", "Red");
values.getPropertyValues().add(red);
when(this.authorExecutor.call(any(), eq(authorReference))).thenReturn(values);
Query query = mock(Query.class);
QueryParameter queryParameter = mock(QueryParameter.class);
when(this.usedValuesQueryBuilder.build(dbListClass)).thenReturn(query);
when(query.bindValue("text")).thenReturn(queryParameter);
when(queryParameter.anyChars()).thenReturn(queryParameter);
when(queryParameter.literal("bar")).thenReturn(queryParameter);
when(query.execute()).thenReturn(Arrays.asList(new Object[] { "blue", 21L }, new Object[] { "red", 17L }));
assertSame(values, this.mocker.getComponentUnderTest().getValues(propertyReference, 3, "bar"));
verify(query).setLimit(2);
verify(query).addFilter(this.mocker.getInstance(QueryFilter.class, "text"));
verify(queryParameter, times(2)).anyChars();
assertEquals(2, values.getPropertyValues().size());
assertEquals("red", values.getPropertyValues().get(0).getValue());
assertEquals(17L, values.getPropertyValues().get(0).getMetaData().get("count"));
assertEquals("blue", values.getPropertyValues().get(1).getValue());
assertEquals(21L, values.getPropertyValues().get(1).getMetaData().get("count"));
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class PageTest method setUpForPageTest.
/**
* Configures the various Components and their mocks with default values for page tests.
*
* @throws Exception in case of errors
*/
@Before
public void setUpForPageTest() throws Exception {
// Configure mocks from OldcoreRule
context = oldcore.getXWikiContext();
xwiki = oldcore.getSpyXWiki();
// We need this one because some component in its init creates a query...
when(oldcore.getQueryManager().createQuery(any(String.class), any(String.class))).thenReturn(mock(Query.class));
// Set up a fake Request
// Configure request so that $!request.outputSyntax" == 'plain
// Need to be executed before ecm.initialize() so that XWikiScriptContextInitializer will initialize the
// script context properly
request = new XWikiServletRequestStub();
request.setScheme("http");
context.setRequest(request);
response = new XWikiServletResponseStub();
context.setResponse(response);
ExecutionContextManager ecm = mocker.getInstance(ExecutionContextManager.class);
ecm.initialize(oldcore.getExecutionContext());
// Let the user have view access to all pages
when(oldcore.getMockRightService().hasAccessLevel(eq("view"), eq("XWiki.XWikiGuest"), any(), eq(context))).thenReturn(true);
// Set up URL Factory
URLFactorySetup.setUp(xwiki, context);
// Set up Localization
LocalizationSetup.setUp(mocker);
// Set up Skin Extensions
SkinExtensionSetup.setUp(xwiki, context);
}
use of org.xwiki.query.Query 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.Query 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);
}
}
Aggregations