use of org.xwiki.query.QueryException 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);
}
}
use of org.xwiki.query.QueryException in project xwiki-platform by xwiki.
the class QueryExceptionTest method getMessageWhenNamedQuery.
@Test
public void getMessageWhenNamedQuery() {
Query query = mock(Query.class);
when(query.isNamed()).thenReturn(true);
when(query.getStatement()).thenReturn("namedquery");
Exception nestedException = mock(Exception.class);
when(nestedException.getMessage()).thenReturn("nestedmessage");
QueryException queryException = new QueryException("message", query, nestedException);
assertEquals("message. Named query = [namedquery]", queryException.getMessage());
}
use of org.xwiki.query.QueryException 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.QueryException in project xwiki-platform by xwiki.
the class SecureQueryExecutorManager method execute.
@Override
public <T> List<T> execute(Query query) throws QueryException {
if (query instanceof SecureQuery) {
SecureQuery secureQuery = (SecureQuery) query;
// Force checking current author rights
secureQuery.checkCurrentAuthor(true);
} else if (!this.authorization.hasAccess(Right.PROGRAM)) {
throw new QueryException("Unsecure query require programming right", query, null);
}
return this.defaultQueryExecutorManager.execute(query);
}
use of org.xwiki.query.QueryException 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;
}
Aggregations