source: josm/trunk/src/org/openstreetmap/josm/actions/UploadSelectionAction.java@ 11339

Last change on this file since 11339 was 11109, checked in by Don-vip, 8 years ago

sonar - squid:S1871 - Two branches in the same conditional structure should not have exactly the same implementation

  • Property svn:eol-style set to native
File size: 13.0 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.io.IOException;
10import java.util.Collection;
11import java.util.HashSet;
12import java.util.Set;
13import java.util.Stack;
14
15import javax.swing.JOptionPane;
16import javax.swing.SwingUtilities;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.APIDataSet;
20import org.openstreetmap.josm.data.osm.Changeset;
21import org.openstreetmap.josm.data.osm.DataSet;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.Relation;
25import org.openstreetmap.josm.data.osm.Way;
26import org.openstreetmap.josm.data.osm.visitor.Visitor;
27import org.openstreetmap.josm.gui.DefaultNameFormatter;
28import org.openstreetmap.josm.gui.PleaseWaitRunnable;
29import org.openstreetmap.josm.gui.io.UploadSelectionDialog;
30import org.openstreetmap.josm.gui.layer.OsmDataLayer;
31import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
32import org.openstreetmap.josm.io.OsmTransferException;
33import org.openstreetmap.josm.tools.CheckParameterUtil;
34import org.openstreetmap.josm.tools.ExceptionUtil;
35import org.openstreetmap.josm.tools.Shortcut;
36import org.xml.sax.SAXException;
37
38/**
39 * Uploads the current selection to the server.
40 * @since 2250
41 */
42public class UploadSelectionAction extends JosmAction {
43 /**
44 * Constructs a new {@code UploadSelectionAction}.
45 */
46 public UploadSelectionAction() {
47 super(
48 tr("Upload selection"),
49 "uploadselection",
50 tr("Upload all changes in the current selection to the OSM server."),
51 // CHECKSTYLE.OFF: LineLength
52 Shortcut.registerShortcut("file:uploadSelection", tr("File: {0}", tr("Upload selection")), KeyEvent.VK_U, Shortcut.ALT_CTRL_SHIFT),
53 // CHECKSTYLE.ON: LineLength
54 true);
55 putValue("help", ht("/Action/UploadSelection"));
56 }
57
58 @Override
59 protected void updateEnabledState() {
60 updateEnabledStateOnCurrentSelection();
61 }
62
63 @Override
64 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
65 setEnabled(selection != null && !selection.isEmpty());
66 }
67
68 protected Set<OsmPrimitive> getDeletedPrimitives(DataSet ds) {
69 Set<OsmPrimitive> ret = new HashSet<>();
70 for (OsmPrimitive p: ds.allPrimitives()) {
71 if (p.isDeleted() && !p.isNew() && p.isVisible() && p.isModified()) {
72 ret.add(p);
73 }
74 }
75 return ret;
76 }
77
78 protected Set<OsmPrimitive> getModifiedPrimitives(Collection<OsmPrimitive> primitives) {
79 Set<OsmPrimitive> ret = new HashSet<>();
80 for (OsmPrimitive p: primitives) {
81 if (p.isNewOrUndeleted() || (p.isModified() && !p.isIncomplete())) {
82 ret.add(p);
83 }
84 }
85 return ret;
86 }
87
88 @Override
89 public void actionPerformed(ActionEvent e) {
90 OsmDataLayer editLayer = getLayerManager().getEditLayer();
91 if (!isEnabled())
92 return;
93 if (editLayer.isUploadDiscouraged()) {
94 if (UploadAction.warnUploadDiscouraged(editLayer)) {
95 return;
96 }
97 }
98 Collection<OsmPrimitive> modifiedCandidates = getModifiedPrimitives(editLayer.data.getAllSelected());
99 Collection<OsmPrimitive> deletedCandidates = getDeletedPrimitives(editLayer.data);
100 if (modifiedCandidates.isEmpty() && deletedCandidates.isEmpty()) {
101 JOptionPane.showMessageDialog(
102 Main.parent,
103 tr("No changes to upload."),
104 tr("Warning"),
105 JOptionPane.INFORMATION_MESSAGE
106 );
107 return;
108 }
109 UploadSelectionDialog dialog = new UploadSelectionDialog();
110 dialog.populate(
111 modifiedCandidates,
112 deletedCandidates
113 );
114 dialog.setVisible(true);
115 if (dialog.isCanceled())
116 return;
117 Collection<OsmPrimitive> toUpload = new UploadHullBuilder().build(dialog.getSelectedPrimitives());
118 if (toUpload.isEmpty()) {
119 JOptionPane.showMessageDialog(
120 Main.parent,
121 tr("No changes to upload."),
122 tr("Warning"),
123 JOptionPane.INFORMATION_MESSAGE
124 );
125 return;
126 }
127 uploadPrimitives(editLayer, toUpload);
128 }
129
130 /**
131 * Replies true if there is at least one non-new, deleted primitive in
132 * <code>primitives</code>
133 *
134 * @param primitives the primitives to scan
135 * @return true if there is at least one non-new, deleted primitive in
136 * <code>primitives</code>
137 */
138 protected boolean hasPrimitivesToDelete(Collection<OsmPrimitive> primitives) {
139 for (OsmPrimitive p: primitives) {
140 if (p.isDeleted() && p.isModified() && !p.isNew())
141 return true;
142 }
143 return false;
144 }
145
146 /**
147 * Uploads the primitives in <code>toUpload</code> to the server. Only
148 * uploads primitives which are either new, modified or deleted.
149 *
150 * Also checks whether <code>toUpload</code> has to be extended with
151 * deleted parents in order to avoid precondition violations on the server.
152 *
153 * @param layer the data layer from which we upload a subset of primitives
154 * @param toUpload the primitives to upload. If null or empty returns immediatelly
155 */
156 public void uploadPrimitives(OsmDataLayer layer, Collection<OsmPrimitive> toUpload) {
157 if (toUpload == null || toUpload.isEmpty()) return;
158 UploadHullBuilder builder = new UploadHullBuilder();
159 toUpload = builder.build(toUpload);
160 if (hasPrimitivesToDelete(toUpload)) {
161 // runs the check for deleted parents and then invokes
162 // processPostParentChecker()
163 //
164 Main.worker.submit(new DeletedParentsChecker(layer, toUpload));
165 } else {
166 processPostParentChecker(layer, toUpload);
167 }
168 }
169
170 protected void processPostParentChecker(OsmDataLayer layer, Collection<OsmPrimitive> toUpload) {
171 APIDataSet ds = new APIDataSet(toUpload);
172 UploadAction action = new UploadAction();
173 action.uploadData(layer, ds);
174 }
175
176 /**
177 * Computes the collection of primitives to upload, given a collection of candidate
178 * primitives.
179 * Some of the candidates are excluded, i.e. if they aren't modified.
180 * Other primitives are added. A typical case is a primitive which is new and and
181 * which is referred by a modified relation. In order to upload the relation the
182 * new primitive has to be uploaded as well, even if it isn't included in the
183 * list of candidate primitives.
184 *
185 */
186 static class UploadHullBuilder implements Visitor {
187 private Set<OsmPrimitive> hull;
188
189 UploadHullBuilder() {
190 hull = new HashSet<>();
191 }
192
193 @Override
194 public void visit(Node n) {
195 if (n.isNewOrUndeleted() || n.isModified() || n.isDeleted()) {
196 // upload new nodes as well as modified and deleted ones
197 hull.add(n);
198 }
199 }
200
201 @Override
202 public void visit(Way w) {
203 if (w.isNewOrUndeleted() || w.isModified() || w.isDeleted()) {
204 // upload new ways as well as modified and deleted ones
205 hull.add(w);
206 for (Node n: w.getNodes()) {
207 // we upload modified nodes even if they aren't in the current
208 // selection.
209 n.accept(this);
210 }
211 }
212 }
213
214 @Override
215 public void visit(Relation r) {
216 if (r.isNewOrUndeleted() || r.isModified() || r.isDeleted()) {
217 hull.add(r);
218 for (OsmPrimitive p : r.getMemberPrimitives()) {
219 // add new relation members. Don't include modified
220 // relation members. r shouldn't refer to deleted primitives,
221 // so wont check here for deleted primitives here
222 //
223 if (p.isNewOrUndeleted()) {
224 p.accept(this);
225 }
226 }
227 }
228 }
229
230 @Override
231 public void visit(Changeset cs) {
232 // do nothing
233 }
234
235 /**
236 * Builds the "hull" of primitives to be uploaded given a base collection
237 * of osm primitives.
238 *
239 * @param base the base collection. Must not be null.
240 * @return the "hull"
241 * @throws IllegalArgumentException if base is null
242 */
243 public Set<OsmPrimitive> build(Collection<OsmPrimitive> base) {
244 CheckParameterUtil.ensureParameterNotNull(base, "base");
245 hull = new HashSet<>();
246 for (OsmPrimitive p: base) {
247 p.accept(this);
248 }
249 return hull;
250 }
251 }
252
253 class DeletedParentsChecker extends PleaseWaitRunnable {
254 private boolean canceled;
255 private Exception lastException;
256 private final Collection<OsmPrimitive> toUpload;
257 private final OsmDataLayer layer;
258 private OsmServerBackreferenceReader reader;
259
260 /**
261 *
262 * @param layer the data layer for which a collection of selected primitives is uploaded
263 * @param toUpload the collection of primitives to upload
264 */
265 DeletedParentsChecker(OsmDataLayer layer, Collection<OsmPrimitive> toUpload) {
266 super(tr("Checking parents for deleted objects"));
267 this.toUpload = toUpload;
268 this.layer = layer;
269 }
270
271 @Override
272 protected void cancel() {
273 this.canceled = true;
274 synchronized (this) {
275 if (reader != null) {
276 reader.cancel();
277 }
278 }
279 }
280
281 @Override
282 protected void finish() {
283 if (canceled)
284 return;
285 if (lastException != null) {
286 ExceptionUtil.explainException(lastException);
287 return;
288 }
289 SwingUtilities.invokeLater(() -> processPostParentChecker(layer, toUpload));
290 }
291
292 /**
293 * Replies the collection of deleted OSM primitives for which we have to check whether
294 * there are dangling references on the server.
295 *
296 * @return primitives to check
297 */
298 protected Set<OsmPrimitive> getPrimitivesToCheckForParents() {
299 Set<OsmPrimitive> ret = new HashSet<>();
300 for (OsmPrimitive p: toUpload) {
301 if (p.isDeleted() && !p.isNewOrUndeleted()) {
302 ret.add(p);
303 }
304 }
305 return ret;
306 }
307
308 @Override
309 protected void realRun() throws SAXException, IOException, OsmTransferException {
310 try {
311 Stack<OsmPrimitive> toCheck = new Stack<>();
312 toCheck.addAll(getPrimitivesToCheckForParents());
313 Set<OsmPrimitive> checked = new HashSet<>();
314 while (!toCheck.isEmpty()) {
315 if (canceled) return;
316 OsmPrimitive current = toCheck.pop();
317 synchronized (this) {
318 reader = new OsmServerBackreferenceReader(current);
319 }
320 getProgressMonitor().subTask(tr("Reading parents of ''{0}''", current.getDisplayName(DefaultNameFormatter.getInstance())));
321 DataSet ds = reader.parseOsm(getProgressMonitor().createSubTaskMonitor(1, false));
322 synchronized (this) {
323 reader = null;
324 }
325 checked.add(current);
326 getProgressMonitor().subTask(tr("Checking for deleted parents in the local dataset"));
327 for (OsmPrimitive p: ds.allPrimitives()) {
328 if (canceled) return;
329 OsmPrimitive myDeletedParent = layer.data.getPrimitiveById(p);
330 // our local dataset includes a deleted parent of a primitive we want
331 // to delete. Include this parent in the collection of uploaded primitives
332 if (myDeletedParent != null && myDeletedParent.isDeleted()) {
333 if (!toUpload.contains(myDeletedParent)) {
334 toUpload.add(myDeletedParent);
335 }
336 if (!checked.contains(myDeletedParent)) {
337 toCheck.push(myDeletedParent);
338 }
339 }
340 }
341 }
342 } catch (OsmTransferException e) {
343 if (canceled)
344 // ignore exception
345 return;
346 lastException = e;
347 }
348 }
349 }
350}
Note: See TracBrowser for help on using the repository browser.