Search in sources :

Example 6 with QueryException

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

Example 7 with QueryException

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

Example 8 with QueryException

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

Example 9 with QueryException

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

Example 10 with QueryException

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;
}
Also used : Query(org.xwiki.query.Query) HashMap(java.util.HashMap) Formatter(java.util.Formatter) XWikiRestException(org.xwiki.rest.XWikiRestException) Calendar(java.util.Calendar) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(org.xwiki.rest.model.jaxb.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachments(org.xwiki.rest.model.jaxb.Attachments) URI(java.net.URI) URL(java.net.URL) QueryException(org.xwiki.query.QueryException) Link(org.xwiki.rest.model.jaxb.Link) HashSet(java.util.HashSet)

Aggregations

QueryException (org.xwiki.query.QueryException)57 Query (org.xwiki.query.Query)32 DocumentReference (org.xwiki.model.reference.DocumentReference)19 XWikiException (com.xpn.xwiki.XWikiException)18 ArrayList (java.util.ArrayList)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 BaseObject (com.xpn.xwiki.objects.BaseObject)8 Test (org.junit.Test)8 QueryFilter (org.xwiki.query.QueryFilter)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 HashMap (java.util.HashMap)6 XWikiRestException (org.xwiki.rest.XWikiRestException)6 Event (org.xwiki.eventstream.Event)5 WikiReference (org.xwiki.model.reference.WikiReference)5 Date (java.util.Date)4 List (java.util.List)4 IconException (org.xwiki.icon.IconException)4 QueryManager (org.xwiki.query.QueryManager)4 SecureQuery (org.xwiki.query.SecureQuery)4 XWiki (com.xpn.xwiki.XWiki)3