View Javadoc

1   package org.apache.maven.plugin.jira;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Locale;
24  import java.util.Map;
25  
26  import org.apache.maven.plugin.logging.Log;
27  
28  /**
29   * JIRA 3.x way of constructing a search query based on URL parameters.
30   * 
31   * @author ton.swieb@finalist.com
32   * @version $Id: ParameterQueryBuilder.java 1380895 2012-09-04 21:25:44Z dennisl $
33   * @since 2.8
34   */
35  public class ParameterQueryBuilder
36      implements JiraQueryBuilder
37  {
38      private String filter = "";
39      /** Log for debug output. */
40      private Log log;
41      private StringBuilder query = new StringBuilder();
42  
43      /** Mapping containing all allowed JIRA priority values. */
44      private final Map<String,String> priorityMap = new HashMap<String,String>( 8 );
45      /** Mapping containing all allowed JIRA resolution values. */
46      private final Map<String,String> resolutionMap = new HashMap<String,String>( 8 );
47      /** Mapping containing all allowed JIRA status values. */
48      private final Map<String,String> statusMap = new HashMap<String,String>( 8 );
49      /** Mapping containing all allowed JIRA type values. */
50      private final Map<String,String> typeMap = new HashMap<String,String>( 8 );
51  
52      public ParameterQueryBuilder( Log log )
53      {
54          this.log = log;
55  
56          priorityMap.put( "Blocker", "1" );
57          priorityMap.put( "Critical", "2" );
58          priorityMap.put( "Major", "3" );
59          priorityMap.put( "Minor", "4" );
60          priorityMap.put( "Trivial", "5" );
61  
62          resolutionMap.put( "Unresolved", "-1" );
63          resolutionMap.put( "Fixed", "1" );
64          resolutionMap.put( "Won't Fix", "2" );
65          resolutionMap.put( "Duplicate", "3" );
66          resolutionMap.put( "Incomplete", "4" );
67          resolutionMap.put( "Cannot Reproduce", "5" );
68  
69          statusMap.put( "Open", "1" );
70          statusMap.put( "In Progress", "3" );
71          statusMap.put( "Reopened", "4" );
72          statusMap.put( "Resolved", "5" );
73          statusMap.put( "Closed", "6" );
74  
75          typeMap.put( "Bug", "1" );
76          typeMap.put( "New Feature", "2" );
77          typeMap.put( "Task", "3" );
78          typeMap.put( "Improvement", "4" );
79          typeMap.put( "Wish", "5" );
80          typeMap.put( "Test", "6" );
81          typeMap.put( "Sub-task", "7" );
82      }
83  
84      public String build()
85      {
86          // If the user has defined a filter - use that
87          if ( ( this.filter != null ) && ( this.filter.length() > 0 ) )
88          {
89              return this.filter;
90          }
91          else
92          {
93              return query.toString();
94          }
95      }
96  
97      public JiraQueryBuilder components( String components )
98      {
99          // add components
100         if ( components != null )
101         {
102             String[] componentsArr = components.split( "," );
103 
104             for ( String component : componentsArr )
105             {
106                 component = component.trim();
107                 if ( component.length() > 0 )
108                 {
109                     query.append( "&component=" ).append( component );
110                 }
111             }
112         }
113         return this;
114     }
115 
116     public JiraQueryBuilder filter( String filter )
117     {
118         this.filter = filter;
119         return this;
120     }
121 
122     /**
123      * This method has no effect in this implementation.
124      */
125     public JiraQueryBuilder fixVersion( String fixVersion )
126     {
127         return this;
128     }
129 
130     public JiraQueryBuilder fixVersionIds( String fixVersionIds )
131     {
132         // add fix versions
133         if ( fixVersionIds != null )
134         {
135             String[] fixVersions = fixVersionIds.split( "," );
136 
137             for ( int i = 0; i < fixVersions.length; i++ )
138             {
139                 if ( fixVersions[i].length() > 0 )
140                 {
141                     query.append( "&fixfor=" ).append( fixVersions[i].trim() );
142                 }
143             }
144         }
145         return this;
146     }
147 
148     public Log getLog()
149     {
150         return log;
151     }
152 
153     public JiraQueryBuilder priorityIds( String priorityIds )
154     {
155         // get the Priority Ids
156         if ( priorityIds != null )
157         {
158             String[] prios = priorityIds.split( "," );
159 
160             for ( String prio : prios )
161             {
162                 prio = prio.trim();
163                 String priorityParam = priorityMap.get( prio );
164 
165                 if ( priorityParam != null )
166                 {
167                     query.append( "&priorityIds=" ).append( priorityParam );
168                 }
169             }
170         }
171         return this;
172     }
173 
174     /**
175      * This method has no effect in this implementation.
176      */
177     public JiraQueryBuilder project( String project )
178     {
179         return this;
180     }
181 
182     public JiraQueryBuilder resolutionIds( String resolutionIds )
183     {
184         // get the Resolution Ids
185         if ( resolutionIds != null )
186         {
187             String[] resos = resolutionIds.split( "," );
188 
189             for ( String reso : resos )
190             {
191                 reso = reso.trim();
192                 String resoParam = resolutionMap.get( reso );
193 
194                 if ( resoParam != null )
195                 {
196                     query.append( "&resolutionIds=" ).append( resoParam );
197                 }
198             }
199         }
200         return this;
201     }
202 
203     public JiraQueryBuilder sortColumnNames( String sortColumnNames )
204     {
205         // get the Sort order
206         int validSortColumnNames = 0;
207         if ( sortColumnNames != null )
208         {
209             String[] sortColumnNamesArray = sortColumnNames.split( "," );
210             // N.B. Add in reverse order (it's the way JIRA 3 likes it!!)
211             for ( int i = sortColumnNamesArray.length - 1; i >= 0; i-- )
212             {
213                 String lowerColumnName = sortColumnNamesArray[i].trim().toLowerCase( Locale.ENGLISH );
214                 boolean descending = false;
215                 String fieldName = null;
216                 if ( lowerColumnName.endsWith( "desc" ) )
217                 {
218                     descending = true;
219                     lowerColumnName = lowerColumnName.substring( 0, lowerColumnName.length() - 4 ).trim();
220                 }
221                 else if ( lowerColumnName.endsWith( "asc" ) )
222                 {
223                     descending = false;
224                     lowerColumnName = lowerColumnName.substring( 0, lowerColumnName.length() - 3 ).trim();
225                 }
226 
227                 if ( "key".equals( lowerColumnName ) )
228                 {
229                     fieldName = "issuekey";
230                 }
231                 else if ( "summary".equals( lowerColumnName ) )
232                 {
233                     fieldName = lowerColumnName;
234                 }
235                 else if ( "status".equals( lowerColumnName ) )
236                 {
237                     fieldName = lowerColumnName;
238                 }
239                 else if ( "resolution".equals( lowerColumnName ) )
240                 {
241                     fieldName = lowerColumnName;
242                 }
243                 else if ( "assignee".equals( lowerColumnName ) )
244                 {
245                     fieldName = lowerColumnName;
246                 }
247                 else if ( "reporter".equals( lowerColumnName ) )
248                 {
249                     fieldName = lowerColumnName;
250                 }
251                 else if ( "type".equals( lowerColumnName ) )
252                 {
253                     fieldName = "issuetype";
254                 }
255                 else if ( "priority".equals( lowerColumnName ) )
256                 {
257                     fieldName = lowerColumnName;
258                 }
259                 else if ( "version".equals( lowerColumnName ) )
260                 {
261                     fieldName = "versions";
262                 }
263                 else if ( "fix version".equals( lowerColumnName ) )
264                 {
265                     fieldName = "fixVersions";
266                 }
267                 else if ( "component".equals( lowerColumnName ) )
268                 {
269                     fieldName = "components";
270                 }
271                 else if ( "created".equals( lowerColumnName ) )
272                 {
273                     fieldName = lowerColumnName;
274                 }
275                 else if ( "updated".equals( lowerColumnName ) )
276                 {
277                     fieldName = lowerColumnName;
278                 }
279                 if ( fieldName != null )
280                 {
281                     query.append( "&sorter/field=" );
282                     query.append( fieldName );
283                     query.append( "&sorter/order=" );
284                     query.append( descending ? "DESC" : "ASC" );
285                     validSortColumnNames++;
286                 }
287                 else
288                 {
289                     // Error in the configuration
290                     getLog().error(
291                         "maven-changes-plugin: The configured value '" + lowerColumnName
292                             + "' for sortColumnNames is not correct." );
293                 }
294             }
295             if ( validSortColumnNames == 0 )
296             {
297                 // Error in the configuration
298                 getLog().error(
299                     "maven-changes-plugin: None of the configured sortColumnNames '" + sortColumnNames + "' are correct." );
300             }
301         }
302         return this;
303     }
304 
305     public JiraQueryBuilder statusIds( String statusIds )
306     {
307         // get the Status Ids
308         if ( statusIds != null )
309         {
310             String[] stats = statusIds.split( "," );
311             for ( String stat : stats )
312             {
313                 stat = stat.trim();
314                 String statusParam = statusMap.get( stat );
315 
316                 if ( statusParam != null )
317                 {
318                     query.append( "&statusIds=" ).append( statusParam );
319                 }
320                 else
321                 {
322                     // if it's numeric we can handle it too.
323                     try
324                     {
325                         Integer.parseInt( stat );
326                         query.append( "&statusIds=" ).append( stat );
327                     }
328                     catch ( NumberFormatException nfe )
329                     {
330                         getLog().error( "maven-changes-plugin: invalid statusId " + stat );
331                     }
332                 }
333             }
334         }
335         return this;
336     }
337 
338     public JiraQueryBuilder typeIds( String typeIds )
339     {
340         // get the Type Ids
341         if ( typeIds != null )
342         {
343             String[] types = typeIds.split( "," );
344 
345             for ( String type : types )
346             {
347                 String typeParam = typeMap.get( type.trim() );
348 
349                 if ( typeParam != null )
350                 {
351                     query.append( "&type=" ).append( typeParam );
352                 }
353             }
354         }
355         return this;
356     }
357 }