1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.uiextension.internal

File WikiUIExtension.java

 

Coverage histogram

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

Code metrics

4
24
13
1
217
107
16
0.67
1.85
13
1.23

Classes

Class Line # Actions
WikiUIExtension 44 24 0% 16 1
0.975609897.6%
 

Contributing tests

This file is covered by 2 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.uiextension.internal;
21   
22    import java.lang.reflect.Type;
23    import java.util.Collections;
24    import java.util.Map;
25   
26    import org.slf4j.Logger;
27    import org.slf4j.LoggerFactory;
28    import org.xwiki.component.wiki.WikiComponent;
29    import org.xwiki.component.wiki.WikiComponentScope;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.ObjectReference;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.block.WordBlock;
34    import org.xwiki.security.authorization.AuthorExecutor;
35    import org.xwiki.uiextension.UIExtension;
36   
37    /**
38    * Represents a dynamic component instance of a UI Extension (ie a UI Extension defined in a Wiki page) that we register
39    * against the Component Manager.
40    *
41    * @version $Id: d825177e801e2b580194f67a08f3ccc204d83862 $
42    * @since 4.2M3
43    */
 
44    public class WikiUIExtension implements UIExtension, WikiComponent
45    {
46    /**
47    * The key used for the UIX context in the script context.
48    */
49    public static final String CONTEXT_UIX_KEY = "uix";
50   
51    /**
52    * The key used for the UIX document in the UIX context.
53    */
54    public static final String CONTEXT_UIX_DOC_KEY = "doc";
55   
56    private static final Logger LOGGER = LoggerFactory.getLogger(WikiUIExtension.class);
57   
58    /**
59    * @see #WikiUIExtension
60    */
61    private final DocumentReference documentReference;
62   
63    /**
64    * @see #WikiUIExtension
65    */
66    private final DocumentReference authorReference;
67   
68    /**
69    * @see #WikiUIExtension
70    */
71    private final String id;
72   
73    /**
74    * @see #WikiUIExtension
75    */
76    private final String extensionPointId;
77   
78    /**
79    * @see #WikiUIExtension
80    */
81    private final String roleHint;
82   
83    private final AuthorExecutor authorExecutor;
84   
85    /**
86    * Parameter manager for this extension.
87    */
88    private WikiUIExtensionParameters parameters;
89   
90    /**
91    * The renderer for this extensions.
92    */
93    private WikiUIExtensionRenderer renderer;
94   
95    /**
96    * @see #setScope(org.xwiki.component.wiki.WikiComponentScope)
97    */
98    private WikiComponentScope scope = WikiComponentScope.WIKI;
99   
100    /**
101    * Default constructor.
102    *
103    * @param roleHint the role hint of the component to create
104    * @param id the id of the extension
105    * @param extensionPointId ID of the extension point this extension is designed for
106    * @param objectReference the reference of the object holding this extension
107    * @param authorReference the reference of the author of the document holding this extension
108    * @param authorExecutor the executor used to execute the extension with the proper user rights
109    */
 
110  129 toggle public WikiUIExtension(String roleHint, String id, String extensionPointId, ObjectReference objectReference,
111    DocumentReference authorReference, AuthorExecutor authorExecutor)
112    {
113  129 this.roleHint = roleHint;
114  129 this.id = id;
115  129 this.extensionPointId = extensionPointId;
116  129 this.authorReference = authorReference;
117  129 this.documentReference = (DocumentReference) objectReference.getParent();
118   
119  129 this.authorExecutor = authorExecutor;
120    }
121   
122    /**
123    * Set the extension parameters.
124    *
125    * @param parameters the extension parameters
126    */
 
127  128 toggle public void setParameters(WikiUIExtensionParameters parameters)
128    {
129  128 this.parameters = parameters;
130    }
131   
132    /**
133    * Set the extension renderer.
134    *
135    * @param renderer the extension renderer
136    */
 
137  128 toggle public void setRenderer(WikiUIExtensionRenderer renderer)
138    {
139  128 this.renderer = renderer;
140    }
141   
142    /**
143    * Set the scope of the extension.
144    *
145    * @param scope the scope of the extension
146    */
 
147  129 toggle public void setScope(WikiComponentScope scope)
148    {
149  129 this.scope = scope;
150    }
151   
 
152  207 toggle @Override
153    public String getId()
154    {
155  207 return this.id;
156    }
157   
 
158  18122 toggle @Override
159    public String getExtensionPointId()
160    {
161  18122 return this.extensionPointId;
162    }
163   
 
164  2965 toggle @Override
165    public Map<String, String> getParameters()
166    {
167  2965 if (this.parameters != null) {
168  2964 return this.parameters.get();
169    } else {
170  1 return Collections.emptyMap();
171    }
172    }
173   
 
174  1294 toggle @Override
175    public Block execute()
176    {
177  1294 if (this.renderer != null) {
178  1293 try {
179  1293 return this.authorExecutor.call(this.renderer::execute, getAuthorReference());
180    } catch (Exception e) {
181  0 LOGGER.error("Error while executing transformation for extension [{}]", documentReference.toString());
182    }
183    }
184   
185  1 return new WordBlock("");
186    }
187   
 
188  385 toggle @Override
189    public DocumentReference getDocumentReference()
190    {
191  385 return documentReference;
192    }
193   
 
194  2684 toggle @Override
195    public DocumentReference getAuthorReference()
196    {
197  2684 return authorReference;
198    }
199   
 
200  133 toggle @Override
201    public Type getRoleType()
202    {
203  133 return UIExtension.class;
204    }
205   
 
206  133 toggle @Override
207    public String getRoleHint()
208    {
209  133 return roleHint;
210    }
211   
 
212  133 toggle @Override
213    public WikiComponentScope getScope()
214    {
215  133 return scope;
216    }
217    }