1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.wiki.internal.descriptor; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.HashSet; |
26 |
|
import java.util.List; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Provider; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.wiki.descriptor.WikiDescriptor; |
35 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
36 |
|
import org.xwiki.wiki.internal.descriptor.builder.WikiDescriptorBuilder; |
37 |
|
import org.xwiki.wiki.internal.descriptor.builder.WikiDescriptorBuilderException; |
38 |
|
import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper; |
39 |
|
import org.xwiki.wiki.internal.manager.WikiDescriptorCache; |
40 |
|
import org.xwiki.wiki.manager.WikiManagerException; |
41 |
|
|
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
44 |
|
import com.xpn.xwiki.objects.BaseObject; |
45 |
|
|
46 |
|
|
47 |
|
@link |
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
|
|
| 95.1% |
Uncovered Elements: 5 (102) |
Complexity: 32 |
Complexity Density: 0.54 |
|
54 |
|
public class DefaultWikiDescriptorManager implements WikiDescriptorManager |
55 |
|
{ |
56 |
|
@Inject |
57 |
|
@Named("readonly") |
58 |
|
private Provider<XWikiContext> xcontextProvider; |
59 |
|
|
60 |
|
@Inject |
61 |
|
private WikiDescriptorCache cache; |
62 |
|
|
63 |
|
@Inject |
64 |
|
private Provider<WikiDescriptorDocumentHelper> descriptorDocumentHelperProvider; |
65 |
|
|
66 |
|
@Inject |
67 |
|
private Provider<WikiDescriptorBuilder> wikiDescriptorBuilderProvider; |
68 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
69 |
49 |
@Override... |
70 |
|
public Collection<WikiDescriptor> getAll() throws WikiManagerException |
71 |
|
{ |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
49 |
Collection<String> wikiIds = getAllIds(); |
79 |
|
|
80 |
49 |
List<WikiDescriptor> result = new ArrayList<WikiDescriptor>(wikiIds.size()); |
81 |
|
|
82 |
49 |
for (String wikiId : wikiIds) { |
83 |
|
|
84 |
68 |
WikiDescriptor descriptor = getById(wikiId); |
85 |
|
|
86 |
|
|
87 |
68 |
if (descriptor != null) { |
88 |
68 |
result.add(descriptor); |
89 |
|
} |
90 |
|
} |
91 |
|
|
92 |
49 |
return result; |
93 |
|
} |
94 |
|
|
|
|
| 95.2% |
Uncovered Elements: 1 (21) |
Complexity: 4 |
Complexity Density: 0.24 |
|
95 |
417 |
@Override... |
96 |
|
public Collection<String> getAllIds() throws WikiManagerException |
97 |
|
{ |
98 |
417 |
Collection<String> wikiIds = this.cache.getWikiIds(); |
99 |
|
|
100 |
418 |
if (wikiIds == null) { |
101 |
51 |
List<String> documentNames; |
102 |
51 |
try { |
103 |
51 |
documentNames = this.descriptorDocumentHelperProvider.get().getAllXWikiServerClassDocumentNames(); |
104 |
|
} catch (Exception e) { |
105 |
0 |
throw new WikiManagerException("Failed to get wiki ids", e); |
106 |
|
} |
107 |
|
|
108 |
51 |
wikiIds = new HashSet<String>(documentNames.size()); |
109 |
|
|
110 |
51 |
boolean foundMainWiki = false; |
111 |
|
|
112 |
51 |
XWikiContext xcontext = this.xcontextProvider.get(); |
113 |
|
|
114 |
51 |
for (String documentName : documentNames) { |
115 |
61 |
String wikId = this.descriptorDocumentHelperProvider.get().getWikiIdFromDocumentFullname(documentName); |
116 |
|
|
117 |
61 |
wikiIds.add(wikId); |
118 |
|
|
119 |
61 |
foundMainWiki |= xcontext.isMainWiki(wikId); |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
51 |
if (!foundMainWiki) { |
124 |
7 |
wikiIds.add(getMainWikiId()); |
125 |
|
} |
126 |
|
|
127 |
51 |
this.cache.setWikiIds(Collections.unmodifiableCollection(wikiIds)); |
128 |
|
} |
129 |
|
|
130 |
418 |
return wikiIds; |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 6 |
Complexity Density: 0.75 |
|
133 |
11593 |
@Override... |
134 |
|
public WikiDescriptor getByAlias(String wikiAlias) throws WikiManagerException |
135 |
|
{ |
136 |
11593 |
WikiDescriptor descriptor = cache.getFromAlias(wikiAlias); |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
11593 |
if (descriptor == null) { |
146 |
2 |
XWikiDocument document = descriptorDocumentHelperProvider.get().findXWikiServerClassDocument(wikiAlias); |
147 |
2 |
if (document != null) { |
148 |
|
|
149 |
1 |
descriptor = buildDescriptorFromDocument(document); |
150 |
|
} |
151 |
|
|
152 |
2 |
if (descriptor == null) { |
153 |
|
|
154 |
1 |
cache.addFromAlias(wikiAlias, DefaultWikiDescriptor.VOID); |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
11592 |
return descriptor != DefaultWikiDescriptor.VOID && descriptor != null ? descriptor.clone() : null; |
159 |
|
} |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 7 |
Complexity Density: 0.7 |
|
161 |
8144 |
@Override... |
162 |
|
public WikiDescriptor getById(String wikiId) throws WikiManagerException |
163 |
|
{ |
164 |
8145 |
WikiDescriptor descriptor = cache.getFromId(wikiId); |
165 |
|
|
166 |
8148 |
if (descriptor == null) { |
167 |
|
|
168 |
5 |
XWikiDocument document = descriptorDocumentHelperProvider.get().getDocumentFromWikiId(wikiId); |
169 |
|
|
170 |
5 |
if (!document.isNew()) { |
171 |
|
|
172 |
3 |
descriptor = buildDescriptorFromDocument(document); |
173 |
2 |
} else if (getMainWikiId().equals(wikiId)) { |
174 |
|
|
175 |
1 |
descriptor = new WikiDescriptor(wikiId, "localhost"); |
176 |
|
} |
177 |
|
|
178 |
5 |
if (descriptor == null) { |
179 |
|
|
180 |
1 |
cache.addFromId(wikiId, DefaultWikiDescriptor.VOID); |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
8147 |
return descriptor != DefaultWikiDescriptor.VOID && descriptor != null ? descriptor.clone() : null; |
185 |
|
} |
186 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
187 |
38 |
@Override... |
188 |
|
public boolean exists(String wikiId) throws WikiManagerException |
189 |
|
{ |
190 |
38 |
return getAllIds().contains(wikiId); |
191 |
|
} |
192 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
193 |
4 |
@Override... |
194 |
|
public void saveDescriptor(WikiDescriptor descriptor) throws WikiManagerException |
195 |
|
{ |
196 |
4 |
try { |
197 |
4 |
this.wikiDescriptorBuilderProvider.get().save(descriptor); |
198 |
|
} catch (WikiDescriptorBuilderException e) { |
199 |
0 |
throw new WikiManagerException( |
200 |
|
String.format("Unable to save wiki descriptor for [%s].", descriptor.getId()), e); |
201 |
|
} |
202 |
|
} |
203 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
204 |
5 |
@Override... |
205 |
|
public WikiDescriptor getMainWikiDescriptor() throws WikiManagerException |
206 |
|
{ |
207 |
5 |
return getById(getMainWikiId()); |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
210 |
10327 |
@Override... |
211 |
|
public String getMainWikiId() |
212 |
|
{ |
213 |
10320 |
XWikiContext xcontext = this.xcontextProvider.get(); |
214 |
|
|
215 |
10308 |
return xcontext != null ? xcontext.getMainXWiki() : "xwiki"; |
216 |
|
} |
217 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
218 |
3634124 |
@Override... |
219 |
|
public String getCurrentWikiId() |
220 |
|
{ |
221 |
3634119 |
XWikiContext xcontext = this.xcontextProvider.get(); |
222 |
|
|
223 |
3634224 |
return xcontext != null ? xcontext.getWikiId() : null; |
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
226 |
3382 |
@Override... |
227 |
|
public WikiDescriptor getCurrentWikiDescriptor() throws WikiManagerException |
228 |
|
{ |
229 |
3382 |
return getById(getCurrentWikiId()); |
230 |
|
} |
231 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
232 |
4 |
private DefaultWikiDescriptor buildDescriptorFromDocument(XWikiDocument document)... |
233 |
|
{ |
234 |
4 |
DefaultWikiDescriptor descriptor = null; |
235 |
4 |
List<BaseObject> serverClassObjects = document.getXObjects(DefaultWikiDescriptor.SERVER_CLASS); |
236 |
4 |
if (serverClassObjects != null && !serverClassObjects.isEmpty()) { |
237 |
4 |
descriptor = this.wikiDescriptorBuilderProvider.get().buildDescriptorObject(serverClassObjects, document); |
238 |
|
|
239 |
4 |
if (descriptor != null) { |
240 |
4 |
cache.add(descriptor); |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
4 |
return descriptor; |
245 |
|
} |
246 |
|
} |