Search in sources :

Example 16 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class CurrentUserPropertyResourceImpl method buildResponse.

private Response buildResponse(XWikiDocument document, String propertyName, XWikiContext xcontext) {
    BaseObject baseObject = document.getXObject(USER_REFERENCE);
    Object object = this.factory.toRestObject(this.uriInfo.getBaseUri(), new Document(document, xcontext), baseObject, false, false);
    for (Property p : object.getProperties()) {
        if (p.getName().equals(propertyName)) {
            return Response.status(Status.ACCEPTED).entity(p).build();
        }
    }
    throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BaseObject(com.xpn.xwiki.objects.BaseObject) Object(org.xwiki.rest.model.jaxb.Object) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Property(org.xwiki.rest.model.jaxb.Property) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 17 with Document

use of com.xpn.xwiki.api.Document 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;
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) Query(org.xwiki.query.Query) HashMap(java.util.HashMap) Formatter(java.util.Formatter) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) URL(java.net.URL) Pages(org.xwiki.rest.model.jaxb.Pages) QueryException(org.xwiki.query.QueryException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PageSummary(org.xwiki.rest.model.jaxb.PageSummary) Link(org.xwiki.rest.model.jaxb.Link)

Example 18 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class ObjectResourceImpl method deleteObject.

@Override
public void deleteObject(String wikiName, String spaceName, String pageName, String className, Integer objectNumber) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        com.xpn.xwiki.api.Object object = doc.getObject(className, objectNumber);
        if (object == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        doc.removeObject(object);
        doc.save();
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiException(com.xpn.xwiki.XWikiException)

Example 19 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class ObjectsForClassNameResourceImpl method getObjects.

@Override
public Objects getObjects(String wikiName, String spaceName, String pageName, String className, Integer start, Integer number, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        Objects objects = objectFactory.createObjects();
        List<com.xpn.xwiki.objects.BaseObject> objectList = getBaseObjects(doc, className);
        RangeIterable<com.xpn.xwiki.objects.BaseObject> ri = new RangeIterable<com.xpn.xwiki.objects.BaseObject>(objectList, start, number);
        for (com.xpn.xwiki.objects.BaseObject object : ri) {
            // By deleting objects, some of them might become null, so we must check for this
            if (object != null) {
                objects.getObjectSummaries().add(DomainObjectFactory.createObjectSummary(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, object, false, Utils.getXWikiApi(componentManager), withPrettyNames));
            }
        }
        return objects;
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : RangeIterable(org.xwiki.rest.internal.RangeIterable) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) Objects(org.xwiki.rest.model.jaxb.Objects) XWikiException(com.xpn.xwiki.XWikiException)

Example 20 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class ObjectsResourceImpl method addObject.

@Override
public Response addObject(String wikiName, String spaceName, String pageName, Boolean minorRevision, Object object) throws XWikiRestException {
    if (object.getClassName() == null) {
        throw new WebApplicationException(Status.BAD_REQUEST);
    }
    try {
        List<String> spaces = parseSpaceSegments(spaceName);
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        BaseObject xwikiObject = xwikiDocument.newObject(object.getClassName(), Utils.getXWikiContext(componentManager));
        if (xwikiObject == null) {
            throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
        }
        // We must initialize all the fields to an empty value in order to correctly create the object
        BaseClass xwikiClass = Utils.getXWiki(componentManager).getClass(xwikiObject.getClassName(), Utils.getXWikiContext(componentManager));
        for (java.lang.Object propertyNameObject : xwikiClass.getPropertyNames()) {
            String propertyName = (String) propertyNameObject;
            xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
        }
        for (Property property : object.getProperties()) {
            xwikiObject.set(property.getName(), property.getValue(), Utils.getXWikiContext(componentManager));
        }
        doc.save("", Boolean.TRUE.equals(minorRevision));
        return Response.created(Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, wikiName, spaces, pageName, object.getClassName(), xwikiObject.getNumber())).entity(DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, xwikiObject, false, Utils.getXWikiApi(componentManager), false)).build();
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) Property(org.xwiki.rest.model.jaxb.Property) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

Document (com.xpn.xwiki.api.Document)97 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)60 XWikiException (com.xpn.xwiki.XWikiException)41 XWikiRestException (org.xwiki.rest.XWikiRestException)40 DocumentReference (org.xwiki.model.reference.DocumentReference)37 BaseObject (com.xpn.xwiki.objects.BaseObject)24 Test (org.junit.Test)23 WebApplicationException (javax.ws.rs.WebApplicationException)22 ArrayList (java.util.ArrayList)16 Attachment (com.xpn.xwiki.api.Attachment)14 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 Vector (java.util.Vector)12 Link (org.xwiki.rest.model.jaxb.Link)10 XWikiContext (com.xpn.xwiki.XWikiContext)9 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)7 Date (java.util.Date)7 XWiki (com.xpn.xwiki.api.XWiki)6 RangeIterable (org.xwiki.rest.internal.RangeIterable)6 Object (org.xwiki.rest.model.jaxb.Object)6 XWiki (com.xpn.xwiki.XWiki)5