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

File XWikiRepositoryModel.java

 

Coverage histogram

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

Code metrics

12
70
18
4
415
244
25
0.36
3.89
4.5
1.39

Classes

Class Line # Actions
XWikiRepositoryModel 43 62 0% 18 8
0.9036144690.4%
XWikiRepositoryModel.ExtensionSolrField 198 2 0% 3 1
0.833333383.3%
XWikiRepositoryModel.RatingSolrField 212 2 0% 3 3
0.550%
XWikiRepositoryModel.SolrField 227 4 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 11 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.repository.internal;
21   
22    import java.net.URI;
23    import java.net.URISyntaxException;
24    import java.util.ArrayList;
25    import java.util.Collection;
26    import java.util.Collections;
27    import java.util.HashMap;
28    import java.util.List;
29    import java.util.Map;
30   
31    import org.apache.commons.lang3.StringUtils;
32    import org.apache.commons.lang3.exception.ExceptionUtils;
33    import org.slf4j.Logger;
34    import org.slf4j.LoggerFactory;
35    import org.xwiki.extension.Extension;
36    import org.xwiki.extension.RemoteExtension;
37    import org.xwiki.extension.internal.ExtensionFactory;
38    import org.xwiki.extension.rating.RatingExtension;
39    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
40    import org.xwiki.model.EntityType;
41    import org.xwiki.model.reference.EntityReference;
42   
 
