use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class QueryExceptionTest method getMessageWhenStatement.
@Test
public void getMessageWhenStatement() {
Query query = mock(Query.class);
when(query.isNamed()).thenReturn(false);
when(query.getStatement()).thenReturn("statement");
Exception nestedException = mock(Exception.class);
when(nestedException.getMessage()).thenReturn("nestedmessage");
QueryException queryException = new QueryException("message", query, nestedException);
assertEquals("message. Query statement = [statement]", queryException.getMessage());
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class SecureQueryExecutorManagerTest method executeNotSecureQueryWithProgrammingRight.
@Test
public void executeNotSecureQueryWithProgrammingRight() throws QueryException {
this.hasProgrammingRight = true;
Query query = mock(Query.class);
this.executor.execute(query);
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class BaseAttachmentsResource method getAttachments.
/**
* Retrieves the attachments by filtering them.
*
* @param wikiName The virtual wiki.
* @param name Name filter (include only attachments that matches this name)
* @param page Page filter (include only attachments are attached to a page matches this string)
* @param space Space filter (include only attachments are attached to a page in a space matching this string)
* @param author Author filter (include only attachments from an author who matches this string)
* @param types A comma separated list of string that will be matched against the actual mime type of the
* attachments.
* @return The list of the retrieved attachments.
*/
public Attachments getAttachments(String wikiName, String name, String page, String space, String author, String types, Integer start, Integer number, Boolean withPrettyNames) throws XWikiRestException {
String database = Utils.getXWikiContext(componentManager).getWikiId();
Attachments attachments = objectFactory.createAttachments();
/* This try is just needed for executing the finally clause. */
try {
Utils.getXWikiContext(componentManager).setWikiId(wikiName);
Map<String, String> filters = new HashMap<String, String>();
if (!name.equals("")) {
filters.put("name", name);
}
if (!page.equals("")) {
filters.put("page", name);
}
if (!space.equals("")) {
filters.put("space", Utils.getLocalSpaceId(parseSpaceSegments(space)));
}
if (!author.equals("")) {
filters.put("author", author);
}
/* Build the query */
Formatter f = new Formatter();
f.format("select doc.space, doc.name, doc.version, attachment from XWikiDocument as doc," + " XWikiAttachment as attachment where (attachment.docId=doc.id ");
if (filters.keySet().size() > 0) {
for (String param : filters.keySet()) {
if (param.equals("name")) {
f.format(" and upper(attachment.filename) like :name ");
}
if (param.equals("page")) {
f.format(" and upper(doc.fullName) like :page ");
}
if (param.equals("space")) {
f.format(" and upper(doc.space) like :space ");
}
if (param.equals("author")) {
f.format(" and upper(attachment.author) like :author ");
}
}
}
f.format(")");
String queryString = f.toString();
/* Execute the query by filling the parameters */
List<Object> queryResult = null;
try {
Query query = queryManager.createQuery(queryString, Query.XWQL).setLimit(number).setOffset(start);
for (String param : filters.keySet()) {
query.bindValue(param, String.format("%%%s%%", filters.get(param).toUpperCase()));
}
queryResult = query.execute();
} catch (QueryException e) {
throw new XWikiRestException(e);
}
Set<String> acceptedMimeTypes = new HashSet<String>();
if (!types.equals("")) {
String[] acceptedMimetypesArray = types.split(",");
for (String type : acceptedMimetypesArray) {
acceptedMimeTypes.add(type);
}
}
for (Object object : queryResult) {
Object[] fields = (Object[]) object;
String pageSpaceId = (String) fields[0];
List<String> pageSpaces = Utils.getSpacesFromSpaceId(pageSpaceId);
String pageName = (String) fields[1];
String pageId = Utils.getPageId(wikiName, pageSpaces, pageName);
String pageVersion = (String) fields[2];
XWikiAttachment xwikiAttachment = (XWikiAttachment) fields[3];
String mimeType = xwikiAttachment.getMimeType(Utils.getXWikiContext(componentManager));
boolean add = true;
/* Check the mime type filter */
if (acceptedMimeTypes.size() > 0) {
add = false;
for (String type : acceptedMimeTypes) {
if (mimeType.toUpperCase().contains(type.toUpperCase())) {
add = true;
break;
}
}
}
if (add) {
/*
* We manufacture attachments in place because we don't have all the data for calling the
* DomainObjectFactory method (doing so would require to retrieve an actual Document)
*/
Attachment attachment = objectFactory.createAttachment();
attachment.setId(String.format("%s@%s", pageId, xwikiAttachment.getFilename()));
attachment.setName(xwikiAttachment.getFilename());
attachment.setLongSize(xwikiAttachment.getLongSize());
// Retro compatibility
attachment.setSize((int) xwikiAttachment.getLongSize());
attachment.setMimeType(mimeType);
attachment.setAuthor(xwikiAttachment.getAuthor());
if (withPrettyNames) {
attachment.setAuthorName(Utils.getAuthorName(xwikiAttachment.getAuthorReference(), componentManager));
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(xwikiAttachment.getDate());
attachment.setDate(calendar);
attachment.setPageId(pageId);
attachment.setPageVersion(pageVersion);
attachment.setVersion(xwikiAttachment.getVersion());
URL absoluteUrl = Utils.getXWikiContext(componentManager).getURLFactory().createAttachmentURL(xwikiAttachment.getFilename(), pageSpaceId, pageName, "download", null, wikiName, Utils.getXWikiContext(componentManager));
attachment.setXwikiAbsoluteUrl(absoluteUrl.toString());
attachment.setXwikiRelativeUrl(Utils.getXWikiContext(componentManager).getURLFactory().getURL(absoluteUrl, Utils.getXWikiContext(componentManager)));
URI pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, wikiName, pageSpaces, pageName);
Link pageLink = objectFactory.createLink();
pageLink.setHref(pageUri.toString());
pageLink.setRel(Relations.PAGE);
attachment.getLinks().add(pageLink);
URI attachmentUri = Utils.createURI(uriInfo.getBaseUri(), AttachmentResource.class, wikiName, pageSpaces, pageName, xwikiAttachment.getFilename());
Link attachmentLink = objectFactory.createLink();
attachmentLink.setHref(attachmentUri.toString());
attachmentLink.setRel(Relations.ATTACHMENT_DATA);
attachment.getLinks().add(attachmentLink);
attachments.getAttachments().add(attachment);
}
}
} finally {
Utils.getXWikiContext(componentManager).setWikiId(database);
}
return attachments;
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class AbstractUsersAndGroupsClassPropertyValuesProvider method getUsedValues.
@Override
protected PropertyValues getUsedValues(T propertyDefinition, int limit, String filter) throws QueryException {
Query query = this.usedValuesQueryBuilder.build(propertyDefinition);
// We know the used values are document references so we can check view access in a better way than what the
// used values query builder does by default.
query.getFilters().clear();
query.addFilter(this.documentFilter);
query.addFilter(this.viewableFilter);
return getValues(query, limit, filter, propertyDefinition);
}
use of org.xwiki.query.Query in project xwiki-platform by xwiki.
the class WikiPagesResourceImpl method getPages.
@Override
public Pages getPages(String wikiName, Integer start, String name, String space, String author, Integer number) throws XWikiRestException {
String database = Utils.getXWikiContext(componentManager).getWikiId();
Pages pages = objectFactory.createPages();
/* This try is just needed for executing the finally clause. */
try {
Map<String, String> filters = new HashMap<String, String>();
if (!name.equals("")) {
filters.put("name", name);
}
if (!space.equals("")) {
filters.put("space", Utils.getLocalSpaceId(parseSpaceSegments(space)));
}
if (!author.equals("")) {
filters.put("author", author);
}
/* Build the query */
Formatter f = new Formatter();
f.format("select doc from XWikiDocument as doc");
if (filters.keySet().size() > 0) {
f.format(" where (");
int i = 0;
for (String param : filters.keySet()) {
if (param.equals("name")) {
f.format(" upper(doc.fullName) like :name ");
}
if (param.equals("space")) {
f.format(" upper(doc.space) like :space ");
}
if (param.equals("author")) {
f.format(" upper(doc.contentAuthor) like :author ");
}
i++;
if (i < filters.keySet().size()) {
f.format(" and ");
}
}
f.format(")");
}
String queryString = f.toString();
/* Execute the query by filling the parameters */
List<Object> queryResult = null;
try {
Query query = queryManager.createQuery(queryString, Query.XWQL).setLimit(number).setOffset(start);
for (String param : filters.keySet()) {
query.bindValue(param, String.format("%%%s%%", filters.get(param).toUpperCase()));
}
queryResult = query.execute();
} catch (QueryException e) {
throw new XWikiRestException(e);
}
/* Get the results and populate the returned representation */
for (Object object : queryResult) {
XWikiDocument xwikiDocument = (XWikiDocument) object;
xwikiDocument.setDatabase(wikiName);
Document doc = new Document(xwikiDocument, Utils.getXWikiContext(componentManager));
/*
* We manufacture page summaries in place because we don't have all the data for calling the
* DomainObjectFactory method (doing so would require to retrieve an actual Document)
*/
PageSummary pageSummary = objectFactory.createPageSummary();
pageSummary.setId(doc.getPrefixedFullName());
pageSummary.setFullName(doc.getFullName());
pageSummary.setWiki(wikiName);
pageSummary.setSpace(doc.getSpace());
pageSummary.setName(doc.getName());
pageSummary.setTitle(doc.getTitle());
pageSummary.setParent(doc.getParent());
URL absoluteUrl = Utils.getXWikiContext(componentManager).getURLFactory().createExternalURL(doc.getSpace(), doc.getName(), "view", null, null, Utils.getXWikiContext(componentManager));
pageSummary.setXwikiAbsoluteUrl(absoluteUrl.toString());
pageSummary.setXwikiRelativeUrl(Utils.getXWikiContext(componentManager).getURLFactory().getURL(absoluteUrl, Utils.getXWikiContext(componentManager)));
String pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName()).toString();
Link pageLink = objectFactory.createLink();
pageLink.setHref(pageUri);
pageLink.setRel(Relations.PAGE);
pageSummary.getLinks().add(pageLink);
pages.getPageSummaries().add(pageSummary);
}
} finally {
Utils.getXWikiContext(componentManager).setWikiId(database);
}
return pages;
}
Aggregations