Search in sources :

Example 1 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class EventClassDocumentInitializerTest method testCreateClass.

@Test
public void testCreateClass() throws Exception {
    BaseClass testXClass = new BaseClass();
    this.mocker.getComponentUnderTest().createClass(testXClass);
    assertEquals(10, testXClass.getFieldList().size());
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) Test(org.junit.Test)

Example 2 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class ObjectAddAction method action.

@Override
public boolean action(XWikiContext context) throws XWikiException {
    // CSRF prevention
    if (!csrfTokenCheck(context)) {
        return false;
    }
    XWiki xwiki = context.getWiki();
    XWikiResponse response = context.getResponse();
    DocumentReference userReference = context.getUserReference();
    XWikiDocument doc = context.getDoc();
    ObjectAddForm oform = (ObjectAddForm) context.getForm();
    String className = oform.getClassName();
    EntityReference classReference = this.relativeResolver.resolve(className, EntityType.DOCUMENT);
    BaseObject object = doc.newXObject(classReference, context);
    BaseClass baseclass = object.getXClass(context);
    // The request parameter names that correspond to object fields must NOT specify the object number because the
    // object number is not known before the object is added. The following is a good parameter name:
    // Space.Class_property. As a consequence we use only the class name to extract the object from the request.
    Map<String, String[]> objmap = oform.getObject(className);
    // We need to have a string in the map for each field for the object to be correctly created.
    // Otherwise, queries using the missing properties will fail to return this object.
    @SuppressWarnings("unchecked") Collection<PropertyClass> fields = baseclass.getFieldList();
    for (PropertyClass property : fields) {
        String name = property.getName();
        if (objmap.get(name) == null) {
            objmap.put(name, EMPTY_PROPERTY);
        }
    }
    // Load the object properties that are defined in the request.
    baseclass.fromMap(objmap, object);
    doc.setAuthorReference(userReference);
    if (doc.isNew()) {
        doc.setCreatorReference(userReference);
    }
    xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addObject"), true, context);
    // If this is an ajax request, no need to redirect.
    if (Utils.isAjaxRequest(context)) {
        context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
        return false;
    }
    // forward to edit
    String redirect = Utils.getRedirect("edit", "editor=object", "xcontinue", "xredirect");
    // If the redirect URL contains the xobjectNumber parameter then inject the number of the added object as its
    // value so that the target page knows which object was added.
    redirect = XOBJECT_NUMBER_PARAMETER.matcher(redirect).replaceFirst("$1xobjectNumber=" + object.getNumber() + "$2");
    sendRedirect(response, redirect);
    return false;
}
Also used : XWiki(com.xpn.xwiki.XWiki) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class AbstractPropChangeAction method action.

/**
 * Tries to change the specified property, and redirect back to the class editor (or the specified {@code xredirect}
 * location). If the property does not exist, forward to the exception page.
 *
 * @param context the current request context
 * @return {@code false} if the operation succeeded and the response is finished, {@code true} if the response must
 *         be rendered by {@link #render(XWikiContext)}
 * @throws XWikiException if saving the document fails
 */
