Search in sources :

Example 1 with Class

use of org.xwiki.rest.model.jaxb.Class in project xwiki-platform by xwiki.

the class ModelFactory method toRestClass.

public Class toRestClass(URI baseUri, com.xpn.xwiki.api.Class xwikiClass) {
    Class clazz = this.objectFactory.createClass();
    clazz.setId(xwikiClass.getName());
    clazz.setName(xwikiClass.getName());
    DocumentReference reference = xwikiClass.getReference();
    String wikiName = reference.getWikiReference().getName();
    for (java.lang.Object xwikiPropertyClassObject : xwikiClass.getProperties()) {
        PropertyClass xwikiPropertyClass = (PropertyClass) xwikiPropertyClassObject;
        Property property = this.objectFactory.createProperty();
        property.setName(xwikiPropertyClass.getName());
        property.setType(xwikiPropertyClass.getxWikiClass().getName());
        for (java.lang.Object xwikiPropertyObject : xwikiPropertyClass.getProperties()) {
            com.xpn.xwiki.api.Property xwikiProperty = (com.xpn.xwiki.api.Property) xwikiPropertyObject;
            java.lang.Object value = xwikiProperty.getValue();
            Attribute attribute = this.objectFactory.createAttribute();
            attribute.setName(xwikiProperty.getName());
            if (value != null) {
                attribute.setValue(value.toString());
            } else {
                attribute.setValue("");
            }
            property.getAttributes().add(attribute);
        }
        String propertyUri = Utils.createURI(baseUri, ClassPropertyResource.class, wikiName, xwikiClass.getName(), xwikiPropertyClass.getName()).toString();
        Link propertyLink = this.objectFactory.createLink();
        propertyLink.setHref(propertyUri);
        propertyLink.setRel(Relations.SELF);
        property.getLinks().add(propertyLink);
        clazz.getProperties().add(property);
    }
    String classUri = Utils.createURI(baseUri, ClassResource.class, wikiName, xwikiClass.getName()).toString();
    Link classLink = this.objectFactory.createLink();
    classLink.setHref(classUri);
    classLink.setRel(Relations.SELF);
    clazz.getLinks().add(classLink);
    String propertiesUri = Utils.createURI(baseUri, ClassPropertiesResource.class, wikiName, xwikiClass.getName()).toString();
    Link propertyLink = this.objectFactory.createLink();
    propertyLink.setHref(propertiesUri);
    propertyLink.setRel(Relations.PROPERTIES);
    clazz.getLinks().add(propertyLink);
    String objectsUri = Utils.createURI(baseUri, AllObjectsForClassNameResource.class, wikiName, xwikiClass.getName()).toString();
    Link objectsLink = this.objectFactory.createLink();
    objectsLink.setHref(objectsUri);
    objectsLink.setRel(Relations.OBJECTS);
    clazz.getLinks().add(objectsLink);
    return clazz;
}
Also used : ClassResource(org.xwiki.rest.resources.classes.ClassResource) Attribute(org.xwiki.rest.model.jaxb.Attribute) ClassPropertyResource(org.xwiki.rest.resources.classes.ClassPropertyResource) PropertyClass(com.xpn.xwiki.api.PropertyClass) ClassPropertiesResource(org.xwiki.rest.resources.classes.ClassPropertiesResource) AllObjectsForClassNameResource(org.xwiki.rest.resources.objects.AllObjectsForClassNameResource) ListClass(com.xpn.xwiki.objects.classes.ListClass) PropertyClass(com.xpn.xwiki.api.PropertyClass) Class(org.xwiki.rest.model.jaxb.Class) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) Property(org.xwiki.rest.model.jaxb.Property) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) Link(org.xwiki.rest.model.jaxb.Link)

Example 2 with Class

use of org.xwiki.rest.model.jaxb.Class in project xwiki-platform by xwiki.

the class ClassPropertyResourceImpl method getClassProperty.

