Skip to content
Snippets Groups Projects
Commit 76511390 authored by tmortagne's avatar tmortagne
Browse files

XWIKI-11694: XWikiHibernateAttachmentStore methods don't take into account...

XWIKI-11694: XWikiHibernateAttachmentStore methods don't take into account attachment wiki as well as they think
parent 08c41339
No related branches found
No related tags found
No related merge requests found
......@@ -103,43 +103,41 @@ public void saveAttachmentContent(XWikiAttachment attachment, boolean parentUpda
// Protect against a corrupted attachment. This can happen if an attachment is listed in the Attachment
// table but not listed in the Attachment Content table! In this case don't save anything.
if (content != null) {
String currentWiki = context.getWikiId();
try {
// Switch context wiki to attachment wiki
String attachmentWiki =
(attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference()
.getWikiReference().getName();
if (attachmentWiki != null) {
context.setWikiId(attachmentWiki);
}
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(context);
}
Session session = getSession(context);
String db = context.getWikiId();
String attachdb = (attachment.getDoc() == null) ? null : attachment.getDoc().getDatabase();
try {
if (attachdb != null) {
context.setWikiId(attachdb);
}
Query query =
session
.createQuery("select attach.id from XWikiAttachmentContent as attach where attach.id = :id");
query.setLong("id", content.getId());
if (query.uniqueResult() == null) {
session.save(content);
} else {
session.update(content);
}
if (attachment.getAttachment_archive() == null) {
attachment.loadArchive(context);
}
// The archive has been updated in XWikiHibernateStore.saveAttachment()
context.getWiki().getAttachmentVersioningStore()
.saveArchive(attachment.getAttachment_archive(), context, false);
Query query =
session.createQuery("select attach.id from XWikiAttachmentContent as attach where attach.id = :id");
query.setLong("id", content.getId());
if (query.uniqueResult() == null) {
session.save(content);
} else {
session.update(content);
}
if (parentUpdate) {
context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, true);
}
if (attachment.getAttachment_archive() == null) {
attachment.loadArchive(context);
}
// The archive has been updated in XWikiHibernateStore.saveAttachment()
context.getWiki().getAttachmentVersioningStore()
.saveArchive(attachment.getAttachment_archive(), context, false);
} finally {
context.setWikiId(db);
if (parentUpdate) {
context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, true);
}
if (bTransaction) {
......@@ -157,6 +155,9 @@ public void saveAttachmentContent(XWikiAttachment attachment, boolean parentUpda
}
} catch (Exception e) {
}
// Restore context wiki
context.setWikiId(currentWiki);
}
} else {
LOGGER.warn("Failed to save the Attachment content for [{}] at [{}] since no content could be found!",
......@@ -202,29 +203,30 @@ public void saveAttachmentsContent(List<XWikiAttachment> attachments, XWikiDocum
public void loadAttachmentContent(XWikiAttachment attachment, XWikiContext context, boolean bTransaction)
throws XWikiException
{
String currentWiki = context.getWikiId();
try {
// Switch context wiki to attachment wiki
String attachmentWiki =
(attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference()
.getWikiReference().getName();
if (attachmentWiki != null) {
context.setWikiId(attachmentWiki);
}
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(false, context);
}
Session session = getSession(context);
String db = context.getWikiId();
String attachdb = (attachment.getDoc() == null) ? null : attachment.getDoc().getDatabase();
try {
if (attachdb != null) {
context.setWikiId(attachdb);
}
XWikiAttachmentContent content = new XWikiAttachmentContent(attachment);
session.load(content, new Long(content.getId()));
XWikiAttachmentContent content = new XWikiAttachmentContent(attachment);
session.load(content, new Long(content.getId()));
// Hibernate calls setContent which causes isContentDirty to be true. This is not what we want.
content.setContentDirty(false);
// Hibernate calls setContent which causes isContentDirty to be true. This is not what we want.
content.setContentDirty(false);
attachment.setAttachment_content(content);
} finally {
context.setWikiId(db);
}
attachment.setAttachment_content(content);
if (bTransaction) {
endTransaction(context, false, false);
......@@ -241,6 +243,9 @@ public void loadAttachmentContent(XWikiAttachment attachment, XWikiContext conte
}
} catch (Exception e) {
}
// Restore context wiki
context.setWikiId(currentWiki);
}
}
......@@ -255,7 +260,17 @@ public void deleteXWikiAttachment(XWikiAttachment attachment, XWikiContext conte
public void deleteXWikiAttachment(XWikiAttachment attachment, boolean parentUpdate, XWikiContext context,
boolean bTransaction) throws XWikiException
{
String currentWiki = context.getWikiId();
try {
// Switch context wiki to attachment wiki
String attachmentWiki =
(attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference()
.getWikiReference().getName();
if (attachmentWiki != null) {
context.setWikiId(attachmentWiki);
}
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(context);
......@@ -263,32 +278,21 @@ public void deleteXWikiAttachment(XWikiAttachment attachment, boolean parentUpda
Session session = getSession(context);
String db = context.getWikiId();
String attachdb = (attachment.getDoc() == null) ? null : attachment.getDoc().getDatabase();
// Delete the three attachment entries
try {
if (attachdb != null) {
context.setWikiId(attachdb);
}
// Delete the three attachment entries
try {
session.delete(new XWikiAttachmentContent(attachment));
} catch (Exception e) {
LOGGER.warn("Error deleting attachment content [{}] of document [{}]", attachment.getFilename(),
attachment.getDoc().getDocumentReference());
}
context.getWiki().getAttachmentVersioningStore().deleteArchive(attachment, context, false);
session.delete(new XWikiAttachmentContent(attachment));
} catch (Exception e) {
LOGGER.warn("Error deleting attachment content [{}] of document [{}]", attachment.getFilename(),
attachment.getDoc().getDocumentReference());
}
try {
session.delete(attachment);
} catch (Exception e) {
LOGGER.warn("Error deleting attachment meta data [{}] of document [{}]", attachment.getFilename(),
attachment.getDoc().getDocumentReference());
}
context.getWiki().getAttachmentVersioningStore().deleteArchive(attachment, context, false);
} finally {
context.setWikiId(db);
try {
session.delete(attachment);
} catch (Exception e) {
LOGGER.warn("Error deleting attachment meta data [{}] of document [{}]", attachment.getFilename(),
attachment.getDoc().getDocumentReference());
}
try {
......@@ -323,6 +327,9 @@ public void deleteXWikiAttachment(XWikiAttachment attachment, boolean parentUpda
}
} catch (Exception e) {
}
// Restore context wiki
context.setWikiId(currentWiki);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment