1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
43 |
|
@Component |
44 |
|
@Singleton |
45 |
|
|
|
|
| 0% |
Uncovered Elements: 47 (47) |
Complexity: 16 |
Complexity Density: 0.53 |
|
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 |
|
|
65 |
|
|
66 |
|
@return |
67 |
|
@throws |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
0 |
protected XWikiContext getXWikiContext()... |
70 |
|
{ |
71 |
0 |
return (XWikiContext) execution.getContext().getProperty("xwikicontext"); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@return |
78 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
0 |
protected XWiki getXWiki()... |
80 |
|
{ |
81 |
0 |
return getXWikiContext().getWiki(); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@param |
88 |
|
@return |
89 |
|
|
|
|
| 0% |
Uncovered Elements: 42 (42) |
Complexity: 14 |
Complexity Density: 0.5 |
|
90 |
0 |
@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 |
|
|
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 |
|
|
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 |
|
} |