use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method deleteXWikiDoc.
@Override
public void deleteXWikiDoc(XWikiDocument doc, XWikiContext inputxcontext) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, true);
try {
boolean bTransaction = true;
MonitorPlugin monitor = Util.getMonitorPlugin(context);
try {
// Start monitoring timer
if (monitor != null) {
monitor.startTimer(HINT);
}
checkHibernate(context);
SessionFactory sfactory = injectCustomMappingsInSessionFactory(doc, context);
bTransaction = bTransaction && beginTransaction(sfactory, context);
Session session = getSession(context);
session.setFlushMode(FlushMode.COMMIT);
if (doc.getStore() == null) {
Object[] args = { doc.getDocumentReference() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_CANNOT_DELETE_UNLOADED_DOC, "Impossible to delete document {0} if it is not loaded", null, args);
}
// Let's delete any attachment this document might have
for (XWikiAttachment attachment : doc.getAttachmentList()) {
XWikiAttachmentStoreInterface store = getXWikiAttachmentStoreInterface(attachment);
store.deleteXWikiAttachment(attachment, false, context, false);
}
// deleting XWikiLinks
if (context.getWiki().hasBacklinks(context)) {
deleteLinks(doc.getId(), context, true);
}
// Remove properties planned for removal
if (!doc.getXObjectsToRemove().isEmpty()) {
for (BaseObject bobj : doc.getXObjectsToRemove()) {
if (bobj != null) {
deleteXWikiCollection(bobj, context, false, false);
}
}
doc.setXObjectsToRemove(new ArrayList<BaseObject>());
}
for (List<BaseObject> objects : doc.getXObjects().values()) {
for (BaseObject obj : objects) {
if (obj != null) {
deleteXWikiCollection(obj, context, false, false);
}
}
}
context.getWiki().getVersioningStore().deleteArchive(doc, false, context);
session.delete(doc);
// We need to ensure that the deleted document becomes the original document
doc.setOriginalDocument(doc.clone());
// Update space table if needed
maybeDeleteXWikiSpace(doc, session);
if (bTransaction) {
endTransaction(context, true);
}
} catch (Exception e) {
Object[] args = { doc.getDocumentReference() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_DOC, "Exception while deleting document {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false);
}
} catch (Exception e) {
}
// End monitoring timer
if (monitor != null) {
monitor.endTimer(HINT);
}
}
} finally {
restoreExecutionXContext();
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method loadLinks.
// ---------------------------------------
// Links
// ---------------------------------------
@Override
public List<XWikiLink> loadLinks(long docId, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, true);
List<XWikiLink> links = new ArrayList<XWikiLink>();
try {
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(false, context);
}
Session session = getSession(context);
Query query = session.createQuery(" from XWikiLink as link where link.id.docId = :docId");
query.setLong("docId", docId);
links = query.list();
if (bTransaction) {
endTransaction(context, false, false);
bTransaction = false;
}
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_LINKS, "Exception while loading links", e);
} finally {
try {
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
}
restoreExecutionXContext();
}
return links;
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method saveAttachment.
private void saveAttachment(XWikiAttachment attachment, XWikiContext context) throws XWikiException {
try {
// If the comment is larger than the max size supported by the Storage, then abbreviate it
String comment = attachment.getComment();
if (comment != null && comment.length() > 1023) {
attachment.setComment(StringUtils.abbreviate(comment, 1023));
}
// See XWIKI-9421: Attachment version is incremented when a document is restored from recycle bin
if (attachment.isContentDirty() && !attachment.getDoc().isNew()) {
attachment.updateContentArchive(context);
}
Session session = getSession(context);
Query query = session.createQuery("select attach.id from XWikiAttachment as attach where attach.id = :id");
query.setLong("id", attachment.getId());
boolean exist = query.uniqueResult() != null;
if (exist) {
session.update(attachment);
} else {
if (attachment.getContentStore() == null) {
// Set content store
attachment.setContentStore(getDefaultAttachmentContentStore(context));
}
if (attachment.getArchiveStore() == null) {
// Set archive store
attachment.setArchiveStore(getDefaultAttachmentArchiveStore(context));
}
session.save(attachment);
}
// Save the attachment content if it's marked as "dirty" (out of sync with the database).
if (attachment.isContentDirty()) {
// updateParent and bTransaction must be false because the content should be saved in the same
// transaction as the attachment and if the parent doc needs to be updated, this function will do it.
XWikiAttachmentStoreInterface store = getXWikiAttachmentStoreInterface(attachment);
store.saveAttachmentContent(attachment, false, context, false);
}
// Mark the attachment content and metadata as not dirty.
// Ideally this would only happen if the transaction is committed successfully but since an unsuccessful
// transaction will most likely be accompanied by an exception, the cache will not have a chance to save
// the copy of the document with erronious information. If this is not set here, the cache will return
// a copy of the attachment which claims to be dirty although it isn't.
attachment.setMetaDataDirty(false);
if (attachment.isContentDirty()) {
attachment.getAttachment_content().setContentDirty(false);
}
} catch (Exception e) {
Object[] args = { attachment.getReference() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, "Exception while saving attachment [{0}]", e, args);
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateStore method deleteLinks.
@Override
public void deleteLinks(long docId, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, true);
try {
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(context);
}
Session session = getSession(context);
Query query = session.createQuery("delete from XWikiLink as link where link.id.docId = :docId");
query.setLong("docId", docId);
query.executeUpdate();
if (bTransaction) {
endTransaction(context, true);
bTransaction = false;
}
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_LINKS, "Exception while deleting links", e);
} finally {
try {
if (bTransaction) {
endTransaction(context, false);
}
} catch (Exception e) {
}
restoreExecutionXContext();
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiHibernateVersioningStore method getXWikiDocVersions.
@Override
public Version[] getXWikiDocVersions(XWikiDocument doc, XWikiContext context) throws XWikiException {
try {
XWikiDocumentArchive archive = getXWikiDocumentArchive(doc, context);
if (archive == null) {
return new Version[0];
}
Collection<XWikiRCSNodeInfo> nodes = archive.getNodes();
Version[] versions = new Version[nodes.size()];
Iterator<XWikiRCSNodeInfo> it = nodes.iterator();
for (int i = 0; i < versions.length; i++) {
XWikiRCSNodeInfo node = it.next();
versions[versions.length - 1 - i] = node.getId().getVersion();
}
return versions;
} catch (Exception e) {
Object[] args = { doc.getFullName() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_READING_REVISIONS, "Exception while reading document {0} revisions", e, args);
}
}
Aggregations