1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.store

File XWikiHibernateAttachmentStore.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

54
110
10
1
343
249
48
0.44
11
10
4.8

Classes

Class Line # Actions
XWikiHibernateAttachmentStore 44 110 0% 48 86
0.5057471450.6%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.store;
21   
22    import java.util.Iterator;
23    import java.util.List;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.hibernate.Query;
29    import org.hibernate.Session;
30    import org.slf4j.Logger;
31    import org.slf4j.LoggerFactory;
32    import org.xwiki.component.annotation.Component;
33   
34    import com.xpn.xwiki.XWiki;
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.XWikiException;
37    import com.xpn.xwiki.doc.XWikiAttachment;
38    import com.xpn.xwiki.doc.XWikiAttachmentContent;
39    import com.xpn.xwiki.doc.XWikiDocument;
40   
41    @Component
42    @Named("hibernate")
43    @Singleton
 
44    public class XWikiHibernateAttachmentStore extends XWikiHibernateBaseStore implements XWikiAttachmentStoreInterface
45    {
46    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiHibernateAttachmentStore.class);
47   
48    /**
49    * This allows to initialize our storage engine. The hibernate config file path is taken from xwiki.cfg or directly
50    * in the WEB-INF directory.
51    *
52    * @param xwiki
53    * @param context
54    * @deprecated 1.6M1. Use ComponentManager.lookup(XWikiAttachmentStoreInterface.class) instead.
55    */
 
56  0 toggle @Deprecated
57    public XWikiHibernateAttachmentStore(XWiki xwiki, XWikiContext context)
58    {
59  0 super(xwiki, context);
60    }
61   
62    /**
63    * @see #XWikiHibernateAttachmentStore(XWiki, XWikiContext)
64    * @deprecated 1.6M1. Use ComponentManager.lookup(XWikiAttachmentStoreInterface.class) instead.
65    */
 
66  0 toggle @Deprecated
67    public XWikiHibernateAttachmentStore(XWikiContext context)
68    {
69  0 this(context.getWiki(), context);
70    }
71   
72    /**
73    * Initialize the storage engine with a specific path This is used for tests.
74    *
75    * @param hibpath
76    * @deprecated 1.6M1. Use ComponentManager.lookup(XWikiAttachmentStoreInterface.class) instead.
77    */
 
78  0 toggle @Deprecated
79    public XWikiHibernateAttachmentStore(String hibpath)
80    {
81  0 super(hibpath);
82    }
83   
84    /**
85    * Empty constructor needed for component manager.
86    */
 
87  87 toggle public XWikiHibernateAttachmentStore()
88    {
89    }
90   
 
91  0 toggle @Override
92    public void saveAttachmentContent(XWikiAttachment attachment, XWikiContext context, boolean bTransaction)
93    throws XWikiException
94    {
95  0 saveAttachmentContent(attachment, true, context, bTransaction);
96    }
97   
 
98  423 toggle @Override
99    public void saveAttachmentContent(XWikiAttachment attachment, boolean parentUpdate, XWikiContext inputxcontext,
100    boolean bTransaction) throws XWikiException
101    {
102  423 XWikiContext context = getXWikiContext(inputxcontext);
103   
104  423 XWikiAttachmentContent content = attachment.getAttachment_content();
105    // Protect against a corrupted attachment. This can happen if an attachment is listed in the Attachment
106    // table but not listed in the Attachment Content table! In this case don't save anything.
107  423 if (content != null) {
108  423 String currentWiki = context.getWikiId();
109   
110  423 try {
111    // Switch context wiki to attachment wiki
112  423 String attachmentWiki =
113  423 (attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference()
114    .getWikiReference().getName();
115  423 if (attachmentWiki != null) {
116  423 context.setWikiId(attachmentWiki);
117    }
118   
119  423 if (bTransaction) {
120  0 checkHibernate(context);
121  0 bTransaction = beginTransaction(context);
122    }
123  423 Session session = getSession(context);
124   
125  423 Query query =
126    session.createQuery("select attach.id from XWikiAttachmentContent as attach where attach.id = :id");
127  423 query.setLong("id", content.getId());
128  423 if (query.uniqueResult() == null) {
129  416 session.save(content);
130    } else {
131  7 session.update(content);
132    }
133   
134  423 if (attachment.getAttachment_archive() == null) {
135  365 attachment.loadArchive(context);
136    }
137    // The archive has been updated in XWikiHibernateStore.saveAttachment()
138  423 context.getWiki().getAttachmentVersioningStore()
139    .saveArchive(attachment.getAttachment_archive(), context, false);
140   
141  423 if (parentUpdate) {
142  0 context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, true);
143    }
144   
145  423 if (bTransaction) {
146  0 endTransaction(context, true);
147    }
148    } catch (Exception e) {
149  0 Object[] args = { attachment.getFilename(), attachment.getDoc().getFullName() };
150  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
151    XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT,
152    "Exception while saving attachment {0} of document {1}", e, args);
153    } finally {
154  423 try {
155  423 if (bTransaction) {
156  0 endTransaction(context, false);
157    }
158    } catch (Exception e) {
159    }
160   
161    // Restore context wiki
162  423 context.setWikiId(currentWiki);
163    }
164    } else {
165  0 LOGGER.warn("Failed to save the Attachment content for [{}] at [{}] since no content could be found!",
166    attachment.getFilename(), attachment.getDoc());
167    }
168    }
169   
 
