source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/ChangesetQueryTask.java@ 11186

Last change on this file since 11186 was 10420, checked in by Don-vip, 8 years ago

sonar - squid:S1166 - Exception handlers should preserve the original exceptions

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.io.IOException;
8
9import javax.swing.JOptionPane;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.UserInfo;
13import org.openstreetmap.josm.gui.JosmUserIdentityManager;
14import org.openstreetmap.josm.gui.util.GuiHelper;
15import org.openstreetmap.josm.io.ChangesetQuery;
16import org.openstreetmap.josm.io.OsmServerUserInfoReader;
17import org.openstreetmap.josm.io.OsmTransferCanceledException;
18import org.openstreetmap.josm.io.OsmTransferException;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20import org.openstreetmap.josm.tools.ExceptionUtil;
21import org.xml.sax.SAXException;
22
23/**
24 * Asynchronous task to send a changeset query to the OSM API.
25 * @since 2689
26 */
27public class ChangesetQueryTask extends AbstractChangesetDownloadTask {
28
29 class DownloadTask extends RunnableDownloadTask {
30 /** the changeset query */
31 private ChangesetQuery query;
32 /** the reader object used to read information about the current user from the API */
33 private final OsmServerUserInfoReader userInfoReader = new OsmServerUserInfoReader();
34
35 DownloadTask(Component parent, ChangesetQuery query) {
36 super(parent, tr("Querying and downloading changesets"));
37 this.query = query;
38 }
39
40 /**
41 * Tries to fully identify the current JOSM user
42 *
43 * @throws OsmTransferException if something went wrong
44 */
45 protected void fullyIdentifyCurrentUser() throws OsmTransferException {
46 getProgressMonitor().indeterminateSubTask(tr("Determine user id for current user..."));
47
48 UserInfo info = userInfoReader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1, false));
49 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
50 im.setFullyIdentified(im.getUserName(), info);
51 }
52
53 @Override
54 protected void realRun() throws SAXException, IOException, OsmTransferException {
55 try {
56 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
57 if (query.isRestrictedToPartiallyIdentifiedUser() && im.isCurrentUser(query.getUserName())) {
58 // if we query changesets for the current user, make sure we query against
59 // its user id, not its user name. If necessary, determine the user id first.
60 //
61 if (im.isPartiallyIdentified()) {
62 fullyIdentifyCurrentUser();
63 }
64 query = query.forUser(JosmUserIdentityManager.getInstance().getUserId());
65 }
66 if (isCanceled())
67 return;
68 getProgressMonitor().indeterminateSubTask(tr("Query and download changesets ..."));
69 downloadedChangesets.addAll(reader.queryChangesets(query, getProgressMonitor().createSubTaskMonitor(0, false)));
70 } catch (OsmTransferCanceledException e) {
71 // thrown if user cancel the authentication dialog
72 setCanceled(true);
73 Main.trace(e);
74 } catch (OsmTransferException e) {
75 if (isCanceled())
76 return;
77 rememberLastException(e);
78 }
79 }
80
81 @Override
82 protected void finish() {
83 rememberDownloadedData(downloadedChangesets);
84 if (isCanceled())
85 return;
86 if (lastException != null) {
87 GuiHelper.runInEDTAndWait(new Runnable() {
88 private final Component parent = progressMonitor != null ? progressMonitor.getWindowParent() : null;
89 @Override
90 public void run() {
91 JOptionPane.showMessageDialog(
92 parent != null ? parent : Main.parent,
93 ExceptionUtil.explainException(lastException),
94 tr("Errors during download"),
95 JOptionPane.ERROR_MESSAGE);
96 }
97 });
98 return;
99 }
100 updateChangesets();
101 }
102
103 @Override
104 protected void cancel() {
105 super.cancel();
106 synchronized (this) {
107 if (userInfoReader != null) {
108 userInfoReader.cancel();
109 }
110 }
111 }
112 }
113
114 /**
115 * Creates the task.
116 *
117 * @param query the query to submit to the OSM server. Must not be null.
118 * @throws IllegalArgumentException if query is null.
119 */
120 public ChangesetQueryTask(ChangesetQuery query) {
121 this(Main.parent, query);
122 }
123
124 /**
125 * Creates the task.
126 *
127 * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed.
128 * Must not be null.
129 * @param query the query to submit to the OSM server. Must not be null.
130 * @throws IllegalArgumentException if query is null.
131 * @throws IllegalArgumentException if parent is null
132 */
133 public ChangesetQueryTask(Component parent, ChangesetQuery query) {
134 CheckParameterUtil.ensureParameterNotNull(query, "query");
135 setDownloadTask(new DownloadTask(parent, query));
136 }
137}
Note: See TracBrowser for help on using the repository browser.