1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.HashSet;
|
---|
8 | import java.util.Optional;
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
12 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
13 | import org.openstreetmap.josm.data.osm.CyclicUploadDependencyException;
|
---|
14 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
15 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.io.OsmApi;
|
---|
22 | import org.openstreetmap.josm.io.OsmApiPrimitiveGoneException;
|
---|
23 | import org.openstreetmap.josm.io.OsmServerWriter;
|
---|
24 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
25 | import org.openstreetmap.josm.io.UploadStrategySpecification;
|
---|
26 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
27 | import org.openstreetmap.josm.tools.Logging;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * UploadLayerTask uploads the data managed by an {@link OsmDataLayer} asynchronously.
|
---|
31 | *
|
---|
32 | * <pre>
|
---|
33 | * ExecutorService executorService = ...
|
---|
34 | * UploadLayerTask task = new UploadLayerTask(layer, monitor);
|
---|
35 | * Future<?> taskFuture = executorService.submit(task)
|
---|
36 | * try {
|
---|
37 | * // wait for the task to complete
|
---|
38 | * taskFuture.get();
|
---|
39 | * } catch (Exception e) {
|
---|
40 | * e.printStackTrace();
|
---|
41 | * }
|
---|
42 | * </pre>
|
---|
43 | */
|
---|
44 | public class UploadLayerTask extends AbstractIOTask {
|
---|
45 | private OsmServerWriter writer;
|
---|
46 | private final OsmDataLayer layer;
|
---|
47 | private final ProgressMonitor monitor;
|
---|
48 | private final Changeset changeset;
|
---|
49 | private Collection<OsmPrimitive> toUpload;
|
---|
50 | private final Set<IPrimitive> processedPrimitives;
|
---|
51 | private final UploadStrategySpecification strategy;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Creates the upload task
|
---|
55 | *
|
---|
56 | * @param strategy the upload strategy specification
|
---|
57 | * @param layer the layer. Must not be null.
|
---|
58 | * @param monitor a progress monitor. If monitor is null, uses {@link NullProgressMonitor#INSTANCE}
|
---|
59 | * @param changeset the changeset to be used
|
---|
60 | * @throws IllegalArgumentException if layer is null
|
---|
61 | * @throws IllegalArgumentException if strategy is null
|
---|
62 | */
|
---|
63 | public UploadLayerTask(UploadStrategySpecification strategy, OsmDataLayer layer, ProgressMonitor monitor, Changeset changeset) {
|
---|
64 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
65 | CheckParameterUtil.ensureParameterNotNull(strategy, "strategy");
|
---|
66 | this.layer = layer;
|
---|
67 | this.monitor = Optional.ofNullable(monitor).orElse(NullProgressMonitor.INSTANCE);
|
---|
68 | this.changeset = changeset;
|
---|
69 | this.strategy = strategy;
|
---|
70 | processedPrimitives = new HashSet<>();
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected OsmPrimitive getPrimitive(OsmPrimitiveType type, long id) {
|
---|
74 | for (OsmPrimitive p: toUpload) {
|
---|
75 | if (OsmPrimitiveType.from(p).equals(type) && p.getId() == id)
|
---|
76 | return p;
|
---|
77 | }
|
---|
78 | return null;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Retries to recover the upload operation from an exception which was thrown because
|
---|
83 | * an uploaded primitive was already deleted on the server.
|
---|
84 | *
|
---|
85 | * @param e the exception throw by the API
|
---|
86 | * @throws OsmTransferException if we can't recover from the exception
|
---|
87 | */
|
---|
88 | protected void recoverFromGoneOnServer(OsmApiPrimitiveGoneException e) throws OsmTransferException {
|
---|
89 | if (!e.isKnownPrimitive()) throw e;
|
---|
90 | OsmPrimitive p = getPrimitive(e.getPrimitiveType(), e.getPrimitiveId());
|
---|
91 | if (p == null) throw e;
|
---|
92 | if (p.isDeleted()) {
|
---|
93 | // we tried to delete an already deleted primitive.
|
---|
94 | Logging.warn(tr("Object ''{0}'' is already deleted on the server. Skipping this object and retrying to upload.",
|
---|
95 | p.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
96 | processedPrimitives.addAll(writer.getProcessedPrimitives());
|
---|
97 | processedPrimitives.add(p);
|
---|
98 | toUpload.removeAll(processedPrimitives);
|
---|
99 | return;
|
---|
100 | }
|
---|
101 | // exception was thrown because we tried to *update* an already deleted primitive. We can't resolve this automatically.
|
---|
102 | // Re-throw exception, a conflict is going to be created later.
|
---|
103 | throw e;
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void run() {
|
---|
108 | monitor.indeterminateSubTask(tr("Preparing objects to upload ..."));
|
---|
109 | APIDataSet ds = new APIDataSet(layer.data);
|
---|
110 | try {
|
---|
111 | ds.adjustRelationUploadOrder();
|
---|
112 | } catch (CyclicUploadDependencyException e) {
|
---|
113 | setLastException(e);
|
---|
114 | return;
|
---|
115 | }
|
---|
116 | toUpload = ds.getPrimitives();
|
---|
117 | if (toUpload.isEmpty())
|
---|
118 | return;
|
---|
119 | writer = new OsmServerWriter();
|
---|
120 | try {
|
---|
121 | while (true) {
|
---|
122 | try {
|
---|
123 | ProgressMonitor m = monitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
---|
124 | if (isCanceled()) return;
|
---|
125 | writer.uploadOsm(strategy, toUpload, changeset, m);
|
---|
126 | processedPrimitives.addAll(writer.getProcessedPrimitives()); // OsmPrimitive in => OsmPrimitive out
|
---|
127 | break;
|
---|
128 | } catch (OsmApiPrimitiveGoneException e) {
|
---|
129 | recoverFromGoneOnServer(e);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | if (strategy.isCloseChangesetAfterUpload() && changeset != null && changeset.getId() > 0) {
|
---|
133 | OsmApi.getOsmApi().closeChangeset(changeset, monitor.createSubTaskMonitor(0, false));
|
---|
134 | }
|
---|
135 | } catch (OsmTransferException sxe) {
|
---|
136 | if (isCanceled()) {
|
---|
137 | Logging.info("Ignoring exception caught because upload is canceled. Exception is: " + sxe);
|
---|
138 | return;
|
---|
139 | }
|
---|
140 | setLastException(sxe);
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (isCanceled())
|
---|
144 | return;
|
---|
145 | layer.cleanupAfterUpload(processedPrimitives);
|
---|
146 | layer.onPostUploadToServer();
|
---|
147 |
|
---|
148 | // don't process exceptions remembered with setLastException().
|
---|
149 | // Caller is supposed to deal with them.
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public void cancel() {
|
---|
154 | setCanceled(true);
|
---|
155 | if (writer != null) {
|
---|
156 | writer.cancel();
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|