1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset.query;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.lang.reflect.InvocationTargetException;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import javax.swing.SwingUtilities;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
15 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
---|
16 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
17 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
19 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetDownloadTask;
|
---|
20 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
21 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
---|
22 | import org.openstreetmap.josm.io.OsmServerUserInfoReader;
|
---|
23 | import org.openstreetmap.josm.io.OsmTransferCancelledException;
|
---|
24 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
25 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
26 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
27 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
28 | import org.xml.sax.SAXException;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Asynchronous task to send a changeset query to the OSM API.
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class ChangesetQueryTask extends PleaseWaitRunnable implements ChangesetDownloadTask{
|
---|
35 |
|
---|
36 | /** the changeset query */
|
---|
37 | private ChangesetQuery query;
|
---|
38 | /** true if the task was canceled */
|
---|
39 | private boolean canceled;
|
---|
40 | /** the set of downloaded changesets */
|
---|
41 | private Set<Changeset> downloadedChangesets;
|
---|
42 | /** the last exception remembered, if any */
|
---|
43 | private Exception lastException;
|
---|
44 | /** the reader object used to read information about the current user from the API */
|
---|
45 | private OsmServerUserInfoReader userInfoReader;
|
---|
46 | /** the reader object used to submit the changeset query to the API */
|
---|
47 | private OsmServerChangesetReader changesetReader;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Creates the task.
|
---|
51 | *
|
---|
52 | * @param query the query to submit to the OSM server. Must not be null.
|
---|
53 | * @throws IllegalArgumentException thrown if query is null.
|
---|
54 | */
|
---|
55 | public ChangesetQueryTask(ChangesetQuery query) throws IllegalArgumentException {
|
---|
56 | super(tr("Querying and downloading changesets",false /* don't ignore exceptions */));
|
---|
57 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
58 | this.query = query;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Creates the task.
|
---|
63 | *
|
---|
64 | * @param parent the parent component relative to which the {@see PleaseWaitDialog} is displayed.
|
---|
65 | * Must not be null.
|
---|
66 | * @param query the query to submit to the OSM server. Must not be null.
|
---|
67 | * @throws IllegalArgumentException thrown if query is null.
|
---|
68 | * @throws IllegalArgumentException thrown if parent is null
|
---|
69 | */
|
---|
70 | public ChangesetQueryTask(Component parent, ChangesetQuery query) throws IllegalArgumentException {
|
---|
71 | super(parent, tr("Querying and downloading changesets"), false /* don't ignore exceptions */);
|
---|
72 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
73 | this.query = query;
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | protected void cancel() {
|
---|
78 | canceled = true;
|
---|
79 | synchronized(this) {
|
---|
80 | if (userInfoReader != null) {
|
---|
81 | userInfoReader.cancel();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | synchronized(this) {
|
---|
85 | if (changesetReader != null) {
|
---|
86 | changesetReader.cancel();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | protected void finish() {
|
---|
93 | if (canceled) return;
|
---|
94 | if (lastException != null) {
|
---|
95 | ExceptionUtil.explainException(lastException);
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // update the global changeset cache with the downloaded changesets;
|
---|
100 | // this will trigger change events which views are listening to. They
|
---|
101 | // will update their views accordingly.
|
---|
102 | //
|
---|
103 | // Run on the EDT because UI updates are triggered.
|
---|
104 | //
|
---|
105 | Runnable r = new Runnable() {
|
---|
106 | public void run() {
|
---|
107 | ChangesetCache.getInstance().update(downloadedChangesets);
|
---|
108 | }
|
---|
109 | };
|
---|
110 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
111 | r.run();
|
---|
112 | } else {
|
---|
113 | try {
|
---|
114 | SwingUtilities.invokeAndWait(r);
|
---|
115 | } catch(InterruptedException e) {
|
---|
116 | e.printStackTrace();
|
---|
117 | } catch(InvocationTargetException e) {
|
---|
118 | Throwable t = e.getTargetException();
|
---|
119 | if (t instanceof RuntimeException) {
|
---|
120 | BugReportExceptionHandler.handleException(t);
|
---|
121 | } else if (t instanceof Exception){
|
---|
122 | ExceptionUtil.explainException(e);
|
---|
123 | } else {
|
---|
124 | BugReportExceptionHandler.handleException(t);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Tries to fully identify the current JOSM user
|
---|
132 | *
|
---|
133 | * @throws OsmTransferException thrown if something went wrong
|
---|
134 | */
|
---|
135 | protected void fullyIdentifyCurrentUser() throws OsmTransferException {
|
---|
136 | getProgressMonitor().indeterminateSubTask(tr("Determine user id for current user..."));
|
---|
137 |
|
---|
138 | synchronized(this) {
|
---|
139 | userInfoReader = new OsmServerUserInfoReader();
|
---|
140 | }
|
---|
141 | UserInfo info = userInfoReader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1,false));
|
---|
142 | synchronized(this) {
|
---|
143 | userInfoReader = null;
|
---|
144 | }
|
---|
145 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
146 | im.setFullyIdentified(im.getUserName(), info);
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
151 | try {
|
---|
152 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
153 | if (query.isRestrictedToPartiallyIdentifiedUser() && im.isCurrentUser(query.getUserName())) {
|
---|
154 | // if we query changesets for the current user, make sure we query against
|
---|
155 | // its user id, not its user name. If necessary, determine the user id
|
---|
156 | // first.
|
---|
157 | //
|
---|
158 | if (im.isPartiallyIdentified() ) {
|
---|
159 | fullyIdentifyCurrentUser();
|
---|
160 | }
|
---|
161 | query = query.forUser(JosmUserIdentityManager.getInstance().getUserId());
|
---|
162 | }
|
---|
163 | if (canceled) return;
|
---|
164 | getProgressMonitor().indeterminateSubTask(tr("Query and download changesets ..."));
|
---|
165 | synchronized(this) {
|
---|
166 | changesetReader= new OsmServerChangesetReader();
|
---|
167 | }
|
---|
168 | downloadedChangesets = new HashSet<Changeset>();
|
---|
169 | downloadedChangesets.addAll(changesetReader.queryChangesets(query, getProgressMonitor().createSubTaskMonitor(0, false)));
|
---|
170 | synchronized (this) {
|
---|
171 | changesetReader = null;
|
---|
172 | }
|
---|
173 | } catch(OsmTransferCancelledException e) {
|
---|
174 | // thrown if user cancel the authentication dialog
|
---|
175 | canceled = true;
|
---|
176 | return;
|
---|
177 | } catch(OsmTransferException e) {
|
---|
178 | if (canceled)
|
---|
179 | return;
|
---|
180 | this.lastException = e;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | /* ------------------------------------------------------------------------------- */
|
---|
185 | /* interface ChangesetDownloadTask */
|
---|
186 | /* ------------------------------------------------------------------------------- */
|
---|
187 | public Set<Changeset> getDownloadedChangesets() {
|
---|
188 | return downloadedChangesets;
|
---|
189 | }
|
---|
190 |
|
---|
191 | public boolean isCanceled() {
|
---|
192 | return canceled;
|
---|
193 | }
|
---|
194 |
|
---|
195 | public boolean isFailed() {
|
---|
196 | return lastException != null;
|
---|
197 | }
|
---|
198 | }
|
---|