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

File DistributionInternalScriptService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart1.png
82% of files have more coverage

Code metrics

24
57
16
1
316
170
29
0.51
3.56
16
1.81

Classes

Class Line # Actions
DistributionInternalScriptService 63 57 0% 29 89
0.0824742248.2%
 

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.extension.distribution.internal;
21   
22    import java.util.Map;
23    import java.util.TreeMap;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Provider;
28    import javax.inject.Singleton;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.extension.CoreExtension;
32    import org.xwiki.extension.ExtensionId;
33    import org.xwiki.extension.distribution.internal.DistributionManager.DistributionState;
34    import org.xwiki.extension.distribution.internal.DocumentsModifiedDuringDistributionListener.DocumentStatus;
35    import org.xwiki.extension.distribution.internal.job.DistributionJob;
36    import org.xwiki.extension.distribution.internal.job.DistributionJobStatus;
37    import org.xwiki.job.event.status.JobStatus;
38    import org.xwiki.job.event.status.JobStatus.State;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.observation.EventListener;
41    import org.xwiki.rendering.block.Block;
42    import org.xwiki.rendering.renderer.BlockRenderer;
43    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
44    import org.xwiki.rendering.renderer.printer.WikiPrinter;
45    import org.xwiki.rendering.transformation.RenderingContext;
46    import org.xwiki.script.internal.safe.ScriptSafeProvider;
47    import org.xwiki.script.service.ScriptService;
48    import org.xwiki.text.StringUtils;
49   
50    import com.xpn.xwiki.XWikiContext;
51   
52    /**
53    * Provide helpers to manage running distribution.
54    * <p>
55    * Note: this script service is strictly internal and intended to be used only from templates for now.
56    *
57    * @version $Id: 5c90f93e85411cb1ab503e07da6523e6b4a5b93f $
58    * @since 4.2M3
59    */
60    @Component
61    @Named("distribution")
62    @Singleton
 
63    public class DistributionInternalScriptService implements ScriptService
64    {
65    /**
66    * The key under which the last encountered error is stored in the current execution context.
67    */
68    public static final String EXTENSIONERROR_KEY = "scriptservice.distribution.error";
69   
70    /**
71    * The component used to get information about the current distribution.
72    */
73    @Inject
74    protected DistributionManager distributionManager;
75   
76    /**
77    * Used to access current {@link XWikiContext}.
78    */
79    @Inject
80    protected Provider<XWikiContext> xcontextProvider;
81   
82    /**
83    * Provides safe objects for scripts.
84    */
85    @Inject
86    @SuppressWarnings("rawtypes")
87    private ScriptSafeProvider scriptProvider;
88   
89    /**
90    * Used to access HTML renderer.
91    */
92    @Inject
93    @Named("xhtml/1.0")
94    private BlockRenderer xhtmlRenderer;
95   
96    @Inject
97    @Named(DocumentsModifiedDuringDistributionListener.NAME)
98    private EventListener modifiedDocumentsListener;
99   
100    @Inject
101    private RenderingContext renderingContext;
102   
103    /**
104    * @param <T> the type of the object
105    * @param unsafe the unsafe object
106    * @return the safe version of the passed object
107    */
 
108  0 toggle @SuppressWarnings("unchecked")
109    private <T> T safe(T unsafe)
110    {
111  0 return (T) this.scriptProvider.get(unsafe);
112    }
113   
114    // Distribution
115   
116    /**
117    * @return the current distribution state
118    */
 
119  0 toggle public DistributionState getState()
120    {
121  0 XWikiContext xcontext = this.xcontextProvider.get();
122   
123  0 return getState(xcontext.getWikiId());
124    }
125   
126    /**
127    * @return the current distribution state
128    */
 
129  0 toggle public DistributionState getState(String wiki)
130    {
131  0 XWikiContext xcontext = this.xcontextProvider.get();
132   
133  0 return xcontext.isMainWiki(wiki) ? this.distributionManager.getFarmDistributionState()
134    : this.distributionManager.getWikiDistributionState(wiki);
135    }
136   
137    /**
138    * @return the extension that defines the current distribution
139    */
 
140  0 toggle public CoreExtension getDistributionExtension()
141    {
142  0 return this.distributionManager.getDistributionExtension();
143    }
144   
145    /**
146    * @return if the main wiki has a default UI configured
147    */
 
148  0 toggle public boolean hasMainDefaultUIExtension()
149    {
150  0 ExtensionId extension = this.distributionManager.getMainUIExtensionId();
151  0 return extension != null && StringUtils.isNotBlank(extension.getId());
152    }
153   
154    /**
155    * @return if wikis have a default UI configured
156    */
 
157  7 toggle public boolean hasWikiDefaultUIExtension()
158    {
159  7 ExtensionId extension = this.distributionManager.getWikiUIExtensionId();
160  7 return extension != null && StringUtils.isNotBlank(extension.getId());
161    }
162   
163    /**
164    * @return the recommended user interface for current wiki
165    */
 
166  0 toggle public ExtensionId getUIExtensionId()
167    {
168  0 XWikiContext xcontext = this.xcontextProvider.get();
169   
170  0 return getUIExtensionId(xcontext.getWikiId());
171    }
172   
173    /**
174    * @param wiki the wiki
175    * @return the recommended user interface for passed wiki
176    */
 
177  0 toggle public ExtensionId getUIExtensionId(String wiki)
178    {
179  0 XWikiContext xcontext = this.xcontextProvider.get();
180   
181  0 return xcontext.isMainWiki(wiki) ? this.distributionManager.getMainUIExtensionId() : this.distributionManager
182    .getWikiUIExtensionId();
183    }
184   
185    /**
186    * @return the previous status of the distribution job for the current wiki (e.g. from last time the distribution
187    * was upgraded)
188    */
 
189  0 toggle public DistributionJobStatus< ? > getPreviousJobStatus()
190    {
191  0 XWikiContext xcontext = this.xcontextProvider.get();
192   
193  0 return getPreviousJobStatus(xcontext.getWikiId());
194    }
195   
196    /**
197    * @param wiki the wiki for which to retrieve the previous status of the distribution job
198    * @return the previous status of the distribution job for the specified wiki (e.g. from last time the distribution
199    * was upgraded)
200    */
 
201  0 toggle public DistributionJobStatus< ? > getPreviousJobStatus(String wiki)
202    {
203  0 XWikiContext xcontext = this.xcontextProvider.get();
204   
205  0 return xcontext.isMainWiki(wiki) ? this.distributionManager.getPreviousFarmJobStatus()
206    : this.distributionManager.getPreviousWikiJobStatus(wiki);
207    }
208   
209    /**
210    * @return indicate of it's allowed to display the Distribution Wizard in the current context
211    */
 
212  0 toggle public boolean canDisplayDistributionWizard()
213    {
214  0 return this.distributionManager.canDisplayDistributionWizard();
215    }
216   
217    /**
218    * @return the status of the current distribution job
219    */
 
220  72 toggle public DistributionJobStatus< ? > getJobStatus()
221    {
222  72 DistributionJob job = this.distributionManager.getCurrentDistributionJob();
223   
224  72 return job != null ? (DistributionJobStatus< ? >) job.getStatus() : null;
225    }
226   
227    /**
228    * @return the HTML resulting in the executing of the current step
229    */
 
230  0 toggle public String renderCurrentStepToXHTML()
231    {
232  0 return renderCurrentStepToXHTML(this.renderingContext.getTransformationId());
233    }
234   
 
235  0 toggle public String renderCurrentStepToXHTML(String transformationId)
236    {
237  0 DistributionJob job = this.distributionManager.getCurrentDistributionJob();
238   
239  0 if (job != null) {
240  0 JobStatus jobStatus = job.getStatus();
241   
242  0 if (jobStatus != null) {
243  0 State jobState = jobStatus.getState();
244   
245  0 if (jobState == State.RUNNING || jobState == State.WAITING) {
246  0 Block block = job.getCurrentStep().execute();
247   
248  0 WikiPrinter printer = new DefaultWikiPrinter();
249   
250  0 this.xhtmlRenderer.render(block, printer);
251   
252  0 return printer.toString();
253    }
254    }
255    }
256   
257  0 return null;
258    }
259   
260    /**
261    * @return the document modified during the Distribution Wizard execution
262    * @since 5.4RC1
263    */
 
264  0 toggle public Map<DocumentReference, DocumentStatus> getModifiedDocuments()
265    {
266  0 return ((DocumentsModifiedDuringDistributionListener) this.modifiedDocumentsListener).getDocuments().get(
267    this.xcontextProvider.get().getWikiId());
268    }
269   
270    /**
271    * @return the document modified during the Distribution Wizard execution
272    * @since 5.4RC1
273    */
 
274  0 toggle public Map<String, Map<String, Map<String, Map<String, DocumentStatus>>>> getModifiedDocumentsTree()
275    {
276  0 Map<DocumentReference, DocumentStatus> documents =
277    ((DocumentsModifiedDuringDistributionListener) this.modifiedDocumentsListener).getDocuments().get(
278    this.xcontextProvider.get().getWikiId());
279   
280  0 Map<String, Map<String, Map<String, Map<String, DocumentStatus>>>> tree =
281    new TreeMap<String, Map<String, Map<String, Map<String, DocumentStatus>>>>();
282   
283  0 if (documents != null) {
284  0 for (Map.Entry<DocumentReference, DocumentStatus> document : documents.entrySet()) {
285  0 DocumentReference reference = document.getKey();
286  0 String wiki = reference.getWikiReference().getName();
287    // TODO: add support for subspaces
288  0 String space = reference.getLastSpaceReference().getName();
289  0 String page = reference.getName();
290  0 String locale = reference.getLocale() != null ? reference.getLocale().toString() : "";
291   
292  0 Map<String, Map<String, Map<String, DocumentStatus>>> spaces = tree.get(wiki);
293  0 if (spaces == null) {
294  0 spaces = new TreeMap<String, Map<String, Map<String, DocumentStatus>>>();
295  0 tree.put(wiki, spaces);
296    }
297   
298  0 Map<String, Map<String, DocumentStatus>> pages = spaces.get(space);
299  0 if (pages == null) {
300  0 pages = new TreeMap<String, Map<String, DocumentStatus>>();
301  0 spaces.put(space, pages);
302    }
303   
304  0 Map<String, DocumentStatus> locales = pages.get(page);
305  0 if (locales == null) {
306  0 locales = new TreeMap<String, DocumentStatus>();
307  0 pages.put(page, locales);
308    }
309   
310  0 locales.put(locale, document.getValue());
311    }
312    }
313   
314  0 return tree;
315    }
316    }