1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.concurrent.Future;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.Bounds;
|
---|
16 | import org.openstreetmap.josm.data.notes.Note;
|
---|
17 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
19 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
---|
22 | import org.openstreetmap.josm.io.OsmApi;
|
---|
23 | import org.openstreetmap.josm.io.OsmServerLocationReader;
|
---|
24 | import org.openstreetmap.josm.io.OsmServerReader;
|
---|
25 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
26 | import org.xml.sax.SAXException;
|
---|
27 |
|
---|
28 | /** Task for downloading notes */
|
---|
29 | public class DownloadNotesTask extends AbstractDownloadTask {
|
---|
30 |
|
---|
31 | private static final String PATTERN_API_URL = "https?://.*/api/0.6/notes.*";
|
---|
32 | private static final String PATTERN_DUMP_FILE = "https?://.*/(.*\\.osn(.bz2)?)";
|
---|
33 | /** Property defining the number of notes to be downloaded */
|
---|
34 | public static final IntegerProperty DOWNLOAD_LIMIT = new IntegerProperty("osm.notes.downloadLimit", 1000);
|
---|
35 | /** Property defining number of days a bug needs to be closed to no longer be downloaded */
|
---|
36 | public static final IntegerProperty DAYS_CLOSED = new IntegerProperty("osm.notes.daysClosed", 7);
|
---|
37 |
|
---|
38 | private DownloadTask downloadTask;
|
---|
39 |
|
---|
40 | public Future<?> download(long id, ProgressMonitor progressMonitor) {
|
---|
41 | final String url = OsmApi.getOsmApi().getBaseUrl() + "notes/" + id;
|
---|
42 | downloadTask = new DownloadRawUrlTask(new OsmServerLocationReader(url), progressMonitor);
|
---|
43 | return Main.worker.submit(downloadTask);
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
|
---|
48 | downloadTask = new DownloadBoundingBoxTask(new BoundingBoxDownloader(downloadArea), progressMonitor);
|
---|
49 | return Main.worker.submit(downloadTask);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
|
---|
54 | if (url.endsWith(".bz2")) {
|
---|
55 | downloadTask = new DownloadBzip2RawUrlTask(new OsmServerLocationReader(url), progressMonitor);
|
---|
56 | } else {
|
---|
57 | downloadTask = new DownloadRawUrlTask(new OsmServerLocationReader(url), progressMonitor);
|
---|
58 | }
|
---|
59 | return Main.worker.submit(downloadTask);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void cancel() {
|
---|
64 | if (downloadTask != null) {
|
---|
65 | downloadTask.cancel();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public String getConfirmationMessage(URL url) {
|
---|
71 | return null;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public String getTitle() {
|
---|
76 | return tr("Download OSM Notes");
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String[] getPatterns() {
|
---|
81 | return new String[] {PATTERN_API_URL, PATTERN_DUMP_FILE};
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public boolean isSafeForRemotecontrolRequests() {
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | abstract class DownloadTask extends PleaseWaitRunnable {
|
---|
90 | protected OsmServerReader reader;
|
---|
91 | protected List<Note> notesData;
|
---|
92 |
|
---|
93 | DownloadTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
|
---|
94 | super(tr("Downloading Notes"), progressMonitor, false);
|
---|
95 | this.reader = reader;
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | protected void finish() {
|
---|
100 | if (isCanceled() || isFailed()) {
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (notesData == null) {
|
---|
105 | return;
|
---|
106 | }
|
---|
107 | if (Main.isDebugEnabled()) {
|
---|
108 | Main.debug("Notes downloaded: " + notesData.size());
|
---|
109 | }
|
---|
110 |
|
---|
111 | List<NoteLayer> noteLayers = null;
|
---|
112 | if (Main.map != null) {
|
---|
113 | noteLayers = Main.map.mapView.getLayersOfType(NoteLayer.class);
|
---|
114 | }
|
---|
115 | NoteLayer layer;
|
---|
116 | if (noteLayers != null && !noteLayers.isEmpty()) {
|
---|
117 | layer = noteLayers.get(0);
|
---|
118 | layer.getNoteData().addNotes(notesData);
|
---|
119 | } else {
|
---|
120 | layer = new NoteLayer(notesData, tr("Notes"));
|
---|
121 | Main.main.addLayer(layer);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | protected void cancel() {
|
---|
127 | setCanceled(true);
|
---|
128 | if (reader != null) {
|
---|
129 | reader.cancel();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public abstract void realRun() throws IOException, SAXException, OsmTransferException;
|
---|
135 | }
|
---|
136 |
|
---|
137 | class DownloadBoundingBoxTask extends DownloadTask {
|
---|
138 |
|
---|
139 | DownloadBoundingBoxTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
|
---|
140 | super(reader, progressMonitor);
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public void realRun() throws IOException, SAXException, OsmTransferException {
|
---|
145 | if (isCanceled()) {
|
---|
146 | return;
|
---|
147 | }
|
---|
148 | ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
---|
149 | try {
|
---|
150 | notesData = reader.parseNotes(DOWNLOAD_LIMIT.get(), DAYS_CLOSED.get(), subMonitor);
|
---|
151 | } catch (BoundingBoxDownloader.MoreNotesException e) {
|
---|
152 | notesData = e.notes;
|
---|
153 | JOptionPane.showMessageDialog(Main.parent, "<html>"
|
---|
154 | + trn("{0} note has been downloaded.", "{0} notes have been downloaded.", e.limit, e.limit)
|
---|
155 | + "<br>"
|
---|
156 | + tr("Since the download limit was {0}, there might be more notes to download.", e.limit)
|
---|
157 | + "<br>"
|
---|
158 | + tr("Request a smaller area to make sure that all notes are being downloaded.")
|
---|
159 | + "</html>",
|
---|
160 | tr("More notes to download"), JOptionPane.INFORMATION_MESSAGE);
|
---|
161 | } catch (Exception e) {
|
---|
162 | if (isCanceled())
|
---|
163 | return;
|
---|
164 | if (e instanceof OsmTransferException) {
|
---|
165 | rememberException(e);
|
---|
166 | } else {
|
---|
167 | rememberException(new OsmTransferException(e));
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | class DownloadRawUrlTask extends DownloadTask {
|
---|
174 |
|
---|
175 | DownloadRawUrlTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
|
---|
176 | super(reader, progressMonitor);
|
---|
177 | }
|
---|
178 |
|
---|
179 | @Override
|
---|
180 | public void realRun() throws IOException, SAXException, OsmTransferException {
|
---|
181 | if (isCanceled()) {
|
---|
182 | return;
|
---|
183 | }
|
---|
184 | ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
---|
185 | try {
|
---|
186 | notesData = reader.parseRawNotes(subMonitor);
|
---|
187 | } catch (Exception e) {
|
---|
188 | if (isCanceled())
|
---|
189 | return;
|
---|
190 | if (e instanceof OsmTransferException) {
|
---|
191 | rememberException(e);
|
---|
192 | } else {
|
---|
193 | rememberException(new OsmTransferException(e));
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | class DownloadBzip2RawUrlTask extends DownloadTask {
|
---|
200 |
|
---|
201 | DownloadBzip2RawUrlTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
|
---|
202 | super(reader, progressMonitor);
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public void realRun() throws IOException, SAXException, OsmTransferException {
|
---|
207 | if (isCanceled()) {
|
---|
208 | return;
|
---|
209 | }
|
---|
210 | ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
---|
211 | try {
|
---|
212 | notesData = reader.parseRawNotesBzip2(subMonitor);
|
---|
213 | } catch (Exception e) {
|
---|
214 | if (isCanceled())
|
---|
215 | return;
|
---|
216 | if (e instanceof OsmTransferException) {
|
---|
217 | rememberException(e);
|
---|
218 | } else {
|
---|
219 | rememberException(new OsmTransferException(e));
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|