1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.platform.flavor.script

File FlavorManagerScriptService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

2
36
14
1
250
121
19
0.53
2.57
14
1.36

Classes

Class Line # Actions
FlavorManagerScriptService 54 36 0% 19 52
0.00%
 

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.platform.flavor.script;
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.xwiki.component.annotation.Component;
30    import org.xwiki.extension.Extension;
31    import org.xwiki.extension.ExtensionId;
32    import org.xwiki.extension.repository.result.IterableResult;
33    import org.xwiki.extension.script.AbstractExtensionScriptService;
34    import org.xwiki.job.Job;
35    import org.xwiki.job.JobException;
36    import org.xwiki.platform.flavor.FlavorManager;
37    import org.xwiki.platform.flavor.FlavorQuery;
38    import org.xwiki.platform.flavor.internal.job.FlavorSearchJob;
39    import org.xwiki.platform.flavor.internal.job.FlavorSearchStatus;
40    import org.xwiki.platform.flavor.job.FlavorSearchRequest;
41    import org.xwiki.rest.XWikiRestException;
42    import org.xwiki.rest.resources.job.JobStatusResource;
43    import org.xwiki.rest.url.ParametrizedRestURLGenerator;
44   
45    /**
46    * Script service to find flavors.
47    *
48    * @version $Id: e5e032a824b5c8b412d66650282d289dcee3dfe4 $
49    * @since 7.1M2
50    */
51    @Component
52    @Named(FlavorManagerScriptService.ROLEHINT)
53    @Singleton
 
54    public class FlavorManagerScriptService extends AbstractExtensionScriptService
55    {
56    /**
57    * The role hint of this component.
58    */
59    public static final String ROLEHINT = "flavor";
60   
61    /**
62    * The id to use in the jobs searching for flavors.
63    */
64    public static final String SEARCH_ID = "search";
65   
66    @Inject
67    private FlavorManager flavorManager;
68   
69    @Inject
70    @Named(JobStatusResource.NAME)
71    private ParametrizedRestURLGenerator<List<String>> jobstatusURLGenerator;
72   
 
73  0 toggle private List<String> getSearchJobId(String namespace)
74    {
75  0 return Arrays.asList(ROLEHINT, SEARCH_ID, namespace);
76    }
77   
78    /**
79    * Creates a flavor query.
80    *
81    * @return a new flavor query
82    */
 
83  0 toggle public FlavorQuery createFlavorQuery()
84    {
85  0 return new FlavorQuery();
86    }
87   
88    /**
89    * Creates a flavor query.
90    *
91    * @param query the query to execute
92    * @return a new flavor query
93    */
 
94  0 toggle public FlavorQuery createFlavorQuery(String query)
95    {
96  0 return new FlavorQuery(query);
97    }
98   
99    /**
100    * Get all flavors matching a query.
101    *
102    * @param query query to execute
103    * @return flavors matching the query
104    * @deprecated since 8.0RC1, use {@link #searchFlavors(FlavorQuery)} instead
105    */
 
106  0 toggle @Deprecated
107    public IterableResult<Extension> getFlavors(FlavorQuery query)
108    {
109  0 setError(null);
110   
111  0 try {
112  0 return this.flavorManager.getFlavors(query);
113    } catch (Exception e) {
114  0 setError(e);
115    }
116   
117  0 return null;
118    }
119   
120    /**
121    * Search for all flavors matching a query.
122    *
123    * @param query query to execute
124    * @return flavors matching the query
125    * @since 8.0RC1
126    */
 
127  0 toggle public IterableResult<Extension> searchFlavors(FlavorQuery query)
128    {
129  0 setError(null);
130   
131  0 try {
132  0 return this.flavorManager.searchFlavors(query);
133    } catch (Exception e) {
134  0 setError(e);
135    }
136   
137  0 return null;
138    }
139   
140    /**
141    * @param namespace the namespace where to validate the flavors
142    * @return the status of the current or last flavor search
143    * @since 8.0
144    */
 
145  0 toggle public FlavorSearchStatus getSearchValidFlavorsStatus(String namespace)
146    {
147  0 return (FlavorSearchStatus) getJobStatus(getSearchJobId(namespace));
148    }
149   
150    /**
151    * @return the status of the current or last flavor search
152    * @since 8.0
153    */
 
154  0 toggle public FlavorSearchStatus getSearchValidFlavorsStatus()
155    {
156  0 return getSearchValidFlavorsStatus(currentNamespace());
157    }
158   
159    /**
160    * @param namespace the namespace where to validate the flavors
161    * @return the REST URL to access the job status
162    * @since 8.0
163    */
 
164  0 toggle public String getSearchValidFlavorsStatusURL(String namespace)
165    {
166  0 setError(null);
167   
168  0 try {
169  0 return this.jobstatusURLGenerator.getURL(getSearchJobId(namespace)).getPath();
170    } catch (XWikiRestException e) {
171  0 setError(e);
172    }
173   
174  0 return null;
175    }
176   
177    /**
178    * @return the valid flavors found in the currently running job status
179    */
 
180  0 toggle public List<Extension> getValidExtensions()
181    {
182  0 FlavorSearchStatus status = getSearchValidFlavorsStatus();
183   
184  0 return status != null ? status.getFlavors() : null;
185    }
186   
 
187  0 toggle private String currentNamespace()
188    {
189  0 return "wiki:" + this.xcontextProvider.get().getWikiId();
190    }
191   
192    /**
193    * @return the REST URL to access the job status
194    * @since 8.0
195    */
 
196  0 toggle public String getSearchValidFlavorsStatusURL()
197    {
198  0 return getSearchValidFlavorsStatusURL(currentNamespace());
199    }
200   
201    /**
202    * Start searching for valid flavors.
203    *
204    * @param namespace the namespace where to validate the flavors
205    * @return the {@link Job} searching the flavors
206    * @since 8.0RC1
207    */
 
208  0 toggle public Job searchValidFlavors(String namespace)
209    {
210  0 setError(null);
211   
212  0 Job job = null;
213  0 try {
214  0 FlavorSearchRequest flavorRequest = new FlavorSearchRequest();
215   
216  0 flavorRequest.setId(getSearchJobId(namespace));
217  0 flavorRequest.addNamespace(namespace);
218   
219  0 setRightsProperties(flavorRequest);
220   
221  0 job = this.jobExecutor.execute(FlavorSearchJob.JOBTYPE, flavorRequest);
222    } catch (JobException e) {
223  0 setError(e);
224    }
225   
226  0 return job;
227    }
228   
229    /**
230    * Start searching for valid flavors in the context of the current wiki.
231    *
232    * @return the {@link Job} searching the flavors
233    * @since 8.0
234    */
 
235  0 toggle public Job searchValidFlavors()
236    {
237  0 return searchValidFlavors(currentNamespace());
238    }
239   
240    /**
241    * Get the flavor installed on a given wiki.
242    *
243    * @param wikiId id of the wiki
244    * @return the id of the flavor installed on the given wiki or null if there is no flavor installed
245    */
 
246  0 toggle public ExtensionId getFlavorOfWiki(String wikiId)
247    {
248  0 return this.flavorManager.getFlavorOfWiki(wikiId);
249    }
250    }