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);
}
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations