1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.wiki.internal

File DefaultWikiComponentBuilderEventListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

20
73
10
1
266
157
22
0.3
7.3
10
2.2

Classes

Class Line # Actions
DefaultWikiComponentBuilderEventListener 57 73 0% 22 1
0.9902912499%
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Initializes the XClasses required by {@link DefaultWikiComponentBuilder}.
50    *
51    * @version $Id: f892b7c957b2d8747c1f6fd6b0675554d648bb4f $
52    * @since 4.2M3
53    */
54    @Component
55    @Named("defaultWikiComponentBuilderEventListener")
56    @Singleton
 
57    public class DefaultWikiComponentBuilderEventListener implements EventListener, WikiComponentConstants
58    {
59    /**
60    * The logger to log.
61    */
62    @Inject
63    private Logger logger;
64   
65    /**
66    * Our execution. Needed to access the XWiki context.
67    */
68    @Inject
69    private Execution execution;
70   
71    /**
72    * Used to serialize wiki pages reference in the log.
73    */
74    @Inject
75    @Named("compactwiki")
76    private EntityReferenceSerializer<String> compactWikiSerializer;
77   
 
78  88 toggle @Override
79    public List<Event> getEvents()
80    {
81  88 return Arrays.<Event> asList(new ApplicationReadyEvent(), new WikiReadyEvent());
82    }
83   
 
84  352 toggle @Override
85    public String getName()
86    {
87  352 return "defaultWikiComponentBuilderEventListener";
88    }
89   
 
90  78 toggle @Override
91    public void onEvent(Event arg0, Object arg1, Object arg2)
92    {
93  78 try {
94  78 this.installOrUpdateComponentXClass();
95  78 this.installOrUpdateComponentRequirementXClass();
96  78 this.installOrUpdateComponentMethodXClass();
97  78 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    * Verify that the {@link #INTERFACE_CLASS} exists and is up-to-date (act if not).
105    *
106    * @throws com.xpn.xwiki.XWikiException on failure
107    */
 
108  78 toggle private void installOrUpdateComponentInterfaceXClass() throws XWikiException
109    {
110  78 XWikiContext xcontext = getXWikiContext();
111  78 XWikiDocument doc = xcontext.getWiki().getDocument(INTERFACE_CLASS_REFERENCE, xcontext);
112   
113  78 BaseClass bclass = doc.getXClass();
114  78 bclass.setDocumentReference(doc.getDocumentReference());
115   
116  78 boolean needsUpdate = false;
117   
118  78 needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Implements Interface XWiki Class");
119  78 needsUpdate |= bclass.addTextField(INTERFACE_NAME_FIELD, "Interface Qualified Name", 30);
120   
121  78 if (needsUpdate) {
122  36 this.update(doc);
123    }
124    }
125   
126    /**
127    * Verify that the {@link #COMPONENT_CLASS} exists and is up-to-date (act if not).
128    *
129    * @throws XWikiException on failure
130    */
 
131  78 toggle private void installOrUpdateComponentXClass() throws XWikiException
132    {
133  78 XWikiContext xcontext = getXWikiContext();
134  78 XWikiDocument doc = xcontext.getWiki().getDocument(COMPONENT_CLASS_REFERENCE, xcontext);
135   
136  78 BaseClass bclass = doc.getXClass();
137  78 bclass.setDocumentReference(doc.getDocumentReference());
138   
139  78 boolean needsUpdate = false;
140   
141  78 needsUpdate |= initializeXClassDocumentMetadata(doc, "Wiki Component XWiki Class");
142  78 needsUpdate |= bclass.addTextField(COMPONENT_ROLE_TYPE_FIELD, "Component Role Type", 30);
143  78 needsUpdate |= bclass.addTextField(COMPONENT_ROLE_HINT_FIELD, "Component Role Hint", 30);
144  78 needsUpdate |= bclass.addStaticListField(COMPONENT_SCOPE_FIELD, "Component Scope", 1, false,
145    "wiki=Current Wiki|user=Current User|global=Global", "select");
146   
147  78 if (needsUpdate) {
148  36 this.update(doc);
149    }
150    }
151   
152    /**
153    * Verify that the {@link #DEPENDENCY_CLASS} exists and is up-to-date (act if not).
154    *
155    * @throws XWikiException on failure
156    */
 
157  78 toggle private void installOrUpdateComponentRequirementXClass() throws XWikiException
158    {
159  78 XWikiContext xcontext = getXWikiContext();
160  78 XWikiDocument doc = xcontext.getWiki().getDocument(DEPENDENCY_CLASS_REFERENCE, xcontext);
161   
162  78 BaseClass bclass = doc.getXClass();
163  78 bclass.setDocumentReference(doc.getDocumentReference());
164   
165  78 boolean needsUpdate = false;
166   
167  78 needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Dependency XWiki Class");
168  78 needsUpdate |= bclass.addTextField(COMPONENT_ROLE_TYPE_FIELD, "Dependency Role Type", 30);
169  78 needsUpdate |= bclass.addTextField(COMPONENT_ROLE_HINT_FIELD, "Dependency Role Hint", 30);
170  78 needsUpdate |= bclass.addTextField(DEPENDENCY_BINDING_NAME_FIELD, "Binding name", 30);
171   
172  78 if (needsUpdate) {
173  36 this.update(doc);
174    }
175    }
176   
177    /**
178    * Verify that the {@link #METHOD_CLASS} exists and is up-to-date (act if not).
179    *
180    * @throws XWikiException on failure
181    */
 
182  78 toggle private void installOrUpdateComponentMethodXClass() throws XWikiException
183    {
184  78 XWikiContext xcontext = getXWikiContext();
185  78 XWikiDocument doc = xcontext.getWiki().getDocument(METHOD_CLASS_REFRENCE, xcontext);
186   
187  78 BaseClass bclass = doc.getXClass();
188  78 bclass.setDocumentReference(doc.getDocumentReference());
189   
190  78 boolean needsUpdate = false;
191   
192  78 needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Method XWiki Class");
193  78 needsUpdate |= bclass.addTextField(METHOD_NAME_FIELD, "Method name", 30);
194   
195    // This field contains wiki syntax (usually some scripting macr) but it's technical content that should be
196    // edited with a Text editor.
197  78 needsUpdate |= bclass.addTextAreaField(METHOD_CODE_FIELD, "Method body code", 40, 20,
198    TextAreaClass.EditorType.TEXT, TextAreaClass.ContentType.WIKI_TEXT);
199   
200  78 if (needsUpdate) {
201  36 this.update(doc);
202    }
203    }
204   
205    /**
206    * Utility method for updating a wiki macro class definition document.
207    *
208    * @param doc xwiki document containing the wiki macro class.
209    * @throws XWikiException if an error occurs while saving the document.
210    */
 
211  144 toggle private void update(XWikiDocument doc) throws XWikiException
212    {
213  144 XWikiContext xcontext = getXWikiContext();
214  144 xcontext.getWiki().saveDocument(doc, xcontext);
215    }
216   
217    /**
218    * Helper method to prepare a document that will hold an XClass definition, setting its initial metadata, if needed
219    * (author, title, parent, content, etc.).
220    *
221    * @param doc the document to prepare
222    * @param title the title to set
223    * @return true if the doc has been modified and needs saving, false otherwise
224    */
 
225  312 toggle private boolean initializeXClassDocumentMetadata(XWikiDocument doc, String title)
226    {
227  312 boolean needsUpdate = false;
228   
229  312 if (doc.getCreatorReference() == null) {
230  144 needsUpdate = true;
231  144 doc.setCreator(CLASS_AUTHOR);
232    }
233  312 if (doc.getAuthorReference() == null) {
234  144 needsUpdate = true;
235  144 doc.setAuthorReference(doc.getCreatorReference());
236    }
237  312 if (doc.getParentReference() == null) {
238  144 needsUpdate = true;
239  144 doc.setParentReference(new EntityReference("XWikiClasses", EntityType.DOCUMENT)
240    .appendParent(new EntityReference("XWiki", EntityType.SPACE)));
241    }
242  312 if (StringUtils.isBlank(doc.getTitle())) {
243  144 needsUpdate = true;
244  144 doc.setTitle(title);
245    }
246  312 if (StringUtils.isBlank(doc.getContent()) || !Syntax.XWIKI_2_0.equals(doc.getSyntax())) {
247  144 needsUpdate = true;
248  144 doc.setContent("{{include reference=\"XWiki.ClassSheet\" /}}");
249  144 doc.setSyntax(Syntax.XWIKI_2_0);
250    }
251  312 if (!doc.isHidden()) {
252  144 needsUpdate = true;
253  144 doc.setHidden(true);
254    }
255   
256  312 return needsUpdate;
257    }
258   
259    /**
260    * @return the XWikiContext extracted from the execution.
261    */
 
262  456 toggle private XWikiContext getXWikiContext()
263    {
264  456 return (XWikiContext) this.execution.getContext().getProperty("xwikicontext");
265    }
266    }