source: josm/trunk/src/org/openstreetmap/josm/actions/upload/UploadNotesTask.java@ 11885

Last change on this file since 11885 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: 5.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.upload;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.util.HashMap;
8import java.util.Map;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.notes.Note;
14import org.openstreetmap.josm.data.notes.NoteComment;
15import org.openstreetmap.josm.data.osm.NoteData;
16import org.openstreetmap.josm.gui.ExceptionDialogUtil;
17import org.openstreetmap.josm.gui.PleaseWaitRunnable;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.io.OsmApi;
20import org.openstreetmap.josm.io.OsmTransferException;
21import org.xml.sax.SAXException;
22
23/**
24 * Class for uploading note changes to the server
25 */
26public class UploadNotesTask {
27
28 private NoteData noteData;
29
30 /**
31 * Upload notes with modifications to the server
32 * @param noteData Note dataset with changes to upload
33 * @param progressMonitor progress monitor for user feedback
34 */
35 public void uploadNotes(NoteData noteData, ProgressMonitor progressMonitor) {
36 this.noteData = noteData;
37 Main.worker.submit(new UploadTask(tr("Uploading modified notes"), progressMonitor));
38 }
39
40 private class UploadTask extends PleaseWaitRunnable {
41
42 private boolean isCanceled;
43 private final Map<Note, Note> updatedNotes = new HashMap<>();
44 private final Map<Note, Exception> failedNotes = new HashMap<>();
45
46 /**
47 * Constructs a new {@code UploadTask}.
48 * @param title message for the user
49 * @param monitor progress monitor
50 */
51 UploadTask(String title, ProgressMonitor monitor) {
52 super(title, monitor, false);
53 }
54
55 @Override
56 protected void cancel() {
57 if (Main.isDebugEnabled()) {
58 Main.debug("note upload canceled");
59 }
60 isCanceled = true;
61 }
62
63 @Override
64 protected void realRun() throws SAXException, IOException, OsmTransferException {
65 ProgressMonitor monitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
66 OsmApi api = OsmApi.getOsmApi();
67 for (Note note : noteData.getNotes()) {
68 if (isCanceled) {
69 Main.info("Note upload interrupted by user");
70 break;
71 }
72 for (NoteComment comment : note.getComments()) {
73 if (comment.isNew()) {
74 if (Main.isDebugEnabled()) {
75 Main.debug("found note change to upload");
76 }
77 processNoteComment(monitor, api, note, comment);
78 }
79 }
80 }
81 }
82
83 private void processNoteComment(ProgressMonitor monitor, OsmApi api, Note note, NoteComment comment) {
84 try {
85 if (updatedNotes.containsKey(note)) {
86 // if note has been created earlier in this task, obtain its real id and not use the placeholder id
87 note = updatedNotes.get(note);
88 }
89 Note newNote;
90 switch (comment.getNoteAction()) {
91 case OPENED:
92 if (Main.isDebugEnabled()) {
93 Main.debug("opening new note");
94 }
95 newNote = api.createNote(note.getLatLon(), comment.getText(), monitor);
96 break;
97 case CLOSED:
98 if (Main.isDebugEnabled()) {
99 Main.debug("closing note " + note.getId());
100 }
101 newNote = api.closeNote(note, comment.getText(), monitor);
102 break;
103 case COMMENTED:
104 if (Main.isDebugEnabled()) {
105 Main.debug("adding comment to note " + note.getId());
106 }
107 newNote = api.addCommentToNote(note, comment.getText(), monitor);
108 break;
109 case REOPENED:
110 if (Main.isDebugEnabled()) {
111 Main.debug("reopening note " + note.getId());
112 }
113 newNote = api.reopenNote(note, comment.getText(), monitor);
114 break;
115 default:
116 newNote = null;
117 }
118 updatedNotes.put(note, newNote);
119 } catch (OsmTransferException e) {
120 Main.error("Failed to upload note to server: " + note.getId());
121 Main.error(e);
122 failedNotes.put(note, e);
123 }
124 }
125
126 /** Updates the note layer with uploaded notes and notifies the user of any upload failures */
127 @Override
128 protected void finish() {
129 if (Main.isDebugEnabled()) {
130 Main.debug("finish called in notes upload task. Notes to update: " + updatedNotes.size());
131 }
132 noteData.updateNotes(updatedNotes);
133 if (!failedNotes.isEmpty()) {
134 StringBuilder sb = new StringBuilder();
135 for (Map.Entry<Note, Exception> entry : failedNotes.entrySet()) {
136 sb.append(tr("Note {0} failed: {1}", entry.getKey().getId(), entry.getValue().getMessage()));
137 sb.append('\n');
138 }
139 Main.error("Notes failed to upload: " + sb.toString());
140 JOptionPane.showMessageDialog(Main.map, sb.toString(), tr("Notes failed to upload"), JOptionPane.ERROR_MESSAGE);
141 ExceptionDialogUtil.explainException(failedNotes.values().iterator().next());
142 }
143 }
144 }
145}
Note: See TracBrowser for help on using the repository browser.