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

File PanelWikiUIExtension.java

 

Coverage histogram

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

Code metrics

0
28
10
1
207
110
11
0.39
2.8
10
1.1

Classes

Class Line # Actions
PanelWikiUIExtension 52 28 0% 11 3
0.9210526392.1%
 

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.panels.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.manager.ComponentLookupException;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.component.wiki.WikiComponent;
31    import org.xwiki.component.wiki.WikiComponentScope;
32    import org.xwiki.job.event.status.JobProgressManager;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35    import org.xwiki.rendering.block.Block;
36    import org.xwiki.rendering.block.CompositeBlock;
37    import org.xwiki.rendering.block.XDOM;
38    import org.xwiki.rendering.internal.transformation.MutableRenderingContext;
39    import org.xwiki.rendering.syntax.Syntax;
40    import org.xwiki.rendering.transformation.RenderingContext;
41    import org.xwiki.rendering.transformation.Transformation;
42    import org.xwiki.rendering.transformation.TransformationContext;
43    import org.xwiki.security.authorization.AuthorExecutor;
44    import org.xwiki.uiextension.UIExtension;
45   
46    /**
47    * Provides a bridge between Panels defined in XObjects and {@link UIExtension}.
48    *
49    * @version $Id: 6b3158781636054a1894bb1d2b21ec12ec1df932 $
50    * @since 4.3M1
51    */
 
52    public class PanelWikiUIExtension implements UIExtension, WikiComponent
53    {
54    /**
55    * The logger to log.
56    */
57    private static final Logger LOGGER = LoggerFactory.getLogger(PanelWikiUIExtension.class);
58   
59    /**
60    * Serializer used to transform the panel document reference into the panel ID, for example 'Panels.Quicklinks'.
61    */
62    private final EntityReferenceSerializer<String> serializer;
63   
64    /**
65    * @see #PanelWikiUIExtension(org.xwiki.model.reference.DocumentReference,
66    * org.xwiki.model.reference.DocumentReference, org.xwiki.rendering.block.XDOM,
67    * org.xwiki.rendering.syntax.Syntax, org.xwiki.component.manager.ComponentManager)
68    */
69    private final DocumentReference documentReference;
70   
71    /**
72    * @see #PanelWikiUIExtension(org.xwiki.model.reference.DocumentReference,
73    * org.xwiki.model.reference.DocumentReference, org.xwiki.rendering.block.XDOM,
74    * org.xwiki.rendering.syntax.Syntax, org.xwiki.component.manager.ComponentManager)
75    */
76    private final DocumentReference authorReference;
77   
78    /**
79    * @see #PanelWikiUIExtension(org.xwiki.model.reference.DocumentReference,
80    * org.xwiki.model.reference.DocumentReference, org.xwiki.rendering.block.XDOM,
81    * org.xwiki.rendering.syntax.Syntax, org.xwiki.component.manager.ComponentManager)
82    */
83    private final XDOM xdom;
84   
85    /**
86    * @see #PanelWikiUIExtension(org.xwiki.model.reference.DocumentReference,
87    * org.xwiki.model.reference.DocumentReference, org.xwiki.rendering.block.XDOM,
88    * org.xwiki.rendering.syntax.Syntax, org.xwiki.component.manager.ComponentManager)
89    */
90    private final Syntax syntax;
91   
92    /**
93    * Used to update the rendering context.
94    */
95    private final RenderingContext renderingContext;
96   
97    /**
98    * Used to transform the macros within the extension content.
99    */
100    private final Transformation macroTransformation;
101   
102    private final AuthorExecutor authorExecutor;
103   
104    private final JobProgressManager progress;
105   
106    /**
107    * Default constructor.
108    *
109    * @param documentReference The document in which the panel is defined
110    * @param authorReference The author of the document in which the panel is defined
111    * @param xdom The content to display for this panel
112    * @param syntax The syntax in which the content is written
113    * @param componentManager The XWiki content manager
114    * @throws ComponentLookupException If module dependencies are missing
115    */
 
116  183 toggle public PanelWikiUIExtension(DocumentReference documentReference, DocumentReference authorReference, XDOM xdom,
117    Syntax syntax, ComponentManager componentManager) throws ComponentLookupException
118    {
119  183 this.documentReference = documentReference;
120  183 this.authorReference = authorReference;
121  183 this.xdom = xdom;
122  183 this.syntax = syntax;
123  183 this.macroTransformation = componentManager.getInstance(Transformation.class, "macro");
124  183 this.serializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING);
125  183 this.renderingContext = componentManager.getInstance(RenderingContext.class);
126  183 this.authorExecutor = componentManager.getInstance(AuthorExecutor.class);
127  183 this.progress = componentManager.getInstance(JobProgressManager.class);
128    }
129   
 
130  1901 toggle @Override
131    public String getId()
132    {
133  1901 return serializer.serialize(documentReference);
134    }
135   
 
136  49540 toggle @Override
137    public String getExtensionPointId()
138    {
139  49540 return "platform.panels";
140    }
141   
 
142  616 toggle @Override
143    public DocumentReference getDocumentReference()
144    {
145  616 return documentReference;
146    }
147   
 
148  206 toggle @Override
149    public DocumentReference getAuthorReference()
150    {
151  206 return authorReference;
152    }
153   
 
154  22 toggle @Override
155    public Block execute()
156    {
157    // We need to clone the xdom to avoid transforming the original and make it useless after the first
158    // transformation
159  22 final XDOM transformedXDOM = xdom.clone();
160   
161  22 this.progress.startStep(getDocumentReference(), "panel.progress.execute", "Execute panel [{}]",
162    getDocumentReference());
163   
164    // Perform panel transformations with the right of the panel author
165  22 try {
166  22 this.authorExecutor.call(() -> {
167  22 TransformationContext transformationContext = new TransformationContext(transformedXDOM, syntax);
168  22 transformationContext.setId(getRoleHint());
169  22 ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation,
170    transformationContext, transformedXDOM);
171   
172  22 return null;
173    }, getAuthorReference());
174    } catch (Exception e) {
175  0 LOGGER.error("Error while executing transformation for panel [{}]", this.documentReference.toString());
176    } finally {
177  22 this.progress.endStep(getDocumentReference());
178    }
179   
180  22 return new CompositeBlock(transformedXDOM.getChildren());
181    }
182   
 
183  0 toggle @Override
184    public Map<String, String> getParameters()
185    {
186  0 return Collections.emptyMap();
187    }
188   
 
189  184 toggle @Override
190    public Type getRoleType()
191    {
192  184 return UIExtension.class;
193    }
194   
 
195  206 toggle @Override
196    public String getRoleHint()
197    {
198  206 return getId();
199    }
200   
 
201  184 toggle @Override
202    public WikiComponentScope getScope()
203    {
204    // TODO: handle scope dynamically, in the meantime it's hardcoded to "global" for backward compatibility
205  184 return WikiComponentScope.GLOBAL;
206    }
207    }