source: josm/trunk/src/org/openstreetmap/josm/actions/UploadAction.java@ 17534

Last change on this file since 17534 was 16824, checked in by simon04, 4 years ago

Remove Collection.contains check for Collection.remove

  • Property svn:eol-style set to native
File size: 11.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.Map;
12import java.util.Optional;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.actions.upload.ApiPreconditionCheckerHook;
17import org.openstreetmap.josm.actions.upload.DiscardTagsHook;
18import org.openstreetmap.josm.actions.upload.FixDataHook;
19import org.openstreetmap.josm.actions.upload.RelationUploadOrderHook;
20import org.openstreetmap.josm.actions.upload.UploadHook;
21import org.openstreetmap.josm.actions.upload.ValidateUploadHook;
22import org.openstreetmap.josm.data.APIDataSet;
23import org.openstreetmap.josm.data.conflict.ConflictCollection;
24import org.openstreetmap.josm.data.osm.Changeset;
25import org.openstreetmap.josm.gui.HelpAwareOptionPane;
26import org.openstreetmap.josm.gui.MainApplication;
27import org.openstreetmap.josm.gui.Notification;
28import org.openstreetmap.josm.gui.io.AsynchronousUploadPrimitivesTask;
29import org.openstreetmap.josm.gui.io.UploadDialog;
30import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
31import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer;
32import org.openstreetmap.josm.gui.layer.OsmDataLayer;
33import org.openstreetmap.josm.gui.util.GuiHelper;
34import org.openstreetmap.josm.io.ChangesetUpdater;
35import org.openstreetmap.josm.io.UploadStrategySpecification;
36import org.openstreetmap.josm.spi.preferences.Config;
37import org.openstreetmap.josm.tools.ImageProvider;
38import org.openstreetmap.josm.tools.Shortcut;
39import org.openstreetmap.josm.tools.Utils;
40
41/**
42 * Action that opens a connection to the osm server and uploads all changes.
43 *
44 * A dialog is displayed asking the user to specify a rectangle to grab.
45 * The url and account settings from the preferences are used.
46 *
47 * If the upload fails this action offers various options to resolve conflicts.
48 *
49 * @author imi
50 */
51public class UploadAction extends AbstractUploadAction {
52 /**
53 * The list of upload hooks. These hooks will be called one after the other
54 * when the user wants to upload data. Plugins can insert their own hooks here
55 * if they want to be able to veto an upload.
56 *
57 * Be default, the standard upload dialog is the only element in the list.
58 * Plugins should normally insert their code before that, so that the upload
59 * dialog is the last thing shown before upload really starts; on occasion
60 * however, a plugin might also want to insert something after that.
61 */
62 private static final List<UploadHook> UPLOAD_HOOKS = new LinkedList<>();
63 private static final List<UploadHook> LATE_UPLOAD_HOOKS = new LinkedList<>();
64
65 private static final String IS_ASYNC_UPLOAD_ENABLED = "asynchronous.upload";
66
67 static {
68 /**
69 * Calls validator before upload.
70 */
71 UPLOAD_HOOKS.add(new ValidateUploadHook());
72
73 /**
74 * Fixes database errors
75 */
76 UPLOAD_HOOKS.add(new FixDataHook());
77
78 /**
79 * Checks server capabilities before upload.
80 */
81 UPLOAD_HOOKS.add(new ApiPreconditionCheckerHook());
82
83 /**
84 * Adjusts the upload order of new relations
85 */
86 UPLOAD_HOOKS.add(new RelationUploadOrderHook());
87
88 /**
89 * Removes discardable tags like created_by on modified objects
90 */
91 LATE_UPLOAD_HOOKS.add(new DiscardTagsHook());
92 }
93
94 /**
95 * Registers an upload hook. Adds the hook at the first position of the upload hooks.
96 *
97 * @param hook the upload hook. Ignored if null.
98 */
99 public static void registerUploadHook(UploadHook hook) {
100 registerUploadHook(hook, false);
101 }
102
103 /**
104 * Registers an upload hook. Adds the hook at the first position of the upload hooks.
105 *
106 * @param hook the upload hook. Ignored if null.
107 * @param late true, if the hook should be executed after the upload dialog
108 * has been confirmed. Late upload hooks should in general succeed and not
109 * abort the upload.
110 */
111 public static void registerUploadHook(UploadHook hook, boolean late) {
112 if (hook == null) return;
113 if (late) {
114 if (!LATE_UPLOAD_HOOKS.contains(hook)) {
115 LATE_UPLOAD_HOOKS.add(0, hook);
116 }
117 } else {
118 if (!UPLOAD_HOOKS.contains(hook)) {
119 UPLOAD_HOOKS.add(0, hook);
120 }
121 }
122 }
123
124 /**
125 * Unregisters an upload hook. Removes the hook from the list of upload hooks.
126 *
127 * @param hook the upload hook. Ignored if null.
128 */
129 public static void unregisterUploadHook(UploadHook hook) {
130 if (hook == null) return;
131 UPLOAD_HOOKS.remove(hook);
132 LATE_UPLOAD_HOOKS.remove(hook);
133 }
134
135 /**
136 * Constructs a new {@code UploadAction}.
137 */
138 public UploadAction() {
139 super(tr("Upload data..."), "upload", tr("Upload all changes in the active data layer to the OSM server"),
140 Shortcut.registerShortcut("file:upload", tr("File: {0}", tr("Upload data")), KeyEvent.VK_UP, Shortcut.CTRL_SHIFT), true);
141 setHelpId(ht("/Action/Upload"));
142 }
143
144 @Override
145 protected boolean listenToSelectionChange() {
146 return false;
147 }
148
149 @Override
150 protected void updateEnabledState() {
151 OsmDataLayer editLayer = getLayerManager().getEditLayer();
152 setEnabled(editLayer != null && editLayer.requiresUploadToServer());
153 }
154
155 /**
156 * Check whether the preconditions are met to upload data from a given layer, if applicable.
157 * @param layer layer to check
158 * @return {@code true} if the preconditions are met, or not applicable
159 * @see #checkPreUploadConditions(AbstractModifiableLayer, APIDataSet)
160 */
161 public static boolean checkPreUploadConditions(AbstractModifiableLayer layer) {
162 return checkPreUploadConditions(layer,
163 layer instanceof OsmDataLayer ? new APIDataSet(((OsmDataLayer) layer).getDataSet()) : null);
164 }
165
166 protected static void alertUnresolvedConflicts(OsmDataLayer layer) {
167 HelpAwareOptionPane.showOptionDialog(
168 MainApplication.getMainFrame(),
169 tr("<html>The data to be uploaded participates in unresolved conflicts of layer ''{0}''.<br>"
170 + "You have to resolve them first.</html>", Utils.escapeReservedCharactersHTML(layer.getName())
171 ),
172 tr("Warning"),
173 JOptionPane.WARNING_MESSAGE,
174 ht("/Action/Upload#PrimitivesParticipateInConflicts")
175 );
176 }
177
178 /**
179 * Warn user about discouraged upload, propose to cancel operation.
180 * @param layer incriminated layer
181 * @return true if the user wants to cancel, false if they want to continue
182 */
183 public static boolean warnUploadDiscouraged(AbstractModifiableLayer layer) {
184 return GuiHelper.warnUser(tr("Upload discouraged"),
185 "<html>" +
186 tr("You are about to upload data from the layer ''{0}''.<br /><br />"+
187 "Sending data from this layer is <b>strongly discouraged</b>. If you continue,<br />"+
188 "it may require you subsequently have to revert your changes, or force other contributors to.<br /><br />"+
189 "Are you sure you want to continue?", Utils.escapeReservedCharactersHTML(layer.getName()))+
190 "</html>",
191 ImageProvider.get("upload"), tr("Ignore this hint and upload anyway"));
192 }
193
194 /**
195 * Check whether the preconditions are met to upload data in <code>apiData</code>.
196 * Makes sure upload is allowed, primitives in <code>apiData</code> don't participate in conflicts and
197 * runs the installed {@link UploadHook}s.
198 *
199 * @param layer the source layer of the data to be uploaded
200 * @param apiData the data to be uploaded
201 * @return true, if the preconditions are met; false, otherwise
202 */
203 public static boolean checkPreUploadConditions(AbstractModifiableLayer layer, APIDataSet apiData) {
204 if (layer.isUploadDiscouraged() && warnUploadDiscouraged(layer)) {
205 return false;
206 }
207 if (layer instanceof OsmDataLayer) {
208 OsmDataLayer osmLayer = (OsmDataLayer) layer;
209 ConflictCollection conflicts = osmLayer.getConflicts();
210 if (apiData.participatesInConflict(conflicts)) {
211 alertUnresolvedConflicts(osmLayer);
212 return false;
213 }
214 }
215 // Call all upload hooks in sequence.
216 // FIXME: this should become an asynchronous task
217 //
218 if (apiData != null) {
219 return UPLOAD_HOOKS.stream().allMatch(hook -> hook.checkUpload(apiData));
220 }
221
222 return true;
223 }
224
225 /**
226 * Uploads data to the OSM API.
227 *
228 * @param layer the source layer for the data to upload
229 * @param apiData the primitives to be added, updated, or deleted
230 */
231 public void uploadData(final OsmDataLayer layer, APIDataSet apiData) {
232 if (apiData.isEmpty()) {
233 new Notification(tr("No changes to upload.")).show();
234 return;
235 }
236 if (!checkPreUploadConditions(layer, apiData))
237 return;
238
239 ChangesetUpdater.check();
240
241 final UploadDialog dialog = UploadDialog.getUploadDialog();
242 dialog.setChangesetTags(layer.getDataSet());
243 dialog.setUploadedPrimitives(apiData);
244 dialog.setVisible(true);
245 dialog.rememberUserInput();
246 if (dialog.isCanceled()) {
247 dialog.clean();
248 return;
249 }
250
251 for (UploadHook hook : LATE_UPLOAD_HOOKS) {
252 if (!hook.checkUpload(apiData)) {
253 dialog.clean();
254 return;
255 }
256 }
257
258 // Any hooks want to change the changeset tags?
259 Changeset cs = dialog.getChangeset();
260 Map<String, String> changesetTags = cs.getKeys();
261 for (UploadHook hook : UPLOAD_HOOKS) {
262 hook.modifyChangesetTags(changesetTags);
263 }
264 for (UploadHook hook : LATE_UPLOAD_HOOKS) {
265 hook.modifyChangesetTags(changesetTags);
266 }
267
268 UploadStrategySpecification uploadStrategySpecification = dialog.getUploadStrategySpecification();
269 dialog.clean();
270
271 if (Config.getPref().getBoolean(IS_ASYNC_UPLOAD_ENABLED, true)) {
272 Optional<AsynchronousUploadPrimitivesTask> asyncUploadTask = AsynchronousUploadPrimitivesTask.createAsynchronousUploadTask(
273 uploadStrategySpecification, layer, apiData, cs);
274
275 if (asyncUploadTask.isPresent()) {
276 MainApplication.worker.execute(asyncUploadTask.get());
277 }
278 } else {
279 MainApplication.worker.execute(new UploadPrimitivesTask(uploadStrategySpecification, layer, apiData, cs));
280 }
281 }
282
283 @Override
284 public void actionPerformed(ActionEvent e) {
285 if (!isEnabled())
286 return;
287 if (MainApplication.getMap() == null) {
288 new Notification(tr("Nothing to upload. Get some data first.")).show();
289 return;
290 }
291 APIDataSet apiData = new APIDataSet(getLayerManager().getEditDataSet());
292 uploadData(getLayerManager().getEditLayer(), apiData);
293 }
294}
Note: See TracBrowser for help on using the repository browser.