View Javadoc

1   /*
2    * Copyright 2008-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.issues.bugzilla;
17  
18  import java.net.MalformedURLException;
19  import java.net.URL;
20  
21  import org.apache.maven.reporting.MavenReportException;
22  import org.codehaus.plexus.util.StringUtils;
23  
24  import de.smartics.maven.issues.ArtifactVersionRange;
25  import de.smartics.maven.issues.QueryData;
26  
27  /**
28   * The data used to construct issue queries for Bugzilla.
29   *
30   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
31   * @version $Revision:591 $
32   */
33  public class BugzillaQueryData implements QueryData
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * Defines the filter parameters to restrict which issues are retrieved from
43     * Bugzilla. The filter parameter must use the same format of url parameters
44     * that is used in a Bugzilla search.
45     */
46    private String filter;
47  
48    /**
49     * Sets the keywords that you want to limit your report to include.
50     */
51    private String keywords;
52  
53    /**
54     * Sets the handling of the keywords. May have any value but Bugzilla supports
55     * currently <code>allwords</code>, <code>nowords</code>,
56     * <code>anywords</code>.
57     */
58    private String keywordsType;
59  
60    /**
61     * Sets the status(es) that you want to limit your report to include. Valid
62     * statuses are: UNCONFIRMED, NEW, ASSIGNED, REOPENED, RESOLVED, VERIFIED, and
63     * CLOSED. Multiple values can be separated by commas.
64     */
65    private String status;
66  
67    /**
68     * Sets the resolution(s) that you want to limit your report to include. Valid
69     * statuses are: FIXED, INVALID, WONTFIX, LATER, REMIND, DUPLICATE,
70     * WORKSFORME, and MOVED. Multiple values can be separated by commas.
71     */
72    private String resolution;
73  
74    /**
75     * Sets the component(s) that you want to limit your report to include.
76     * Multiple components can be separated by commas. If this is set to empty -
77     * that means all components.
78     */
79    private String component;
80  
81    /**
82     * Sets the product(s) that you want to limit your report to include. Multiple
83     * products can be separated by commas. If this is set to empty - that means
84     * all products.
85     */
86    private String product;
87  
88    /**
89     * The order of the bugs returned.
90     */
91    private String order;
92  
93    /**
94     * The name of the query to execute. If the query name is specified none of
95     * the other query properties is taken into account.
96     */
97    private String queryName;
98  
99    /**
100    * The type of the command executed. This property is currently only relevant
101    * is <code>queryName</code> is set.
102    */
103   private String commandType;
104 
105   /**
106    * The version range to query for.
107    */
108   private ArtifactVersionRange versionRange;
109 
110   // ****************************** Initializer *******************************
111 
112   // ****************************** Constructors ******************************
113 
114   /**
115    * Default constructor.
116    */
117   public BugzillaQueryData()
118   {
119   }
120 
121   // ****************************** Inner Classes *****************************
122 
123   // ********************************* Methods ********************************
124 
125   // --- init -----------------------------------------------------------------
126 
127   // --- get&set --------------------------------------------------------------
128 
129   /**
130    * Returns the value for filter.
131    * <p>
132    * Defines the filter parameters to restrict which issues are retrieved from
133    * Bugzilla. The filter parameter must use the same format of url parameters
134    * that is used in a Bugzilla search.
135    *
136    * @return the value for filter.
137    */
138   public String getFilter()
139   {
140     return filter;
141   }
142 
143   /**
144    * Sets the value for filter.
145    * <p>
146    * Defines the filter parameters to restrict which issues are retrieved from
147    * Bugzilla. The filter parameter must use the same format of url parameters
148    * that is used in a Bugzilla search.
149    *
150    * @param filter the value for filter.
151    */
152   public void setFilter(final String filter)
153   {
154     this.filter = filter;
155   }
156 
157   /**
158    * Returns the value for keywords.
159    * <p>
160    * Sets the keywords that you want to limit your report to include.
161    *
162    * @return the value for keywords.
163    */
164   public String getKeywords()
165   {
166     return keywords;
167   }
168 
169   /**
170    * Sets the value for keywords.
171    * <p>
172    * Sets the keywords that you want to limit your report to include.
173    *
174    * @param keywords the value for keywords.
175    */
176   public void setKeywords(final String keywords)
177   {
178     this.keywords = keywords;
179   }
180 
181   /**
182    * Returns the value for keywordsType.
183    * <p>
184    * Sets the handling of the keywords. May have any value but Bugzilla supports
185    * currently <code>allwords</code>, <code>nowords</code>,
186    * <code>anywords</code>.
187    *
188    * @return the value for keywordsType.
189    */
190   public String getKeywordsType()
191   {
192     return keywordsType;
193   }
194 
195   /**
196    * Sets the value for keywordsType.
197    * <p>
198    * Sets the handling of the keywords. May have any value but Bugzilla supports
199    * currently <code>allwords</code>, <code>nowords</code>,
200    * <code>anywords</code>.
201    *
202    * @param keywordsType the value for keywordsType.
203    */
204   public void setKeywordsType(final String keywordsType)
205   {
206     this.keywordsType = keywordsType;
207   }
208 
209   /**
210    * Returns the value for status.
211    * <p>
212    * Sets the status(es) that you want to limit your report to include. Valid
213    * statuses are: UNCONFIRMED, NEW, ASSIGNED, REOPENED, RESOLVED, VERIFIED, and
214    * CLOSED. Multiple values can be separated by commas.
215    *
216    * @return the value for status.
217    */
218   public String getStatus()
219   {
220     return status;
221   }
222 
223   /**
224    * Sets the value for status.
225    * <p>
226    * Sets the status(es) that you want to limit your report to include. Valid
227    * statuses are: UNCONFIRMED, NEW, ASSIGNED, REOPENED, RESOLVED, VERIFIED, and
228    * CLOSED. Multiple values can be separated by commas.
229    *
230    * @param status the value for status.
231    */
232   public void setStatus(final String status)
233   {
234     this.status = status;
235   }
236 
237   /**
238    * Returns the value for resolution.
239    * <p>
240    * Sets the resolution(s) that you want to limit your report to include. Valid
241    * statuses are: FIXED, INVALID, WONTFIX, LATER, REMIND, DUPLICATE,
242    * WORKSFORME, and MOVED. Multiple values can be separated by commas.
243    *
244    * @return the value for resolution.
245    */
246   public String getResolution()
247   {
248     return resolution;
249   }
250 
251   /**
252    * Sets the value for resolution.
253    * <p>
254    * Sets the resolution(s) that you want to limit your report to include. Valid
255    * statuses are: FIXED, INVALID, WONTFIX, LATER, REMIND, DUPLICATE,
256    * WORKSFORME, and MOVED. Multiple values can be separated by commas.
257    *
258    * @param resolution the value for resolution.
259    */
260   public void setResolution(final String resolution)
261   {
262     this.resolution = resolution;
263   }
264 
265   /**
266    * Returns the value for component.
267    * <p>
268    * Sets the component(s) that you want to limit your report to include.
269    * Multiple components can be separated by commas. If this is set to empty -
270    * that means all components.
271    *
272    * @return the value for component.
273    */
274   public String getComponent()
275   {
276     return component;
277   }
278 
279   /**
280    * Sets the value for component.
281    * <p>
282    * Sets the component(s) that you want to limit your report to include.
283    * Multiple components can be separated by commas. If this is set to empty -
284    * that means all components.
285    *
286    * @param component the value for component.
287    */
288   public void setComponent(final String component)
289   {
290     this.component = component;
291   }
292 
293   /**
294    * Returns the value for product.
295    * <p>
296    * Sets the product(s) that you want to limit your report to include. Multiple
297    * products can be separated by commas. If this is set to empty - that means
298    * all products.
299    *
300    * @return the value for product.
301    */
302   public String getProduct()
303   {
304     return product;
305   }
306 
307   /**
308    * Sets the value for product.
309    * <p>
310    * Sets the product(s) that you want to limit your report to include. Multiple
311    * products can be separated by commas. If this is set to empty - that means
312    * all products.
313    *
314    * @param product the value for product.
315    */
316   public void setProduct(final String product)
317   {
318     this.product = product;
319   }
320 
321   /**
322    * Returns the order of the bugs returned.
323    *
324    * @return the order of the bugs returned.
325    */
326   public String getOrder()
327   {
328     return order;
329   }
330 
331   /**
332    * Sets the order of the bugs returned.
333    *
334    * @param order the order of the bugs returned.
335    */
336   public void setOrder(final String order)
337   {
338     this.order = order;
339   }
340 
341   /**
342    * Returns the name of the query to execute. If the query name is specified
343    * none of the other query properties is taken into account.
344    *
345    * @return the name of the query to execute.
346    */
347   public String getQueryName()
348   {
349     return queryName;
350   }
351 
352   /**
353    * Sets the name of the query to execute. If the query name is specified none
354    * of the other query properties is taken into account.
355    *
356    * @param queryName the name of the query to execute.
357    */
358   public void setQueryName(final String queryName)
359   {
360     this.queryName = queryName;
361   }
362 
363   /**
364    * Returns the type of the command executed. This property is currently only
365    * relevant is <code>queryName</code> is set.
366    *
367    * @return the type of the command executed.
368    */
369   public String getCommandType()
370   {
371     return commandType;
372   }
373 
374   /**
375    * Sets the type of the command executed. This property is currently only
376    * relevant is <code>queryName</code> is set.
377    *
378    * @param commandType the type of the command executed.
379    */
380   public void setCommandType(final String commandType)
381   {
382     this.commandType = commandType;
383   }
384 
385   /**
386    * Returns the version range to query for.
387    *
388    * @return the version range to query for.
389    */
390   public ArtifactVersionRange getVersionRange()
391   {
392     return versionRange;
393   }
394 
395   /**
396    * Sets the version range to query for.
397    *
398    * @param versionRange the version range to query for.
399    */
400   public void setVersionRange(final ArtifactVersionRange versionRange)
401   {
402     this.versionRange = versionRange;
403   }
404 
405   // --- business -------------------------------------------------------------
406 
407   /**
408    * {@inheritDoc}
409    *
410    * @see de.smartics.maven.issues.QueryData#createUrl(String)
411    */
412   public String createUrl(final String connectionUrl)
413     throws MavenReportException
414   {
415     final StringBuilder buffer = new StringBuilder(connectionUrl);
416     buffer.append("/buglist.cgi?");
417     if (StringUtils.isNotBlank(queryName))
418     {
419       appendNamedQuery(buffer);
420     }
421     else
422     {
423       createManualQuery(buffer);
424     }
425 
426     String urlString = buffer.toString();
427     try
428     {
429       if (urlString.endsWith("&"))
430       {
431         urlString = StringUtils.chop(urlString);
432       }
433       final String url = new URL(urlString).toString();
434       return url;
435     }
436     catch (final MalformedURLException e)
437     {
438       throw new MavenReportException("There was a syntax error in your URL: "
439                                      + urlString, e);
440     }
441   }
442 
443   /**
444    * Appends the parameters for a named query to the buffer.
445    *
446    * @param buffer the buffer to append to.
447    */
448   private void appendNamedQuery(final StringBuilder buffer)
449   {
450     appendParameterIfPresent(buffer, "cmdtype", commandType);
451     appendParameterIfPresent(buffer, "namedcmd", queryName);
452   }
453 
454   /**
455    * Appends the parameters for a query where the parameters have been set one
456    * by one or with a filter.
457    *
458    * @param buffer the buffer to append to.
459    */
460   private void createManualQuery(final StringBuilder buffer)
461   {
462     appendIfPresent(buffer, "product", product);
463     appendIfPresent(buffer, "component", component);
464     appendIfPresent(buffer, "bug_status", status);
465     appendIfPresent(buffer, "resolution", resolution);
466     appendParameterIfPresent(buffer, "keywords", keywords); // Keywords are
467                                                             // added as a list
468                                                             // value
469 
470     if (StringUtils.isNotBlank(keywords))
471     {
472       appendParameterIfPresent(buffer, "keywords_type", keywordsType);
473     }
474     appendParameterIfPresent(buffer, "order", order);
475 
476     if(versionRange != null)
477     {
478       versionRange.appendToUrl(buffer);
479     }
480 
481     if (StringUtils.isNotBlank(filter))
482     {
483       buffer.append(filter);
484     }
485   }
486 
487   /**
488    * Appends the items of the value individually if present.
489    *
490    * @param buffer the buffer to append to.
491    * @param paramName the name of the parameter.
492    * @param paramValue the value of the parameter.
493    */
494   private void appendIfPresent(final StringBuilder buffer,
495       final String paramName, final String paramValue)
496   {
497     if (StringUtils.isNotBlank(paramValue))
498     {
499       for (String item : paramValue.split(","))
500       {
501         appendParameterIfPresent(buffer, paramName, item);
502       }
503     }
504   }
505 
506   /**
507    * Appends the parameter to the buffer.
508    *
509    * @param buffer the buffer to append to.
510    * @param paramName the name of the parameter.
511    * @param paramValue the value of the parameter.
512    */
513   private void appendParameterIfPresent(final StringBuilder buffer,
514       final String paramName, final String paramValue)
515   {
516     if (StringUtils.isNotBlank(paramValue))
517     {
518       buffer.append(paramName).append('=').append(paramValue).append('&');
519     }
520   }
521 
522   // --- object basics --------------------------------------------------------
523 
524 }