Search in sources :

Example 46 with Query

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.%");
}
Also used : Query(org.xwiki.query.Query) SpaceReference(org.xwiki.model.reference.SpaceReference) QueryManager(org.xwiki.query.QueryManager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 47 with Query

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"));
}
Also used : QueryParameter(org.xwiki.query.QueryParameter) QueryFilter(org.xwiki.query.QueryFilter) PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) Query(org.xwiki.query.Query) PropertyValue(org.xwiki.rest.model.jaxb.PropertyValue) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 48 with Query

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);
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) Query(org.xwiki.query.Query) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) Before(org.junit.Before)

Example 49 with Query

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;
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) XWikiException(com.xpn.xwiki.XWikiException)

Example 50 with Query

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);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query)

Aggregations

Query (org.xwiki.query.Query)129 DocumentReference (org.xwiki.model.reference.DocumentReference)41 Test (org.junit.Test)39 QueryException (org.xwiki.query.QueryException)36 ArrayList (java.util.ArrayList)29 XWikiException (com.xpn.xwiki.XWikiException)18 QueryFilter (org.xwiki.query.QueryFilter)18 QueryManager (org.xwiki.query.QueryManager)18 XWikiContext (com.xpn.xwiki.XWikiContext)15 HashMap (java.util.HashMap)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)12 Map (java.util.Map)11 List (java.util.List)9 Before (org.junit.Before)9 SecureQuery (org.xwiki.query.SecureQuery)9 XWiki (com.xpn.xwiki.XWiki)8 BaseObject (com.xpn.xwiki.objects.BaseObject)8 SQLQuery (org.hibernate.SQLQuery)8 WikiReference (org.xwiki.model.reference.WikiReference)8 WrappingQuery (org.xwiki.query.WrappingQuery)8