170  0 toggle @Override
171    public void saveAttachmentsContent(List<XWikiAttachment> attachments, XWikiDocument doc, boolean bParentUpdate,
172    XWikiContext inputxcontext, boolean bTransaction) throws XWikiException
173    {
174  0 XWikiContext context = getXWikiContext(inputxcontext);
175   
176  0 if (attachments == null) {
177  0 return;
178    }
179  0 try {
180  0 if (bTransaction) {
181  0 checkHibernate(context);
182  0 bTransaction = beginTransaction(context);
183    }
184  0 Iterator<XWikiAttachment> it = attachments.iterator();
185  0 while (it.hasNext()) {
186  0 XWikiAttachment att = it.next();
187  0 saveAttachmentContent(att, false, context, false);
188    }
189  0 if (bParentUpdate) {
190  0 context.getWiki().getStore().saveXWikiDoc(doc, context, false);
191    }
192    } catch (Exception e) {
193  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
194    XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, "Exception while saving attachments", e);
195    } finally {
196  0 try {
197  0 if (bTransaction) {
198  0 endTransaction(context, false);
199    }
200    } catch (Exception e) {
201    }
202    }
203   
204    }
205   
 
206  281 toggle @Override
207    public void loadAttachmentContent(XWikiAttachment attachment, XWikiContext inputxcontext, boolean bTransaction)
208    throws XWikiException
209    {
210  281 XWikiContext context = getXWikiContext(inputxcontext);
211   
212  279 String currentWiki = context.getWikiId();
213   
214  280 try {
215    // Switch context wiki to attachment wiki
216  281 String attachmentWiki =
217  281 (attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference()
218    .getWikiReference().getName();
219  281 if (attachmentWiki != null) {
220  280 context.setWikiId(attachmentWiki);
221    }
222   
223  281 if (bTransaction) {
224  281 checkHibernate(context);
225  278 bTransaction = beginTransaction(false, context);
226    }
227  279 Session session = getSession(context);
228   
229  281 XWikiAttachmentContent content = new XWikiAttachmentContent(attachment);
230  281 session.load(content, new Long(content.getId()));
231   
232    // Hibernate calls setContent which causes isContentDirty to be true. This is not what we want.
233  281 content.setContentDirty(false);
234   
235  281 attachment.setAttachment_content(content);
236   
237  281 if (bTransaction) {
238  281 endTransaction(context, false, false);
239    }
240    } catch (Exception e) {
241  0 Object[] args = { attachment.getFilename(), attachment.getDoc().getFullName() };
242  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
243    XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_ATTACHMENT,
244    "Exception while loading attachment {0} of document {1}", e, args);
245    } finally {
246  281 try {
247  281 if (bTransaction) {
248  280 endTransaction(context, false, false);
249    }
250    } catch (Exception e) {
251    }
252   
253    // Restore context wiki
254  281 context.setWikiId(currentWiki);
255    }
256    }
257   
 
258  0 toggle @Override
259    public void deleteXWikiAttachment(XWikiAttachment attachment, XWikiContext context, boolean bTransaction)
260    throws XWikiException
261    {
262  0 deleteXWikiAttachment(attachment, true, context, bTransaction);
263    }
264   
 
265  22 toggle @Override
266    public void deleteXWikiAttachment(XWikiAttachment attachment, boolean parentUpdate, XWikiContext inputxcontext,
267    boolean bTransaction) throws XWikiException
268    {
269  22 XWikiContext context = getXWikiContext(inputxcontext);
270   
271  22 String currentWiki = context.getWikiId();
272   
273  22 try {
274    // Switch context wiki to attachment wiki
275  22 String attachmentWiki =
276  22 (attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference()
277    .getWikiReference().getName();
278  22 if (attachmentWiki != null) {
279  22 context.setWikiId(attachmentWiki);
280    }
281   
282  22 if (bTransaction) {
283  0 checkHibernate(context);
284  0 bTransaction = beginTransaction(context);
285    }
286   
287  22 Session session = getSession(context);
288   
289    // Delete the three attachment entries
290  22 try {
291  22 session.delete(new XWikiAttachmentContent(attachment));
292    } catch (Exception e) {
293  0 LOGGER.warn("Error deleting attachment content [{}] of document [{}]", attachment.getFilename(),
294    attachment.getDoc().getDocumentReference());
295    }
296   
297  22 context.getWiki().getAttachmentVersioningStore().deleteArchive(attachment, context, false);
298   
299  22 try {
300  22 session.delete(attachment);
301    } catch (Exception e) {
302  0 LOGGER.warn("Error deleting attachment meta data [{}] of document [{}]", attachment.getFilename(),
303    attachment.getDoc().getDocumentReference());
304    }
305   
306  22 try {
307  22 if (parentUpdate) {
308  0 List<XWikiAttachment> list = attachment.getDoc().getAttachmentList();
309  0 for (int i = 0; i < list.size(); i++) {
310  0 XWikiAttachment attach = list.get(i);
311  0 if (attachment.getFilename().equals(attach.getFilename())) {
312  0 list.remove(i);
313  0 break;
314    }
315    }
316  0 context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, false);
317    }
318    } catch (Exception e) {
319  0 LOGGER.warn("Error updating document when deleting attachment [{}] of document [{}]",
320    attachment.getFilename(), attachment.getDoc().getDocumentReference());
321    }
322   
323  22 if (bTransaction) {
324  0 endTransaction(context, true);
325    }
326    } catch (Exception e) {
327  0 Object[] args = { attachment.getFilename(), attachment.getDoc().getFullName() };
328  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
329    XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_ATTACHMENT,
330    "Exception while deleting attachment {0} of document {1}", e, args);
331    } finally {
332  22 try {
333  22 if (bTransaction) {
334  0 endTransaction(context, false);
335    }
336    } catch (Exception e) {
337    }
338   
339    // Restore context wiki
340  22 context.setWikiId(currentWiki);
341    }
342    }
343    }