source: josm/trunk/src/org/openstreetmap/josm/io/ChangesetQuery.java@ 8395

Last change on this file since 8395 was 8394, checked in by Don-vip, 9 years ago
  • global use of String.isEmpty()
  • Correctness - Method throws alternative exception from catch block without history
  • Property svn:eol-style set to native
File size: 20.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.text.DateFormat;
7import java.text.MessageFormat;
8import java.text.ParseException;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.Date;
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.Map;
16import java.util.Map.Entry;
17
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.tools.CheckParameterUtil;
21import org.openstreetmap.josm.tools.Utils;
22import org.openstreetmap.josm.tools.date.DateUtils;
23
24public class ChangesetQuery {
25
26 /**
27 * Replies a changeset query object from the query part of a OSM API URL for querying changesets.
28 *
29 * @param query the query part
30 * @return the query object
31 * @throws ChangesetQueryUrlException if query doesn't consist of valid query parameters
32 */
33 public static ChangesetQuery buildFromUrlQuery(String query) throws ChangesetQueryUrlException{
34 return new ChangesetQueryUrlParser().parse(query);
35 }
36
37 /** the user id this query is restricted to. null, if no restriction to a user id applies */
38 private Integer uid = null;
39 /** the user name this query is restricted to. null, if no restriction to a user name applies */
40 private String userName = null;
41 /** the bounding box this query is restricted to. null, if no restriction to a bounding box applies */
42 private Bounds bounds = null;
43
44 private Date closedAfter = null;
45 private Date createdBefore = null;
46 /** indicates whether only open changesets are queried. null, if no restrictions regarding open changesets apply */
47 private Boolean open = null;
48 /** indicates whether only closed changesets are queried. null, if no restrictions regarding open changesets apply */
49 private Boolean closed = null;
50 /** a collection of changeset ids to query for */
51 private Collection<Long> changesetIds = null;
52
53 /**
54 * Constructs a new {@code ChangesetQuery}.
55 */
56 public ChangesetQuery() {
57
58 }
59
60 /**
61 * Restricts the query to changesets owned by the user with id <code>uid</code>.
62 *
63 * @param uid the uid of the user. &gt; 0 expected.
64 * @return the query object with the applied restriction
65 * @throws IllegalArgumentException if uid &lt;= 0
66 * @see #forUser(String)
67 */
68 public ChangesetQuery forUser(int uid) {
69 if (uid <= 0)
70 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "uid", uid));
71 this.uid = uid;
72 this.userName = null;
73 return this;
74 }
75
76 /**
77 * Restricts the query to changesets owned by the user with user name <code>username</code>.
78 *
79 * Caveat: for historical reasons the username might not be unique! It is recommended to use
80 * {@link #forUser(int)} to restrict the query to a specific user.
81 *
82 * @param username the username. Must not be null.
83 * @return the query object with the applied restriction
84 * @throws IllegalArgumentException if username is null.
85 * @see #forUser(int)
86 */
87 public ChangesetQuery forUser(String username) {
88 CheckParameterUtil.ensureParameterNotNull(username, "username");
89 this.userName = username;
90 this.uid = null;
91 return this;
92 }
93
94 /**
95 * Replies true if this query is restricted to user whom we only know the user name for.
96 *
97 * @return true if this query is restricted to user whom we only know the user name for
98 */
99 public boolean isRestrictedToPartiallyIdentifiedUser() {
100 return userName != null;
101 }
102
103 /**
104 * Replies the user name which this query is restricted to. null, if this query isn't
105 * restricted to a user name, i.e. if {@link #isRestrictedToPartiallyIdentifiedUser()} is false.
106 *
107 * @return the user name which this query is restricted to
108 */
109 public String getUserName() {
110 return userName;
111 }
112
113 /**
114 * Replies true if this query is restricted to user whom know the user id for.
115 *
116 * @return true if this query is restricted to user whom know the user id for
117 */
118 public boolean isRestrictedToFullyIdentifiedUser() {
119 return uid > 0;
120 }
121
122 /**
123 * Replies a query which is restricted to a bounding box.
124 *
125 * @param minLon min longitude of the bounding box. Valid longitude value expected.
126 * @param minLat min latitude of the bounding box. Valid latitude value expected.
127 * @param maxLon max longitude of the bounding box. Valid longitude value expected.
128 * @param maxLat max latitude of the bounding box. Valid latitude value expected.
129 *
130 * @return the restricted changeset query
131 * @throws IllegalArgumentException if either of the parameters isn't a valid longitude or
132 * latitude value
133 */
134 public ChangesetQuery inBbox(double minLon, double minLat, double maxLon, double maxLat) {
135 if (!LatLon.isValidLon(minLon))
136 throw new IllegalArgumentException(tr("Illegal longitude value for parameter ''{0}'', got {1}", "minLon", minLon));
137 if (!LatLon.isValidLon(maxLon))
138 throw new IllegalArgumentException(tr("Illegal longitude value for parameter ''{0}'', got {1}", "maxLon", maxLon));
139 if (!LatLon.isValidLat(minLat))
140 throw new IllegalArgumentException(tr("Illegal latitude value for parameter ''{0}'', got {1}", "minLat", minLat));
141 if (!LatLon.isValidLat(maxLat))
142 throw new IllegalArgumentException(tr("Illegal longitude value for parameter ''{0}'', got {1}", "maxLat", maxLat));
143
144 return inBbox(new LatLon(minLon, minLat), new LatLon(maxLon, maxLat));
145 }
146
147 /**
148 * Replies a query which is restricted to a bounding box.
149 *
150 * @param min the min lat/lon coordinates of the bounding box. Must not be null.
151 * @param max the max lat/lon coordiantes of the bounding box. Must not be null.
152 *
153 * @return the restricted changeset query
154 * @throws IllegalArgumentException if min is null
155 * @throws IllegalArgumentException if max is null
156 */
157 public ChangesetQuery inBbox(LatLon min, LatLon max) {
158 CheckParameterUtil.ensureParameterNotNull(min, "min");
159 CheckParameterUtil.ensureParameterNotNull(max, "max");
160 this.bounds = new Bounds(min,max);
161 return this;
162 }
163
164 /**
165 * Replies a query which is restricted to a bounding box given by <code>bbox</code>.
166 *
167 * @param bbox the bounding box. Must not be null.
168 * @return the changeset query
169 * @throws IllegalArgumentException if bbox is null.
170 */
171 public ChangesetQuery inBbox(Bounds bbox) {
172 CheckParameterUtil.ensureParameterNotNull(bbox, "bbox");
173 this.bounds = bbox;
174 return this;
175 }
176
177 /**
178 * Restricts the result to changesets which have been closed after the date given by <code>d</code>.
179 * <code>d</code> d is a date relative to the current time zone.
180 *
181 * @param d the date . Must not be null.
182 * @return the restricted changeset query
183 * @throws IllegalArgumentException if d is null
184 */
185 public ChangesetQuery closedAfter(Date d) {
186 CheckParameterUtil.ensureParameterNotNull(d, "d");
187 this.closedAfter = d;
188 return this;
189 }
190
191 /**
192 * Restricts the result to changesets which have been closed after <code>closedAfter</code> and which
193 * habe been created before <code>createdBefore</code>. Both dates are expressed relative to the current
194 * time zone.
195 *
196 * @param closedAfter only reply changesets closed after this date. Must not be null.
197 * @param createdBefore only reply changesets created before this date. Must not be null.
198 * @return the restricted changeset query
199 * @throws IllegalArgumentException if closedAfter is null
200 * @throws IllegalArgumentException if createdBefore is null
201 */
202 public ChangesetQuery closedAfterAndCreatedBefore(Date closedAfter, Date createdBefore ) {
203 CheckParameterUtil.ensureParameterNotNull(closedAfter, "closedAfter");
204 CheckParameterUtil.ensureParameterNotNull(createdBefore, "createdBefore");
205 this.closedAfter = closedAfter;
206 this.createdBefore = createdBefore;
207 return this;
208 }
209
210 /**
211 * Restricts the result to changesets which are or aren't open, depending on the value of
212 * <code>isOpen</code>
213 *
214 * @param isOpen whether changesets should or should not be open
215 * @return the restricted changeset query
216 */
217 public ChangesetQuery beingOpen(boolean isOpen) {
218 this.open = isOpen;
219 return this;
220 }
221
222 /**
223 * Restricts the result to changesets which are or aren't closed, depending on the value of
224 * <code>isClosed</code>
225 *
226 * @param isClosed whether changesets should or should not be open
227 * @return the restricted changeset query
228 */
229 public ChangesetQuery beingClosed(boolean isClosed) {
230 this.closed = isClosed;
231 return this;
232 }
233
234 /**
235 * Restricts the query to the given changeset ids (which are added to previously added ones).
236 *
237 * @param changesetIds the changeset ids
238 * @return the query object with the applied restriction
239 * @throws IllegalArgumentException if changesetIds is null.
240 */
241 public ChangesetQuery forChangesetIds(Collection<Long> changesetIds) {
242 CheckParameterUtil.ensureParameterNotNull(changesetIds, "changesetIds");
243 this.changesetIds = changesetIds;
244 return this;
245 }
246
247 /**
248 * Replies the query string to be used in a query URL for the OSM API.
249 *
250 * @return the query string
251 */
252 public String getQueryString() {
253 StringBuilder sb = new StringBuilder();
254 if (uid != null) {
255 sb.append("user=").append(uid);
256 } else if (userName != null) {
257 sb.append("display_name=").append(Utils.encodeUrl(userName));
258 }
259 if (bounds != null) {
260 if (sb.length() > 0) {
261 sb.append('&');
262 }
263 sb.append("bbox=").append(bounds.encodeAsString(","));
264 }
265 if (closedAfter != null && createdBefore != null) {
266 if (sb.length() > 0) {
267 sb.append('&');
268 }
269 DateFormat df = DateUtils.newIsoDateTimeFormat();
270 sb.append("time=").append(df.format(closedAfter));
271 sb.append(',').append(df.format(createdBefore));
272 } else if (closedAfter != null) {
273 if (sb.length() > 0) {
274 sb.append('&');
275 }
276 DateFormat df = DateUtils.newIsoDateTimeFormat();
277 sb.append("time=").append(df.format(closedAfter));
278 }
279
280 if (open != null) {
281 if (sb.length() > 0) {
282 sb.append('&');
283 }
284 sb.append("open=").append(Boolean.toString(open));
285 } else if (closed != null) {
286 if (sb.length() > 0) {
287 sb.append('&');
288 }
289 sb.append("closed=").append(Boolean.toString(closed));
290 } else if (changesetIds != null) {
291 // since 2013-12-05, see https://github.com/openstreetmap/openstreetmap-website/commit/1d1f194d598e54a5d6fb4f38fb569d4138af0dc8
292 if (sb.length() > 0) {
293 sb.append('&');
294 }
295 sb.append("changesets=").append(Utils.join(",", changesetIds));
296 }
297 return sb.toString();
298 }
299
300 @Override
301 public String toString() {
302 return getQueryString();
303 }
304
305 public static class ChangesetQueryUrlException extends Exception {
306
307 /**
308 * Constructs a new {@code ChangesetQueryUrlException} with the specified detail message.
309 *
310 * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method.
311 */
312 public ChangesetQueryUrlException(String message) {
313 super(message);
314 }
315
316 /**
317 * Constructs a new {@code ChangesetQueryUrlException} with the specified cause and detail message.
318 *
319 * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method.
320 * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method).
321 * (A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown.)
322 */
323 public ChangesetQueryUrlException(String message, Throwable cause) {
324 super(message, cause);
325 }
326
327 /**
328 * Constructs a new {@code ChangesetQueryUrlException} with the specified cause and a detail message of
329 * <tt>(cause==null ? null : cause.toString())</tt> (which typically contains the class and detail message of <tt>cause</tt>).
330 *
331 * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method).
332 * (A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown.)
333 */
334 public ChangesetQueryUrlException(Throwable cause) {
335 super(cause);
336 }
337 }
338
339 public static class ChangesetQueryUrlParser {
340 protected int parseUid(String value) throws ChangesetQueryUrlException {
341 if (value == null || value.trim().isEmpty())
342 throw new ChangesetQueryUrlException(
343 tr("Unexpected value for ''{0}'' in changeset query url, got {1}", "uid", value));
344 int id;
345 try {
346 id = Integer.parseInt(value);
347 if (id <= 0)
348 throw new ChangesetQueryUrlException(
349 tr("Unexpected value for ''{0}'' in changeset query url, got {1}", "uid", value));
350 } catch(NumberFormatException e) {
351 throw new ChangesetQueryUrlException(
352 tr("Unexpected value for ''{0}'' in changeset query url, got {1}", "uid", value), e);
353 }
354 return id;
355 }
356
357 protected boolean parseBoolean(String value, String parameter) throws ChangesetQueryUrlException {
358 if (value == null || value.trim().isEmpty())
359 throw new ChangesetQueryUrlException(
360 tr("Unexpected value for ''{0}'' in changeset query url, got {1}", parameter, value));
361 switch (value) {
362 case "true":
363 return true;
364 case "false":
365 return false;
366 default:
367 throw new ChangesetQueryUrlException(
368 tr("Unexpected value for ''{0}'' in changeset query url, got {1}", parameter, value));
369 }
370 }
371
372 protected Date parseDate(String value, String parameter) throws ChangesetQueryUrlException {
373 if (value == null || value.trim().isEmpty())
374 throw new ChangesetQueryUrlException(
375 tr("Unexpected value for ''{0}'' in changeset query url, got {1}", parameter, value));
376 DateFormat formatter = DateUtils.newIsoDateTimeFormat();
377 try {
378 return formatter.parse(value);
379 } catch(ParseException e) {
380 throw new ChangesetQueryUrlException(
381 tr("Unexpected value for ''{0}'' in changeset query url, got {1}", parameter, value), e);
382 }
383 }
384
385 protected Date[] parseTime(String value) throws ChangesetQueryUrlException {
386 String[] dates = value.split(",");
387 if (dates == null || dates.length == 0 || dates.length > 2)
388 throw new ChangesetQueryUrlException(
389 tr("Unexpected value for ''{0}'' in changeset query url, got {1}", "time", value));
390 if (dates.length == 1)
391 return new Date[]{parseDate(dates[0], "time")};
392 else if (dates.length == 2)
393 return new Date[]{parseDate(dates[0], "time"), parseDate(dates[1], "time")};
394 return null;
395 }
396
397 protected Collection<Long> parseLongs(String value) {
398 return value == null || value.isEmpty()
399 ? Collections.<Long>emptySet() :
400 new HashSet<>(Utils.transform(Arrays.asList(value.split(",")), new Utils.Function<String, Long>() {
401 @Override
402 public Long apply(String x) {
403 return Long.valueOf(x);
404 }
405 }));
406 }
407
408 protected ChangesetQuery createFromMap(Map<String, String> queryParams) throws ChangesetQueryUrlException {
409 ChangesetQuery csQuery = new ChangesetQuery();
410
411 for (Entry<String, String> entry: queryParams.entrySet()) {
412 String k = entry.getKey();
413 switch(k) {
414 case "uid":
415 if (queryParams.containsKey("display_name"))
416 throw new ChangesetQueryUrlException(
417 tr("Cannot create a changeset query including both the query parameters ''uid'' and ''display_name''"));
418 csQuery.forUser(parseUid(queryParams.get("uid")));
419 break;
420 case "display_name":
421 if (queryParams.containsKey("uid"))
422 throw new ChangesetQueryUrlException(
423 tr("Cannot create a changeset query including both the query parameters ''uid'' and ''display_name''"));
424 csQuery.forUser(queryParams.get("display_name"));
425 break;
426 case "open":
427 csQuery.beingOpen(parseBoolean(entry.getValue(), "open"));
428 break;
429 case "closed":
430 csQuery.beingClosed(parseBoolean(entry.getValue(), "closed"));
431 break;
432 case "time":
433 Date[] dates = parseTime(entry.getValue());
434 switch(dates.length) {
435 case 1:
436 csQuery.closedAfter(dates[0]);
437 break;
438 case 2:
439 csQuery.closedAfterAndCreatedBefore(dates[0], dates[1]);
440 break;
441 }
442 break;
443 case "bbox":
444 try {
445 csQuery.inBbox(new Bounds(entry.getValue(), ","));
446 } catch (IllegalArgumentException e) {
447 throw new ChangesetQueryUrlException(e);
448 }
449 break;
450 case "changesets":
451 try {
452 csQuery.forChangesetIds(parseLongs(entry.getValue()));
453 } catch (NumberFormatException e) {
454 throw new ChangesetQueryUrlException(e);
455 }
456 break;
457 default:
458 throw new ChangesetQueryUrlException(
459 tr("Unsupported parameter ''{0}'' in changeset query string", k));
460 }
461 }
462 return csQuery;
463 }
464
465 protected Map<String,String> createMapFromQueryString(String query) {
466 Map<String,String> queryParams = new HashMap<>();
467 String[] keyValuePairs = query.split("&");
468 for (String keyValuePair: keyValuePairs) {
469 String[] kv = keyValuePair.split("=");
470 queryParams.put(kv[0], kv.length > 1 ? kv[1] : "");
471 }
472 return queryParams;
473 }
474
475 /**
476 * Parses the changeset query given as URL query parameters and replies a {@link ChangesetQuery}.
477 *
478 * <code>query</code> is the query part of a API url for querying changesets,
479 * see <a href="http://wiki.openstreetmap.org/wiki/API_v0.6#Query:_GET_.2Fapi.2F0.6.2Fchangesets">OSM API</a>.
480 *
481 * Example for an query string:<br>
482 * <pre>
483 * uid=1234&amp;open=true
484 * </pre>
485 *
486 * @param query the query string. If null, an empty query (identical to a query for all changesets) is
487 * assumed
488 * @return the changeset query
489 * @throws ChangesetQueryUrlException if the query string doesn't represent a legal query for changesets
490 */
491 public ChangesetQuery parse(String query) throws ChangesetQueryUrlException {
492 if (query == null)
493 return new ChangesetQuery();
494 query = query.trim();
495 if (query.isEmpty())
496 return new ChangesetQuery();
497 Map<String,String> queryParams = createMapFromQueryString(query);
498 return createFromMap(queryParams);
499 }
500 }
501}
Note: See TracBrowser for help on using the repository browser.