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