43    public class XWikiRepositoryModel
44    {
45    // References
46   
47    public static final String EXTENSION_CLASSNAME = "ExtensionCode.ExtensionClass";
48   
49    public static final String AVERAGERATING_CLASSNAME = "XWiki.AverageRatingsClass";
50   
51    public static final String EXTENSIONVERSION_CLASSNAME = "ExtensionCode.ExtensionVersionClass";
52   
53    public static final String EXTENSIONDEPENDENCY_CLASSNAME = "ExtensionCode.ExtensionDependencyClass";
54   
55    public static final String EXTENSIONPROXY_CLASSNAME = "ExtensionCode.ExtensionProxyClass";
56   
57    public static final EntityReference EXTENSION_CLASSREFERENCE = new EntityReference("ExtensionClass",
58    EntityType.DOCUMENT, new EntityReference("ExtensionCode", EntityType.SPACE));
59   
60    public static final EntityReference EXTENSIONVERSION_CLASSREFERENCE = new EntityReference("ExtensionVersionClass",
61    EntityType.DOCUMENT, new EntityReference("ExtensionCode", EntityType.SPACE));
62   
63    public static final EntityReference EXTENSIONDEPENDENCY_CLASSREFERENCE = new EntityReference(
64    "ExtensionDependencyClass", EntityType.DOCUMENT, new EntityReference("ExtensionCode", EntityType.SPACE));
65   
66    public static final EntityReference EXTENSIONPROXY_CLASSREFERENCE = new EntityReference("ExtensionProxyClass",
67    EntityType.DOCUMENT, new EntityReference("ExtensionCode", EntityType.SPACE));
68   
69    public static final EntityReference EXTENSION_TEMPLATEREFERENCE = new EntityReference("ExtensionTemplate",
70    EntityType.DOCUMENT, new EntityReference("ExtensionCode", EntityType.SPACE));
71   
72    public static final String CONFIGURATION_CLASSNAME = "ExtensionCode.RepositoryConfigClass";
73   
74    public static final EntityReference CONFIGURATION_CLASSREFERENCE = new EntityReference("RepositoryConfigClass",
75    EntityType.DOCUMENT, new EntityReference("ExtensionCode", EntityType.SPACE));
76   
77    public static final EntityReference CONFIGURATION_REFERENCE = new EntityReference("RepositoryConfig",
78    EntityType.DOCUMENT, new EntityReference("ExtensionCode", EntityType.SPACE));
79   
80    // Properties
81   
82    public static final String PROP_EXTENSION_ID = "id";
83   
84    public static final String PROP_EXTENSION_TYPE = "type";
85   
86    public static final String PROP_EXTENSION_LICENSENAME = "licenseName";
87   
88    public static final String PROP_EXTENSION_SUMMARY = "summary";
89   
90    public static final String PROP_EXTENSION_DESCRIPTION = "description";
91   
92    public static final String PROP_EXTENSION_NAME = "name";
93   
94    public static final String PROP_EXTENSION_WEBSITE = "website";
95   
96    public static final String PROP_EXTENSION_AUTHORS = "authors";
97   
98    public static final String PROP_EXTENSION_FEATURES = "features";
99   
100    public static final String PROP_EXTENSION_CATEGORY = "category";
101   
102    /**
103    * @since 8.3RC1
104    */
105    public static final String PROP_EXTENSION_RECOMMENDED = "recommended";
106   
107    public static final String PROP_EXTENSION_ALLOWEDNAMESPACES = "allowednamespaces";
108   
109    public static final String PROP_EXTENSION_ALLOWEDNAMESPACES_EMPTY = "allowednamespaces_empty";
110   
111    public static final String PROP_EXTENSION_SCMURL = "source";
112   
113    public static final String PROP_EXTENSION_SCMCONNECTION = "scmconnection";
114   
115    public static final String PROP_EXTENSION_SCMDEVCONNECTION = "scmdevconnection";
116   
117    /**
118    * @since 7.3M1
119    */
120    public static final String PROP_EXTENSION_ISSUEMANAGEMENT_SYSTEM = "issueManagementSystem";
121   
122    /**
123    * @since 7.3M1
124    */
125    public static final String PROP_EXTENSION_ISSUEMANAGEMENT_URL = "issueManagementURL";
126   
127    public static final String PROP_EXTENSION_PROPERTIES = "properties";
128   
129    public static final String PROP_VERSION_ID = "id";
130   
131    public static final String PROP_VERSION_VERSION = "version";
132   
133    public static final String PROP_VERSION_DOWNLOAD = "download";
134   
135    /**
136    * @since 7.3M1
137    */
138    public static final String PROP_VERSION_FEATURES = "features";
139   
140    /**
141    * @since 7.3M1
142    */
143    public static final String PROP_VERSION_REPOSITORIES = "repositories";
144   
145    public static final String PROP_DEPENDENCY_EXTENSIONVERSION = "extensionVersion";
146   
147    public static final String PROP_DEPENDENCY_ID = "id";
148   
149    public static final String PROP_DEPENDENCY_CONSTRAINT = "constraint";
150   
151    /**
152    * @since 7.3M1
153    */
154    public static final String PROP_DEPENDENCY_REPOSITORIES = PROP_VERSION_REPOSITORIES;
155   
156    public static final String PROP_PROXY_REPOSITORYID = "repositoryId";
157   
158    public static final String PROP_PROXY_REPOSITORYTYPE = "repositoryType";
159   
160    public static final String PROP_PROXY_REPOSITORYURI = "repositoryURI";
161   
162    public static final String PROP_PROXY_AUTOUPDATE = "autoUpdate";
163   
164    public static final String PROP_CONFIGURATION_DEFAULTIDPREFIX = "defaultIdPrefix";
165   
166    public static final String PROP_CONFIGURATION_VALIDTYPEs = "validTypes";
167   
168    public static final String PROP_RATING_TOTALVOTES = "nbvotes";
169   
170    public static final String PROP_RATING_AVERAGEVOTE = "averagevote";
171   
172    // Consolidation
173   
174    public static final String PROP_EXTENSION_LASTVERSION = "lastVersion";
175   
176    public static final String PROP_EXTENSION_VALIDEXTENSION = "validExtension";
177   
178    // Tools
179   
180    /**
181    * @since 8.4RC1
182    */
183    public static final String PROPSUFFIX_EMPTYCOLLECTION = "_empty";
184   
185    // Solr
186   
187    public static final String SOLRPROP_EXTENSION_VALIDEXTENSION =
188    toExtensionClassSolrPropertyName(PROP_EXTENSION_VALIDEXTENSION, "boolean");
189   
190    public static final String SOLR_STRING = "string";
191   
192    public static final String SOLR_INTEGER = "int";
193   
194    public static final String SOLR_BOOLEAN = "boolean";
195   
196    public static final Map<String, SolrField> SOLR_FIELDS = new HashMap<>();
197   
 
198    public static class ExtensionSolrField extends SolrField
199    {
 
200  57 toggle public ExtensionSolrField(String name, Float boostValue)
201    {
202  57 this(name, SOLR_STRING, boostValue);
203    }
204   
 
205  60 toggle public ExtensionSolrField(String name, String type, Float boostValue)
206    {
207  60 super(toExtensionClassSolrPropertyName(name, type), toExtensionClassSolrOrderPropertyName(name, type),
208  60 type != null ? toExtensionClassSolrPropertyName(name) : null, boostValue);
209    }
210    }
211   
 
212    public static class RatingSolrField extends SolrField
213    {
 
214  0 toggle public RatingSolrField(String name, Float boostValue)
215    {
216  0 this(name, SOLR_STRING, boostValue);
217    }
218   
 
219  6 toggle public RatingSolrField(String name, String type, Float boostValue)
220    {
221  6 super(toAverageRatingClassSolrPropertyName(name, type),
222    toAverageRatingClassSolrOrderPropertyName(name, type),
223  6 type != null ? toAverageRatingClassSolrPropertyName(name) : null, boostValue);
224    }
225    }
226   
 
227    public static class SolrField
228    {
229    public final String name;
230   
231    public final String orderName;
232   
233    public final String boostName;
234   
235    public final Float boostValue;
236   
 
237  69 toggle public SolrField(String name, String orderName, String boostName, Float boostValue)
238    {
239  69 this.name = name;
240  69 this.orderName = orderName;
241  69 this.boostName = boostName;
242  69 this.boostValue = boostValue;
243    }
244    }
245   
 
246  3 toggle static {
247  3 SOLR_FIELDS.put(Extension.FIELD_ID, new ExtensionSolrField(PROP_EXTENSION_ID, 10.0f));
248  3 SOLR_FIELDS.put(Extension.FIELD_FEATURE, new ExtensionSolrField(PROP_EXTENSION_FEATURES, 9.0f));
249  3 SOLR_FIELDS.put(Extension.FIELD_FEATURES, SOLR_FIELDS.get(Extension.FIELD_FEATURE));
250  3 SOLR_FIELDS.put(Extension.FIELD_NAME, new ExtensionSolrField(PROP_EXTENSION_NAME, 8.0f));
251  3 SOLR_FIELDS.put(Extension.FIELD_TYPE, new ExtensionSolrField(PROP_EXTENSION_TYPE, 8.0f));
252  3 SOLR_FIELDS.put(Extension.FIELD_CATEGORY, new ExtensionSolrField(PROP_EXTENSION_CATEGORY, 7.0f));
253  3 SOLR_FIELDS.put(Extension.FIELD_SUMMARY, new ExtensionSolrField(PROP_EXTENSION_SUMMARY, 6.0f));
254   
255    // We only search in the description but we don't retrieve it (because it's not stored in a stable field)
256  3 SOLR_FIELDS.put(Extension.FIELD_DESCRIPTION, new SolrField(PROP_EXTENSION_DESCRIPTION, null, null, 5.0f));
257   
258    // Not very interesting for fulltext search
259  3 SOLR_FIELDS.put(Extension.FIELD_AUTHOR, new ExtensionSolrField(PROP_EXTENSION_AUTHORS, null));
260  3 SOLR_FIELDS.put(Extension.FIELD_AUTHORS, SOLR_FIELDS.get(Extension.FIELD_AUTHOR));
261  3 SOLR_FIELDS.put(Extension.FIELD_VERSION, new ExtensionSolrField(PROP_EXTENSION_LASTVERSION, null));
262  3 SOLR_FIELDS.put(Extension.FIELD_LICENSE, new ExtensionSolrField(PROP_EXTENSION_LICENSENAME, null));
263  3 SOLR_FIELDS.put(Extension.FIELD_LICENSES, SOLR_FIELDS.get(Extension.FIELD_LICENSE));
264  3 SOLR_FIELDS.put(Extension.FIELD_SCM, new ExtensionSolrField(PROP_EXTENSION_SCMURL, null));
265  3 SOLR_FIELDS.put(PROP_EXTENSION_SCMCONNECTION, new ExtensionSolrField(PROP_EXTENSION_SCMCONNECTION, null));
266  3 SOLR_FIELDS.put(PROP_EXTENSION_SCMDEVCONNECTION, new ExtensionSolrField(PROP_EXTENSION_SCMDEVCONNECTION, null));
267  3 SOLR_FIELDS.put(PROP_EXTENSION_ISSUEMANAGEMENT_SYSTEM,
268    new ExtensionSolrField(PROP_EXTENSION_ISSUEMANAGEMENT_SYSTEM, null));
269  3 SOLR_FIELDS.put(PROP_EXTENSION_ISSUEMANAGEMENT_URL,
270    new ExtensionSolrField(PROP_EXTENSION_ISSUEMANAGEMENT_URL, null));
271  3 SOLR_FIELDS.put(Extension.FIELD_WEBSITE, new ExtensionSolrField(PROP_EXTENSION_WEBSITE, null));
272  3 SOLR_FIELDS.put(Extension.FIELD_NAMESPACES, new ExtensionSolrField(PROP_EXTENSION_ALLOWEDNAMESPACES, null));
273  3 SOLR_FIELDS.put(PROP_EXTENSION_ALLOWEDNAMESPACES_EMPTY,
274    new ExtensionSolrField(PROP_EXTENSION_ALLOWEDNAMESPACES_EMPTY, null));
275  3 SOLR_FIELDS.put(Extension.FIELD_ALLOWEDNAMESPACE, SOLR_FIELDS.get(Extension.FIELD_NAMESPACES));
276  3 SOLR_FIELDS.put(Extension.FIELD_ALLOWEDNAMESPACES, SOLR_FIELDS.get(Extension.FIELD_NAMESPACES));
277  3 SOLR_FIELDS.put(Extension.FIELD_REPOSITORIES, new ExtensionSolrField(PROP_VERSION_REPOSITORIES, null));
278  3 SOLR_FIELDS.put(Extension.FIELD_PROPERTIES, new ExtensionSolrField(PROP_EXTENSION_PROPERTIES, null));
279   
280    // Rating
281  3 SOLR_FIELDS.put(RatingExtension.FIELD_TOTAL_VOTES, new RatingSolrField(PROP_RATING_TOTALVOTES, "int", null));
282  3 SOLR_FIELDS.put("votes", SOLR_FIELDS.get(PROP_RATING_TOTALVOTES));
283  3 SOLR_FIELDS.put(RatingExtension.FIELD_AVERAGE_VOTE,
284    new RatingSolrField(PROP_RATING_AVERAGEVOTE, "float", null));
285  3 SOLR_FIELDS.put("vote", SOLR_FIELDS.get(PROP_RATING_AVERAGEVOTE));
286   
287    // Recommended
288  3 SOLR_FIELDS.put(RemoteExtension.FIELD_RECOMMENDED,
289    new ExtensionSolrField(PROP_EXTENSION_RECOMMENDED, SOLR_BOOLEAN, null));
290   
291    // Fields not stored
292    // Extension.FIELD_REPOSITORY
293    }
294   
295    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiRepositoryModel.class);
296   
 
