Search in sources :

Example 36 with BaseClass

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) HashMap(java.util.HashMap) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWiki(com.xpn.xwiki.XWiki) Collection(java.util.Collection) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 37 with BaseClass

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);
        }
    }
}
Also used : ListClass(com.xpn.xwiki.objects.classes.ListClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWiki(com.xpn.xwiki.XWiki) QueryBuilder(org.xwiki.query.QueryBuilder) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference)

Example 38 with BaseClass

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);
        }
    }
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 39 with BaseClass

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;
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) DBStringListProperty(com.xpn.xwiki.objects.DBStringListProperty) BaseProperty(com.xpn.xwiki.objects.BaseProperty) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) XWikiException(com.xpn.xwiki.XWikiException)

Example 40 with BaseClass

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

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