1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.component.wiki.internal; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.bridge.event.ApplicationReadyEvent; |
32 |
|
import org.xwiki.bridge.event.WikiReadyEvent; |
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.EntityReference; |
37 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
38 |
|
import org.xwiki.observation.EventListener; |
39 |
|
import org.xwiki.observation.event.Event; |
40 |
|
import org.xwiki.rendering.syntax.Syntax; |
41 |
|
|
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.XWikiException; |
44 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
45 |
|
import com.xpn.xwiki.objects.classes.BaseClass; |
46 |
|
import com.xpn.xwiki.objects.classes.TextAreaClass; |
47 |
|
|
48 |
|
|
49 |
|
@link |
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Named("defaultWikiComponentBuilderEventListener") |
56 |
|
@Singleton |
|
|
| 99% |
Uncovered Elements: 1 (103) |
Complexity: 22 |
Complexity Density: 0.3 |
|
57 |
|
public class DefaultWikiComponentBuilderEventListener implements EventListener, WikiComponentConstants |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
private Logger logger; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
private Execution execution; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@Inject |
75 |
|
@Named("compactwiki") |
76 |
|
private EntityReferenceSerializer<String> compactWikiSerializer; |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
60 |
@Override... |
79 |
|
public List<Event> getEvents() |
80 |
|
{ |
81 |
60 |
return Arrays.<Event> asList(new ApplicationReadyEvent(), new WikiReadyEvent()); |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
84 |
240 |
@Override... |
85 |
|
public String getName() |
86 |
|
{ |
87 |
240 |
return "defaultWikiComponentBuilderEventListener"; |
88 |
|
} |
89 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
90 |
64 |
@Override... |
91 |
|
public void onEvent(Event arg0, Object arg1, Object arg2) |
92 |
|
{ |
93 |
64 |
try { |
94 |
64 |
this.installOrUpdateComponentXClass(); |
95 |
64 |
this.installOrUpdateComponentRequirementXClass(); |
96 |
64 |
this.installOrUpdateComponentMethodXClass(); |
97 |
64 |
this.installOrUpdateComponentInterfaceXClass(); |
98 |
|
} catch (XWikiException e) { |
99 |
0 |
this.logger.error("Failed to install or update wiki component XClasses", e); |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
@link |
105 |
|
|
106 |
|
@throws |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
108 |
64 |
private void installOrUpdateComponentInterfaceXClass() throws XWikiException... |
109 |
|
{ |
110 |
64 |
XWikiContext xcontext = getXWikiContext(); |
111 |
64 |
XWikiDocument doc = xcontext.getWiki().getDocument(INTERFACE_CLASS_REFERENCE, xcontext); |
112 |
|
|
113 |
64 |
BaseClass bclass = doc.getXClass(); |
114 |
64 |
bclass.setDocumentReference(doc.getDocumentReference()); |
115 |
|
|
116 |
64 |
boolean needsUpdate = false; |
117 |
|
|
118 |
64 |
needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Implements Interface XWiki Class"); |
119 |
64 |
needsUpdate |= bclass.addTextField(INTERFACE_NAME_FIELD, "Interface Qualified Name", 30); |
120 |
|
|
121 |
64 |
if (needsUpdate) { |
122 |
38 |
this.update(doc); |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
@link |
128 |
|
|
129 |
|
@throws |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
131 |
64 |
private void installOrUpdateComponentXClass() throws XWikiException... |
132 |
|
{ |
133 |
64 |
XWikiContext xcontext = getXWikiContext(); |
134 |
64 |
XWikiDocument doc = xcontext.getWiki().getDocument(COMPONENT_CLASS_REFERENCE, xcontext); |
135 |
|
|
136 |
64 |
BaseClass bclass = doc.getXClass(); |
137 |
64 |
bclass.setDocumentReference(doc.getDocumentReference()); |
138 |
|
|
139 |
64 |
boolean needsUpdate = false; |
140 |
|
|
141 |
64 |
needsUpdate |= initializeXClassDocumentMetadata(doc, "Wiki Component XWiki Class"); |
142 |
64 |
needsUpdate |= bclass.addTextField(COMPONENT_ROLE_TYPE_FIELD, "Component Role Type", 30); |
143 |
64 |
needsUpdate |= bclass.addTextField(COMPONENT_ROLE_HINT_FIELD, "Component Role Hint", 30); |
144 |
64 |
needsUpdate |= bclass.addStaticListField(COMPONENT_SCOPE_FIELD, "Component Scope", 1, false, |
145 |
|
"wiki=Current Wiki|user=Current User|global=Global", "select"); |
146 |
|
|
147 |
64 |
if (needsUpdate) { |
148 |
38 |
this.update(doc); |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
@link |
154 |
|
|
155 |
|
@throws |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
157 |
64 |
private void installOrUpdateComponentRequirementXClass() throws XWikiException... |
158 |
|
{ |
159 |
64 |
XWikiContext xcontext = getXWikiContext(); |
160 |
64 |
XWikiDocument doc = xcontext.getWiki().getDocument(DEPENDENCY_CLASS_REFERENCE, xcontext); |
161 |
|
|
162 |
64 |
BaseClass bclass = doc.getXClass(); |
163 |
64 |
bclass.setDocumentReference(doc.getDocumentReference()); |
164 |
|
|
165 |
64 |
boolean needsUpdate = false; |
166 |
|
|
167 |
64 |
needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Dependency XWiki Class"); |
168 |
64 |
needsUpdate |= bclass.addTextField(COMPONENT_ROLE_TYPE_FIELD, "Dependency Role Type", 30); |
169 |
64 |
needsUpdate |= bclass.addTextField(COMPONENT_ROLE_HINT_FIELD, "Dependency Role Hint", 30); |
170 |
64 |
needsUpdate |= bclass.addTextField(DEPENDENCY_BINDING_NAME_FIELD, "Binding name", 30); |
171 |
|
|
172 |
64 |
if (needsUpdate) { |
173 |
38 |
this.update(doc); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
@link |
179 |
|
|
180 |
|
@throws |
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
182 |
64 |
private void installOrUpdateComponentMethodXClass() throws XWikiException... |
183 |
|
{ |
184 |
64 |
XWikiContext xcontext = getXWikiContext(); |
185 |
64 |
XWikiDocument doc = xcontext.getWiki().getDocument(METHOD_CLASS_REFRENCE, xcontext); |
186 |
|
|
187 |
64 |
BaseClass bclass = doc.getXClass(); |
188 |
64 |
bclass.setDocumentReference(doc.getDocumentReference()); |
189 |
|
|
190 |
64 |
boolean needsUpdate = false; |
191 |
|
|
192 |
64 |
needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Method XWiki Class"); |
193 |
64 |
needsUpdate |= bclass.addTextField(METHOD_NAME_FIELD, "Method name", 30); |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
64 |
needsUpdate |= bclass.addTextAreaField(METHOD_CODE_FIELD, "Method body code", 40, 20, |
198 |
|
TextAreaClass.EditorType.TEXT, TextAreaClass.ContentType.WIKI_TEXT); |
199 |
|
|
200 |
64 |
if (needsUpdate) { |
201 |
38 |
this.update(doc); |
202 |
|
} |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
@param |
209 |
|
@throws |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
211 |
152 |
private void update(XWikiDocument doc) throws XWikiException... |
212 |
|
{ |
213 |
152 |
XWikiContext xcontext = getXWikiContext(); |
214 |
152 |
xcontext.getWiki().saveDocument(doc, xcontext); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
@param |
222 |
|
@param |
223 |
|
@return |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (33) |
Complexity: 8 |
Complexity Density: 0.38 |
|
225 |
256 |
private boolean initializeXClassDocumentMetadata(XWikiDocument doc, String title)... |
226 |
|
{ |
227 |
256 |
boolean needsUpdate = false; |
228 |
|
|
229 |
256 |
if (doc.getCreatorReference() == null) { |
230 |
152 |
needsUpdate = true; |
231 |
152 |
doc.setCreator(CLASS_AUTHOR); |
232 |
|
} |
233 |
256 |
if (doc.getAuthorReference() == null) { |
234 |
152 |
needsUpdate = true; |
235 |
152 |
doc.setAuthorReference(doc.getCreatorReference()); |
236 |
|
} |
237 |
256 |
if (doc.getParentReference() == null) { |
238 |
152 |
needsUpdate = true; |
239 |
152 |
doc.setParentReference(new EntityReference("XWikiClasses", EntityType.DOCUMENT) |
240 |
|
.appendParent(new EntityReference("XWiki", EntityType.SPACE))); |
241 |
|
} |
242 |
256 |
if (StringUtils.isBlank(doc.getTitle())) { |
243 |
152 |
needsUpdate = true; |
244 |
152 |
doc.setTitle(title); |
245 |
|
} |
246 |
256 |
if (StringUtils.isBlank(doc.getContent()) || !Syntax.XWIKI_2_0.equals(doc.getSyntax())) { |
247 |
152 |
needsUpdate = true; |
248 |
152 |
doc.setContent("{{include reference=\"XWiki.ClassSheet\" /}}"); |
249 |
152 |
doc.setSyntax(Syntax.XWIKI_2_0); |
250 |
|
} |
251 |
256 |
if (!doc.isHidden()) { |
252 |
152 |
needsUpdate = true; |
253 |
152 |
doc.setHidden(true); |
254 |
|
} |
255 |
|
|
256 |
256 |
return needsUpdate; |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
@return |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
408 |
private XWikiContext getXWikiContext()... |
263 |
|
{ |
264 |
408 |
return (XWikiContext) this.execution.getContext().getProperty("xwikicontext"); |
265 |
|
} |
266 |
|
} |