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