Search in sources :

Example 31 with BaseClass

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

the class XWikiDocument method addXObjectFromRequest.

/**
 * Adds an object from an new object creation form.
 *
 * @since 2.2M2
 */
public BaseObject addXObjectFromRequest(XWikiContext context) throws XWikiException {
    // Read info in object
    ObjectAddForm form = new ObjectAddForm();
    form.setRequest(context.getRequest());
    form.readRequest();
    EntityReference classReference = getXClassEntityReferenceResolver().resolve(form.getClassName(), EntityType.DOCUMENT, getDocumentReference());
    BaseObject object = newXObject(classReference, context);
    BaseClass baseclass = object.getXClass(context);
    baseclass.fromMap(form.getObject(LOCAL_REFERENCE_SERIALIZER.serialize(resolveClassReference(classReference))), object);
    return object;
}
Also used : ObjectAddForm(com.xpn.xwiki.web.ObjectAddForm) EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 32 with BaseClass

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

the class AbstractMandatoryClassInitializer method updateDocument.

@Override
public boolean updateDocument(XWikiDocument document) {
    boolean needUpdate = super.updateDocument(document);
    // Class related document fields
    needUpdate |= updateClassDocumentFields(document);
    // Get document class
    BaseClass currentClass = document.getXClass();
    // Generate the class from scratch
    BaseClass newClass = new BaseClass();
    newClass.setDocumentReference(currentClass.getDocumentReference());
    createClass(newClass);
    // Make sure the current class contains required properties
    needUpdate |= currentClass.apply(newClass, false);
    return needUpdate;
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Example 33 with BaseClass

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

the class XWikiDocumentMockitoTest method generateFakeClass.

/**
 * Generate the fake class that is used for the test of {@link #readObjectsFromForm()} and
 * {@link #readObjectsFromFormUpdateOrCreate()}.
 *
 * @return The fake BaseClass
 */
private BaseClass generateFakeClass() {
    BaseClass baseClass = this.document.getXClass();
    baseClass.addTextField("string", "String", 30);
    baseClass.addTextAreaField("area", "Area", 10, 10);
    baseClass.addTextAreaField("puretextarea", "Pure text area", 10, 10);
    // set the text areas an non interpreted content
    ((TextAreaClass) baseClass.getField("puretextarea")).setContentType("puretext");
    baseClass.addPasswordField("passwd", "Password", 30);
    baseClass.addBooleanField("boolean", "Boolean", "yesno");
    baseClass.addNumberField("int", "Int", 10, "integer");
    baseClass.addStaticListField("stringlist", "StringList", "value1, value2");
    return baseClass;
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass)

Example 34 with BaseClass

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

the class UserTest method testGetEmail.

public void testGetEmail() throws Exception {
    Mock mockXWiki = mock(XWiki.class);
    getContext().setWiki((XWiki) mockXWiki.proxy());
    XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "XWiki", "Admin"));
    BaseClass userClass = new BaseClass();
    userClass.addTextField("email", "email address", 20);
    mockXWiki.stubs().method("getXClass").will(returnValue(userClass));
    BaseObject userObj = doc.newXObject(new DocumentReference("xwiki", "XWiki", "XWikiUsers"), getContext());
    userObj.setStringValue("email", "admin@mail.com");
    mockXWiki.stubs().method("getDocument").will(returnValue(doc));
    User u = new User(null, null);
    assertNull(u.getEmail());
    XWikiUser xu = new XWikiUser("XWiki.Admin");
    u = new User(xu, getContext());
    assertEquals("admin@mail.com", u.getEmail());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 35 with BaseClass

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

the class PropAddAction 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();
    XWikiDocument doc = context.getDoc();
    XWikiForm form = context.getForm();
    String propName = ((PropAddForm) form).getPropName();
    if (!Util.isValidXMLElementName(propName)) {
        context.put("message", "action.addClassProperty.error.invalidName");
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST, localizePlainOrKey("action.addClassProperty.error.invalidName"));
        return true;
    }
    String propType = ((PropAddForm) form).getPropType();
    BaseClass bclass = doc.getXClass();
    bclass.setName(doc.getFullName());
    if (bclass.get(propName) != null) {
        context.put("message", "action.addClassProperty.error.alreadyExists");
        List<String> parameters = new ArrayList<String>();
        parameters.add(propName);
        context.put("messageParameters", parameters);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST, localizePlainOrKey("action.addClassProperty.error.alreadyExists", parameters.toArray()));
        return true;
    } else {
        MetaClass mclass = xwiki.getMetaclass();
        PropertyMetaClass pmclass = (PropertyMetaClass) mclass.get(propType);
        if (pmclass != null) {
            PropertyClass pclass = (PropertyClass) pmclass.newObject(context);
            pclass.setObject(bclass);
            pclass.setName(propName);
            pclass.setPrettyName(propName);
            bclass.put(propName, pclass);
            doc.setAuthorReference(context.getUserReference());
            if (doc.isNew()) {
                doc.setCreatorReference(context.getUserReference());
            }
            doc.setMetaDataDirty(true);
            xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addClassProperty"), true, context);
        }
    }
    // forward to edit
    String redirect = Utils.getRedirect("edit", "editor=class", context);
    sendRedirect(response, redirect);
    return false;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass) MetaClass(com.xpn.xwiki.objects.meta.MetaClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.XWiki) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass)

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