1. Project Clover database Tue Dec 20 2016 21:24:09 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

No tests hitting this source file were found.

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  60 toggle @Override
79    public List<Event> getEvents()
80    {
81  60 return Arrays.<Event> asList(new ApplicationReadyEvent(), new WikiReadyEvent());
82    }
83   
 
84  240 toggle @Override
85    public String getName()
86    {
87  240 return "defaultWikiComponentBuilderEventListener";
88    }
89   
 
90  64 toggle @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    * 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  64 toggle 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    * Verify that the {@link #COMPONENT_CLASS} exists and is up-to-date (act if not).
128    *
129    * @throws XWikiException on failure
130    */
 
131  64 toggle 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    * Verify that the {@link #DEPENDENCY_CLASS} exists and is up-to-date (act if not).
154    *
155    * @throws XWikiException on failure
156    */
 
157  64 toggle 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    * Verify that the {@link #METHOD_CLASS} exists and is up-to-date (act if not).
179    *
180    * @throws XWikiException on failure
181    */
 
182  64 toggle 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    // This field contains wiki syntax (usually some scripting macr) but it's technical content that should be
196    // edited with a Text editor.
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    * 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  152 toggle private void update(XWikiDocument doc) throws XWikiException
212    {
213  152 XWikiContext xcontext = getXWikiContext();
214  152 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  256 toggle 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 the XWikiContext extracted from the execution.
261    */
 
262  408 toggle private XWikiContext getXWikiContext()
263    {
264  408 return (XWikiContext) this.execution.getContext().getProperty("xwikicontext");
265    }
266    }