Search in sources :

Example 1 with MetaClass

use of com.xpn.xwiki.objects.meta.MetaClass 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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass) MetaClass(com.xpn.xwiki.objects.meta.MetaClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.XWiki) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass)

Example 2 with MetaClass

use of com.xpn.xwiki.objects.meta.MetaClass in project xwiki-platform by xwiki.

the class BaseClass method setName.

/**
 * {@inheritDoc}
 * <p>
 * Note: BaseElement#setName() does not support setting reference anymore since 2.4M2. This was broken and has been
 * replaced by this overridden method. See XWIKI-5285
 * </p>
 *
 * @deprecated since 2.2M2 use {@link #setDocumentReference(org.xwiki.model.reference.DocumentReference)}
 */
@Deprecated
@Override
public void setName(String name) {
    if (this instanceof MetaClass || this instanceof PropertyMetaClass) {
        super.setName(name);
    } else {
        DocumentReference reference = getDocumentReference();
        if (reference != null) {
            EntityReference relativeReference = getRelativeEntityReferenceResolver().resolve(name, EntityType.DOCUMENT);
            reference = new DocumentReference(relativeReference.extractReference(EntityType.DOCUMENT).getName(), new SpaceReference(relativeReference.extractReference(EntityType.SPACE).getName(), reference.getParent().getParent()));
        } else {
            reference = getCurrentMixedDocumentReferenceResolver().resolve(name);
        }
        setDocumentReference(reference);
    }
    setDirty(true);
}
Also used : MetaClass(com.xpn.xwiki.objects.meta.MetaClass) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass) SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass)

Example 3 with MetaClass

use of com.xpn.xwiki.objects.meta.MetaClass in project xwiki-platform by xwiki.

the class PropertyClass method getxWikiClass.

public BaseClass getxWikiClass() {
    if (this.pMetaClass == null) {
        MetaClass metaClass = MetaClass.getMetaClass();
        this.pMetaClass = (PropertyMetaClass) metaClass.get(getClassType());
    }
    return this.pMetaClass;
}
Also used : MetaClass(com.xpn.xwiki.objects.meta.MetaClass) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass)

Aggregations

MetaClass (com.xpn.xwiki.objects.meta.MetaClass)3 PropertyMetaClass (com.xpn.xwiki.objects.meta.PropertyMetaClass)3 XWiki (com.xpn.xwiki.XWiki)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)1 ArrayList (java.util.ArrayList)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReference (org.xwiki.model.reference.EntityReference)1 SpaceReference (org.xwiki.model.reference.SpaceReference)1