use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.
the class PropUpdateAction method propUpdate.
public boolean propUpdate(XWikiContext context) throws XWikiException {
XWiki xwiki = context.getWiki();
XWikiDocument doc = context.getDoc();
XWikiForm form = context.getForm();
// Prepare new class
BaseClass bclass = doc.getXClass();
BaseClass bclass2 = bclass.clone();
bclass2.setFields(new HashMap());
// Prepare a Map for field renames
Map<String, String> fieldsToRename = new HashMap<String, String>();
for (PropertyClass originalProperty : (Collection<PropertyClass>) bclass.getFieldList()) {
PropertyClass newProperty = originalProperty.clone();
String name = newProperty.getName();
Map<String, ?> map = ((EditForm) form).getObject(name);
newProperty.getXClass(context).fromMap(map, newProperty);
String newName = newProperty.getName();
if (!Util.isValidXMLElementName(newName)) {
context.put("message", "propertynamenotcorrect");
return true;
}
if (newName.indexOf(" ") != -1) {
newName = newName.replaceAll(" ", "");
newProperty.setName(newName);
}
bclass2.addField(newName, newProperty);
if (!newName.equals(name)) {
fieldsToRename.put(name, newName);
bclass2.addPropertyForRemoval(originalProperty);
}
}
doc.setXClass(bclass2);
doc.renameProperties(bclass.getName(), fieldsToRename);
doc.setMetaDataDirty(true);
if (doc.isNew()) {
doc.setCreator(context.getUser());
}
doc.setAuthor(context.getUser());
xwiki.saveDocument(doc, localizePlainOrKey("core.comment.updateClassProperty"), true, context);
// We need to load all documents that use this property and rename it
if (fieldsToRename.size() > 0) {
List<String> list = xwiki.getStore().searchDocumentsNames(", BaseObject as obj where obj.name=doc.fullName and obj.className='" + Utils.SQLFilter(bclass.getName()) + "' and doc.fullName <> '" + Utils.SQLFilter(bclass.getName()) + "'", context);
for (String docName : list) {
XWikiDocument doc2 = xwiki.getDocument(docName, context);
doc2.renameProperties(bclass.getName(), fieldsToRename);
xwiki.saveDocument(doc2, localizePlainOrKey("core.comment.updateClassPropertyName"), true, context);
}
}
return false;
}
use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.
the class AbstractListClassPropertyValuesProviderTest method addProperty.
protected void addProperty(String name, PropertyClass definition, boolean withQueryBuilders) throws Exception {
XWiki xwiki = this.xcontext.getWiki();
BaseClass xclass = this.classDocument.getXClass();
ClassPropertyReference propertyReference = new ClassPropertyReference(name, this.classReference);
when(xwiki.getDocument(propertyReference, this.xcontext)).thenReturn(this.classDocument);
when(xclass.get(name)).thenReturn(definition);
definition.setOwnerDocument(this.classDocument);
if (withQueryBuilders) {
DefaultParameterizedType allowedValuesQueryBuilderType = new DefaultParameterizedType(null, QueryBuilder.class, definition.getClass());
QueryBuilder allowedValuesQueryBuilder = getMocker().getInstance(allowedValuesQueryBuilderType);
when(allowedValuesQueryBuilder.build(definition)).thenReturn(this.allowedValuesQuery);
if (definition instanceof ListClass) {
DefaultParameterizedType usedValuesQueryBuilderType = new DefaultParameterizedType(null, QueryBuilder.class, ListClass.class);
this.usedValuesQueryBuilder = getMocker().getInstance(usedValuesQueryBuilderType, "usedValues");
when(this.usedValuesQueryBuilder.build((ListClass) definition)).thenReturn(this.usedValuesQuery);
}
}
}
use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.
the class AbstractSolrMetadataExtractor method setObjectContent.
/**
* Adds the properties of a given object to a Solr document.
*
* @param solrDocument the document where to add the properties
* @param object the object whose properties to add
* @param locale the locale of the indexed document; in case of translations, this will obviously be different than
* the original document's locale
*/
protected void setObjectContent(SolrInputDocument solrDocument, BaseObject object, Locale locale) {
if (object == null) {
// Yes, the platform can return null objects.
return;
}
BaseClass xClass = object.getXClass(this.xcontextProvider.get());
for (Object field : object.getFieldList()) {
@SuppressWarnings("unchecked") BaseProperty<EntityReference> property = (BaseProperty<EntityReference>) field;
// Avoid indexing empty properties.
if (property.getValue() != null) {
PropertyClass propertyClass = (PropertyClass) xClass.get(property.getName());
setPropertyValue(solrDocument, property, propertyClass, locale);
}
}
}
use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.
the class TagPlugin method createTagProperty.
/**
* Create and add the main tag property to the provided tag object. The new property corresponds to the definition
* in the tag class, but in case of an error, the default type is a relational-stored list.
*
* @param tagObject the target tag object
* @param context the current request context
* @return the created property
* @see #TAG_PROPERTY
*/
private BaseProperty createTagProperty(BaseObject tagObject, XWikiContext context) {
BaseProperty tagProperty;
try {
BaseClass tagClass = context.getWiki().getClass(TAG_CLASS, context);
PropertyClass tagPropertyDefinition = (PropertyClass) tagClass.getField(TAG_PROPERTY);
tagProperty = tagPropertyDefinition.newProperty();
} catch (XWikiException ex) {
LOGGER.warn("Failed to properly create tag property for the tag object, creating a default one");
tagProperty = new DBStringListProperty();
}
tagProperty.setName(TAG_PROPERTY);
tagProperty.setObject(tagObject);
tagObject.safeput(TAG_PROPERTY, tagProperty);
return tagProperty;
}
use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.
the class SyndEntryDocumentSourceTest method initArticleClass.
protected BaseClass initArticleClass() throws XWikiException {
XWikiDocument doc = getContext().getWiki().getDocument(ARTICLE_CLASS_NAME, getContext());
boolean needsUpdate = doc.isNew();
BaseClass bclass = doc.getXClass();
bclass.setName(ARTICLE_CLASS_NAME);
needsUpdate |= bclass.addTextField("title", "Title", 64);
needsUpdate |= bclass.addTextAreaField("content", "Content", 45, 4);
needsUpdate |= bclass.addTextField("category", "Category", 64);
if (needsUpdate) {
getContext().getWiki().saveDocument(doc, getContext());
}
return bclass;
}
Aggregations