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

File DefaultRatingsConfiguration.java

 

Coverage histogram

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

Code metrics

12
25
3
1
133
78
14
0.56
8.33
3
4.67

Classes

Class Line # Actions
DefaultRatingsConfiguration 51 25 0% 14 7
0.82582.5%
 

Contributing tests

This file is covered by 4 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.ratings.internal;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24    import javax.inject.Singleton;
25   
26    import org.slf4j.Logger;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.model.EntityType;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.EntityReference;
31    import org.xwiki.model.reference.SpaceReference;
32    import org.xwiki.ratings.RatingsConfiguration;
33    import org.xwiki.ratings.RatingsManager;
34    import org.xwiki.stability.Unstable;
35   
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.objects.BaseProperty;
40   
41    /**
42    * Provides Ratings configuration.
43    *
44    * @see RatingsConfiguration
45    * @version $Id: 18099d371ceb52982c402dc9a56dc1739728c14f $
46    * @since 8.2.1
47    */
48    @Component
49    @Singleton
50    @Unstable
 
51    public class DefaultRatingsConfiguration implements RatingsConfiguration
52    {
53    @Inject
54    private Logger logger;
55   
56    @Inject
57    private Provider<XWikiContext> xcontextProvider;
58   
59    /**
60    * Get document.
61    *
62    * @param reference the reference for which to return the document
63    * @return the document
64    */
 
65  227 toggle public XWikiDocument getDocument(EntityReference reference)
66    {
67  227 XWikiContext context = xcontextProvider.get();
68  227 try
69    {
70  227 return context.getWiki().getDocument(reference, context);
71    } catch (XWikiException e) {
72  0 logger.error("Failed to retrieve the document for the reference [{}].", reference, e);
73  0 return null;
74    }
75    }
76   
77    /**
78    * Get configuration document.
79    *
80    * @param documentReference the documentReference for which to return the configuration document
81    * @return the configuration document
82    */
 
83  117 toggle public XWikiDocument getConfigurationDocument(DocumentReference documentReference)
84    {
85  117 SpaceReference lastSpaceReference = documentReference.getLastSpaceReference();
86  206 while (lastSpaceReference.getType() == EntityType.SPACE) {
87  206 DocumentReference configurationDocumentReference =
88    new DocumentReference(RatingsManager.RATINGS_CONFIG_SPACE_PAGE, lastSpaceReference);
89  206 XWikiDocument spaceConfigurationDocument = getDocument((EntityReference) configurationDocumentReference);
90  206 if (spaceConfigurationDocument != null
91    && spaceConfigurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE) != null) {
92  96 return spaceConfigurationDocument;
93    }
94  110 if (lastSpaceReference.getParent().getType() == EntityType.SPACE) {
95  89 lastSpaceReference = new SpaceReference(lastSpaceReference.getParent());
96    } else {
97  21 break;
98    }
99    }
100  21 XWikiDocument globalConfigurationDocument = getDocument(RatingsManager.RATINGS_CONFIG_GLOBAL_REFERENCE);
101  21 return globalConfigurationDocument;
102    }
103   
104    /**
105    * Retrieves configuration parameter from the current space's WebPreferences and fallback to XWiki.RatingsConfig if
106    * it does not exist.
107    *
108    * @param documentReference the document being rated or for which the existing ratings are fetched
109    * @param parameterName the parameter for which to retrieve the value
110    * @param defaultValue the default value for the parameter
111    * @return the value of the given parameter name from the current configuration context
112    */
 
113  70 toggle public String getConfigurationParameter(DocumentReference documentReference, String parameterName,
114    String defaultValue)
115    {
116  70 XWikiDocument configurationDocument = getConfigurationDocument(documentReference);
117  70 if (configurationDocument != null && !configurationDocument.isNew()
118    && configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE) != null) {
119  69 try
120    {
121  69 BaseProperty prop = (BaseProperty) configurationDocument.
122    getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE).get(parameterName);
123  69 String propValue = (prop == null) ? defaultValue : prop.getValue().toString();
124  69 return (propValue.equals("") ? defaultValue : propValue);
125    } catch (XWikiException e) {
126  0 logger.error("Failed to retrieve the property for the configurationDocument [{}].",
127    configurationDocument, e);
128  0 return null;
129    }
130    }
131  1 return defaultValue;
132    }
133    }