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();
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations