Search in sources :

Example 1 with AttachmentDiff

use of com.xpn.xwiki.doc.AttachmentDiff in project xwiki-platform by xwiki.

the class WatchListEvent method getHTMLDiff.

/**
 * @return The diff, formatted in HTML, to display to the user when a document has been updated, or null if an error
 *         occurred while computing the diff
 */
public String getHTMLDiff() {
    if (htmlDiff == null) {
        try {
            DiffPluginApi diff = (DiffPluginApi) context.getWiki().getPluginApi("diff", context);
            StringBuffer result = new StringBuffer();
            XWikiDocument d2 = context.getWiki().getDocument(getPrefixedFullName(), context);
            if (getType().equals(WatchListEventType.CREATE)) {
                d2 = context.getWiki().getDocument(d2, INITIAL_DOCUMENT_VERSION, context);
            }
            XWikiDocument d1 = context.getWiki().getDocument(d2, getPreviousVersion(), context);
            List<AttachmentDiff> attachDiffs = d2.getAttachmentDiff(d1, d2, context);
            List<List<ObjectDiff>> objectDiffs = d2.getObjectDiff(d1, d2, context);
            List<List<ObjectDiff>> classDiffs = d2.getClassDiff(d1, d2, context);
            List<MetaDataDiff> metaDiffs = d2.getMetaDataDiff(d1, d2, context);
            if (!d1.getContent().equals(d2.getContent())) {
                Div contentDiv = createDiffDiv("contentDiff");
                String contentDiff = diff.getDifferencesAsHTML(d1.getContent(), d2.getContent(), false);
                contentDiv.addElement(contentDiff);
                result.append(contentDiv);
            }
            for (AttachmentDiff aDiff : attachDiffs) {
                Div attachmentDiv = createDiffDiv("attachmentDiff");
                attachmentDiv.addElement(HTML_IMG_ATTACHMENT_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX);
                attachmentDiv.addElement(aDiff.toString());
                result.append(attachmentDiv);
            }
            result.append(getObjectsHTMLDiff(objectDiffs, false, getFullName(), diff));
            result.append(getObjectsHTMLDiff(classDiffs, true, getFullName(), diff));
            for (MetaDataDiff mDiff : metaDiffs) {
                Div metaDiv = createDiffDiv("metaDiff");
                metaDiv.addElement(HTML_IMG_METADATA_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX);
                metaDiv.addElement(mDiff.toString());
                result.append(metaDiv);
            }
            htmlDiff = result.toString();
        } catch (XWikiException e) {
            // Catch the exception to be sure we won't send emails containing stacktraces to users.
            e.printStackTrace();
        }
    }
    return htmlDiff;
}
Also used : MetaDataDiff(com.xpn.xwiki.doc.MetaDataDiff) Div(org.apache.ecs.html.Div) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DiffPluginApi(com.xpn.xwiki.plugin.diff.DiffPluginApi) ArrayList(java.util.ArrayList) List(java.util.List) XWikiException(com.xpn.xwiki.XWikiException) AttachmentDiff(com.xpn.xwiki.doc.AttachmentDiff)

Example 2 with AttachmentDiff

use of com.xpn.xwiki.doc.AttachmentDiff in project xwiki-platform by xwiki.

the class AttachmentEventGeneratorListener 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) {
    ObservationManager observation = Utils.getComponent(ObservationManager.class);
    String reference = this.defaultEntityReferenceSerializer.serialize(doc.getDocumentReference());
    for (AttachmentDiff diff : doc.getAttachmentDiff(originalDoc, doc, context)) {
        if (StringUtils.isEmpty(diff.getOrigVersion())) {
            observation.notify(new AttachmentAddedEvent(reference, diff.getFileName()), doc, context);
        } else if (StringUtils.isEmpty(diff.getNewVersion())) {
            observation.notify(new AttachmentDeletedEvent(reference, diff.getFileName()), doc, context);
        } else {
            observation.notify(new AttachmentUpdatedEvent(reference, diff.getFileName()), doc, context);
        }
    }
}
Also used : ObservationManager(org.xwiki.observation.ObservationManager) AttachmentDiff(com.xpn.xwiki.doc.AttachmentDiff)

