Search in sources :

Example 6 with ObjectDiff

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

the class XObjectEventGeneratorListener method onDocumentUpdatedEvent.

/**
 * @param originalDoc the previous version of the document
 * @param doc the new version of the document
 * @param context the XWiki context
 */
private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context) {
    for (List<ObjectDiff> objectChanges : doc.getObjectDiff(originalDoc, doc, context)) {
        boolean modified = false;
        for (ObjectDiff diff : objectChanges) {
            BaseObject xobject = doc.getXObject(diff.getXClassReference(), diff.getNumber());
            BaseObject xobjectOriginal = originalDoc.getXObject(diff.getXClassReference(), diff.getNumber());
            if (ObjectDiff.ACTION_OBJECTREMOVED.equals(diff.getAction())) {
                this.observation.notify(new XObjectDeletedEvent(xobjectOriginal.getReference()), doc, context);
            } else {
                if (ObjectDiff.ACTION_OBJECTADDED.equals(diff.getAction())) {
                    this.observation.notify(new XObjectAddedEvent(xobject.getReference()), doc, context);
                } else {
                    if (!modified && xobject != null && xobjectOriginal != null) {
                        this.observation.notify(new XObjectUpdatedEvent(xobject.getReference()), doc, context);
                        modified = true;
                    }
                    onObjectPropertyModified(doc, diff, context);
                }
            }
        }
    }
}
Also used : ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 7 with ObjectDiff

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

the class XClassPropertyEventGeneratorListener method onDocumentUpdatedEvent.

/**
 * @param originalDoc the previous version of the document
 * @param doc the new version of the document
 * @param context the XWiki context
 */
private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context) {
    BaseClass baseClass = doc.getXClass();
    BaseClass baseClassOriginal = originalDoc.getXClass();
    for (List<ObjectDiff> objectChanges : doc.getClassDiff(originalDoc, doc, context)) {
        for (ObjectDiff diff : objectChanges) {
            PropertyInterface property = baseClass.getField(diff.getPropName());
            PropertyInterface propertyOriginal = baseClassOriginal.getField(diff.getPropName());
            if (ObjectDiff.ACTION_PROPERTYREMOVED.equals(diff.getAction())) {
                this.observation.notify(new XClassPropertyDeletedEvent(propertyOriginal.getReference()), doc, context);
            } else if (ObjectDiff.ACTION_PROPERTYADDED.equals(diff.getAction())) {
                this.observation.notify(new XClassPropertyAddedEvent(property.getReference()), doc, context);
            } else if (ObjectDiff.ACTION_PROPERTYCHANGED.equals(diff.getAction())) {
                this.observation.notify(new XClassPropertyUpdatedEvent(property.getReference()), doc, context);
            }
        }
    }
}
Also used : ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) PropertyInterface(com.xpn.xwiki.objects.PropertyInterface) BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Example 8 with ObjectDiff

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

the class XWikiDocument method getObjectDiff.

/**
 * Return the object differences between two document versions. There is no hard requirement on the order of the two
 * versions, but the results are semantically correct only if the two versions are given in the right order.
 *
 * @param fromDoc The old ('before') version of the document.
 * @param toDoc The new ('after') version of the document.
 * @param context The {@link com.xpn.xwiki.XWikiContext context}.
 * @return The object differences. The returned list's elements are other lists, one for each changed object. The
 *         inner lists contain {@link ObjectDiff} elements, one object for each changed property of the object.
 *         Additionally, if the object was added or removed, then the first entry in the list will be an
 *         "object-added" or "object-removed" marker.
 */
public List<List<ObjectDiff>> getObjectDiff(XWikiDocument fromDoc, XWikiDocument toDoc, XWikiContext context) {
    List<List<ObjectDiff>> difflist = new ArrayList<List<ObjectDiff>>();
    // First, iterate over the old objects.
    for (List<BaseObject> objects : fromDoc.getXObjects().values()) {
        for (BaseObject originalObj : objects) {
            // storage.
            if (originalObj != null) {
                BaseObject newObj = toDoc.getXObject(originalObj.getXClassReference(), originalObj.getNumber());
                List<ObjectDiff> dlist;
                if (newObj == null) {
                    // The object was deleted.
                    dlist = new BaseObject().getDiff(originalObj, context);
                    ObjectDiff deleteMarker = new ObjectDiff(originalObj.getXClassReference(), originalObj.getNumber(), originalObj.getGuid(), ObjectDiff.ACTION_OBJECTREMOVED, "", "", "", "");
                    dlist.add(0, deleteMarker);
                } else {
                    // The object exists in both versions, but might have been changed.
                    dlist = newObj.getDiff(originalObj, context);
                }
                if (!dlist.isEmpty()) {
                    difflist.add(dlist);
                }
            }
        }
    }
    // Second, iterate over the objects which are only in the new version.
    for (List<BaseObject> objects : toDoc.getXObjects().values()) {
        for (BaseObject newObj : objects) {
            // storage.
            if (newObj != null) {
                BaseObject originalObj = fromDoc.getXObject(newObj.getXClassReference(), newObj.getNumber());
                if (originalObj == null) {
                    // TODO: Refactor this so that getDiff() accepts null Object as input.
                    // Only consider added objects, the other case was treated above.
                    originalObj = new BaseObject();
                    originalObj.setXClassReference(newObj.getRelativeXClassReference());
                    originalObj.setNumber(newObj.getNumber());
                    originalObj.setGuid(newObj.getGuid());
                    List<ObjectDiff> dlist = newObj.getDiff(originalObj, context);
                    ObjectDiff addMarker = new ObjectDiff(newObj.getXClassReference(), newObj.getNumber(), newObj.getGuid(), ObjectDiff.ACTION_OBJECTADDED, "", "", "", "");
                    dlist.add(0, addMarker);
                    if (!dlist.isEmpty()) {
                        difflist.add(dlist);
                    }
                }
            }
        }
    }
    return difflist;
}
Also used : ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) ArrayList(java.util.ArrayList) XWikiAttachmentList(com.xpn.xwiki.internal.doc.XWikiAttachmentList) ArrayList(java.util.ArrayList) List(java.util.List) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

ObjectDiff (com.xpn.xwiki.objects.ObjectDiff)8 ArrayList (java.util.ArrayList)4 XWikiException (com.xpn.xwiki.XWikiException)3 XWikiAttachmentList (com.xpn.xwiki.internal.doc.XWikiAttachmentList)3 BaseObject (com.xpn.xwiki.objects.BaseObject)3 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)3 List (java.util.List)3 PropertyInterface (com.xpn.xwiki.objects.PropertyInterface)2 Div (org.apache.ecs.html.Div)2 Span (org.apache.ecs.html.Span)2 MergeResult (com.xpn.xwiki.doc.merge.MergeResult)1 BaseCollection (com.xpn.xwiki.objects.BaseCollection)1 Collection (java.util.Collection)1