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