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