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);
}
}
}
}
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;
}
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);
}
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");
}
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);
}
}
Aggregations