Example 3 with AttachmentDiff

use of com.xpn.xwiki.doc.AttachmentDiff in project xwiki-platform by xwiki.

the class DefaultWatchListEventHTMLDiffExtractor method getHTMLDiff.

@Override
public String getHTMLDiff(WatchListEvent event) throws WatchListException {
    XWikiContext context = contextProvider.get();
    StringBuffer result = new StringBuffer();
    try {
        DiffPluginApi diff = (DiffPluginApi) context.getWiki().getPluginApi("diff", context);
        XWikiDocument d2 = context.getWiki().getDocument(event.getDocumentReference(), context);
        if (event.getType().equals(WatchListEventType.CREATE)) {
            d2 = context.getWiki().getDocument(d2, INITIAL_DOCUMENT_VERSION, context);
        }
        XWikiDocument d1 = context.getWiki().getDocument(d2, event.getPreviousVersion(), context);
        List<AttachmentDiff> attachDiffs = d2.getAttachmentDiff(d1, d2, context);
        List<List<ObjectDiff>> objectDiffs = d2.getObjectDiff(d1, d2, context);
        List<List<ObjectDiff>> classDiffs = d2.getClassDiff(d1, d2, context);
        List<MetaDataDiff> metaDiffs = d2.getMetaDataDiff(d1, d2, context);
        if (!d1.getContent().equals(d2.getContent())) {
            Div contentDiv = createDiffDiv("contentDiff");
            String contentDiff = diff.getDifferencesAsHTML(d1.getContent(), d2.getContent(), false);
            contentDiv.addElement(contentDiff);
            result.append(contentDiv);
        }
        for (AttachmentDiff aDiff : attachDiffs) {
            Div attachmentDiv = createDiffDiv("attachmentDiff");
            attachmentDiv.addElement(HTML_IMG_ATTACHMENT_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX);
            attachmentDiv.addElement(aDiff.toString());
            result.append(attachmentDiv);
        }
        result.append(getObjectsHTMLDiff(objectDiffs, false, event.getFullName(), diff));
        result.append(getObjectsHTMLDiff(classDiffs, true, event.getFullName(), diff));
        for (MetaDataDiff mDiff : metaDiffs) {
            Div metaDiv = createDiffDiv("metaDiff");
            metaDiv.addElement(HTML_IMG_METADATA_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX);
            metaDiv.addElement(mDiff.toString());
            result.append(metaDiv);
        }
        return result.toString();
    } catch (Exception e) {
        throw new WatchListException(String.format("Failed to compute HTML diff for event type [%s] on [%s]", event.getType(), event.getPrefixedFullName()), e);
    }
}
Also used : MetaDataDiff(com.xpn.xwiki.doc.MetaDataDiff) DiffPluginApi(com.xpn.xwiki.plugin.diff.DiffPluginApi) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) Div(org.apache.ecs.html.Div) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) List(java.util.List) AttachmentDiff(com.xpn.xwiki.doc.AttachmentDiff)

Aggregations

AttachmentDiff (com.xpn.xwiki.doc.AttachmentDiff)3 XWikiException (com.xpn.xwiki.XWikiException)2 MetaDataDiff (com.xpn.xwiki.doc.MetaDataDiff)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 DiffPluginApi (com.xpn.xwiki.plugin.diff.DiffPluginApi)2 List (java.util.List)2 Div (org.apache.ecs.html.Div)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 ArrayList (java.util.ArrayList)1 ObservationManager (org.xwiki.observation.ObservationManager)1 WatchListException (org.xwiki.watchlist.internal.api.WatchListException)1