Search in sources :

Example 16 with XWikiContext

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

the class TranslationsTreeNode method getChildCount.

@Override
protected int getChildCount(DocumentReference documentReference) throws Exception {
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext);
    return document.getTranslationLocales(xcontext).size();
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 17 with XWikiContext

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

the class DefaultInstanceIdManager method initialize.

@Override
public void initialize() {
    // Load it from the database
    XWikiContext context = getXWikiContext();
    XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStoreProvider.get();
    // Try retrieving the UUID from the database
    // First ensure that we're on the main wiki since we store the unique id only on the main wiki
    String originalDatabase = context.getWikiId();
    context.setWikiId(context.getMainXWiki());
    try {
        InstanceId id = store.failSafeExecuteRead(context, new XWikiHibernateBaseStore.HibernateCallback<InstanceId>() {

            @Override
            public InstanceId doInHibernate(Session session) throws HibernateException {
                // Retrieve the version from the database
                return (InstanceId) session.createCriteria(InstanceId.class).uniqueResult();
            }
        });
        // If the database doesn't hold the UUID then compute one and save it
        if (id == null) {
            // Compute UUID
            final InstanceId newId = new InstanceId(UUID.randomUUID().toString());
            // will be retried again next time the wiki is restarted.
            try {
                store.executeWrite(context, new XWikiHibernateBaseStore.HibernateCallback<Object>() {

                    @Override
                    public Object doInHibernate(Session session) throws HibernateException {
                        session.createQuery("delete from " + InstanceId.class.getName()).executeUpdate();
                        session.save(newId);
                        return null;
                    }
                });
            } catch (XWikiException e) {
                this.logger.warn("Failed to save Instance id to database. Reason: [{}]", ExceptionUtils.getRootCauseMessage(e));
            }
            id = newId;
        }
        this.instanceId = id;
    } finally {
        // Restore original database
        context.setWikiId(originalDatabase);
    }
}
Also used : InstanceId(org.xwiki.instance.InstanceId) HibernateException(org.hibernate.HibernateException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 18 with XWikiContext

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

the class AttachmentsTreeNode method getChildCount.

@Override
protected int getChildCount(DocumentReference documentReference) throws Exception {
    int count = 0;
    if (showAddAttachment(documentReference)) {
        count++;
    }
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext);
    count += document.getAttachmentList().size();
    return count;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 19 with XWikiContext

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

the class AttachmentsTreeNode method getChildren.

@Override
protected List<String> getChildren(DocumentReference documentReference, int offset, int limit) throws Exception {
    List<String> children = new ArrayList<String>();
    if (offset == 0 && showAddAttachment(documentReference)) {
        children.add("addAttachment:" + this.defaultEntityReferenceSerializer.serialize(documentReference));
    }
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext);
    List<XWikiAttachment> attachments = document.getAttachmentList();
    for (XWikiAttachment attachment : subList(attachments, offset, limit)) {
        children.add(serialize(attachment.getReference()));
    }
    return children;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 20 with XWikiContext

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

the class XWikiContextCopier method copy.

/**
 * {@inheritDoc}
 *
 * Any in progress session/transaction on the store retained by the original context will be closed/rollbacked
 * prior cloning (see {@link com.xpn.xwiki.store.XWikiStoreInterface#cleanUp(XWikiContext)}). Therefore,
 * the copy operation has a side effect on the original context. You should never copy a context
 * while a create/update transaction is in progress, since some changes would get rollbacked.
 */
@Override
public XWikiContext copy(XWikiContext originalXWikiContext) {
    // Clean up the store session/transaction before cloning. For the hibernate store, in progress
    // session/transaction is stored in the context, and would be swallow copied when the context is cloned.
    // Cleaning after clone would not help, since it would close/rollback the session/transaction still referenced
    // in the original context as well, causing this context to be corrupted.
    // The correct way would be to not shallow clone the session/transaction when cloning the original context,
    // since session/transaction are lazy initialize on request when missing.
    originalXWikiContext.getWiki().getStore().cleanUp(originalXWikiContext);
    // This is still a shallow clone, but at least for stuff like wikiID and userReference it gets the job done.
    XWikiContext clonedXWikiContext = originalXWikiContext.clone();
    // lets now build the stub context
    // Copy the request from the context.
    clonedXWikiContext.setRequest(this.xwikiRequestCloner.copy(originalXWikiContext.getRequest()));
    // Force forged context response to a stub response, since the current context response
    // will not mean anything anymore when running in the scheduler's thread, and can cause
    // errors.
    XWikiResponse stub = new XWikiServletResponseStub();
    clonedXWikiContext.setResponse(stub);
    // feed the dummy context
    if (clonedXWikiContext.getURL() == null) {
        try {
            clonedXWikiContext.setURL(new URL("http://www.mystuburl.com/"));
        } catch (Exception e) {
        // the URL is clearly well formed
        }
    }
    XWikiURLFactory xurf = originalXWikiContext.getWiki().getURLFactoryService().createURLFactory(originalXWikiContext.getMode(), originalXWikiContext);
    clonedXWikiContext.setURLFactory(xurf);
    return clonedXWikiContext;
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiResponse(com.xpn.xwiki.web.XWikiResponse) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL)

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