From 7651139027ecdcc789a70a55f0378b68815348ca Mon Sep 17 00:00:00 2001
From: tmortagne <thomas.mortagne@gmail.com>
Date: Mon, 2 Feb 2015 11:04:04 +0100
Subject: [PATCH] XWIKI-11694: XWikiHibernateAttachmentStore methods don't take
 into account attachment wiki as well as they think

---
 .../store/XWikiHibernateAttachmentStore.java  | 137 +++++++++---------
 1 file changed, 72 insertions(+), 65 deletions(-)

diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/store/XWikiHibernateAttachmentStore.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/store/XWikiHibernateAttachmentStore.java
index ad7423cf579..c052e5b5b5f 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/store/XWikiHibernateAttachmentStore.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/store/XWikiHibernateAttachmentStore.java
@@ -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);
         }
     }
 }
-- 
GitLab