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
42 private Log log;
43
44 private StringBuilder query = new StringBuilder();
45
46
47 private final Map<String, String> priorityMap = new HashMap<String, String>( 8 );
48
49
50 private final Map<String, String> resolutionMap = new HashMap<String, String>( 8 );
51
52
53 private final Map<String, String> statusMap = new HashMap<String, String>( 8 );
54
55
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
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
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
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
147
148 public JiraQueryBuilder fixVersion( String fixVersion )
149 {
150 return this;
151 }
152
153 public JiraQueryBuilder fixVersionIds( String fixVersionIds )
154 {
155
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
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
209
210 public JiraQueryBuilder project( String project )
211 {
212 return this;
213 }
214
215 public JiraQueryBuilder resolutionIds( String resolutionIds )
216 {
217
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
244 int validSortColumnNames = 0;
245 if ( sortColumnNames != null )
246 {
247 String[] sortColumnNamesArray = sortColumnNames.split( "," );
248
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
328 getLog().error( "maven-changes-plugin: The configured value '" + lowerColumnName
329 + "' for sortColumnNames is not correct." );
330 }
331 }
332 if ( validSortColumnNames == 0 )
333 {
334
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
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
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
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 }