Search in sources :

Example 81 with BaseClass

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

the class XClassPropertyEventGeneratorListener method onDocumentUpdatedEvent.

/**
 * @param originalDoc the previous version of the document
 * @param doc the new version of the document
 * @param context the XWiki context
 */
private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context) {
    BaseClass baseClass = doc.getXClass();
    BaseClass baseClassOriginal = originalDoc.getXClass();
    for (List<ObjectDiff> objectChanges : doc.getClassDiff(originalDoc, doc, context)) {
        for (ObjectDiff diff : objectChanges) {
            PropertyInterface property = baseClass.getField(diff.getPropName());
            PropertyInterface propertyOriginal = baseClassOriginal.getField(diff.getPropName());
            if (ObjectDiff.ACTION_PROPERTYREMOVED.equals(diff.getAction())) {
                this.observation.notify(new XClassPropertyDeletedEvent(propertyOriginal.getReference()), doc, context);
            } else if (ObjectDiff.ACTION_PROPERTYADDED.equals(diff.getAction())) {
                this.observation.notify(new XClassPropertyAddedEvent(property.getReference()), doc, context);
            } else if (ObjectDiff.ACTION_PROPERTYCHANGED.equals(diff.getAction())) {
                this.observation.notify(new XClassPropertyUpdatedEvent(property.getReference()), doc, context);
            }
        }
    }
}
Also used : ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) PropertyInterface(com.xpn.xwiki.objects.PropertyInterface) BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Example 82 with BaseClass

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

the class CommentAddAction 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();
    ObjectAddForm oform = (ObjectAddForm) context.getForm();
    // Make sure this class exists
    BaseClass baseclass = xwiki.getCommentsClass(context);
    if (doc.isNew()) {
        return true;
    } else if (context.getUser().equals(XWikiRightService.GUEST_USER_FULLNAME) && !checkCaptcha(context)) {
        getCurrentScriptContext().setAttribute("captchaAnswerWrong", Boolean.TRUE, ScriptContext.ENGINE_SCOPE);
    } else {
        // className = XWiki.XWikiComments
        String className = baseclass.getName();
        // Create a new comment object and mark the document as dirty.
        BaseObject object = doc.newObject(className, context);
        // TODO The map should be pre-filled with empty strings for all class properties, just like in
        // ObjectAddAction, so that properties missing from the request are still added to the database.
        baseclass.fromMap(oform.getObject(className), object);
        // Comment author checks
        if (XWikiRightService.GUEST_USER_FULLNAME.equals(context.getUser())) {
            // Guests should not be allowed to enter names that look like real XWiki user names.
            String author = ((BaseProperty) object.get(AUTHOR_PROPERTY_NAME)).getValue() + "";
            author = StringUtils.remove(author, ':');
            while (author.startsWith(USER_SPACE_PREFIX)) {
                author = StringUtils.removeStart(author, USER_SPACE_PREFIX);
            }
            // We need to make sure the author will fit in a String property, this is mostly a protection against
            // spammers who try to put large texts in this field
            author = author.substring(0, Math.min(author.length(), 255));
            object.set(AUTHOR_PROPERTY_NAME, author, context);
        } else {
            // A registered user must always post with his name.
            object.set(AUTHOR_PROPERTY_NAME, context.getUser(), context);
        }
        doc.setAuthorReference(context.getUserReference());
        // Save the new comment.
        xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addComment"), true, context);
    }
    // If xpage is specified then allow the specified template to be parsed.
    if (context.getRequest().get("xpage") != null) {
        return true;
    }
    // forward to edit
    String redirect = Utils.getRedirect("edit", context);
    sendRedirect(response, redirect);
    return false;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWiki(com.xpn.xwiki.XWiki) BaseProperty(com.xpn.xwiki.objects.BaseProperty) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 83 with BaseClass

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

the class AbstractListClassPropertyValuesProviderTest method configure.

public void configure() throws Exception {
    Provider<XWikiContext> xcontextProvider = getMocker().getInstance(XWikiContext.TYPE_PROVIDER);
    XWiki xwiki = mock(XWiki.class);
    BaseClass xclass = mock(BaseClass.class);
    DocumentReference authorReference = new DocumentReference("wiki", "Users", "Alice");
    when(xcontextProvider.get()).thenReturn(this.xcontext);
    when(this.xcontext.getWiki()).thenReturn(xwiki);
    when(this.classDocument.getXClass()).thenReturn(xclass);
    when(this.classDocument.getDocumentReference()).thenReturn(this.classReference);
    when(this.classDocument.getAuthorReference()).thenReturn(authorReference);
    QueryParameter queryParameter = mock(QueryParameter.class);
    when(this.allowedValuesQuery.bindValue("text")).thenReturn(queryParameter);
    when(this.usedValuesQuery.bindValue("text")).thenReturn(queryParameter);
    when(queryParameter.anyChars()).thenReturn(queryParameter);
    when(queryParameter.literal("foo")).thenReturn(queryParameter);
}
Also used : QueryParameter(org.xwiki.query.QueryParameter) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 84 with BaseClass

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

the class DefaultClassPropertyValuesProviderTest method configure.

@Before
public void configure() throws Exception {
    this.xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
    XWikiContext xcontext = mock(XWikiContext.class);
    XWiki xwiki = mock(XWiki.class);
    XWikiDocument classDocument = mock(XWikiDocument.class);
    BaseClass xclass = mock(BaseClass.class);
    PropertyClass propertyClass = mock(PropertyClass.class);
    when(this.xcontextProvider.get()).thenReturn(xcontext);
    when(xcontext.getWiki()).thenReturn(xwiki);
    when(xwiki.getDocument((EntityReference) this.classReference, xcontext)).thenReturn(classDocument);
    when(classDocument.getXClass()).thenReturn(xclass);
    when(xclass.get("category")).thenReturn(propertyClass);
    when(propertyClass.getClassType()).thenReturn("DBList");
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) Before(org.junit.Before)

Example 85 with BaseClass

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

the class DefaultWikiComponentBuilderEventListener method installOrUpdateComponentInterfaceXClass.

/**
 * Verify that the {@link #INTERFACE_CLASS} exists and is up-to-date (act if not).
 *
 * @throws com.xpn.xwiki.XWikiException on failure
 */
private void installOrUpdateComponentInterfaceXClass() throws XWikiException {
    XWikiContext xcontext = getXWikiContext();
    XWikiDocument doc = xcontext.getWiki().getDocument(INTERFACE_CLASS_REFERENCE, xcontext);
    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(doc.getDocumentReference());
    boolean needsUpdate = false;
    needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Implements Interface XWiki Class");
    needsUpdate |= bclass.addTextField(INTERFACE_NAME_FIELD, "Interface Qualified Name", 30);
    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