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

File HibernateAttachmentVersioningStore.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

2
23
8
1
138
95
12
0.52
2.88
8
1.5

Classes

Class Line # Actions
HibernateAttachmentVersioningStore 48 23 0% 12 8
0.7575757575.8%
 

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.hibernate;
21   
22    import javax.inject.Named;
23    import javax.inject.Singleton;
24   
25    import org.hibernate.HibernateException;
26    import org.hibernate.ObjectNotFoundException;
27    import org.hibernate.Session;
28    import org.slf4j.Logger;
29    import org.slf4j.LoggerFactory;
30    import org.xwiki.component.annotation.Component;
31   
32    import com.xpn.xwiki.XWikiContext;
33    import com.xpn.xwiki.XWikiException;
34    import com.xpn.xwiki.doc.XWikiAttachment;
35    import com.xpn.xwiki.doc.XWikiAttachmentArchive;
36    import com.xpn.xwiki.store.AttachmentVersioningStore;
37    import com.xpn.xwiki.store.XWikiHibernateBaseStore;
38   
39    /**
40    * Realization of {@link AttachmentVersioningStore} for Hibernate-based storage.
41    *
42    * @version $Id: 45dad9dacf66277a6d8a1c55f43cf025cba3c3cf $
43    * @since 1.4M2
44    */
45    @Component
46    @Named("hibernate")
47    @Singleton
 
48    public class HibernateAttachmentVersioningStore extends XWikiHibernateBaseStore implements AttachmentVersioningStore
49    {
50    /** logger. */
51    private static final Logger LOGGER = LoggerFactory.getLogger(HibernateAttachmentVersioningStore.class);
52   
53    /**
54    * @param context the current context.
55    * @deprecated 1.6M1. Use ComponentManager.lookup(AttachmentVersioningStore.class) instead.
56    */
 
57  0 toggle @Deprecated
58    public HibernateAttachmentVersioningStore(XWikiContext context)
59    {
60  0 super(context.getWiki(), context);
61    }
62   
63    /**
64    * Empty constructor needed for component manager.
65    */
 
66  87 toggle public HibernateAttachmentVersioningStore()
67    {
68    }
69   
 
70  455 toggle @Override
71    public XWikiAttachmentArchive loadArchive(final XWikiAttachment attachment, XWikiContext context,
72    boolean bTransaction) throws XWikiException
73    {
74  455 try {
75  455 final XWikiAttachmentArchive archive = new XWikiAttachmentArchive();
76  455 archive.setAttachment(attachment);
77  455 executeRead(context, bTransaction, new HibernateCallback<Object>()
78    {
 
79  455 toggle @Override
80    public Object doInHibernate(Session session) throws HibernateException
81    {
82  455 try {
83  455 session.load(archive, archive.getId());
84    } catch (ObjectNotFoundException e) {
85    // if none found then return empty created archive
86    }
87  455 return null;
88    }
89    });
90  455 attachment.setAttachment_archive(archive);
91  455 return archive;
92    } catch (Exception e) {
93  0 Object[] args = { attachment.getFilename(), attachment.getDoc() };
94  0 throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
95    XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_ATTACHMENT,
96    "Exception while loading attachment archive {0} of document {1}", e, args);
97    }
98    }
99   
 
100  423 toggle @Override
101    public void saveArchive(final XWikiAttachmentArchive archive, XWikiContext context, boolean bTransaction)
102    throws XWikiException
103    {
104  423 executeWrite(context, bTransaction, new HibernateCallback<Object>()
105    {
 
106  423 toggle @Override
107    public Object doInHibernate(Session session) throws HibernateException, XWikiException
108    {
109  423 session.saveOrUpdate(archive);
110  423 return null;
111    }
112    });
113    }
114   
 
115  22 toggle @Override
116    public void deleteArchive(final XWikiAttachment attachment, final XWikiContext context, boolean bTransaction)
117    throws XWikiException
118    {
119  22 try {
120  22 executeWrite(context, bTransaction, new HibernateCallback<Object>()
121    {
 
122  22 toggle @Override
123    public Object doInHibernate(Session session) throws HibernateException, XWikiException
124    {
125  22 XWikiAttachmentArchive archive = new XWikiAttachmentArchive();
126  22 archive.setAttachment(attachment);
127  22 session.delete(archive);
128  22 return null;
129    }
130    });
131    } catch (Exception e) {
132  0 if (LOGGER.isWarnEnabled()) {
133  0 LOGGER.warn(String.format("Error deleting attachment archive [%s] of doc [%s]",
134    attachment.getFilename(), attachment.getDoc().getFullName()), e);
135    }
136    }
137    }
138    }