Search in sources :

Example 21 with Document

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

the class ModifiablePageResource method deletePage.

void deletePage(DocumentInfo documentInfo) throws XWikiException {
    Document doc = documentInfo.getDocument();
    doc.delete();
}
Also used : Document(com.xpn.xwiki.api.Document)

Example 22 with Document

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

the class PageTagsResourceImpl method setTags.

@Override
public Response setTags(String wikiName, String spaceName, String pageName, Boolean minorRevision, Tags tags) 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);
        }
        List<String> tagNames = new ArrayList<String>();
        for (Tag tag : tags.getTags()) {
            tagNames.add(tag.getName());
        }
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        BaseObject xwikiObject = xwikiDocument.getObject("XWiki.TagClass", 0);
        if (xwikiObject == null) {
            int objectNumber = xwikiDocument.createNewObject("XWiki.TagClass", Utils.getXWikiContext(componentManager));
            xwikiObject = xwikiDocument.getObject("XWiki.TagClass", objectNumber);
            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 (Object propertyNameObject : xwikiClass.getPropertyNames()) {
                String propertyName = (String) propertyNameObject;
                xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
            }
        }
        xwikiObject.set("tags", tagNames, Utils.getXWikiContext(componentManager));
        doc.save("", Boolean.TRUE.equals(minorRevision));
        return Response.status(Status.ACCEPTED).entity(tags).build();
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) ArrayList(java.util.ArrayList) 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) BaseObject(com.xpn.xwiki.objects.BaseObject) Tag(org.xwiki.rest.model.jaxb.Tag) XWikiException(com.xpn.xwiki.XWikiException)

Example 23 with Document

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

the class AllObjectsForClassNameResourceImpl method getObjects.

@Override
public Objects getObjects(String wikiName, String className, Integer start, Integer number, String order, Boolean withPrettyNames) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    try {
        Objects objects = new Objects();
        Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        String query = "select doc, obj from BaseObject as obj, XWikiDocument as doc where obj.name=doc.fullName and obj.className=:className";
        if ("date".equals(order)) {
            query += " order by doc.date desc";
        }
        List<Object> queryResult = null;
        queryResult = queryManager.createQuery(query, Query.XWQL).bindValue("className", className).setLimit(number).setOffset(start).execute();
        for (Object object : queryResult) {
            Object[] fields = (Object[]) object;
            XWikiDocument xwikiDocument = (XWikiDocument) fields[0];
            xwikiDocument.setDatabase(wikiName);
            Document doc = new Document(xwikiDocument, Utils.getXWikiContext(componentManager));
            BaseObject xwikiObject = (BaseObject) fields[1];
            ObjectSummary objectSummary = DomainObjectFactory.createObjectSummary(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, xwikiObject, false, Utils.getXWikiApi(componentManager), withPrettyNames);
            objects.getObjectSummaries().add(objectSummary);
        }
        return objects;
    } catch (Exception e) {
        throw new XWikiRestException(e);
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiRestException(org.xwiki.rest.XWikiRestException) Objects(org.xwiki.rest.model.jaxb.Objects) BaseObject(com.xpn.xwiki.objects.BaseObject) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) XWikiRestException(org.xwiki.rest.XWikiRestException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 24 with Document

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

the class ObjectPropertiesResourceImpl method getObjectProperties.

@Override
public Properties getObjectProperties(String wikiName, String spaceName, String pageName, String className, Integer objectNumber, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        com.xpn.xwiki.objects.BaseObject baseObject = getBaseObject(doc, className, objectNumber);
        if (baseObject == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        Object object = DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, baseObject, false, Utils.getXWikiApi(componentManager), withPrettyNames);
        Properties properties = objectFactory.createProperties();
        properties.getProperties().addAll(object.getProperties());
        String objectUri = Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), object.getClassName(), object.getNumber()).toString();
        Link objectLink = objectFactory.createLink();
        objectLink.setHref(objectUri);
        objectLink.setRel(Relations.OBJECT);
        properties.getLinks().add(objectLink);
        return properties;
    } 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) Properties(org.xwiki.rest.model.jaxb.Properties) Object(org.xwiki.rest.model.jaxb.Object) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 25 with Document

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

the class ObjectPropertyAtPageVersionResourceImpl method getObjectProperty.

@Override
public Property getObjectProperty(String wikiName, String spaceName, String pageName, String version, String className, Integer objectNumber, String propertyName, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, version, true, false);
        Document doc = documentInfo.getDocument();
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        xwikiDocument = Utils.getXWiki(componentManager).getDocument(xwikiDocument, doc.getVersion(), Utils.getXWikiContext(componentManager));
        com.xpn.xwiki.objects.BaseObject baseObject = xwikiDocument.getObject(className, objectNumber);
        if (baseObject == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        Object object = DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, baseObject, true, Utils.getXWikiApi(componentManager), withPrettyNames);
        for (Property property : object.getProperties()) {
            if (property.getName().equals(propertyName)) {
                String objectUri = Utils.createURI(uriInfo.getBaseUri(), ObjectAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), version, object.getClassName(), object.getNumber()).toString();
                Link objectLink = objectFactory.createLink();
                objectLink.setHref(objectUri);
                objectLink.setRel(Relations.OBJECT);
                property.getLinks().add(objectLink);
                return property;
            }
        }
        throw new WebApplicationException(Status.NOT_FOUND);
    } 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) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Object(org.xwiki.rest.model.jaxb.Object) ObjectAtPageVersionResource(org.xwiki.rest.resources.objects.ObjectAtPageVersionResource) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) 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