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

File HibernateAttachmentRecycleBinStore.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

6
27
14
1
196
140
19
0.7
1.93
14
1.36

Classes

Class Line # Actions
HibernateAttachmentRecycleBinStore 53 27 0% 19 39
0.1702127617%
 

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 java.util.Date;
23    import java.util.List;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.apache.commons.lang3.StringUtils;
29    import org.hibernate.Criteria;
30    import org.hibernate.HibernateException;
31    import org.hibernate.Session;
32    import org.hibernate.criterion.Order;
33    import org.hibernate.criterion.Restrictions;
34    import org.xwiki.component.annotation.Component;
35   
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.doc.DeletedAttachment;
39    import com.xpn.xwiki.doc.XWikiAttachment;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.store.AttachmentRecycleBinStore;
42    import com.xpn.xwiki.store.XWikiHibernateBaseStore;
43   
44    /**
45    * Realization of {@link AttachmentRecycleBinStore} for Hibernate-based storage.
46    *
47    * @version $Id: df80ed9e34f4a1518338a16dcf0b06f719aab1b8 $
48    * @since 1.4M1
49    */
50    @Component
51    @Named("hibernate")
52    @Singleton
 
53    public class HibernateAttachmentRecycleBinStore extends XWikiHibernateBaseStore implements AttachmentRecycleBinStore
54    {
55    /** String used to annotate unchecked exceptions. */
56    private static final String ANOTATE_UNCHECKED = "unchecked";
57   
58    /** Constant string used to refer Document ID. */
59    private static final String DOC_ID = "docId";
60   
61    /** Constant string used to refer date. */
62    private static final String DATE = "date";
63   
64    /**
65    * Constructor used by {@link com.xpn.xwiki.XWiki} during storage initialization.
66    *
67    * @param context The current context.
68    * @deprecated 1.6M1. Use ComponentManager.lookup(AttachmentRecycleBinStore.class) instead.
69    */
 
70  0 toggle @Deprecated
71    public HibernateAttachmentRecycleBinStore(XWikiContext context)
72    {
73  0 super(context.getWiki(), context);
74    }
75   
76    /**
77    * Empty constructor needed for component manager.
78    */
 
79  87 toggle public HibernateAttachmentRecycleBinStore()
80    {
81    }
82   
 
83  5 toggle @Override
84    public void saveToRecycleBin(XWikiAttachment attachment, String deleter, Date date, XWikiContext inputxcontext,
85    boolean bTransaction) throws XWikiException
86    {
87  5 XWikiContext context = getXWikiContext(inputxcontext);
88   
89  5 final DeletedAttachment trashAtachment = new DeletedAttachment(attachment, deleter, date, context);
90  5 executeWrite(context, bTransaction, new HibernateCallback<Object>()
91    {
 
92  5 toggle @Override
93    public Object doInHibernate(Session session) throws HibernateException
94    {
95  5 session.save(trashAtachment);
96  5 return null;
97    }
98    });
99    }
100   
 
101  0 toggle @Override
102    public XWikiAttachment restoreFromRecycleBin(final XWikiAttachment attachment, final long index,
103    final XWikiContext inputxcontext, boolean bTransaction) throws XWikiException
104    {
105  0 XWikiContext context = getXWikiContext(inputxcontext);
106   
107  0 return executeRead(context, bTransaction, new HibernateCallback<XWikiAttachment>()
108    {
 
109  0 toggle @Override
110    public XWikiAttachment doInHibernate(Session session) throws HibernateException, XWikiException
111    {
112  0 try {
113  0 DeletedAttachment trashAttachment =
114    (DeletedAttachment) session.load(DeletedAttachment.class, Long.valueOf(index));
115  0 return trashAttachment.restoreAttachment(attachment, context);
116    } catch (Exception ex) {
117    // Invalid recycle entry.
118  0 return null;
119    }
120    }
121    });
122    }
123   
 
124  0 toggle @Override
125    public DeletedAttachment getDeletedAttachment(final long index, XWikiContext context, boolean bTransaction)
126    throws XWikiException
127    {
128  0 return executeRead(context, bTransaction, new HibernateCallback<DeletedAttachment>()
129    {
 
130  0 toggle @Override
131    public DeletedAttachment doInHibernate(Session session) throws HibernateException, XWikiException
132    {
133  0 return (DeletedAttachment) session.get(DeletedAttachment.class, Long.valueOf(index));
134    }
135    });
136    }
137   
 
138  0 toggle @Override
139    public List<DeletedAttachment> getAllDeletedAttachments(final XWikiAttachment attachment, XWikiContext context,
140    boolean bTransaction) throws XWikiException
141    {
142  0 return executeRead(context, bTransaction, new HibernateCallback<List<DeletedAttachment>>()
143    {
 
144  0 toggle @SuppressWarnings(ANOTATE_UNCHECKED)
145    @Override
146    public List<DeletedAttachment> doInHibernate(Session session) throws HibernateException, XWikiException
147    {
148  0 Criteria c = session.createCriteria(DeletedAttachment.class);
149  0 if (attachment != null) {
150  0 c.add(Restrictions.eq(DOC_ID, attachment.getDocId()));
151  0 if (!StringUtils.isBlank(attachment.getFilename())) {
152  0 c.add(Restrictions.eq("filename", attachment.getFilename()));
153    }
154    }
155  0 return c.addOrder(Order.desc(DATE)).list();
156    }
157    });
158    }
159   
 
160  0 toggle @Override
161    public List<DeletedAttachment> getAllDeletedAttachments(final XWikiDocument doc, XWikiContext context,
162    boolean bTransaction) throws XWikiException
163    {
164  0 return executeRead(context, bTransaction, new HibernateCallback<List<DeletedAttachment>>()
165    {
 
166  0 toggle @SuppressWarnings(ANOTATE_UNCHECKED)
167    @Override
168    public List<DeletedAttachment> doInHibernate(Session session) throws HibernateException, XWikiException
169    {
170  0 assert doc != null;
171  0 return session.createCriteria(DeletedAttachment.class).add(Restrictions.eq(DOC_ID, doc.getId()))
172    .addOrder(Order.desc(DATE)).list();
173    }
174    });
175    }
176   
 
177  0 toggle @Override
178    public void deleteFromRecycleBin(final long index, XWikiContext context, boolean bTransaction)
179    throws XWikiException
180    {
181  0 executeWrite(context, bTransaction, new HibernateCallback<Object>()
182    {
 
183  0 toggle @Override
184    public Object doInHibernate(Session session) throws HibernateException, XWikiException
185    {
186  0 try {
187  0 session.createQuery("delete from " + DeletedAttachment.class.getName() + " where id=?")
188    .setLong(0, index).executeUpdate();
189    } catch (Exception ex) {
190    // Invalid ID?
191    }
192  0 return null;
193    }
194    });
195    }
196    }