1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.upload;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.notes.Note;
|
---|
14 | import org.openstreetmap.josm.data.notes.NoteComment;
|
---|
15 | import org.openstreetmap.josm.data.osm.NoteData;
|
---|
16 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.io.OsmApi;
|
---|
21 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
22 | import org.openstreetmap.josm.tools.Logging;
|
---|
23 | import org.xml.sax.SAXException;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Class for uploading note changes to the server
|
---|
27 | */
|
---|
28 | public 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 | failedNotes.put(note, e);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Updates the note layer with uploaded notes and notifies the user of any upload failures */
|
---|
117 | @Override
|
---|
118 | protected void finish() {
|
---|
119 | if (Logging.isDebugEnabled()) {
|
---|
120 | Logging.debug("finish called in notes upload task. Notes to update: {0}", updatedNotes.size());
|
---|
121 | }
|
---|
122 | noteData.updateNotes(updatedNotes);
|
---|
123 | if (!failedNotes.isEmpty()) {
|
---|
124 | StringBuilder sb = new StringBuilder();
|
---|
125 | for (Map.Entry<Note, Exception> entry : failedNotes.entrySet()) {
|
---|
126 | sb.append(tr("Note {0} failed: {1}", entry.getKey().getId(), entry.getValue().getMessage()))
|
---|
127 | .append('\n');
|
---|
128 | }
|
---|
129 | Logging.error("Notes failed to upload: " + sb.toString());
|
---|
130 | JOptionPane.showMessageDialog(MainApplication.getMap(), sb.toString(),
|
---|
131 | tr("Notes failed to upload"), JOptionPane.ERROR_MESSAGE);
|
---|
132 | ExceptionDialogUtil.explainException(failedNotes.values().iterator().next());
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|