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