297  183 toggle public static String toExtensionClassSolrPropertyName(String propertyName)
298    {
299  183 return "property." + EXTENSION_CLASSNAME + '.' + propertyName;
300    }
301   
 
302  63 toggle public static String toExtensionClassSolrPropertyName(String propertyName, String type)
303    {
304  63 return toExtensionClassSolrPropertyName(propertyName) + '_' + type;
305    }
306   
 
307  60 toggle public static String toExtensionClassSolrOrderPropertyName(String propertyName, String type)
308    {
309  60 return toExtensionClassSolrPropertyName(propertyName) + "_sort" + StringUtils.capitalize(type);
310    }
311   
 
312  18 toggle public static String toAverageRatingClassSolrPropertyName(String propertyName)
313    {
314  18 return "property." + AVERAGERATING_CLASSNAME + '.' + propertyName;
315    }
316   
 
317  6 toggle public static String toAverageRatingClassSolrPropertyName(String propertyName, String type)
318    {
319  6 return toAverageRatingClassSolrPropertyName(propertyName) + '_' + type;
320    }
321   
 
322  6 toggle public static String toAverageRatingClassSolrOrderPropertyName(String propertyName, String type)
323    {
324  6 return toAverageRatingClassSolrPropertyName(propertyName) + "_sort" + StringUtils.capitalize(type);
325    }
326   
 
