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.util.Collections; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
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.slf4j.Logger; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.component.manager.ComponentLookupException; |
35 |
|
import org.xwiki.component.manager.ComponentManager; |
36 |
|
import org.xwiki.rendering.block.Block; |
37 |
|
import org.xwiki.rendering.block.GroupBlock; |
38 |
|
import org.xwiki.rendering.internal.macro.script.NestedScriptMacroEnabled; |
39 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
40 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
41 |
|
import org.xwiki.rendering.macro.dashboard.DashboardMacroParameters; |
42 |
|
import org.xwiki.rendering.macro.dashboard.DashboardRenderer; |
43 |
|
import org.xwiki.rendering.macro.dashboard.Gadget; |
44 |
|
import org.xwiki.rendering.macro.dashboard.GadgetRenderer; |
45 |
|
import org.xwiki.rendering.macro.dashboard.GadgetSource; |
46 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
47 |
|
import org.xwiki.skinx.SkinExtension; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
55 |
|
@Component |
56 |
|
@Named(DashboardMacro.MACRO_NAME) |
57 |
|
@Singleton |
|
|
| 74.6% |
Uncovered Elements: 17 (67) |
Complexity: 16 |
Complexity Density: 0.33 |
|
58 |
|
public class DashboardMacro extends AbstractMacro<DashboardMacroParameters> implements NestedScriptMacroEnabled |
59 |
|
{ |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
public static final String GADGET_CONTAINER = "gadget-container"; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
public static final String GADGET_CONTAINER_PREFIX = "gadgetcontainer_"; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
public static final String METADATA = "metadata"; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
public static final String EDIT_URL = "editurl"; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
public static final String ADD_URL = "addurl"; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
public static final String REMOVE_URL = "removeurl"; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
public static final String SOURCE_PAGE = "sourcepage"; |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
public static final String SOURCE_SPACE = "sourcespace"; |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
public static final String SOURCE_WIKI = "sourcewiki"; |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
public static final String SOURCE_URL = "sourceurl"; |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
public static final String MACRO_NAME = "dashboard"; |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
private static final String DESCRIPTION = "A macro to define a dashboard."; |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@Inject |
127 |
|
@Named("ssfx") |
128 |
|
private SkinExtension ssfx; |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@Inject |
134 |
|
@Named("jsfx") |
135 |
|
private SkinExtension jsfx; |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
@Inject |
141 |
|
private ComponentManager componentManager; |
142 |
|
|
143 |
|
|
144 |
|
@link |
145 |
|
|
146 |
|
@Inject |
147 |
|
private GadgetSource gadgetSource; |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
@Inject |
153 |
|
private Logger logger; |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
3 |
public DashboardMacro()... |
159 |
|
{ |
160 |
3 |
super("Dashboard", DESCRIPTION, DashboardMacroParameters.class); |
161 |
|
} |
162 |
|
|
|
|
| 64.7% |
Uncovered Elements: 12 (34) |
Complexity: 7 |
Complexity Density: 0.27 |
|
163 |
3 |
@Override... |
164 |
|
public List<Block> execute(DashboardMacroParameters parameters, String content, MacroTransformationContext context) |
165 |
|
throws MacroExecutionException |
166 |
|
{ |
167 |
|
|
168 |
3 |
List<Gadget> gadgets; |
169 |
3 |
try { |
170 |
3 |
gadgets = this.gadgetSource.getGadgets(parameters.getSource(), context); |
171 |
|
} catch (Exception e) { |
172 |
0 |
String message = "Could not get the gadgets."; |
173 |
|
|
174 |
0 |
this.logger.error(message, e); |
175 |
0 |
throw new MacroExecutionException(message, e); |
176 |
|
} |
177 |
|
|
178 |
3 |
boolean isInEditMode = this.gadgetSource.isEditing(); |
179 |
|
|
180 |
3 |
DashboardRenderer renderer = |
181 |
3 |
getDashboardRenderer(StringUtils.isEmpty(parameters.getLayout()) ? "columns" : parameters.getLayout()); |
182 |
3 |
if (renderer == null) { |
183 |
0 |
String message = "Could not find dashboard renderer " + parameters.getLayout(); |
184 |
|
|
185 |
0 |
this.logger.error(message); |
186 |
0 |
throw new MacroExecutionException(message); |
187 |
|
} |
188 |
|
|
189 |
3 |
GadgetRenderer gadgetRenderer = getGadgetRenderer(isInEditMode); |
190 |
|
|
191 |
|
|
192 |
3 |
List<Block> layoutedResult; |
193 |
3 |
try { |
194 |
3 |
layoutedResult = renderer.renderGadgets(gadgets, gadgetRenderer, context); |
195 |
|
} catch (Exception e) { |
196 |
0 |
String message = "Could not render the gadgets for layout " + parameters.getLayout(); |
197 |
|
|
198 |
0 |
this.logger.error(message, e); |
199 |
0 |
throw new MacroExecutionException(message, e); |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
3 |
this.includeResources(isInEditMode); |
205 |
|
|
206 |
|
|
207 |
3 |
GroupBlock topLevel = new GroupBlock(); |
208 |
|
|
209 |
|
|
210 |
3 |
if (isInEditMode) { |
211 |
2 |
topLevel.addChildren(this.gadgetSource.getDashboardSourceMetadata(parameters.getSource(), context)); |
212 |
|
} |
213 |
3 |
topLevel.addChildren(layoutedResult); |
214 |
|
|
215 |
3 |
topLevel.setParameter("class", |
216 |
3 |
MACRO_NAME + (StringUtils.isEmpty(parameters.getStyle()) ? "" : " " + parameters.getStyle())); |
217 |
|
|
218 |
3 |
return Collections.<Block> singletonList(topLevel); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
@param |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
226 |
3 |
protected void includeResources(boolean editMode)... |
227 |
|
{ |
228 |
3 |
Map<String, Object> fxParamsForceSkinAction = new HashMap<String, Object>(); |
229 |
3 |
fxParamsForceSkinAction.put("forceSkinAction", true); |
230 |
3 |
this.ssfx.use("uicomponents/dashboard/dashboard.css", fxParamsForceSkinAction); |
231 |
|
|
232 |
3 |
if (editMode) { |
233 |
|
|
234 |
2 |
this.jsfx.use("js/scriptaculous/effects.js"); |
235 |
2 |
this.jsfx.use("js/scriptaculous/dragdrop.js"); |
236 |
2 |
Map<String, Object> fxParamsNonDeferredForceSkinAction = new HashMap<String, Object>(); |
237 |
2 |
fxParamsNonDeferredForceSkinAction.put("defer", false); |
238 |
2 |
fxParamsNonDeferredForceSkinAction.putAll(fxParamsForceSkinAction); |
239 |
2 |
this.jsfx.use("js/xwiki/wysiwyg/xwe/XWikiWysiwyg.js", fxParamsNonDeferredForceSkinAction); |
240 |
2 |
this.jsfx.use("uicomponents/dashboard/dashboard.js", fxParamsForceSkinAction); |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
@param |
246 |
|
@return |
247 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
248 |
3 |
protected DashboardRenderer getDashboardRenderer(String layout)... |
249 |
|
{ |
250 |
3 |
try { |
251 |
3 |
return this.componentManager.getInstance(DashboardRenderer.class, layout); |
252 |
|
} catch (ComponentLookupException e) { |
253 |
0 |
this.logger.warn("Could not find the Dashboard renderer for layout \"" + layout + "\""); |
254 |
0 |
return null; |
255 |
|
} |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
@param |
260 |
|
@return |
261 |
|
@throws |
262 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
263 |
3 |
protected GadgetRenderer getGadgetRenderer(boolean isEditing) throws MacroExecutionException... |
264 |
|
{ |
265 |
3 |
String hint = "default"; |
266 |
3 |
if (isEditing) { |
267 |
2 |
hint = "edit"; |
268 |
|
} |
269 |
3 |
try { |
270 |
3 |
return this.componentManager.getInstance(GadgetRenderer.class, hint); |
271 |
|
} catch (ComponentLookupException e) { |
272 |
0 |
throw new MacroExecutionException(String.format("Could not find the Gadgets renderer for hint [%s].", |
273 |
|
hint), e); |
274 |
|
} |
275 |
|
} |
276 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
277 |
0 |
@Override... |
278 |
|
public boolean supportsInlineMode() |
279 |
|
{ |
280 |
0 |
return false; |
281 |
|
} |
282 |
|
} |