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