1   package org.apache.maven.plugin.jira;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
31  
32  
33  
34  
35  
36  public class ParameterQueryBuilder
37      implements JiraQueryBuilder
38  {
39      private String filter = "";
40      
41      private Log log;
42      private StringBuilder query = new StringBuilder();
43  
44      
45      private final Map<String,String> priorityMap = new HashMap<String,String>( 8 );
46      
47      private final Map<String,String> resolutionMap = new HashMap<String,String>( 8 );
48      
49      private final Map<String,String> statusMap = new HashMap<String,String>( 8 );
50      
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          
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         
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         
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 
142 
143     public JiraQueryBuilder fixVersion( String fixVersion )
144     {
145         return this;
146     }
147 
148     public JiraQueryBuilder fixVersionIds( String fixVersionIds )
149     {
150         
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         
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 
204 
205     public JiraQueryBuilder project( String project )
206     {
207         return this;
208     }
209 
210     public JiraQueryBuilder resolutionIds( String resolutionIds )
211     {
212         
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         
239         int validSortColumnNames = 0;
240         if ( sortColumnNames != null )
241         {
242             String[] sortColumnNamesArray = sortColumnNames.split( "," );
243             
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                     
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                 
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         
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                     
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         
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 }