1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.internal.descriptor.document

File DefaultWikiDescriptorDocumentHelper.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

10
46
9
1
195
140
18
0.39
5.11
9
2

Classes

Class Line # Actions
DefaultWikiDescriptorDocumentHelper 55 46 0% 18 41
0.3692307836.9%
 

Contributing tests

This file is covered by 3 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 org.xwiki.wiki.internal.descriptor.document;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Provider;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.query.Query;
37    import org.xwiki.query.QueryException;
38    import org.xwiki.query.QueryFilter;
39    import org.xwiki.query.QueryManager;
40    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
41    import org.xwiki.wiki.manager.WikiManagerException;
42   
43    import com.xpn.xwiki.XWiki;
44    import com.xpn.xwiki.XWikiContext;
45    import com.xpn.xwiki.XWikiException;
46    import com.xpn.xwiki.doc.XWikiDocument;
47   
48    /**
49    * Component to load and resolve wiki descriptor documents.
50    * @version $Id: ee722feb12b6d9aeb3f745544e4c3b4e594472b4 $
51    * @since 5.3M2
52    */
53    @Component
54    @Singleton
 
55    public class DefaultWikiDescriptorDocumentHelper implements WikiDescriptorDocumentHelper
56    {
57    @Inject
58    private Provider<XWikiContext> xcontextProvider;
59   
60    @Inject
61    private QueryManager queryManager;
62   
63    @Inject
64    private Provider<WikiDescriptorManager> wikiDescriptorManagerProvider;
65   
66    @Inject
67    @Named("current")
68    private DocumentReferenceResolver<String> documentReferenceResolver;
69   
70    @Inject
71    private ComponentManager componentManager;
72   
 
73  36 toggle @Override
74    public DocumentReference getDocumentReferenceFromId(String wikiId)
75    {
76  36 if (wikiId == null) {
77  0 throw new IllegalArgumentException("Wiki id cannot be null");
78    }
79   
80  36 WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
81  36 return new DocumentReference(wikiDescriptorManager.getMainWikiId(),
82    XWiki.SYSTEM_SPACE, String.format("XWikiServer%s", StringUtils.capitalize(wikiId)));
83    }
84   
 
85  70 toggle @Override
86    public String getWikiIdFromDocumentReference(DocumentReference descriptorDocumentReference)
87    {
88  70 String docName = descriptorDocumentReference.getName();
89  70 return StringUtils.removeStart(docName, "XWikiServer").toLowerCase();
90    }
91   
 
92  61 toggle @Override
93    public String getWikiIdFromDocumentFullname(String descriptorDocumentFullname)
94    {
95  61 return StringUtils.removeStart(descriptorDocumentFullname, "XWiki.XWikiServer").toLowerCase();
96    }
97   
 
98  35 toggle @Override
99    public XWikiDocument getDocumentFromWikiId(String wikiId) throws WikiManagerException
100    {
101  35 return getDocument(getDocumentReferenceFromId(wikiId));
102    }
103   
 
104  0 toggle @Override
105    public DocumentReference findXWikiServerClassDocumentReference(String wikiAlias) throws WikiManagerException
106    {
107  0 if (wikiAlias == null) {
108  0 throw new IllegalArgumentException("Wiki alias cannot be null");
109    }
110   
111  0 WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
112   
113  0 DocumentReference result = null;
114   
115  0 try {
116  0 Query query = this.queryManager.createQuery(
117    "where doc.object(XWiki.XWikiServerClass).server = :wikiAlias and doc.name like 'XWikiServer%'",
118    Query.XWQL);
119  0 query.bindValue("wikiAlias", wikiAlias);
120  0 query.setWiki(wikiDescriptorManager.getMainWikiId());
121  0 List<String> documentNames = query.execute();
122   
123    // Resolve the document name into a references
124  0 if (documentNames != null && !documentNames.isEmpty()) {
125  0 result = documentReferenceResolver.resolve(documentNames.get(0));
126    }
127    } catch (QueryException e) {
128  0 throw new WikiManagerException(String.format(
129    "Failed to locate XWiki.XWikiServerClass document for wiki alias [%s]", wikiAlias), e);
130    }
131   
132  0 return result;
133    }
134   
 
135  0 toggle @Override
136    public XWikiDocument findXWikiServerClassDocument(String wikiAlias) throws WikiManagerException
137    {
138  0 XWikiDocument document = null;
139   
140  0 DocumentReference documentReference = findXWikiServerClassDocumentReference(wikiAlias);
141  0 if (documentReference != null) {
142  0 document = getDocument(documentReference);
143    }
144   
145  0 return document;
146    }
147   
 
148  0 toggle @Override
149    public List<XWikiDocument> getAllXWikiServerClassDocument() throws WikiManagerException
150    {
151  0 WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
152   
153  0 List<XWikiDocument> result = new ArrayList<XWikiDocument>();
154   
155  0 List<String> documentNames = getAllXWikiServerClassDocumentNames();
156  0 if (documentNames != null) {
157  0 WikiReference mainWikiReference = new WikiReference(wikiDescriptorManager.getMainWikiId());
158  0 for (String documentName : documentNames) {
159  0 result.add(getDocument(documentReferenceResolver.resolve(documentName, mainWikiReference)));
160    }
161    }
162   
163  0 return result;
164    }
165   
 
166  50 toggle @Override
167    public List<String> getAllXWikiServerClassDocumentNames() throws WikiManagerException
168    {
169  50 WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
170   
171  50 try {
172  50 Query query = this.queryManager.createQuery(
173    "from doc.object(XWiki.XWikiServerClass) as descriptor where doc.name like 'XWikiServer%' "
174    + "and doc.fullName <> 'XWiki.XWikiServerClassTemplate'",
175    Query.XWQL);
176  50 query.setWiki(wikiDescriptorManager.getMainWikiId());
177  50 query.addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "unique"));
178  50 return query.execute();
179    } catch (Exception e) {
180  0 throw new WikiManagerException("Failed to locate XWiki.XWikiServerClass documents", e);
181    }
182    }
183   
 
184  35 toggle private XWikiDocument getDocument(DocumentReference reference) throws WikiManagerException
185    {
186  35 XWikiContext context = xcontextProvider.get();
187  35 com.xpn.xwiki.XWiki xwiki = context.getWiki();
188  35 try {
189  35 return xwiki.getDocument(reference, context);
190    } catch (XWikiException e) {
191  0 throw new WikiManagerException(String.format(
192    "Failed to get document [%s] containing a XWiki.XWikiServerClass object", reference), e);
193    }
194    }
195    }