1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.watchlist.script; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Provider; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.context.Execution; |
34 |
|
import org.xwiki.script.service.ScriptService; |
35 |
|
import org.xwiki.security.authorization.AccessDeniedException; |
36 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
37 |
|
import org.xwiki.security.authorization.Right; |
38 |
|
import org.xwiki.watchlist.internal.DefaultWatchListStore; |
39 |
|
import org.xwiki.watchlist.internal.api.WatchList; |
40 |
|
import org.xwiki.watchlist.internal.api.WatchedElementType; |
41 |
|
|
42 |
|
import com.sun.syndication.feed.synd.SyndFeed; |
43 |
|
import com.xpn.xwiki.XWikiContext; |
44 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
55 |
|
@Component |
56 |
|
@Named("watchlist") |
57 |
|
@Singleton |
|
|
| 69.2% |
Uncovered Elements: 28 (91) |
Complexity: 28 |
Complexity Density: 0.4 |
|
58 |
|
public class WatchListScriptService implements ScriptService |
59 |
|
{ |
60 |
|
private static final String ERROR_KEY = "scriptservice.watchlist.error"; |
61 |
|
|
62 |
|
@Inject |
63 |
|
private Provider<XWikiContext> contextProvider; |
64 |
|
|
65 |
|
@Inject |
66 |
|
private WatchList watchlist; |
67 |
|
|
68 |
|
@Inject |
69 |
|
private ContextualAuthorizationManager authorizationManager; |
70 |
|
|
71 |
|
@Inject |
72 |
|
private Execution execution; |
73 |
|
|
74 |
|
|
75 |
|
@param@link |
76 |
|
@return |
77 |
|
|
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
246 |
public boolean isWatched(WatchedElementType type)... |
80 |
|
{ |
81 |
246 |
return isWatched(null, type); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
@param |
86 |
|
|
87 |
|
@param@link |
88 |
|
@return |
89 |
|
|
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
91 |
248 |
public boolean isWatched(String element, WatchedElementType type)... |
92 |
|
{ |
93 |
248 |
XWikiContext context = contextProvider.get(); |
94 |
|
|
95 |
248 |
try { |
96 |
248 |
String safeElement = getSafeElement(element, type); |
97 |
|
|
98 |
248 |
return watchlist.getStore().isWatched(safeElement, context.getUser(), type); |
99 |
|
} catch (Exception e) { |
100 |
6 |
setError(e); |
101 |
6 |
return false; |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
@param |
107 |
|
@param |
108 |
|
@return@link |
109 |
|
|
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
111 |
258 |
private String getSafeElement(String element, WatchedElementType type)... |
112 |
|
{ |
113 |
258 |
String safeElement = element; |
114 |
258 |
if (safeElement == null) { |
115 |
250 |
safeElement = getCurrentElementOfType(type); |
116 |
|
} |
117 |
|
|
118 |
258 |
return safeElement; |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
|
@param@link |
123 |
|
@return |
124 |
|
|
|
|
| 73.7% |
Uncovered Elements: 5 (19) |
Complexity: 5 |
Complexity Density: 0.26 |
|
125 |
250 |
private String getCurrentElementOfType(WatchedElementType type)... |
126 |
|
{ |
127 |
250 |
XWikiContext context = contextProvider.get(); |
128 |
250 |
XWikiDocument currentDocument = context.getDoc(); |
129 |
|
|
130 |
250 |
String element = null; |
131 |
250 |
switch (type) { |
132 |
83 |
case DOCUMENT: |
133 |
83 |
element = currentDocument.getPrefixedFullName(); |
134 |
83 |
break; |
135 |
84 |
case SPACE: |
136 |
84 |
element = context.getWikiId() + DefaultWatchListStore.WIKI_SPACE_SEP + currentDocument.getSpace(); |
137 |
84 |
break; |
138 |
83 |
case WIKI: |
139 |
83 |
element = context.getWikiId(); |
140 |
83 |
break; |
141 |
0 |
case USER: |
142 |
0 |
element = context.getUser(); |
143 |
0 |
break; |
144 |
0 |
default: |
145 |
0 |
break; |
146 |
|
} |
147 |
|
|
148 |
250 |
return element; |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
@param |
155 |
|
|
156 |
|
@param@link |
157 |
|
@return |
158 |
|
|
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
160 |
5 |
public boolean addWatchedElement(String element, WatchedElementType type)... |
161 |
|
{ |
162 |
5 |
return addWatchedElement(null, element, type); |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
@param |
169 |
|
|
170 |
|
@param |
171 |
|
|
172 |
|
@param@link |
173 |
|
@return |
174 |
|
|
175 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
176 |
5 |
public boolean addWatchedElement(String user, String element, WatchedElementType type)... |
177 |
|
{ |
178 |
5 |
XWikiContext context = contextProvider.get(); |
179 |
|
|
180 |
5 |
try { |
181 |
5 |
String safeUser = getSafeUser(user, context); |
182 |
5 |
String safeElement = getSafeElement(element, type); |
183 |
|
|
184 |
5 |
return watchlist.getStore().addWatchedElement(safeUser, safeElement, type); |
185 |
|
} catch (Exception e) { |
186 |
0 |
setError(e); |
187 |
0 |
return false; |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
@param |
193 |
|
@param |
194 |
|
@return |
195 |
|
@throws |
196 |
|
|
197 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
198 |
10 |
private String getSafeUser(String user, XWikiContext context) throws AccessDeniedException... |
199 |
|
{ |
200 |
10 |
String safeUser = user; |
201 |
|
|
202 |
10 |
if (safeUser == null) { |
203 |
10 |
safeUser = context.getUser(); |
204 |
|
} else { |
205 |
0 |
authorizationManager.checkAccess(Right.ADMIN); |
206 |
|
} |
207 |
|
|
208 |
10 |
return safeUser; |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
@param |
215 |
|
|
216 |
|
@param@link |
217 |
|
@return |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
5 |
public boolean removeWatchedElement(String element, WatchedElementType type)... |
220 |
|
{ |
221 |
5 |
return removeWatchedElement(null, element, type); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
@param |
228 |
|
|
229 |
|
|
230 |
|
@param |
231 |
|
|
232 |
|
@param@link |
233 |
|
@return |
234 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
235 |
5 |
public boolean removeWatchedElement(String user, String element, WatchedElementType type)... |
236 |
|
{ |
237 |
5 |
XWikiContext context = contextProvider.get(); |
238 |
|
|
239 |
5 |
try { |
240 |
5 |
String safeUser = getSafeUser(user, context); |
241 |
5 |
String safeElement = getSafeElement(element, type); |
242 |
|
|
243 |
5 |
return watchlist.getStore().removeWatchedElement(safeUser, safeElement, type); |
244 |
|
} catch (Exception e) { |
245 |
0 |
setError(e); |
246 |
0 |
return false; |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
@param@link |
252 |
|
@return |
253 |
|
|
254 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
255 |
73 |
public Collection<String> getWatchedElements(WatchedElementType type)... |
256 |
|
{ |
257 |
73 |
XWikiContext context = contextProvider.get(); |
258 |
|
|
259 |
73 |
try { |
260 |
73 |
return watchlist.getStore().getWatchedElements(context.getUser(), type); |
261 |
|
} catch (Exception e) { |
262 |
0 |
setError(e); |
263 |
0 |
return Collections.emptyList(); |
264 |
|
} |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
@return |
271 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
272 |
0 |
public List<String> getWatchedElements()... |
273 |
|
{ |
274 |
0 |
List<String> elements = new ArrayList<String>(); |
275 |
0 |
for (WatchedElementType type : WatchedElementType.values()) { |
276 |
0 |
elements.addAll(getWatchedElements(type)); |
277 |
|
} |
278 |
|
|
279 |
0 |
return elements; |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
|
@param |
284 |
|
@return |
285 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
286 |
0 |
public SyndFeed getFeed(int entryNumber)... |
287 |
|
{ |
288 |
0 |
XWikiContext context = contextProvider.get(); |
289 |
|
|
290 |
0 |
return getFeed(context.getUser(), entryNumber); |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
|
@param |
295 |
|
@param |
296 |
|
@return |
297 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
298 |
0 |
public SyndFeed getFeed(String user, int entryNumber)... |
299 |
|
{ |
300 |
0 |
try { |
301 |
0 |
return watchlist.getFeedManager().getFeed(user, entryNumber); |
302 |
|
} catch (Exception e) { |
303 |
0 |
setError(e); |
304 |
0 |
return null; |
305 |
|
} |
306 |
|
} |
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
@return |
313 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
314 |
16 |
public List<String> getIntervals()... |
315 |
|
{ |
316 |
16 |
return watchlist.getStore().getIntervals(); |
317 |
|
} |
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
@param |
323 |
|
@return |
324 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
325 |
16 |
public String getInterval(String user)... |
326 |
|
{ |
327 |
16 |
return watchlist.getStore().getInterval(user); |
328 |
|
} |
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
|
@return |
334 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
335 |
0 |
public Exception getLastError()... |
336 |
|
{ |
337 |
0 |
return (Exception) this.execution.getContext().getProperty(ERROR_KEY); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
@link |
342 |
|
|
343 |
|
@param |
344 |
|
@see |
345 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
346 |
6 |
protected void setError(Exception e)... |
347 |
|
{ |
348 |
6 |
this.execution.getContext().setProperty(ERROR_KEY, e); |
349 |
|
} |
350 |
|
} |