327  260 toggle public static String toSolrField(String restField)
328    {
329  260 SolrField field = SOLR_FIELDS.get(restField);
330   
331  260 if (field == null) {
332  0 return null;
333    }
334   
335  260 return field.name;
336    }
337   
 
338  74 toggle public static String toSolrOrderField(String restField)
339    {
340  74 SolrField field = SOLR_FIELDS.get(restField);
341   
342  74 if (field == null) {
343  0 return null;
344    }
345   
346  74 return field.orderName;
347    }
348   
349    /**
350    * @since 8.4
351    */
 
352  3 toggle public static List<ExtensionRepositoryDescriptor> toRepositoryDescriptors(Collection<String> stringRepositories,
353    ExtensionFactory factory)
354    {
355  3 if (stringRepositories == null) {
356  0 return Collections.emptyList();
357    }
358   
359  3 List<ExtensionRepositoryDescriptor> reposiories = new ArrayList<>(stringRepositories.size());
360   
361  3 for (String stringRepository : stringRepositories) {
362  3 try {
363  3 reposiories.add(toRepositoryDescriptor(stringRepository, factory));
364    } catch (URISyntaxException e) {
365  0 LOGGER.warn("Failed to parse repository descriptor [{}]", stringRepository,
366    ExceptionUtils.getRootCauseMessage(e));
367    }
368    }
369   
370  3 return reposiories;
371    }
372   
373    /**
374    * @since 8.4
375    */
 
