Search in sources :

Example 16 with PropertyClass

use of com.xpn.xwiki.objects.classes.PropertyClass 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 17 with PropertyClass

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

the class BaseCollection method getDiff.

public List<ObjectDiff> getDiff(Object oldObject, XWikiContext context) {
    ArrayList<ObjectDiff> difflist = new ArrayList<ObjectDiff>();
    BaseCollection oldCollection = (BaseCollection) oldObject;
    // Iterate over the new properties first, to handle changed and added objects
    for (Object key : this.getFields().keySet()) {
        String propertyName = (String) key;
        BaseProperty newProperty = (BaseProperty) this.getFields().get(propertyName);
        BaseProperty oldProperty = (BaseProperty) oldCollection.getFields().get(propertyName);
        BaseClass bclass = getXClass(context);
        PropertyClass pclass = (PropertyClass) ((bclass == null) ? null : bclass.getField(propertyName));
        String propertyType = (pclass == null) ? "" : pclass.getClassType();
        if (oldProperty == null) {
            // The property exist in the new object, but not in the old one
            if ((newProperty != null) && (!newProperty.toText().equals(""))) {
                if (pclass != null) {
                    String newPropertyValue = (newProperty.getValue() instanceof String) ? newProperty.toText() : pclass.displayView(propertyName, this, context);
                    difflist.add(new ObjectDiff(getXClassReference(), getNumber(), "", ObjectDiff.ACTION_PROPERTYADDED, propertyName, propertyType, "", newPropertyValue));
                }
            }
        } else if (!oldProperty.toText().equals(((newProperty == null) ? "" : newProperty.toText()))) {
            // The property exists in both objects and is different
            if (pclass != null) {
                // Put the values as they would be displayed in the interface
                String newPropertyValue = (newProperty.getValue() instanceof String) ? newProperty.toText() : pclass.displayView(propertyName, this, context);
                String oldPropertyValue = (oldProperty.getValue() instanceof String) ? oldProperty.toText() : pclass.displayView(propertyName, oldCollection, context);
                difflist.add(new ObjectDiff(getXClassReference(), getNumber(), "", ObjectDiff.ACTION_PROPERTYCHANGED, propertyName, propertyType, oldPropertyValue, newPropertyValue));
            } else {
                // Cannot get property definition, so use the plain value
                difflist.add(new ObjectDiff(getXClassReference(), getNumber(), "", ObjectDiff.ACTION_PROPERTYCHANGED, propertyName, propertyType, oldProperty.toText(), newProperty.toText()));
            }
        }
    }
    // Iterate over the old properties, in case there are some removed properties
    for (Object key : oldCollection.getFields().keySet()) {
        String propertyName = (String) key;
        BaseProperty newProperty = (BaseProperty) this.getFields().get(propertyName);
        BaseProperty oldProperty = (BaseProperty) oldCollection.getFields().get(propertyName);
        BaseClass bclass = getXClass(context);
        PropertyClass pclass = (PropertyClass) ((bclass == null) ? null : bclass.getField(propertyName));
        String propertyType = (pclass == null) ? "" : pclass.getClassType();
        if (newProperty == null) {
            // The property exists in the old object, but not in the new one
            if ((oldProperty != null) && (!oldProperty.toText().equals(""))) {
                if (pclass != null) {
                    // Put the values as they would be displayed in the interface
                    String oldPropertyValue = (oldProperty.getValue() instanceof String) ? oldProperty.toText() : pclass.displayView(propertyName, oldCollection, context);
                    difflist.add(new ObjectDiff(oldCollection.getXClassReference(), oldCollection.getNumber(), "", ObjectDiff.ACTION_PROPERTYREMOVED, propertyName, propertyType, oldPropertyValue, ""));
                } else {
                    // Cannot get property definition, so use the plain value
                    difflist.add(new ObjectDiff(oldCollection.getXClassReference(), oldCollection.getNumber(), "", ObjectDiff.ACTION_PROPERTYREMOVED, propertyName, propertyType, oldProperty.toText(), ""));
                }
            }
        }
    }
    return difflist;
}
Also used : ArrayList(java.util.ArrayList) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 18 with PropertyClass

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

the class BaseObject method set.

public void set(String fieldname, java.lang.Object value, XWikiContext context) {
    BaseClass bclass = getXClass(context);
    PropertyClass pclass = (PropertyClass) bclass.get(fieldname);
    BaseProperty prop = (BaseProperty) safeget(fieldname);
    if ((value instanceof String) && (pclass != null)) {
        prop = pclass.fromString((String) value);
    } else {
        if ((prop == null) && (pclass != null)) {
            prop = pclass.newProperty();
        }
        if (prop != null) {
            prop.setValue(value);
        }
    }
    if (prop != null) {
        prop.setOwnerDocument(getOwnerDocument());
        safeput(fieldname, prop);
    }
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 19 with PropertyClass

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

the class PropertyClassOutputFilterStream method onWikiClassPropertyField.

@Override
public void onWikiClassPropertyField(String name, String value, FilterEventParameters parameters) throws FilterException {
    if (this.entity != null) {
        PropertyClass propertyClass;
        try {
            propertyClass = (PropertyClass) this.currentClassPropertyMeta.get(name);
        } catch (XWikiException e) {
            throw new FilterException(String.format("Failed to get definition of field [%s] for property type [%s]", name, this.entity.getClassType()), e);
        }
        // Make sure the property is known
        if (propertyClass == null) {
            this.logger.warn("Unknown property meta class field [{}] for property type [{}]", name, this.entity.getClassType());
            return;
        }
        BaseProperty<?> field = propertyClass.fromString(value);
        this.entity.safeput(name, field);
    }
}
Also used : FilterException(org.xwiki.filter.FilterException) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) XWikiException(com.xpn.xwiki.XWikiException)

Example 20 with PropertyClass

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

the class DocumentUnifiedDiffBuilder method addClassPropertyDiff.

private void addClassPropertyDiff(PropertyClass previousProperty, PropertyClass nextProperty, DocumentUnifiedDiff documentDiff) {
    ClassPropertyReference previousReference = getClassPropertyVersionReference(previousProperty, documentDiff.getPreviousReference());
    ClassPropertyReference nextReference = getClassPropertyVersionReference(nextProperty, documentDiff.getNextReference());
    EntityUnifiedDiff<ClassPropertyReference> classPropertyDiff = new EntityUnifiedDiff<>(previousReference, nextReference);
    // Catch a property type change.
    maybeAddDiff(classPropertyDiff, "type", previousProperty == null ? null : previousProperty.getClassType(), nextProperty == null ? null : nextProperty.getClassType());
    addObjectDiff(previousProperty == null ? new PropertyClass() : previousProperty, nextProperty == null ? new PropertyClass() : nextProperty, classPropertyDiff);
    // The property name is already specified by the previous / next reference.
    classPropertyDiff.remove("name");
    // This meta property is not used (there's no UI to change it).
    classPropertyDiff.remove("unmodifiable");
    if (classPropertyDiff.size() > 0) {
        documentDiff.getClassPropertyDiffs().add(classPropertyDiff);
    }
}
Also used : EntityUnifiedDiff(org.xwiki.extension.xar.job.diff.EntityUnifiedDiff) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) 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