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

Last change on this file since 14170 was 14170, checked in by Don-vip, 6 years ago

fix #16542 - don't display error dialog when notes upload is canceled

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