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 java.io.UnsupportedEncodingException; |
23 | |
import java.net.URLEncoder; |
24 | |
import java.util.Locale; |
25 | |
|
26 | |
import org.apache.maven.plugin.logging.Log; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class JqlQueryBuilder |
37 | |
implements JiraQueryBuilder |
38 | |
{ |
39 | 17 | private String filter = ""; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private Log log; |
45 | |
|
46 | 17 | private StringBuilder orderBy = new StringBuilder(); |
47 | |
|
48 | 17 | private StringBuilder query = new StringBuilder(); |
49 | |
|
50 | |
public JqlQueryBuilder( Log log ) |
51 | 17 | { |
52 | 17 | this.log = log; |
53 | 17 | } |
54 | |
|
55 | |
public String build() |
56 | |
{ |
57 | |
try |
58 | |
{ |
59 | |
String jqlQuery; |
60 | |
|
61 | 17 | if ( ( this.filter != null ) && ( this.filter.length() > 0 ) ) |
62 | |
{ |
63 | 0 | jqlQuery = filter; |
64 | |
} |
65 | |
else |
66 | |
{ |
67 | 17 | jqlQuery = query.toString() + orderBy.toString(); |
68 | |
} |
69 | 17 | getLog().debug( "Encoding JQL query " + jqlQuery ); |
70 | 17 | String encodedQuery = URLEncoder.encode( jqlQuery, "UTF-8" ); |
71 | 17 | getLog().debug( "Encoded JQL query " + encodedQuery ); |
72 | 17 | return encodedQuery; |
73 | |
} |
74 | 0 | catch ( UnsupportedEncodingException e ) |
75 | |
{ |
76 | 0 | getLog().error( "Unable to encode JQL query with UTF-8", e ); |
77 | 0 | throw new RuntimeException( e ); |
78 | |
} |
79 | |
} |
80 | |
|
81 | |
public JiraQueryBuilder components( String components ) |
82 | |
{ |
83 | 0 | addCommaSeparatedValues( "component", components ); |
84 | 0 | return this; |
85 | |
} |
86 | |
|
87 | |
public JiraQueryBuilder filter( String filter ) |
88 | |
{ |
89 | 0 | this.filter = filter; |
90 | 0 | return this; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public JiraQueryBuilder fixVersion( String fixVersion ) |
101 | |
{ |
102 | 2 | addSingleValue( "fixVersion", fixVersion ); |
103 | 2 | return this; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public JiraQueryBuilder fixVersionIds( String fixVersionIds ) |
114 | |
{ |
115 | 0 | addCommaSeparatedValues( "fixVersion", fixVersionIds ); |
116 | 0 | return this; |
117 | |
} |
118 | |
|
119 | |
public Log getLog() |
120 | |
{ |
121 | 34 | return log; |
122 | |
} |
123 | |
|
124 | |
public JiraQueryBuilder priorityIds( String priorityIds ) |
125 | |
{ |
126 | 5 | addCommaSeparatedValues( "priority", priorityIds ); |
127 | 5 | return this; |
128 | |
} |
129 | |
|
130 | |
public JiraQueryBuilder project( String project ) |
131 | |
{ |
132 | 9 | addSingleValue( "project", project ); |
133 | 9 | return this; |
134 | |
} |
135 | |
|
136 | |
public JiraQueryBuilder resolutionIds( String resolutionIds ) |
137 | |
{ |
138 | 0 | addCommaSeparatedValues( "resolution", resolutionIds ); |
139 | 0 | return this; |
140 | |
} |
141 | |
|
142 | |
public JiraQueryBuilder sortColumnNames( String sortColumnNames ) |
143 | |
{ |
144 | 7 | if ( sortColumnNames != null ) |
145 | |
{ |
146 | 7 | orderBy.append( " ORDER BY " ); |
147 | |
|
148 | 7 | String[] sortColumnNamesArray = sortColumnNames.split( "," ); |
149 | |
|
150 | 11 | for ( int i = 0; i < sortColumnNamesArray.length - 1; i++ ) |
151 | |
{ |
152 | 4 | addSingleSortColumn( sortColumnNamesArray[i] ); |
153 | 4 | orderBy.append( ", " ); |
154 | |
} |
155 | 7 | addSingleSortColumn( sortColumnNamesArray[sortColumnNamesArray.length - 1] ); |
156 | |
} |
157 | 7 | return this; |
158 | |
} |
159 | |
|
160 | |
public JiraQueryBuilder statusIds( String statusIds ) |
161 | |
{ |
162 | 2 | addCommaSeparatedValues( "status", statusIds ); |
163 | 2 | return this; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
public JiraQueryBuilder typeIds( String typeIds ) |
168 | |
{ |
169 | 0 | addCommaSeparatedValues( "type", typeIds ); |
170 | 0 | return this; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
private void addCommaSeparatedValues( String key, String values ) |
178 | |
{ |
179 | 7 | if ( values != null ) |
180 | |
{ |
181 | 7 | if ( query.length() > 0 ) |
182 | |
{ |
183 | 1 | query.append( " AND " ); |
184 | |
} |
185 | |
|
186 | 7 | query.append( key + " in (" ); |
187 | |
|
188 | 7 | String[] valuesArr = values.split( "," ); |
189 | |
|
190 | 11 | for ( int i = 0; i < ( valuesArr.length - 1 ); i++ ) |
191 | |
{ |
192 | 4 | trimAndQuoteValue( valuesArr[i] ); |
193 | 4 | query.append( ", " ); |
194 | |
} |
195 | 7 | trimAndQuoteValue( valuesArr[valuesArr.length - 1] ); |
196 | 7 | query.append( ")" ); |
197 | |
} |
198 | 7 | } |
199 | |
|
200 | |
private void addSingleSortColumn( String name ) |
201 | |
{ |
202 | 11 | boolean descending = false; |
203 | 11 | name = name.trim().toLowerCase( Locale.ENGLISH ); |
204 | 11 | if ( name.endsWith( "desc" ) ) |
205 | |
{ |
206 | 4 | descending = true; |
207 | 4 | name = name.substring( 0, name.length() - 4 ).trim(); |
208 | |
} |
209 | 7 | else if ( name.endsWith( "asc" ) ) |
210 | |
{ |
211 | 6 | descending = false; |
212 | 6 | name = name.substring( 0, name.length() - 3 ).trim(); |
213 | |
} |
214 | |
|
215 | 11 | name = name.replaceAll( " ", "" ); |
216 | 11 | orderBy.append( name ); |
217 | 11 | orderBy.append( descending ? " DESC" : " ASC" ); |
218 | 11 | } |
219 | |
|
220 | |
private void addSingleValue( String key, String value ) |
221 | |
{ |
222 | 11 | if ( value != null ) |
223 | |
{ |
224 | 11 | if ( query.length() > 0 ) |
225 | |
{ |
226 | 1 | query.append( " AND " ); |
227 | |
} |
228 | 11 | query.append( key ).append( " = " ); |
229 | 11 | trimAndQuoteValue( value ); |
230 | |
} |
231 | 11 | } |
232 | |
|
233 | |
private void trimAndQuoteValue( String value ) |
234 | |
{ |
235 | 22 | String trimmedValue = value.trim(); |
236 | 22 | if ( trimmedValue.contains( " " ) || trimmedValue.contains( "." ) ) |
237 | |
{ |
238 | 3 | query.append( "\"" ).append( trimmedValue ).append( "\"" ); |
239 | |
} |
240 | |
else |
241 | |
{ |
242 | 19 | query.append( trimmedValue ); |
243 | |
} |
244 | 22 | } |
245 | |
} |