Search in sources :

Example 1 with PropertyClass

use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.

the class XClassMigratorListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XClassPropertyUpdatedEvent propertyEvent = (XClassPropertyUpdatedEvent) event;
    XWikiDocument newDocument = (XWikiDocument) source;
    XWikiDocument previousDocument = newDocument.getOriginalDocument();
    PropertyClass newPropertyClass = (PropertyClass) newDocument.getXClass().getField(propertyEvent.getReference().getName());
    PropertyClass previousPropertyClass = (PropertyClass) previousDocument.getXClass().getField(propertyEvent.getReference().getName());
    if (newPropertyClass != null && previousPropertyClass != null) {
        BaseProperty newProperty = newPropertyClass.newProperty();
        BaseProperty previousProperty = previousPropertyClass.newProperty();
        // New and previous class property generate different kind of properties
        if (newProperty.getClass() != previousProperty.getClass()) {
            try {
                migrate(newPropertyClass);
            } catch (QueryException e) {
                this.logger.error("Failed to migrate XClass property [{}]", newPropertyClass.getReference(), e);
            }
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) XClassPropertyUpdatedEvent(com.xpn.xwiki.internal.event.XClassPropertyUpdatedEvent) BaseProperty(com.xpn.xwiki.objects.BaseProperty) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 2 with PropertyClass

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

Example 3 with PropertyClass

use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.

the class DocumentUnifiedDiffBuilder method addClassPropertyDiffs.

private void addClassPropertyDiffs(BaseClass previousClass, BaseClass nextClass, DocumentUnifiedDiff documentDiff) {
    // Check the properties that have been deleted or modified.
    for (String propertyName : previousClass.getPropertyList()) {
        PropertyClass previousProperty = (PropertyClass) previousClass.get(propertyName);
        PropertyClass nextProperty = (PropertyClass) nextClass.get(propertyName);
        addClassPropertyDiff(previousProperty, nextProperty, documentDiff);
    }
    // Check the properties that have been added.
    for (String propertyName : nextClass.getPropertyList()) {
        PropertyClass previousProperty = (PropertyClass) previousClass.get(propertyName);
        PropertyClass nextProperty = (PropertyClass) nextClass.get(propertyName);
        if (previousProperty == null) {
            addClassPropertyDiff(previousProperty, nextProperty, documentDiff);
        }
    }
}
Also used : PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 4 with PropertyClass

use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.

the class XWiki method getPropertyClassFromName.

public PropertyClass getPropertyClassFromName(String propPath, XWikiContext context) {
    int i1 = propPath.indexOf('_');
    if (i1 == -1) {
        return null;
    } else {
        String className = propPath.substring(0, i1);
        String propName = propPath.substring(i1 + 1);
        try {
            return (PropertyClass) getDocument(className, context).getXClass().get(propName);
        } catch (XWikiException e) {
            return null;
        }
    }
}
Also used : ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 5 with PropertyClass

use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.

the class XWikiStats method fromXML.

/**
 * Initialize statistics object from XML schema.
 *
 * @param oel the XML root node containing statistics datas.
 * @throws XWikiException error when parsing XML schema.
 */
public void fromXML(Element oel) throws XWikiException {
    Element cel = oel.element("class");
    BaseClass bclass = new BaseClass();
    if (cel != null) {
        bclass.fromXML(cel);
        setClassName(bclass.getName());
    }
    setName(oel.element(XMLNODE_NAME).getText());
    List<?> list = oel.elements(XMLNODE_PROPERTY);
    for (int i = 0; i < list.size(); i++) {
        Element pcel = (Element) ((Element) list.get(i)).elements().get(0);
        String name = pcel.getName();
        PropertyClass pclass = (PropertyClass) bclass.get(name);
        if (pclass != null) {
            BaseProperty property = pclass.newPropertyfromXML(pcel);
            property.setName(name);
            property.setObject(this);
            safeput(name, property);
        }
    }
}
Also used : Element(org.dom4j.Element) DOMElement(org.dom4j.dom.DOMElement) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseProperty(com.xpn.xwiki.objects.BaseProperty) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Aggregations

PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)28 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)16 XWikiException (com.xpn.xwiki.XWikiException)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 BaseObject (com.xpn.xwiki.objects.BaseObject)5 BaseProperty (com.xpn.xwiki.objects.BaseProperty)5 ToString (org.suigeneris.jrcs.util.ToString)5 XWiki (com.xpn.xwiki.XWiki)4 ArrayList (java.util.ArrayList)4 QueryException (org.xwiki.query.QueryException)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 IOException (java.io.IOException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 ExecutionContextException (org.xwiki.context.ExecutionContextException)3 MissingParserException (org.xwiki.rendering.parser.MissingParserException)3 ParseException (org.xwiki.rendering.parser.ParseException)3 TransformationException (org.xwiki.rendering.transformation.TransformationException)3 HashMap (java.util.HashMap)2