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

Last change on this file since 13632 was 12635, checked in by Don-vip, 7 years ago

see #15182 - checkstyle

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