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

File XWikiHibernateRecycleBinStore.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
27
13
2
191
123
14
0.52
2.08
6.5
1.08

Classes

Class Line # Actions
XWikiHibernateRecycleBinStore 49 16 0% 11 0
1.0100%
XWikiHibernateRecycleBinStore.DeletedDocumentsHibernateCallback 54 11 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

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.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.XWikiDeletedDocument;
39    import com.xpn.xwiki.doc.XWikiDocument;
40   
41    /**
42    * Realization of {@link XWikiRecycleBinStoreInterface} for Hibernate store.
43    *
44    * @version $Id: a4bb1765f0c4bde6712003f703051f74ef7534f2 $
45    */
46    @Component
47    @Named("hibernate")
48    @Singleton
 
49    public class XWikiHibernateRecycleBinStore extends XWikiHibernateBaseStore implements XWikiRecycleBinStoreInterface
50    {
51    /**
52    * {@link HibernateCallback} used to retrieve from the recycle bin store the deleted versions of a document.
53    */
 
54    private static class DeletedDocumentsHibernateCallback implements HibernateCallback<XWikiDeletedDocument[]>
55    {
56    /**
57    * The document whose versions are retrieved from the recycle bin store.
58    */
59    private XWikiDocument document;
60   
61    /**
62    * Creates a new call-back for the given document.
63    *
64    * @param document the document whose deleted versions you want to retrieve from the recycle bin store
65    */
 
66  307 toggle DeletedDocumentsHibernateCallback(XWikiDocument document)
67    {
68  307 this.document = document;
69    }
70   
 
71  303 toggle @Override
72    public XWikiDeletedDocument[] doInHibernate(Session session) throws HibernateException, XWikiException
73    {
74  305 Criteria c = session.createCriteria(XWikiDeletedDocument.class);
75  305 c.add(Restrictions.eq("fullName", this.document.getFullName()));
76   
77    // Note: We need to support databases who treats empty strings as NULL like Oracle. For those checking
78    // for equality when the string is empty is not going to work and thus we need to handle the special
79    // empty case separately.
80  300 String language = this.document.getLanguage();
81  302 if (StringUtils.isEmpty(language)) {
82  305 c.add(Restrictions.or(Restrictions.eq(LANGUAGE_PROPERTY_NAME, ""),
83    Restrictions.isNull(LANGUAGE_PROPERTY_NAME)));
84    } else {
85  1 c.add(Restrictions.eq(LANGUAGE_PROPERTY_NAME, language));
86    }
87   
88  306 c.addOrder(Order.desc("date"));
89  301 @SuppressWarnings("unchecked")
90    List<XWikiDeletedDocument> deletedVersions = c.list();
91  307 XWikiDeletedDocument[] result = new XWikiDeletedDocument[deletedVersions.size()];
92  304 return deletedVersions.toArray(result);
93    }
94    }
95   
96    /**
97    * Name of the language property in the Hibernate mapping.
98    */
99    private static final String LANGUAGE_PROPERTY_NAME = "language";
100   
101    /**
102    * @param context used for environment
103    * @deprecated 1.6M1. Use ComponentManager.lookup(XWikiRecycleBinStoreInterface.class) instead.
104    */
 
105  7 toggle @Deprecated
106    public XWikiHibernateRecycleBinStore(XWikiContext context)
107    {
108  7 super(context.getWiki(), context);
109    }
110   
111    /**
112    * Empty constructor needed for component manager.
113    */
 
114  89 toggle public XWikiHibernateRecycleBinStore()
115    {
116    }
117   
 
118  153 toggle @Override
119    public void saveToRecycleBin(XWikiDocument doc, String deleter, Date date, XWikiContext inputxcontext,
120    boolean bTransaction) throws XWikiException
121    {
122  153 XWikiContext context = getXWikiContext(inputxcontext);
123   
124  153 final XWikiDeletedDocument trashdoc = new XWikiDeletedDocument(doc, deleter, date, context);
125   
126  153 executeWrite(context, new HibernateCallback<Object>()
127    {
 
128  153 toggle @Override
129    public Object doInHibernate(Session session) throws HibernateException
130    {
131  153 session.save(trashdoc);
132  153 return null;
133    }
134    });
135    }
136   
 
137  3 toggle @Override
138    public XWikiDocument restoreFromRecycleBin(final XWikiDocument doc, final long index,
139    final XWikiContext inputxcontext, boolean bTransaction) throws XWikiException
140    {
141  3 XWikiContext context = getXWikiContext(inputxcontext);
142   
143  3 return executeRead(context, new HibernateCallback<XWikiDocument>()
144    {
 
145  3 toggle @Override
146    public XWikiDocument doInHibernate(Session session) throws HibernateException, XWikiException
147    {
148  3 XWikiDeletedDocument trashdoc =
149    (XWikiDeletedDocument) session.load(XWikiDeletedDocument.class, Long.valueOf(index));
150  3 return trashdoc.restoreDocument(null, context);
151    }
152    });
153    }
154   
 
155  4 toggle @Override
156    public XWikiDeletedDocument getDeletedDocument(XWikiDocument doc, final long index, XWikiContext context,
157    boolean bTransaction) throws XWikiException
158    {
159  4 return executeRead(context, new HibernateCallback<XWikiDeletedDocument>()
160    {
 
161  4 toggle @Override
162    public XWikiDeletedDocument doInHibernate(Session session) throws HibernateException, XWikiException
163    {
164  4 return (XWikiDeletedDocument) session.get(XWikiDeletedDocument.class, Long.valueOf(index));
165    }
166    });
167    }
168   
 
169  307 toggle @Override
170    public XWikiDeletedDocument[] getAllDeletedDocuments(XWikiDocument doc, XWikiContext context, boolean bTransaction)
171    throws XWikiException
172    {
173  307 return executeRead(context, new DeletedDocumentsHibernateCallback(doc));
174    }
175   
 
176  4 toggle @Override
177    public void deleteFromRecycleBin(XWikiDocument doc, final long index, XWikiContext context, boolean bTransaction)
178    throws XWikiException
179    {
180  4 executeWrite(context, new HibernateCallback<Object>()
181    {
 
182  4 toggle @Override
183    public Object doInHibernate(Session session) throws HibernateException, XWikiException
184    {
185  4 session.createQuery("delete from " + XWikiDeletedDocument.class.getName() + " where id=?")
186    .setLong(0, index).executeUpdate();
187  4 return null;
188    }
189    });
190    }
191    }