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

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

code style - A getX() method which returns a boolean should be named isX()

  • 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.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.PleaseWaitRunnable;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.io.OsmApi;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.xml.sax.SAXException;
21
22/**
23 * Class for uploading note changes to the server
24 */
25public class UploadNotesTask {
26
27 private UploadTask uploadTask;
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 uploadTask = new UploadTask(tr("Uploading modified notes"), progressMonitor);
38 Main.worker.submit(uploadTask);
39 }
40
41 private class UploadTask extends PleaseWaitRunnable {
42
43 private boolean isCanceled = false;
44 private Map<Note, Note> updatedNotes = new HashMap<>();
45 private 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 public UploadTask(String title, ProgressMonitor monitor) {
53 super(title, monitor, false);
54 }
55
56 @Override
57 protected void cancel() {
58 if (Main.isDebugEnabled()) {
59 Main.debug("note upload canceled");
60 }
61 isCanceled = true;
62 }
63
64 @Override
65 protected void realRun() throws SAXException, IOException, OsmTransferException {
66 ProgressMonitor monitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
67 OsmApi api = OsmApi.getOsmApi();
68 for (Note note : noteData.getNotes()) {
69 if(isCanceled) {
70 Main.info("Note upload interrupted by user");
71 break;
72 }
73 for (NoteComment comment : note.getComments()) {
74 if (comment.isNew()) {
75 if (Main.isDebugEnabled()) {
76 Main.debug("found note change to upload");
77 }
78 processNoteComment(monitor, api, note, comment);
79 }
80 }
81 }
82 }
83
84 private void processNoteComment(ProgressMonitor monitor, OsmApi api, Note note, NoteComment comment) {
85 try {
86 Note newNote;
87 switch (comment.getNoteAction()) {
88 case opened:
89 if (Main.isDebugEnabled()) {
90 Main.debug("opening new note");
91 }
92 newNote = api.createNote(note.getLatLon(), comment.getText(), monitor);
93 note.setId(newNote.getId());
94 break;
95 case closed:
96 if (Main.isDebugEnabled()) {
97 Main.debug("closing note " + note.getId());
98 }
99 newNote = api.closeNote(note, comment.getText(), monitor);
100 break;
101 case commented:
102 if (Main.isDebugEnabled()) {
103 Main.debug("adding comment to note " + note.getId());
104 }
105 newNote = api.addCommentToNote(note, comment.getText(), monitor);
106 break;
107 case reopened:
108 if (Main.isDebugEnabled()) {
109 Main.debug("reopening note " + note.getId());
110 }
111 newNote = api.reopenNote(note, comment.getText(), monitor);
112 break;
113 default:
114 newNote = null;
115 }
116 updatedNotes.put(note, newNote);
117 } catch (Exception e) {
118 Main.error("Failed to upload note to server: " + note.getId());
119 failedNotes.put(note, e);
120 }
121 }
122
123 /** Updates the note layer with uploaded notes and notifies the user of any upload failures */
124 @Override
125 protected void finish() {
126 if (Main.isDebugEnabled()) {
127 Main.debug("finish called in notes upload task. Notes to update: " + updatedNotes.size());
128 }
129 noteData.updateNotes(updatedNotes);
130 if (!failedNotes.isEmpty()) {
131 Main.error("Some notes failed to upload");
132 StringBuilder sb = new StringBuilder();
133 for (Map.Entry<Note, Exception> entry : failedNotes.entrySet()) {
134 sb.append(tr("Note {0} failed: {1}", entry.getKey().getId(), entry.getValue().getMessage()));
135 sb.append("\n");
136 }
137 Main.error("Notes failed to upload: " + sb.toString());
138 JOptionPane.showMessageDialog(Main.map, sb.toString(), tr("Notes failed to upload"), JOptionPane.ERROR_MESSAGE);
139 }
140 }
141
142 }
143
144}
Note: See TracBrowser for help on using the repository browser.