Search in sources :

Example 91 with XWikiContext

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

the class DefaultWikiComponentBuilderEventListener method installOrUpdateComponentRequirementXClass.

/**
 * Verify that the {@link #DEPENDENCY_CLASS} exists and is up-to-date (act if not).
 *
 * @throws XWikiException on failure
 */
private void installOrUpdateComponentRequirementXClass() throws XWikiException {
    XWikiContext xcontext = getXWikiContext();
    XWikiDocument doc = xcontext.getWiki().getDocument(DEPENDENCY_CLASS_REFERENCE, xcontext);
    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(doc.getDocumentReference());
    boolean needsUpdate = false;
    needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Dependency XWiki Class");
    needsUpdate |= bclass.addTextField(COMPONENT_ROLE_TYPE_FIELD, "Dependency Role Type", 30);
    needsUpdate |= bclass.addTextField(COMPONENT_ROLE_HINT_FIELD, "Dependency Role Hint", 30);
    needsUpdate |= bclass.addTextField(DEPENDENCY_BINDING_NAME_FIELD, "Binding name", 30);
    if (needsUpdate) {
        this.update(doc);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 92 with XWikiContext

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

the class DefaultWikiComponentBuilderEventListener method update.

/**
 * Utility method for updating a wiki macro class definition document.
 *
 * @param doc xwiki document containing the wiki macro class.
 * @throws XWikiException if an error occurs while saving the document.
 */
private void update(XWikiDocument doc) throws XWikiException {
    XWikiContext xcontext = getXWikiContext();
    xcontext.getWiki().saveDocument(doc, xcontext);
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext)

Example 93 with XWikiContext

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

the class AbstractExtensionScriptService method getCallerDocument.

protected XWikiDocument getCallerDocument() {
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument sdoc = (XWikiDocument) xcontext.get("sdoc");
    if (sdoc == null) {
        sdoc = xcontext.getDoc();
    }
    return sdoc;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 94 with XWikiContext

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

the class Packager method reset.

/**
 * @since 9.3RC1
 */
public void reset(DocumentReference reference, DocumentReference authorReference) throws IOException, XarException, XWikiException, XarExtensionExtension {
    Collection<XarInstalledExtension> installedExtensions = getXarInstalledExtensionRepository().getXarInstalledExtensions(reference);
    if (!installedExtensions.isEmpty()) {
        XarInstalledExtension extension = installedExtensions.iterator().next();
        // Remove the version if any since it does not make sense in a XAR
        DocumentReference documentReference = cleanDocumentReference(reference);
        XWikiDocument document = getXWikiDocument(documentReference, extension);
        if (document != null) {
            XWikiContext xcontext = this.xcontextProvider.get();
            // Get database document
            XWikiDocument databaseDocument = xcontext.getWiki().getDocument(documentReference, xcontext);
            // Override data of database document with extension document
            databaseDocument.apply(document, true);
            // Make sure new version will have the right author
            databaseDocument.setAuthorReference(authorReference);
            databaseDocument.setContentAuthorReference(authorReference);
            // Force generating new version
            databaseDocument.setMetaDataDirty(true);
            databaseDocument.setContentDirty(true);
            // Save
            xcontext.getWiki().saveDocument(databaseDocument, "Reset document from extension [" + extension + "]", xcontext);
        } else {
            throw new XarExtensionExtension("Can't find any document with reference [" + documentReference + "] in extension [" + extension.getId() + "]");
        }
    } else {
        throw new XarExtensionExtension("Can't find any installed extension associated with the document reference [" + reference + "]");
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) XarExtensionExtension(org.xwiki.extension.xar.XarExtensionExtension) XWikiContext(com.xpn.xwiki.XWikiContext) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 95 with XWikiContext

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

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)564 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)203 XWikiException (com.xpn.xwiki.XWikiException)195 DocumentReference (org.xwiki.model.reference.DocumentReference)150 XWiki (com.xpn.xwiki.XWiki)106 BaseObject (com.xpn.xwiki.objects.BaseObject)104 Test (org.junit.Test)64 ArrayList (java.util.ArrayList)55 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)43 ExecutionContext (org.xwiki.context.ExecutionContext)43 Session (org.hibernate.Session)37 InitializationException (org.xwiki.component.phase.InitializationException)36 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)34 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)33 WikiReference (org.xwiki.model.reference.WikiReference)31 Before (org.junit.Before)29 EntityReference (org.xwiki.model.reference.EntityReference)28 QueryException (org.xwiki.query.QueryException)28 Execution (org.xwiki.context.Execution)27 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)24