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