001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions.upload; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.IOException; 007import java.util.Collection; 008import java.util.HashMap; 009import java.util.Map; 010import java.util.Optional; 011import java.util.stream.Collectors; 012 013import javax.swing.JOptionPane; 014 015import org.openstreetmap.josm.data.notes.Note; 016import org.openstreetmap.josm.data.notes.NoteComment; 017import org.openstreetmap.josm.data.osm.NoteData; 018import org.openstreetmap.josm.gui.ExceptionDialogUtil; 019import org.openstreetmap.josm.gui.MainApplication; 020import org.openstreetmap.josm.gui.PleaseWaitRunnable; 021import org.openstreetmap.josm.gui.progress.ProgressMonitor; 022import org.openstreetmap.josm.io.OsmApi; 023import org.openstreetmap.josm.io.OsmTransferCanceledException; 024import org.openstreetmap.josm.io.OsmTransferException; 025import org.openstreetmap.josm.tools.Logging; 026import org.xml.sax.SAXException; 027 028/** 029 * Class for uploading note changes to the server 030 */ 031public class UploadNotesTask { 032 033 private NoteData noteData; 034 035 /** 036 * Upload notes with modifications to the server 037 * @param noteData Note dataset with changes to upload 038 * @param progressMonitor progress monitor for user feedback 039 */ 040 public void uploadNotes(NoteData noteData, ProgressMonitor progressMonitor) { 041 this.noteData = noteData; 042 MainApplication.worker.submit(new UploadTask(tr("Uploading modified notes"), progressMonitor)); 043 } 044 045 private class UploadTask extends PleaseWaitRunnable { 046 047 private boolean isCanceled; 048 private final Map<Note, Note> updatedNotes = new HashMap<>(); 049 private final Map<Note, Exception> failedNotes = new HashMap<>(); 050 051 /** 052 * Constructs a new {@code UploadTask}. 053 * @param title message for the user 054 * @param monitor progress monitor 055 */ 056 UploadTask(String title, ProgressMonitor monitor) { 057 super(title, monitor, false); 058 } 059 060 @Override 061 protected void cancel() { 062 Logging.debug("note upload canceled"); 063 isCanceled = true; 064 } 065 066 @Override 067 protected void realRun() throws SAXException, IOException, OsmTransferException { 068 OsmApi api = OsmApi.getOsmApi(); 069 final Collection<Note> modifiedNotes = noteData.getNotes().stream() 070 .filter(note -> Optional.ofNullable(note.getLastComment()).map(NoteComment::isNew).orElse(false)) 071 .collect(Collectors.toList()); 072 getProgressMonitor().setTicksCount(modifiedNotes.size()); 073 for (Note note : modifiedNotes) { 074 getProgressMonitor().setCustomText(tr("Uploading notes {0}/{1}", getProgressMonitor().getTicks(), 075 getProgressMonitor().getTicksCount())); 076 if (isCanceled) { 077 Logging.info("Note upload interrupted by user"); 078 break; 079 } 080 for (NoteComment comment : note.getComments()) { 081 if (comment.isNew()) { 082 Logging.debug("found note change to upload"); 083 processNoteComment(getProgressMonitor(), api, note, comment); 084 } 085 } 086 getProgressMonitor().worked(1); 087 } 088 } 089 090 private void processNoteComment(ProgressMonitor monitor, OsmApi api, Note note, NoteComment comment) { 091 try { 092 if (updatedNotes.containsKey(note)) { 093 // if note has been created earlier in this task, obtain its real id and not use the placeholder id 094 note = updatedNotes.get(note); 095 } 096 Note newNote; 097 switch (comment.getNoteAction()) { 098 case OPENED: 099 Logging.debug("opening new note"); 100 newNote = api.createNote(note.getLatLon(), comment.getText(), monitor); 101 break; 102 case CLOSED: 103 Logging.debug("closing note {0}", note.getId()); 104 newNote = api.closeNote(note, comment.getText(), monitor); 105 break; 106 case COMMENTED: 107 Logging.debug("adding comment to note {0}", note.getId()); 108 newNote = api.addCommentToNote(note, comment.getText(), monitor); 109 break; 110 case REOPENED: 111 Logging.debug("reopening note {0}", note.getId()); 112 newNote = api.reopenNote(note, comment.getText(), monitor); 113 break; 114 default: 115 newNote = null; 116 } 117 updatedNotes.put(note, newNote); 118 } catch (OsmTransferException e) { 119 Logging.error("Failed to upload note to server: {0}", note.getId()); 120 Logging.error(e); 121 if (!(e instanceof OsmTransferCanceledException)) { 122 failedNotes.put(note, e); 123 } 124 } 125 } 126 127 /** Updates the note layer with uploaded notes and notifies the user of any upload failures */ 128 @Override 129 protected void finish() { 130 if (Logging.isDebugEnabled()) { 131 Logging.debug("finish called in notes upload task. Notes to update: {0}", updatedNotes.size()); 132 } 133 noteData.updateNotes(updatedNotes); 134 if (!failedNotes.isEmpty()) { 135 String message = failedNotes.entrySet().stream() 136 .map(entry -> tr("Note {0} failed: {1}", entry.getKey().getId(), entry.getValue().getMessage())) 137 .collect(Collectors.joining("\n")); 138 Logging.error("Notes failed to upload: " + message); 139 JOptionPane.showMessageDialog(MainApplication.getMap(), message, 140 tr("Notes failed to upload"), JOptionPane.ERROR_MESSAGE); 141 ExceptionDialogUtil.explainException(failedNotes.values().iterator().next()); 142 } 143 } 144 } 145}