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.io.UnsupportedEncodingException;
25 import java.net.URLEncoder;
26 import java.util.List;
27 import java.util.Locale;
28
29
30
31
32
33
34
35
36
37 public class JqlQueryBuilder
38 implements JiraQueryBuilder
39 {
40 private String filter = "";
41 private boolean urlEncode = true;
42
43
44
45
46 private Log log;
47
48 private StringBuilder orderBy = new StringBuilder();
49
50 private StringBuilder query = new StringBuilder();
51
52 public JqlQueryBuilder( Log log )
53 {
54 this.log = log;
55 }
56
57 public String build()
58 {
59 try
60 {
61 String jqlQuery;
62
63 if ( ( this.filter != null ) && ( this.filter.length() > 0 ) )
64 {
65 jqlQuery = filter;
66 }
67 else
68 {
69 jqlQuery = query.toString() + orderBy.toString();
70 }
71
72 if ( urlEncode )
73 {
74 getLog().debug( "Encoding JQL query " + jqlQuery );
75 String encodedQuery = URLEncoder.encode( jqlQuery, "UTF-8" );
76 getLog().debug( "Encoded JQL query " + encodedQuery );
77 return encodedQuery;
78 }
79 else
80 {
81 return jqlQuery;
82 }
83 }
84 catch ( UnsupportedEncodingException e )
85 {
86 getLog().error( "Unable to encode JQL query with UTF-8", e );
87 throw new RuntimeException( e );
88 }
89 }
90
91 public JiraQueryBuilder components( String components )
92 {
93 addCommaSeparatedValues( "component", components );
94 return this;
95 }
96
97 public JiraQueryBuilder components( List<String> components )
98 {
99 addValues( "component", components );
100 return this;
101 }
102
103 public JiraQueryBuilder filter( String filter )
104 {
105 this.filter = filter;
106 return this;
107 }
108
109
110
111
112
113
114
115
116 public JiraQueryBuilder fixVersion( String fixVersion )
117 {
118 addSingleValue( "fixVersion", fixVersion );
119 return this;
120 }
121
122
123
124
125
126
127
128
129 public JiraQueryBuilder fixVersionIds( String fixVersionIds )
130 {
131 addCommaSeparatedValues( "fixVersion", fixVersionIds );
132 return this;
133 }
134
135
136
137
138
139
140 public JiraQueryBuilder fixVersionIds( List<String> fixVersionIds )
141 {
142 addValues( "fixVersion", fixVersionIds );
143 return this;
144 }
145
146 public Log getLog()
147 {
148 return log;
149 }
150
151 public JiraQueryBuilder priorityIds( String priorityIds )
152 {
153 addCommaSeparatedValues( "priority", priorityIds );
154 return this;
155 }
156
157 public JiraQueryBuilder priorityIds( List<String> priorityIds )
158 {
159 addValues( "priority", priorityIds );
160 return this;
161 }
162
163 public JiraQueryBuilder project( String project )
164 {
165 addSingleValue( "project", project );
166 return this;
167 }
168
169 public JiraQueryBuilder resolutionIds( String resolutionIds )
170 {
171 addCommaSeparatedValues( "resolution", resolutionIds );
172 return this;
173 }
174
175 public JiraQueryBuilder resolutionIds( List<String> resolutionIds )
176 {
177 addValues( "resolution", resolutionIds );
178 return this;
179 }
180
181 public JiraQueryBuilder sortColumnNames( String sortColumnNames )
182 {
183 if ( sortColumnNames != null )
184 {
185 orderBy.append( " ORDER BY " );
186
187 String[] sortColumnNamesArray = sortColumnNames.split( "," );
188
189 for ( int i = 0; i < sortColumnNamesArray.length - 1; i++ )
190 {
191 addSingleSortColumn( sortColumnNamesArray[i] );
192 orderBy.append( ", " );
193 }
194 addSingleSortColumn( sortColumnNamesArray[sortColumnNamesArray.length - 1] );
195 }
196 return this;
197 }
198
199 public JiraQueryBuilder statusIds( String statusIds )
200 {
201 addCommaSeparatedValues( "status", statusIds );
202 return this;
203 }
204
205 public JiraQueryBuilder statusIds( List<String> statusIds )
206 {
207 addValues( "status", statusIds );
208 return this;
209 }
210
211
212 public JiraQueryBuilder typeIds( String typeIds )
213 {
214 addCommaSeparatedValues( "type", typeIds );
215 return this;
216 }
217
218 public JiraQueryBuilder typeIds( List<String> typeIds )
219 {
220 addValues( "type", typeIds );
221 return this;
222 }
223
224 public JiraQueryBuilder urlEncode( boolean doEncoding )
225 {
226 urlEncode = doEncoding;
227 return this;
228 }
229
230 public boolean urlEncode()
231 {
232 return urlEncode;
233 }
234
235
236
237
238
239 private void addCommaSeparatedValues( String key, String values )
240 {
241 if ( values != null )
242 {
243 if ( query.length() > 0 )
244 {
245 query.append( " AND " );
246 }
247
248 query.append( key ).append( " in (" );
249
250 String[] valuesArr = values.split( "," );
251
252 for ( int i = 0; i < ( valuesArr.length - 1 ); i++ )
253 {
254 trimAndQuoteValue( valuesArr[i] );
255 query.append( ", " );
256 }
257 trimAndQuoteValue( valuesArr[valuesArr.length - 1] );
258 query.append( ")" );
259 }
260 }
261
262 private void addValues( String key, List<String> values )
263 {
264 if ( values != null && values.size() > 0 )
265 {
266 if ( query.length() > 0 )
267 {
268 query.append( " AND " );
269 }
270
271 query.append( key ).append( " in (" );
272
273 for ( int i = 0; i < ( values.size() - 1 ); i++ )
274 {
275 trimAndQuoteValue( values.get( i ) );
276 query.append( ", " );
277 }
278 trimAndQuoteValue( values.get ( values.size() - 1 ) );
279 query.append( ")" );
280 }
281 }
282
283 private void addSingleSortColumn( String name )
284 {
285 boolean descending = false;
286 name = name.trim().toLowerCase( Locale.ENGLISH );
287 if ( name.endsWith( "desc" ) )
288 {
289 descending = true;
290 name = name.substring( 0, name.length() - 4 ).trim();
291 }
292 else if ( name.endsWith( "asc" ) )
293 {
294 descending = false;
295 name = name.substring( 0, name.length() - 3 ).trim();
296 }
297
298 name = name.replaceAll( " ", "" );
299 orderBy.append( name );
300 orderBy.append( descending ? " DESC" : " ASC" );
301 }
302
303 private void addSingleValue( String key, String value )
304 {
305 if ( value != null )
306 {
307 if ( query.length() > 0 )
308 {
309 query.append( " AND " );
310 }
311 query.append( key ).append( " = " );
312 trimAndQuoteValue( value );
313 }
314 }
315
316 private void trimAndQuoteValue( String value )
317 {
318 String trimmedValue = value.trim();
319 if ( trimmedValue.contains( " " ) || trimmedValue.contains( "." ) )
320 {
321 query.append( "\"" ).append( trimmedValue ).append( "\"" );
322 }
323 else
324 {
325 query.append( trimmedValue );
326 }
327 }
328 }