1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.doc

File DatabaseDocumentRevisionProvider.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

12
24
3
1
107
65
10
0.42
8
3
3.33

Classes

Class Line # Actions
DatabaseDocumentRevisionProvider 45 24 0% 10 7
0.8205128382.1%
 

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.internal.doc;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Provider;
25    import javax.inject.Singleton;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.model.reference.DocumentReference;
30   
31    import com.xpn.xwiki.XWikiContext;
32    import com.xpn.xwiki.XWikiException;
33    import com.xpn.xwiki.doc.DocumentRevisionProvider;
34    import com.xpn.xwiki.doc.XWikiDocument;
35   
36    /**
37    * Get document revisions from the database.
38    *
39    * @version $Id: dcf0038206bca5bbab2639c5773af3ae3f40bf03 $
40    * @since 9.3RC1
41    */
42    @Component
43    @Named("database")
44    @Singleton
 
45    public class DatabaseDocumentRevisionProvider implements DocumentRevisionProvider
46    {
47    @Inject
48    private Provider<XWikiContext> xcontextProvider;
49   
 
50  60 toggle @Override
51    public XWikiDocument getRevision(DocumentReference reference, String revision) throws XWikiException
52    {
53  60 if (StringUtils.isEmpty(revision)) {
54  9 return null;
55    }
56   
57  51 XWikiContext xcontext = this.xcontextProvider.get();
58   
59  51 XWikiDocument document = xcontext.getWiki().getDocument(reference, xcontext);
60   
61  51 return getRevision(document, revision);
62    }
63   
 
64  51 toggle @Override
65    public XWikiDocument getRevision(XWikiDocument document, String revision) throws XWikiException
66    {
67  51 if (StringUtils.isEmpty(revision)) {
68  0 return null;
69    }
70   
71  51 XWikiContext xcontext = this.xcontextProvider.get();
72   
73  51 XWikiDocument newdoc;
74   
75  51 if (revision.equals(document.getVersion())) {
76  4 newdoc = document;
77  47 } else if (xcontext.getWiki().hasVersioning(xcontext)) {
78  47 newdoc = loadRevision(document, revision, xcontext);
79    } else {
80  0 newdoc = null;
81    }
82   
83  45 return newdoc;
84    }
85   
 
86  47 toggle private XWikiDocument loadRevision(XWikiDocument document, String revision, XWikiContext xcontext)
87    throws XWikiException
88    {
89  47 String database = xcontext.getWikiId();
90  47 try {
91  47 if (document.getDocumentReference().getWikiReference().getName() != null) {
92  47 xcontext.setWikiId(document.getDocumentReference().getWikiReference().getName());
93    }
94   
95  47 return xcontext.getWiki().getVersioningStore().loadXWikiDoc(document, revision, xcontext);
96    } catch (XWikiException e) {
97    // If the errot is that the version does not exist return null
98  6 if (e.getCode() != XWikiException.ERROR_XWIKI_STORE_HIBERNATE_UNEXISTANT_VERSION) {
99  0 throw e;
100    }
101    } finally {
102  47 xcontext.setWikiId(database);
103    }
104   
105  6 return null;
106    }
107    }