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

File ConfiguredReputationAlgorithmProvider.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

14
30
3
1
150
90
16
0.53
10
3
5.33

Classes

Class Line # Actions
ConfiguredReputationAlgorithmProvider 46 30 0% 16 47
0.00%
 

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 ReputationAlgorithm by looking at the current configuration settings.
39    *
40    * @version $Id: d39061a8e0493476c7ff5add9e98fbe998c5fdbc $
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 ConfiguredReputationAlgorithmProvider implements ConfiguredProvider<ReputationAlgorithm>
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 ConfiguredProvider<RatingsManager> ratingsManagerProvider;
59   
60    @Inject
61    private RatingsConfiguration ratingsConfiguration;
62   
63    /**
64    * Retrieve the XWiki context from the current execution context.
65    *
66    * @return the XWiki context
67    * @throws RuntimeException if there was an error retrieving the context
68    */
 
69  0 toggle protected XWikiContext getXWikiContext()
70    {
71  0 return (XWikiContext) execution.getContext().getProperty("xwikicontext");
72    }
73   
74    /**
75    * Retrieve the XWiki private API object.
76    *
77    * @return the XWiki private API object
78    */
 
79  0 toggle protected XWiki getXWiki()
80    {
81  0 return getXWikiContext().getWiki();
82    }
83   
84    /**
85    * Retrieve an instance of the desired ReputationAlorithm (default/simple/custom).
86    *
87    * @param documentRef documentRef the document to which the ratings are associated to
88    * @return the reputation algorithm selected by looking at the current configuration settings
89    */
 
90  0 toggle @Override
91    public ReputationAlgorithm get(DocumentReference documentRef)
92    {
93  0 String defaultAlgorithmHint = "default";
94  0 String reputationAlgorithmHint =
95    getXWiki().Param(
96    RatingsManager.RATINGS_CONFIG_PARAM_PREFIX
97    + RatingsManager.RATINGS_CONFIG_FIELDNAME_REPUTATIONALGORITHM_HINT, defaultAlgorithmHint);
98   
99  0 try {
100  0 XWikiDocument configurationDocument = ratingsConfiguration.getConfigurationDocument(documentRef);
101  0 if (configurationDocument != null && !configurationDocument.isNew()
102    && configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE) != null) {
103  0 BaseProperty prop =
104    (BaseProperty) configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE).get(
105    RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION_ALGORITHM_HINT);
106  0 String hint = (prop == null) ? null : (String) prop.getValue();
107  0 if (hint == "custom") {
108  0 prop =
109    (BaseProperty) configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE).get(
110    RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION_CUSTOM_ALGORITHM);
111  0 hint = (prop == null) ? null : (String) prop.getValue();
112    }
113  0 reputationAlgorithmHint = (hint == null) ? reputationAlgorithmHint : hint;
114    }
115    } catch (Exception e) {
116  0 logger.error("Cannot read reputation algorithm config", e);
117    }
118   
119    // if the reputation algorithm hint is a page let's try to get the instance from groovy
120  0 if (reputationAlgorithmHint.contains(".")) {
121  0 try {
122  0 ReputationAlgorithmGroovy reputationInstance =
123    (ReputationAlgorithmGroovy) getXWiki().parseGroovyFromPage(reputationAlgorithmHint,
124    getXWikiContext());
125   
126  0 if (reputationInstance != null) {
127  0 reputationInstance.setComponentManager(componentManager);
128  0 reputationInstance.setExecution(execution);
129  0 reputationInstance.setXWikiContext(getXWikiContext());
130  0 reputationInstance.setRatingsManager(ratingsManagerProvider.get(documentRef));
131  0 return reputationInstance;
132    }
133    } catch (Throwable e) {
134  0 logger.error("Cannot instanciate Reputation algorithm from page " + reputationAlgorithmHint, e);
135    }
136    }
137   
138  0 try {
139  0 return componentManager.getInstance(ReputationAlgorithm.class, reputationAlgorithmHint);
140    } catch (ComponentLookupException e) {
141    // TODO Auto-generated catch block
142  0 logger.error("Error loading ratings manager component for hint " + reputationAlgorithmHint, e);
143  0 try {
144  0 return componentManager.getInstance(ReputationAlgorithm.class, defaultAlgorithmHint);
145    } catch (ComponentLookupException e1) {
146  0 return null;
147    }
148    }
149    }
150    }