376  9 toggle public static ExtensionRepositoryDescriptor toRepositoryDescriptor(String repository, ExtensionFactory factory)
377    throws URISyntaxException
378    {
379  9 int index;
380   
381    // Id
382  9 String id = repository.substring(0, index = repository.indexOf(':'));
383   
384    // Type
385  9 String type = repository.substring(index + 1, index = repository.indexOf(':', index + 1));
386   
387    // URI
388  9 URI uri = new URI(repository.substring(index + 1, repository.length()));
389   
390  9 return factory.getExtensionRepositoryDescriptor(id, type, uri);
391    }
392   
393    /**
394    * @since 7.3M1
395    */
 
396  24 toggle public static List<String> toStringList(Collection<ExtensionRepositoryDescriptor> repositories)
397    {
398  24 List<String> stringRepositories = new ArrayList<>(repositories.size());
399   
400  24 for (ExtensionRepositoryDescriptor repository : repositories) {
401  9 stringRepositories.add(toString(repository));
402    }
403   
404  24 return stringRepositories;
405    }
406   
407    /**
408    * @since 7.3M1
409    */
 
410  9 toggle public static String toString(ExtensionRepositoryDescriptor repository)
411    {
412  9 return repository != null ? StringUtils.defaultString(repository.getId()) + ':' + repository.getType() + ':'
413    + repository.getURI().toString() : null;
414    }
415    }