Search in sources :

Example 31 with XWikiContext

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

the class WikiSkinUtils method getSkinDocument.

public XWikiDocument getSkinDocument(String skin) {
    XWikiContext xcontext = this.xcontextProvider.get();
    if (xcontext != null) {
        DocumentReference skinReference = this.currentMixedDocumentReferenceResolver.resolve(skin);
        XWiki xwiki = xcontext.getWiki();
        if (xwiki != null && xwiki.getStore() != null) {
            XWikiDocument doc;
            try {
                doc = xwiki.getDocument(skinReference, xcontext);
            } catch (XWikiException e) {
                this.logger.error("Faied to get document [{}]", skinReference, e);
                return null;
            }
            if (!doc.isNew()) {
                return doc;
            }
        }
    }
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 32 with XWikiContext

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

the class AbstractXWikiStore method getExecutionXContext.

/**
 * @param inputxcontext the XWikiContext given as input to the API
 * @return the {@link XWikiContext} located in the {@link ExecutionContext} if any
 */
protected XWikiContext getExecutionXContext(XWikiContext inputxcontext, boolean savewiki) {
    // If not a component return input context
    if (this.readonlyxcontextProvider == null) {
        return inputxcontext;
    }
    XWikiContext xcontext = this.readonlyxcontextProvider.get();
    // If no context can be found return input context
    if (xcontext == null) {
        return inputxcontext;
    }
    if (inputxcontext != null && xcontext != inputxcontext) {
        LOGGER.warn("ExecutionContext and passed XWikiContext argument mismatched, for data safety," + " the XWikiContext from the ExecutionContext has been used.", new Exception("Stack trace"));
        // Make sure to use the wiki expected by the called for the API
        if (savewiki && !Objects.equals(inputxcontext.getWikiReference(), xcontext.getWikiReference())) {
            xcontext.put(PREVIOUS_WIKI, xcontext.getWikiReference());
            xcontext.setWikiReference(inputxcontext.getWikiReference());
        }
    }
    return xcontext;
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext)

Example 33 with XWikiContext

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

the class XClassMigratorListener method migrate.

private void migrate(PropertyClass newPropertyClass) throws QueryException {
    ClassPropertyReference propertyReference = newPropertyClass.getReference();
    EntityReference classReference = propertyReference.extractReference(EntityType.DOCUMENT);
    EntityReference wikiReference = propertyReference.extractReference(EntityType.WIKI);
    // Get all document containing object of modified class
    Query query = this.queryManager.createQuery("from doc.object(" + this.localSerializer.serialize(classReference) + ") as obj", Query.XWQL);
    query.setWiki(wikiReference.getName());
    List<String> documents = query.execute();
    if (!documents.isEmpty()) {
        XWikiContext xcontext = this.xcontextProvider.get();
        String currentWikiId = xcontext.getWikiId();
        try {
            // Switch to class wiki to be safer
            xcontext.setWikiId(wikiReference.getName());
            for (String documentName : documents) {
                try {
                    migrate(newPropertyClass, documentName, xcontext);
                } catch (XWikiException e) {
                    this.logger.error("Failed to migrate property [{}] in document [{}]", propertyReference, documentName, xcontext);
                }
            }
        } finally {
            // Restore context wiki
            xcontext.setWikiId(currentWikiId);
        }
    }
}
Also used : Query(org.xwiki.query.Query) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 34 with XWikiContext

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

the class AbstractXWikiEventConverter method getXWikiStubContext.

/**
 * @return a stub XWikiContext, null if none can be generated (XWiki has never been accessed yet)
 */
private XWikiContext getXWikiStubContext() {
    ExecutionContext context = this.execution.getContext();
    XWikiContext xcontext = (XWikiContext) context.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
    if (xcontext == null) {
        xcontext = this.stubContextProvider.createStubContext();
        if (xcontext != null) {
            xcontext.declareInExecutionContext(context);
        }
    }
    return xcontext;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 35 with XWikiContext

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

the class ActionExecutionEventConverter method toRemote.

@Override
public boolean toRemote(LocalEventData localEvent, RemoteEventData remoteEvent) {
    if (localEvent.getEvent() instanceof ActionExecutedEvent || localEvent.getEvent() instanceof ActionExecutingEvent) {
        AbstractActionExecutionEvent event = (AbstractActionExecutionEvent) localEvent.getEvent();
        if (this.actions.contains(event.getActionName())) {
            // fill the remote event
            remoteEvent.setEvent(event);
            remoteEvent.setSource(serializeXWikiDocument((XWikiDocument) localEvent.getSource()));
            remoteEvent.setData(serializeXWikiContext((XWikiContext) localEvent.getData()));
        }
        return true;
    }
    return false;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) AbstractActionExecutionEvent(org.xwiki.bridge.event.AbstractActionExecutionEvent) ActionExecutedEvent(org.xwiki.bridge.event.ActionExecutedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) ActionExecutingEvent(org.xwiki.bridge.event.ActionExecutingEvent)

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