Search in sources :

Example 66 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class DefaultWikiComponentBuilder method getDocumentReferences.

@Override
public List<DocumentReference> getDocumentReferences() {
    List<DocumentReference> results = new ArrayList<DocumentReference>();
    // Note that the query is made to work with Oracle which treats empty strings as null.
    String query = ", BaseObject as obj, StringProperty as role where obj.className=? and obj.name=doc.fullName " + "and role.id.id=obj.id and role.id.name=? " + "and  (role.value <> '' or (role.value is not null and '' is null))";
    List<String> parameters = new ArrayList<String>();
    parameters.add(COMPONENT_CLASS);
    parameters.add(COMPONENT_ROLE_TYPE_FIELD);
    try {
        XWikiContext xcontext = xcontextProvider.get();
        results.addAll(xcontext.getWiki().getStore().searchDocumentReferences(query, parameters, xcontext));
    } catch (XWikiException e) {
        this.logger.error("Failed to search for existing wiki components [{}]", e.getMessage());
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 67 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class Packager method importDocumentToWiki.

private XarEntryMergeResult importDocumentToWiki(String comment, WikiReference wikiReference, InputStream inputStream, PackageConfiguration configuration) throws XWikiException, XarException, IOException {
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument nextDocument;
    try {
        nextDocument = getXWikiDocument(inputStream, wikiReference);
    } catch (Exception e) {
        this.logger.error("Failed to parse document", e);
        return null;
    }
    DocumentReference reference = nextDocument.getDocumentReferenceWithLocale();
    XWikiDocument currentDocument = xcontext.getWiki().getDocument(reference, xcontext);
    currentDocument.loadAttachmentsContentSafe(xcontext);
    XWikiDocument previousDocument;
    XarExtensionPlan xarExtensionPlan = configuration.getXarExtensionPlan();
    if (xarExtensionPlan != null) {
        previousDocument = xarExtensionPlan.getPreviousXWikiDocument(reference, this);
    } else {
        previousDocument = null;
    }
    if (configuration.isVerbose()) {
        this.logger.info(LOG_INSTALLDOCUMENT_BEGIN, "Installing document [{}]", nextDocument.getDocumentReferenceWithLocale());
    }
    try {
        XarEntryMergeResult entityMergeResult = this.importer.saveDocument(comment, previousDocument, currentDocument, nextDocument, configuration);
        if (configuration.isVerbose()) {
            this.logger.info(LOG_INSTALLDOCUMENT_SUCCESS_END, "Done installing document [{}]", nextDocument.getDocumentReferenceWithLocale());
        }
        return entityMergeResult;
    } catch (Exception e) {
        if (configuration.isVerbose()) {
            this.logger.error(LOG_INSTALLDOCUMENT_FAILURE_END, "Failed to install document [{}]", nextDocument.getDocumentReferenceWithLocale(), e);
        }
    }
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XarExtensionPlan(org.xwiki.extension.xar.internal.handler.XarExtensionPlan) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) FilterException(org.xwiki.filter.FilterException) XarException(org.xwiki.xar.XarException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) IOException(java.io.IOException) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 68 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class DiffXarJob method diff.

private void diff(XWikiDocument document, ExtensionId extensionId) {
    if (getRequest().isVerbose()) {
        this.logger.info("Computing differences for document [{}]", document.getDocumentReferenceWithLocale());
    }
    // Use the extension id as the document version.
    XWikiDocument previousDocument = document.duplicate(new DocumentVersionReference(document.getDocumentReference(), extensionId));
    XWikiContext xcontext = this.xcontextProvider.get();
    try {
        XWikiDocument nextDocument = xcontext.getWiki().getDocument(document.getDocumentReferenceWithLocale(), xcontext);
        if (nextDocument.isNew()) {
            nextDocument = null;
        }
        maybeAddDocumentDiff(this.documentDiffBuilder.diff(previousDocument, nextDocument));
    } catch (XWikiException e) {
        this.logger.error("Failed to get document [{}] from the database.", document.getDocumentReference(), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentVersionReference(org.xwiki.extension.xar.job.diff.DocumentVersionReference) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 69 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class WatchListEvent method getObjectsHTMLDiff.

/**
 * @param objectDiffs List of object diff
 * @param isXWikiClass is the diff to compute the diff for a xwiki class, the other possibility being a plain xwiki
 *            object
 * @param documentFullName full name of the document the diff is computed for
 * @param diff the diff plugin API
 * @return The HTML diff
 */
private String getObjectsHTMLDiff(List<List<ObjectDiff>> objectDiffs, boolean isXWikiClass, String documentFullName, DiffPluginApi diff) {
    StringBuffer result = new StringBuffer();
    String propSeparator = ": ";
    String prefix = (isXWikiClass) ? "class" : "object";
    try {
        for (List<ObjectDiff> oList : objectDiffs) {
            if (oList.size() > 0) {
                Div mainDiv = createDiffDiv(prefix + "Diff");
                Span objectName = createDiffSpan(prefix + "ClassName");
                if (isXWikiClass) {
                    objectName.addElement(getFullName());
                } else {
                    objectName.addElement(oList.get(0).getClassName());
                }
                mainDiv.addElement(prefix + HTML_IMG_PLACEHOLDER_SUFFIX);
                mainDiv.addElement(objectName);
                for (ObjectDiff oDiff : oList) {
                    String propDiff = getPropertyHTMLDiff(oDiff, diff);
                    if (!StringUtils.isBlank(oDiff.getPropName()) && !StringUtils.isBlank(propDiff)) {
                        Div propDiv = createDiffDiv("propDiffContainer");
                        Span propNameSpan = createDiffSpan("propName");
                        propNameSpan.addElement(oDiff.getPropName() + propSeparator);
                        String shortPropType = StringUtils.removeEnd(oDiff.getPropType(), "Class").toLowerCase();
                        if (StringUtils.isBlank(shortPropType)) {
                            // When the diff shows a property that has been deleted, its type is not available.
                            shortPropType = HTML_IMG_METADATA_PREFIX;
                        }
                        propDiv.addElement(shortPropType + HTML_IMG_PLACEHOLDER_SUFFIX);
                        propDiv.addElement(propNameSpan);
                        Div propDiffDiv = createDiffDiv("propDiff");
                        propDiffDiv.addElement(propDiff);
                        propDiv.addElement(propDiffDiv);
                        mainDiv.addElement(propDiv);
                    }
                }
                result.append(mainDiv);
            }
        }
    } catch (XWikiException e) {
        // Catch the exception to be sure we won't send emails containing stacktraces to users.
        e.printStackTrace();
    }
    return result.toString();
}
Also used : Div(org.apache.ecs.html.Div) ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) Span(org.apache.ecs.html.Span) XWikiException(com.xpn.xwiki.XWikiException)

Example 70 with XWikiException

use of com.xpn.xwiki.XWikiException 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)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)442 XWikiContext (com.xpn.xwiki.XWikiContext)156 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)147 DocumentReference (org.xwiki.model.reference.DocumentReference)98 BaseObject (com.xpn.xwiki.objects.BaseObject)88 IOException (java.io.IOException)57 QueryException (org.xwiki.query.QueryException)57 ArrayList (java.util.ArrayList)56 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)51 XWiki (com.xpn.xwiki.XWiki)48 XWikiRestException (org.xwiki.rest.XWikiRestException)44 Session (org.hibernate.Session)42 Document (com.xpn.xwiki.api.Document)38 InitializationException (org.xwiki.component.phase.InitializationException)36 WebApplicationException (javax.ws.rs.WebApplicationException)32 SQLException (java.sql.SQLException)31 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)30 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)29 UnexpectedException (org.xwiki.store.UnexpectedException)29 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)25