1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.dashboard; |
21 |
|
|
22 |
|
import java.io.StringWriter; |
23 |
|
import java.util.ArrayList; |
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.Singleton; |
30 |
|
|
31 |
|
import org.apache.commons.lang3.StringUtils; |
32 |
|
import org.apache.velocity.VelocityContext; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.context.Execution; |
35 |
|
import org.xwiki.model.EntityType; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
38 |
|
import org.xwiki.model.reference.EntityReference; |
39 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
40 |
|
import org.xwiki.rendering.block.Block; |
41 |
|
import org.xwiki.rendering.block.GroupBlock; |
42 |
|
import org.xwiki.rendering.block.LinkBlock; |
43 |
|
import org.xwiki.rendering.block.WordBlock; |
44 |
|
import org.xwiki.rendering.block.XDOM; |
45 |
|
import org.xwiki.rendering.executor.ContentExecutor; |
46 |
|
import org.xwiki.rendering.executor.ContentExecutorException; |
47 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
48 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
49 |
|
import org.xwiki.rendering.macro.dashboard.Gadget; |
50 |
|
import org.xwiki.rendering.macro.dashboard.GadgetSource; |
51 |
|
import org.xwiki.rendering.parser.MissingParserException; |
52 |
|
import org.xwiki.rendering.parser.ParseException; |
53 |
|
import org.xwiki.rendering.syntax.Syntax; |
54 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
55 |
|
import org.xwiki.rendering.util.ParserUtils; |
56 |
|
import org.xwiki.velocity.VelocityEngine; |
57 |
|
import org.xwiki.velocity.VelocityManager; |
58 |
|
|
59 |
|
import com.xpn.xwiki.XWiki; |
60 |
|
import com.xpn.xwiki.XWikiContext; |
61 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
62 |
|
import com.xpn.xwiki.objects.BaseObject; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@version |
68 |
|
@since |
69 |
|
|
70 |
|
@Component |
71 |
|
@Singleton |
|
|
| 52.1% |
Uncovered Elements: 45 (94) |
Complexity: 12 |
Complexity Density: 0.16 |
|
72 |
|
public class DefaultGadgetSource implements GadgetSource |
73 |
|
{ |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
private static final EntityReference GADGET_CLASS = |
79 |
|
new EntityReference("GadgetClass", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
@Inject |
85 |
|
protected Execution execution; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@Inject |
92 |
|
@Named("current") |
93 |
|
protected DocumentReferenceResolver<String> currentReferenceResolver; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
@Inject |
99 |
|
@Named("current") |
100 |
|
protected DocumentReferenceResolver<EntityReference> currentReferenceEntityResolver; |
101 |
|
|
102 |
|
@Inject |
103 |
|
@Named("local") |
104 |
|
private EntityReferenceSerializer<String> localReferenceSerializer; |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
@Inject |
110 |
|
private VelocityManager velocityManager; |
111 |
|
|
112 |
|
@Inject |
113 |
|
private ContentExecutor<MacroTransformationContext> contentExecutor; |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
private ParserUtils parserUtils = new ParserUtils(); |
119 |
|
|
|
|
| 73.3% |
Uncovered Elements: 4 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
120 |
1 |
@Override... |
121 |
|
public List<Gadget> getGadgets(String source, MacroTransformationContext context) throws Exception |
122 |
|
{ |
123 |
|
|
124 |
1 |
DocumentReference sourceDocRef = getSourceDocumentReference(source); |
125 |
1 |
if (sourceDocRef == null) { |
126 |
0 |
return new ArrayList<>(); |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
1 |
XWikiContext xContext = getXWikiContext(); |
131 |
1 |
XWiki xWiki = xContext.getWiki(); |
132 |
1 |
XWikiDocument sourceDoc = xWiki.getDocument(sourceDocRef, xContext); |
133 |
1 |
DocumentReference gadgetsClass = currentReferenceEntityResolver.resolve(GADGET_CLASS); |
134 |
1 |
List<BaseObject> gadgetObjects = sourceDoc.getXObjects(gadgetsClass); |
135 |
|
|
136 |
1 |
if (gadgetObjects == null) { |
137 |
0 |
return new ArrayList<>(); |
138 |
|
} |
139 |
|
|
140 |
1 |
return prepareGadgets(gadgetObjects, sourceDoc.getSyntax(), context); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
@param |
148 |
|
@param |
149 |
|
@return |
150 |
|
@throws |
151 |
|
|
|
|
| 84.6% |
Uncovered Elements: 4 (26) |
Complexity: 3 |
Complexity Density: 0.14 |
|
152 |
1 |
private List<Gadget> prepareGadgets(List<BaseObject> objects, Syntax sourceSyntax,... |
153 |
|
MacroTransformationContext context) throws Exception |
154 |
|
{ |
155 |
1 |
List<Gadget> gadgets = new ArrayList<>(); |
156 |
|
|
157 |
|
|
158 |
1 |
VelocityContext velocityContext = velocityManager.getVelocityContext(); |
159 |
|
|
160 |
|
|
161 |
1 |
String key = context.getTransformationContext().getId(); |
162 |
1 |
if (key == null) { |
163 |
0 |
key = "unknown namespace"; |
164 |
|
} |
165 |
1 |
VelocityEngine velocityEngine = velocityManager.getVelocityEngine(); |
166 |
|
|
167 |
1 |
for (BaseObject xObject : objects) { |
168 |
11 |
if (xObject == null) { |
169 |
0 |
continue; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
11 |
String title = xObject.getStringValue("title"); |
174 |
11 |
String content = xObject.getLargeStringValue("content"); |
175 |
11 |
String position = xObject.getStringValue("position"); |
176 |
11 |
String id = xObject.getNumber() + ""; |
177 |
|
|
178 |
|
|
179 |
11 |
StringWriter writer = new StringWriter(); |
180 |
|
|
181 |
11 |
velocityEngine.evaluate(velocityContext, writer, key, title); |
182 |
11 |
String gadgetTitle = writer.toString(); |
183 |
|
|
184 |
|
|
185 |
11 |
List<Block> titleBlocks = |
186 |
|
renderGadgetProperty(gadgetTitle, sourceSyntax, xObject.getDocumentReference(), context); |
187 |
11 |
List<Block> contentBlocks = |
188 |
|
renderGadgetProperty(content, sourceSyntax, xObject.getDocumentReference(), context); |
189 |
|
|
190 |
|
|
191 |
11 |
Gadget gadget = new Gadget(id, titleBlocks, contentBlocks, position); |
192 |
11 |
gadget.setTitleSource(title); |
193 |
11 |
gadgets.add(gadget); |
194 |
|
} |
195 |
1 |
return gadgets; |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
198 |
22 |
private List<Block> renderGadgetProperty(String content, Syntax sourceSyntax, EntityReference sourceReference,... |
199 |
|
MacroTransformationContext context) throws MissingParserException, ParseException, ContentExecutorException |
200 |
|
{ |
201 |
22 |
XDOM xdom = this.contentExecutor.execute(content, sourceSyntax, sourceReference, context); |
202 |
22 |
List<Block> xdomBlocks = xdom.getChildren(); |
203 |
22 |
this.parserUtils.removeTopLevelParagraph(xdomBlocks); |
204 |
22 |
return xdomBlocks; |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
@param |
212 |
|
@return |
213 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
214 |
1 |
private DocumentReference getSourceDocumentReference(String source)... |
215 |
|
{ |
216 |
|
|
217 |
1 |
if (StringUtils.isEmpty(source)) { |
218 |
1 |
return getXWikiContext().getDoc().getDocumentReference(); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
0 |
return currentReferenceResolver.resolve(source); |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
@return |
229 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
230 |
3 |
private XWikiContext getXWikiContext()... |
231 |
|
{ |
232 |
3 |
return (XWikiContext) execution.getContext().getProperty("xwikicontext"); |
233 |
|
} |
234 |
|
|
|
|
| 0% |
Uncovered Elements: 34 (34) |
Complexity: 1 |
Complexity Density: 0.03 |
|
235 |
0 |
@Override... |
236 |
|
public List<Block> getDashboardSourceMetadata(String source, MacroTransformationContext context) |
237 |
|
{ |
238 |
0 |
DocumentReference sourceDoc = getSourceDocumentReference(source); |
239 |
0 |
String classParameterName = "class"; |
240 |
0 |
GroupBlock metadataContainer = new GroupBlock(); |
241 |
0 |
metadataContainer.setParameter(classParameterName, DashboardMacro.METADATA); |
242 |
|
|
243 |
|
|
244 |
0 |
XWikiContext xContext = getXWikiContext(); |
245 |
0 |
String editURL = xContext.getWiki().getURL(sourceDoc, "save", "", "", xContext); |
246 |
0 |
LinkBlock editURLBlock = |
247 |
|
new LinkBlock(Collections.<Block> emptyList(), new ResourceReference(editURL, ResourceType.URL), false); |
248 |
0 |
editURLBlock.setParameter(classParameterName, DashboardMacro.EDIT_URL); |
249 |
0 |
metadataContainer.addChild(editURLBlock); |
250 |
0 |
String removeURL = xContext.getWiki().getURL(sourceDoc, "objectremove", "", "", xContext); |
251 |
0 |
LinkBlock removeURLBlock = |
252 |
|
new LinkBlock(Collections.<Block> emptyList(), new ResourceReference(removeURL, ResourceType.URL), false); |
253 |
0 |
removeURLBlock.setParameter(classParameterName, DashboardMacro.REMOVE_URL); |
254 |
0 |
metadataContainer.addChild(removeURLBlock); |
255 |
0 |
String addURL = xContext.getWiki().getURL(sourceDoc, "objectadd", "", "", xContext); |
256 |
0 |
LinkBlock addURLBlock = |
257 |
|
new LinkBlock(Collections.<Block> emptyList(), new ResourceReference(addURL, ResourceType.URL), false); |
258 |
0 |
addURLBlock.setParameter(classParameterName, DashboardMacro.ADD_URL); |
259 |
0 |
metadataContainer.addChild(addURLBlock); |
260 |
|
|
261 |
|
|
262 |
0 |
GroupBlock sourcePageBlock = new GroupBlock(); |
263 |
0 |
sourcePageBlock.addChild(new WordBlock(sourceDoc.getName())); |
264 |
0 |
sourcePageBlock.setParameter(classParameterName, DashboardMacro.SOURCE_PAGE); |
265 |
0 |
metadataContainer.addChild(sourcePageBlock); |
266 |
0 |
GroupBlock sourceSpaceBlock = new GroupBlock(); |
267 |
|
|
268 |
0 |
sourceSpaceBlock.addChild(new WordBlock( |
269 |
|
this.localReferenceSerializer.serialize(sourceDoc.getLastSpaceReference()))); |
270 |
0 |
sourceSpaceBlock.setParameter(classParameterName, DashboardMacro.SOURCE_SPACE); |
271 |
0 |
metadataContainer.addChild(sourceSpaceBlock); |
272 |
0 |
GroupBlock sourceWikiBlock = new GroupBlock(); |
273 |
0 |
sourceWikiBlock.addChild(new WordBlock(sourceDoc.getWikiReference().getName())); |
274 |
0 |
sourceWikiBlock.setParameter(classParameterName, DashboardMacro.SOURCE_WIKI); |
275 |
0 |
metadataContainer.addChild(sourceWikiBlock); |
276 |
0 |
String sourceURL = xContext.getWiki().getURL(sourceDoc, "view", "", "", xContext); |
277 |
0 |
LinkBlock sourceURLBlock = |
278 |
|
new LinkBlock(Collections.<Block> emptyList(), new ResourceReference(sourceURL, ResourceType.URL), false); |
279 |
0 |
sourceURLBlock.setParameter(classParameterName, DashboardMacro.SOURCE_URL); |
280 |
0 |
metadataContainer.addChild(sourceURLBlock); |
281 |
|
|
282 |
0 |
return Collections.<Block> singletonList(metadataContainer); |
283 |
|
} |
284 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
285 |
1 |
@Override... |
286 |
|
public boolean isEditing() |
287 |
|
{ |
288 |
|
|
289 |
1 |
XWikiContext context = getXWikiContext(); |
290 |
1 |
return "inline".equals(context.getAction()) || "edit".equals(context.getAction()); |
291 |
|
} |
292 |
|
} |