source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadNotesTask.java@ 10252

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

sonar - squid:S2221 - "Exception" should not be caught when not required by called methods

  • Property svn:eol-style set to native
File size: 7.8 KB
RevLine 
[7531]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
[8216]5import static org.openstreetmap.josm.tools.I18n.trn;
[7531]6
7import java.io.IOException;
8import java.net.URL;
9import java.util.List;
10import java.util.concurrent.Future;
11
[8216]12import javax.swing.JOptionPane;
13
[7531]14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.Bounds;
16import org.openstreetmap.josm.data.notes.Note;
[8908]17import org.openstreetmap.josm.data.osm.NoteData;
[8218]18import org.openstreetmap.josm.data.preferences.IntegerProperty;
[7531]19import org.openstreetmap.josm.gui.PleaseWaitRunnable;
20import org.openstreetmap.josm.gui.layer.NoteLayer;
21import org.openstreetmap.josm.gui.progress.ProgressMonitor;
22import org.openstreetmap.josm.io.BoundingBoxDownloader;
[8195]23import org.openstreetmap.josm.io.OsmApi;
[7531]24import org.openstreetmap.josm.io.OsmServerLocationReader;
25import org.openstreetmap.josm.io.OsmServerReader;
26import org.openstreetmap.josm.io.OsmTransferException;
27import org.xml.sax.SAXException;
28
29/** Task for downloading notes */
[8908]30public class DownloadNotesTask extends AbstractDownloadTask<NoteData> {
[7531]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)?)";
[8218]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);
[7531]38
39 private DownloadTask downloadTask;
40
[8908]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 */
[8855]47 public Future<?> download(long id, ProgressMonitor progressMonitor) {
[8195]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
[7531]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() {
[8115]83 return tr("Download OSM Notes");
[7531]84 }
85
86 @Override
87 public String[] getPatterns() {
88 return new String[] {PATTERN_API_URL, PATTERN_DUMP_FILE};
89 }
90
[7749]91 @Override
92 public boolean isSafeForRemotecontrolRequests() {
93 return true;
94 }
95
[7531]96 abstract class DownloadTask extends PleaseWaitRunnable {
97 protected OsmServerReader reader;
98 protected List<Note> notesData;
99
[8836]100 DownloadTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
[7597]101 super(tr("Downloading Notes"), progressMonitor, false);
[7531]102 this.reader = reader;
103 }
104
105 @Override
106 protected void finish() {
[8908]107 rememberDownloadedData(new NoteData(notesData));
[10032]108 if (isCanceled() || isFailed() || notesData == null || notesData.isEmpty()) {
[7531]109 return;
110 }
[8115]111 if (Main.isDebugEnabled()) {
112 Main.debug("Notes downloaded: " + notesData.size());
113 }
[7531]114
115 List<NoteLayer> noteLayers = null;
116 if (Main.map != null) {
117 noteLayers = Main.map.mapView.getLayersOfType(NoteLayer.class);
118 }
[8318]119 if (noteLayers != null && !noteLayers.isEmpty()) {
[10032]120 noteLayers.get(0).getNoteData().addNotes(notesData);
[7531]121 } else {
[10032]122 Main.main.addLayer(new NoteLayer(notesData, tr("Notes")));
[7531]123 }
124 }
125
126 @Override
127 protected void cancel() {
128 setCanceled(true);
129 if (reader != null) {
130 reader.cancel();
131 }
132 }
133
134 @Override
135 public abstract void realRun() throws IOException, SAXException, OsmTransferException;
136 }
137
138 class DownloadBoundingBoxTask extends DownloadTask {
139
[8836]140 DownloadBoundingBoxTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
[7531]141 super(reader, progressMonitor);
142 }
143
144 @Override
145 public void realRun() throws IOException, SAXException, OsmTransferException {
146 if (isCanceled()) {
147 return;
148 }
149 ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
150 try {
[8218]151 notesData = reader.parseNotes(DOWNLOAD_LIMIT.get(), DAYS_CLOSED.get(), subMonitor);
[8216]152 } catch (BoundingBoxDownloader.MoreNotesException e) {
[8217]153 notesData = e.notes;
[8216]154 JOptionPane.showMessageDialog(Main.parent, "<html>"
155 + trn("{0} note has been downloaded.", "{0} notes have been downloaded.", e.limit, e.limit)
156 + "<br>"
157 + tr("Since the download limit was {0}, there might be more notes to download.", e.limit)
158 + "<br>"
159 + tr("Request a smaller area to make sure that all notes are being downloaded.")
160 + "</html>",
161 tr("More notes to download"), JOptionPane.INFORMATION_MESSAGE);
[10212]162 } catch (OsmTransferException e) {
[7531]163 if (isCanceled())
164 return;
[10212]165 rememberException(e);
[7531]166 }
167 }
168 }
169
170 class DownloadRawUrlTask extends DownloadTask {
171
[8836]172 DownloadRawUrlTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
[7531]173 super(reader, progressMonitor);
174 }
175
176 @Override
177 public void realRun() throws IOException, SAXException, OsmTransferException {
178 if (isCanceled()) {
179 return;
180 }
181 ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
182 try {
183 notesData = reader.parseRawNotes(subMonitor);
[10212]184 } catch (OsmTransferException e) {
[7531]185 if (isCanceled())
186 return;
[10212]187 rememberException(e);
[7531]188 }
189 }
190 }
191
192 class DownloadBzip2RawUrlTask extends DownloadTask {
193
[8836]194 DownloadBzip2RawUrlTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
[7531]195 super(reader, progressMonitor);
196 }
197
198 @Override
199 public void realRun() throws IOException, SAXException, OsmTransferException {
200 if (isCanceled()) {
201 return;
202 }
203 ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
204 try {
205 notesData = reader.parseRawNotesBzip2(subMonitor);
[10212]206 } catch (OsmTransferException e) {
[7531]207 if (isCanceled())
208 return;
[10212]209 rememberException(e);
[7531]210 }
211 }
212 }
213}
Note: See TracBrowser for help on using the repository browser.