@Override
public Property getClassProperty(String wikiName, String className, String propertyName) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    try {
        Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        com.xpn.xwiki.api.Class xwikiClass = Utils.getXWikiApi(componentManager).getClass(className);
        if (xwikiClass == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        Class clazz = DomainObjectFactory.createClass(objectFactory, uriInfo.getBaseUri(), wikiName, xwikiClass);
        for (Property property : clazz.getProperties()) {
            if (property.getName().equals(propertyName)) {
                String classUri = Utils.createURI(uriInfo.getBaseUri(), ClassResource.class, wikiName, xwikiClass.getName()).toString();
                Link classLink = objectFactory.createLink();
                classLink.setHref(classUri);
                classLink.setRel(Relations.CLASS);
                property.getLinks().add(classLink);
                return property;
            }
        }
        throw new WebApplicationException(Status.NOT_FOUND);
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
}
Also used : ClassResource(org.xwiki.rest.resources.classes.ClassResource) WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Class(org.xwiki.rest.model.jaxb.Class) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with Class

use of org.xwiki.rest.model.jaxb.Class in project xwiki-platform by xwiki.

the class ClassesResourceTest method testRepresentation.

@Override
@Test
public void testRepresentation() throws Exception {
    GetMethod getMethod = executeGet(buildURI(ClassesResource.class, getWiki()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Classes classes = (Classes) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    for (Class clazz : classes.getClazzs()) {
        checkLinks(clazz);
        for (Property property : clazz.getProperties()) {
            checkLinks(property);
        }
    }
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) Class(org.xwiki.rest.model.jaxb.Class) Property(org.xwiki.rest.model.jaxb.Property) Classes(org.xwiki.rest.model.jaxb.Classes) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 4 with Class

use of org.xwiki.rest.model.jaxb.Class in project xwiki-platform by xwiki.

the class ClassPropertiesResourceImpl method getClassProperties.

@Override
public Properties getClassProperties(String wikiName, String className) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    try {
        Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        com.xpn.xwiki.api.Class xwikiClass = Utils.getXWikiApi(componentManager).getClass(className);
        if (xwikiClass == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        Class clazz = DomainObjectFactory.createClass(objectFactory, uriInfo.getBaseUri(), wikiName, xwikiClass);
        Properties properties = objectFactory.createProperties();
        properties.getProperties().addAll(clazz.getProperties());
        String classUri = Utils.createURI(uriInfo.getBaseUri(), ClassResource.class, wikiName, xwikiClass.getName()).toString();
        Link classLink = objectFactory.createLink();
        classLink.setHref(classUri);
        classLink.setRel(Relations.CLASS);
        properties.getLinks().add(classLink);
        return properties;
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
}
Also used : ClassResource(org.xwiki.rest.resources.classes.ClassResource) WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Class(org.xwiki.rest.model.jaxb.Class) Properties(org.xwiki.rest.model.jaxb.Properties) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

Class (org.xwiki.rest.model.jaxb.Class)4 Link (org.xwiki.rest.model.jaxb.Link)3 Property (org.xwiki.rest.model.jaxb.Property)3 ClassResource (org.xwiki.rest.resources.classes.ClassResource)3 XWikiException (com.xpn.xwiki.XWikiException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 XWikiRestException (org.xwiki.rest.XWikiRestException)2 PropertyClass (com.xpn.xwiki.api.PropertyClass)1 BaseProperty (com.xpn.xwiki.objects.BaseProperty)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 ListClass (com.xpn.xwiki.objects.classes.ListClass)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 Test (org.junit.Test)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 Attribute (org.xwiki.rest.model.jaxb.Attribute)1 Classes (org.xwiki.rest.model.jaxb.Classes)1 Properties (org.xwiki.rest.model.jaxb.Properties)1 ClassPropertiesResource (org.xwiki.rest.resources.classes.ClassPropertiesResource)1 ClassPropertyResource (org.xwiki.rest.resources.classes.ClassPropertyResource)1 AllObjectsForClassNameResource (org.xwiki.rest.resources.objects.AllObjectsForClassNameResource)1