Search in sources :

Example 16 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class XWQLQueryExecutor method execute.

@Override
public <T> List<T> execute(Query query) throws QueryException {
    EntityReference currentEntityReference = this.context.getCurrentEntityReference();
    Query nativeQuery;
    try {
        this.progress.startStep(query, "query.xwql.progress.execute", "Execute XWQL query [{}]", query);
        if (query.getWiki() != null) {
            if (currentEntityReference.getType() == EntityType.WIKI) {
                this.context.setCurrentEntityReference(new WikiReference(query.getWiki()));
            } else {
                this.context.setCurrentEntityReference(currentEntityReference.replaceParent(currentEntityReference.extractReference(EntityType.WIKI), new WikiReference(query.getWiki())));
            }
        }
        nativeQuery = getQueryManager().createQuery(this.translator.translate(query.getStatement()), this.translator.getOutputLanguage());
        nativeQuery.setLimit(query.getLimit());
        nativeQuery.setOffset(query.getOffset());
        nativeQuery.setWiki(query.getWiki());
        if (query.getFilters() != null) {
            for (QueryFilter filter : query.getFilters()) {
                nativeQuery.addFilter(filter);
            }
        }
        for (Entry<String, Object> e : query.getNamedParameters().entrySet()) {
            nativeQuery.bindValue(e.getKey(), e.getValue());
        }
        for (Entry<Integer, Object> e : query.getPositionalParameters().entrySet()) {
            nativeQuery.bindValue(e.getKey(), e.getValue());
        }
        if (nativeQuery instanceof SecureQuery && query instanceof SecureQuery) {
            // No need to validate the HQL query for short XWQL queries
            if (((SecureQuery) query).isCurrentAuthorChecked() && !isShortFormStatement(query.getStatement())) {
                ((SecureQuery) nativeQuery).checkCurrentAuthor(true);
            }
            // Let HQL module take care of that is supported
            ((SecureQuery) nativeQuery).checkCurrentUser(((SecureQuery) query).isCurrentUserChecked());
        }
        return nativeQuery.execute();
    } catch (Exception e) {
        if (e instanceof QueryException) {
            throw (QueryException) e;
        }
        throw new QueryException("Exception while translating [" + query.getStatement() + "] XWQL query to the [" + this.translator.getOutputLanguage() + "] language", query, e);
    } finally {
        this.context.setCurrentEntityReference(currentEntityReference);
        this.progress.endStep(query);
    }
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) SecureQuery(org.xwiki.query.SecureQuery) EntityReference(org.xwiki.model.reference.EntityReference) WikiReference(org.xwiki.model.reference.WikiReference) SecureQuery(org.xwiki.query.SecureQuery) QueryException(org.xwiki.query.QueryException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 17 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class RepositoryManager method getExistingExtensionDocumentById.

public XWikiDocument getExistingExtensionDocumentById(String extensionId) throws QueryException, XWikiException {
    XWikiContext xcontext = this.xcontextProvider.get();
    DocumentReference[] cachedDocumentReference = this.documentReferenceCache.get(extensionId);
    if (cachedDocumentReference == null) {
        Query query = this.queryManager.createQuery("select doc.fullName from Document doc, doc.object(" + XWikiRepositoryModel.EXTENSION_CLASSNAME + ") as extension where extension." + XWikiRepositoryModel.PROP_EXTENSION_ID + " = :extensionId", Query.XWQL);
        query.bindValue("extensionId", extensionId);
        List<String> documentNames = query.execute();
        if (!documentNames.isEmpty()) {
            cachedDocumentReference = new DocumentReference[] { this.currentStringResolver.resolve(documentNames.get(0)) };
        } else {
            cachedDocumentReference = new DocumentReference[1];
        }
        this.documentReferenceCache.set(extensionId, cachedDocumentReference);
    }
    return cachedDocumentReference[0] != null ? xcontext.getWiki().getDocument(cachedDocumentReference[0], xcontext) : null;
}
Also used : Query(org.xwiki.query.Query) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 18 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class ExtensionsRESTResource method getExtensions.

@GET
public Extensions getExtensions(@QueryParam(Resources.QPARAM_LIST_START) @DefaultValue("0") Integer offset, @QueryParam(Resources.QPARAM_LIST_NUMBER) @DefaultValue("-1") int number, @QueryParam(Resources.QPARAM_LIST_REQUIRETOTALHITS) @DefaultValue("true") boolean requireTotalHits) throws QueryException {
    Extensions extensions = this.extensionObjectFactory.createExtensions();
    if (requireTotalHits) {
        Query countQuery = createExtensionsCountQuery(null, null);
        extensions.setTotalHits((int) getExtensionsCountResult(countQuery));
    } else {
        extensions.setTotalHits(-1);
    }
    extensions.setOffset(offset);
    Query query = createExtensionsSummariesQuery(null, null, offset, number, false);
    getExtensionSummaries(extensions.getExtensionSummaries(), query);
    return extensions;
}
Also used : Query(org.xwiki.query.Query) Extensions(org.xwiki.extension.repository.xwiki.model.jaxb.Extensions) GET(javax.ws.rs.GET)

Example 19 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class DefaultWikiTemplateManager method getTemplates.

@Override
public Collection<WikiDescriptor> getTemplates() throws WikiTemplateManagerException {
    List<WikiDescriptor> result = new ArrayList<WikiDescriptor>();
    try {
        Query query = this.queryManager.createQuery("from doc.object(WikiManager.WikiTemplateClass) as descriptor where doc.name like 'XWikiServer%' " + "and descriptor.iswikitemplate = 1", Query.XWQL);
        query.setWiki(xcontextProvider.get().getMainXWiki());
        List<String> documentNames = query.execute();
        if (documentNames != null && !documentNames.isEmpty()) {
            for (String documentName : documentNames) {
                String id = documentName.substring("XWiki.XWikiServer".length()).toLowerCase();
                result.add(wikiDescriptorManager.getById(id));
            }
        }
    } catch (Exception e) {
        throw new WikiTemplateManagerException("Failed to locate XWiki.XWikiServerClass documents", e);
    }
    return result;
}
Also used : Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) WikiProvisioningJobException(org.xwiki.wiki.provisioning.WikiProvisioningJobException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiPropertyGroupException(org.xwiki.wiki.properties.WikiPropertyGroupException) WikiTemplateManagerException(org.xwiki.wiki.template.WikiTemplateManagerException) WikiTemplateManagerException(org.xwiki.wiki.template.WikiTemplateManagerException)

Example 20 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class WikiTemplateMigration method hibernateMigrate.

@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
    // XWiki objects
    XWikiContext context = getXWikiContext();
    XWiki xwiki = context.getWiki();
    // WikiManager.WikiTemplateClass reference
    DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME);
    // XWiki.XWikiServerClass reference
    DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass");
    // Superadmin reference
    DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin");
    try {
        // Get all the descriptor documents
        String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj";
        Query query = queryManager.createQuery(statement, Query.XWQL);
        List<String> results = query.execute();
        for (String wikiPage : results) {
            XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context);
            // Get the "iswikitemplate" value
            BaseObject descriptorObject = document.getXObject(descriptorClassReference);
            int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0);
            // We remove the deprecated property from the descriptor
            descriptorObject.removeField(OLD_TEMPLATE_PROPERTY);
            // Add the new WikiManager.WikiTemplateClass object
            BaseObject object = document.getXObject(templateClassReference, true, context);
            // The new object might already exists and have a template property already set
            isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
            // Set the (new) value
            object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
            // The document must have an author
            document.setAuthorReference(superAdmin);
            // Save the document
            xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context);
        }
    } catch (QueryException e) {
        throw new DataMigrationException("Failed to get the list of all existing descriptors.", e);
    } catch (XWikiException e) {
        throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

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