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