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

File ConfiguredRatingsManagerProvider.java

 

Coverage histogram

../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

6
17
3
1
123
66
10
0.59
5.67
3
3.33

Classes

Class Line # Actions
ConfiguredRatingsManagerProvider 46 17 0% 10 8
0.692307769.2%
 

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 org.xwiki.ratings;
21   
22    import javax.inject.Inject;
23    import javax.inject.Singleton;
24   
25    import org.slf4j.Logger;
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.component.manager.ComponentLookupException;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.context.Execution;
30    import org.xwiki.model.reference.DocumentReference;
31   
32    import com.xpn.xwiki.XWiki;
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.objects.BaseProperty;
36   
37    /**
38    * Retrieve the appropriate RatingsManager by looking at the current configuration settings.
39    *
40    * @version $Id: c56b5c280d4fe1243a58992e8ca42938a6a24a43 $
41    * @since 6.4M3
42    */
43    @Component
44    @Singleton
45    // TODO: replace this system by a default component dynamically taking into account the configuration behind the scene
 
46    public class ConfiguredRatingsManagerProvider implements ConfiguredProvider<RatingsManager>
47    {
48    @Inject
49    private Logger logger;
50   
51    @Inject
52    private Execution execution;
53   
54    @Inject
55    private ComponentManager componentManager;
56   
57    @Inject
58    private RatingsConfiguration ratingsConfiguration;
59   
60    /**
61    * Retrieve the XWiki context from the current execution context.
62    *
63    * @return the XWiki context
64    * @throws RuntimeException if there was an error retrieving the context
65    */
 
66  12 toggle protected XWikiContext getXWikiContext()
67    {
68  12 return (XWikiContext) execution.getContext().getProperty("xwikicontext");
69    }
70   
71    /**
72    * Retrieve the XWiki private API object.
73    *
74    * @return the XWiki private API object
75    */
 
76  12 toggle protected XWiki getXWiki()
77    {
78  12 return getXWikiContext().getWiki();
79    }
80   
81    /**
82    * Retrieve an instance of the desired RatingsManager (default/separate). - default: save the rating information in
83    * the same page - separate: save the rating information in a specified space
84    *
85    * @param documentRef the document to which the ratings are associated to
86    * @return the ratings manager selected by looking at the current configuration settings
87    */
 
88  12 toggle @Override
89    public RatingsManager get(DocumentReference documentRef)
90    {
91  12 String defaultHint = "default";
92  12 String ratingsHint =
93    getXWiki().Param(
94    RatingsManager.RATINGS_CONFIG_PARAM_PREFIX + RatingsManager.RATINGS_CONFIG_FIELDNAME_MANAGER_HINT,
95    defaultHint);
96   
97  12 try {
98  12 XWikiDocument configurationDocument = ratingsConfiguration.getConfigurationDocument(documentRef);
99  12 if (!configurationDocument.isNew()
100    && configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE) != null) {
101  12 BaseProperty prop =
102    (BaseProperty) configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE).get(
103    RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_MANAGER_HINT);
104  12 String hint = (prop == null) ? null : (String) prop.getValue();
105  12 ratingsHint = (hint == null) ? ratingsHint : hint;
106    }
107    } catch (Exception e) {
108  0 logger.error("Cannot read ratings config", e);
109    }
110   
111  12 try {
112  12 return componentManager.getInstance(RatingsManager.class, ratingsHint);
113    } catch (ComponentLookupException e) {
114    // TODO Auto-generated catch block
115  0 logger.error("Error loading ratings manager component for hint " + ratingsHint, e);
116  0 try {
117  0 return componentManager.getInstance(RatingsManager.class, defaultHint);
118    } catch (ComponentLookupException e1) {
119  0 return null;
120    }
121    }
122    }
123    }