@Override
public boolean action(XWikiContext context) throws XWikiException {
    XWikiResponse response = context.getResponse();
    XWikiDocument doc = context.getDoc();
    PropChangeForm form = (PropChangeForm) context.getForm();
    String propertyName = form.getPropertyName();
    BaseClass xclass = doc.getXClass();
    if (propertyName != null && xclass.get(propertyName) != null) {
        // CSRF prevention
        if (!csrfTokenCheck(context)) {
            return false;
        }
        changePropertyDefinition(xclass, propertyName, context);
    } else {
        return true;
    }
    if (Utils.isAjaxRequest(context)) {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        response.setContentLength(0);
    } else {
        String redirect = Utils.getRedirect("edit", "editor=class", context);
        sendRedirect(response, redirect);
    }
    return false;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Example 4 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class ModelFactory method toRestObject.

public Object toRestObject(URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion, Boolean withPrettyNames) {
    Object object = this.objectFactory.createObject();
    fillObjectSummary(object, doc, xwikiObject, withPrettyNames);
    XWikiContext xwikiContext = this.xcontextProvider.get();
    BaseClass xwikiClass = xwikiObject.getXClass(xwikiContext);
    for (java.lang.Object propertyClassObject : xwikiClass.getProperties()) {
        com.xpn.xwiki.objects.classes.PropertyClass propertyClass = (com.xpn.xwiki.objects.classes.PropertyClass) propertyClassObject;
        Property property = this.objectFactory.createProperty();
        for (java.lang.Object o : propertyClass.getProperties()) {
            BaseProperty baseProperty = (BaseProperty) o;
            Attribute attribute = this.objectFactory.createAttribute();
            attribute.setName(baseProperty.getName());
            /* Check for null values in order to prevent NPEs */
            if (baseProperty.getValue() != null) {
                attribute.setValue(baseProperty.getValue().toString());
            } else {
                attribute.setValue("");
            }
            property.getAttributes().add(attribute);
        }
        if (propertyClass instanceof ListClass) {
            ListClass listClass = (ListClass) propertyClass;
            List allowedValueList = listClass.getList(xwikiContext);
            if (!allowedValueList.isEmpty()) {
                Formatter f = new Formatter();
                for (int i = 0; i < allowedValueList.size(); i++) {
                    if (i != allowedValueList.size() - 1) {
                        f.format("%s,", allowedValueList.get(i).toString());
                    } else {
                        f.format("%s", allowedValueList.get(i).toString());
                    }
                }
                Attribute attribute = this.objectFactory.createAttribute();
                attribute.setName(Constants.ALLOWED_VALUES_ATTRIBUTE_NAME);
                attribute.setValue(f.toString());
                property.getAttributes().add(attribute);
            }
        }
        property.setName(propertyClass.getName());
        property.setType(propertyClass.getClassType());
        try {
            property.setValue(serializePropertyValue(xwikiObject.get(propertyClass.getName())));
        } catch (XWikiException e) {
        // Should never happen
        }
        String propertyUri;
        if (useVersion) {
            propertyUri = Utils.createURI(baseUri, ObjectPropertyAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
        } else {
            propertyUri = Utils.createURI(baseUri, ObjectPropertyResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
        }
        Link propertyLink = this.objectFactory.createLink();
        propertyLink.setHref(propertyUri);
        propertyLink.setRel(Relations.SELF);
        property.getLinks().add(propertyLink);
        object.getProperties().add(property);
    }
    Link objectLink = getObjectLink(this.objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.SELF);
    object.getLinks().add(objectLink);
    return object;
}
Also used : Attribute(org.xwiki.rest.model.jaxb.Attribute) Formatter(java.util.Formatter) XWikiContext(com.xpn.xwiki.XWikiContext) PropertyClass(com.xpn.xwiki.api.PropertyClass) ListClass(com.xpn.xwiki.objects.classes.ListClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) Object(org.xwiki.rest.model.jaxb.Object) List(java.util.List) ArrayList(java.util.ArrayList) BaseProperty(com.xpn.xwiki.objects.BaseProperty) Property(org.xwiki.rest.model.jaxb.Property) BaseProperty(com.xpn.xwiki.objects.BaseProperty) XWikiException(com.xpn.xwiki.XWikiException) Link(org.xwiki.rest.model.jaxb.Link)

Example 5 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class DefaultWikiComponentBuilderEventListener method installOrUpdateComponentXClass.

/**
 * Verify that the {@link #COMPONENT_CLASS} exists and is up-to-date (act if not).
 *
 * @throws XWikiException on failure
 */
private void installOrUpdateComponentXClass() throws XWikiException {
    XWikiContext xcontext = getXWikiContext();
    XWikiDocument doc = xcontext.getWiki().getDocument(COMPONENT_CLASS_REFERENCE, xcontext);
    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(doc.getDocumentReference());
    boolean needsUpdate = false;
    needsUpdate |= initializeXClassDocumentMetadata(doc, "Wiki Component XWiki Class");
    needsUpdate |= bclass.addTextField(COMPONENT_ROLE_TYPE_FIELD, "Component Role Type", 30);
    needsUpdate |= bclass.addTextField(COMPONENT_ROLE_HINT_FIELD, "Component Role Hint", 30);
    needsUpdate |= bclass.addStaticListField(COMPONENT_SCOPE_FIELD, "Component Scope", 1, false, "wiki=Current Wiki|user=Current User|global=Global", "select");
    if (needsUpdate) {
        this.update(doc);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext)

Aggregations

BaseClass (com.xpn.xwiki.objects.classes.BaseClass)100 DocumentReference (org.xwiki.model.reference.DocumentReference)42 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)41 BaseObject (com.xpn.xwiki.objects.BaseObject)40 XWikiException (com.xpn.xwiki.XWikiException)26 XWikiContext (com.xpn.xwiki.XWikiContext)24 ArrayList (java.util.ArrayList)18 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)16 XWiki (com.xpn.xwiki.XWiki)15 Test (org.junit.Test)15 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)15 EntityReference (org.xwiki.model.reference.EntityReference)10 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)9 BaseProperty (com.xpn.xwiki.objects.BaseProperty)9 List (java.util.List)9 ToString (org.suigeneris.jrcs.util.ToString)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Before (org.junit.Before)6 TextAreaClass (com.xpn.xwiki.objects.classes.TextAreaClass)5 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)5