1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Iterator;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
17 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
18 | import org.openstreetmap.josm.gui.io.UploadStrategySpecification;
|
---|
19 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Class that uploads all changes to the osm server.
|
---|
25 | *
|
---|
26 | * This is done like this: - All objects with id = 0 are uploaded as new, except
|
---|
27 | * those in deleted, which are ignored - All objects in deleted list are
|
---|
28 | * deleted. - All remaining objects with modified flag set are updated.
|
---|
29 | */
|
---|
30 | public class OsmServerWriter {
|
---|
31 | /**
|
---|
32 | * This list contains all successfully processed objects. The caller of
|
---|
33 | * upload* has to check this after the call and update its dataset.
|
---|
34 | *
|
---|
35 | * If a server connection error occurs, this may contain fewer entries
|
---|
36 | * than where passed in the list to upload*.
|
---|
37 | */
|
---|
38 | private Collection<OsmPrimitive> processed;
|
---|
39 |
|
---|
40 | private static volatile List<OsmServerWritePostprocessor> postprocessors;
|
---|
41 | public static void registerPostprocessor(OsmServerWritePostprocessor pp) {
|
---|
42 | if (postprocessors == null) {
|
---|
43 | postprocessors = new ArrayList<>();
|
---|
44 | }
|
---|
45 | postprocessors.add(pp);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static void unregisterPostprocessor(OsmServerWritePostprocessor pp) {
|
---|
49 | if (postprocessors != null) {
|
---|
50 | postprocessors.remove(pp);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private final OsmApi api = OsmApi.getOsmApi();
|
---|
55 | private boolean canceled;
|
---|
56 |
|
---|
57 | private static final int MSECS_PER_SECOND = 1000;
|
---|
58 | private static final int SECONDS_PER_MINUTE = 60;
|
---|
59 | private static final int MSECS_PER_MINUTE = MSECS_PER_SECOND * SECONDS_PER_MINUTE;
|
---|
60 |
|
---|
61 | private long uploadStartTime;
|
---|
62 |
|
---|
63 | public String timeLeft(int progress, int list_size) {
|
---|
64 | long now = System.currentTimeMillis();
|
---|
65 | long elapsed = now - uploadStartTime;
|
---|
66 | if (elapsed == 0) {
|
---|
67 | elapsed = 1;
|
---|
68 | }
|
---|
69 | double uploads_per_ms = (double) progress / elapsed;
|
---|
70 | double uploads_left = list_size - progress;
|
---|
71 | long ms_left = (long) (uploads_left / uploads_per_ms);
|
---|
72 | long minutes_left = ms_left / MSECS_PER_MINUTE;
|
---|
73 | long seconds_left = (ms_left / MSECS_PER_SECOND) % SECONDS_PER_MINUTE;
|
---|
74 | StringBuilder time_left_str = new StringBuilder().append(minutes_left).append(':');
|
---|
75 | if (seconds_left < 10) {
|
---|
76 | time_left_str.append('0');
|
---|
77 | }
|
---|
78 | return time_left_str.append(seconds_left).toString();
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Uploads the changes individually. Invokes one API call per uploaded primitmive.
|
---|
83 | *
|
---|
84 | * @param primitives the collection of primitives to upload
|
---|
85 | * @param progressMonitor the progress monitor
|
---|
86 | * @throws OsmTransferException if an exception occurs
|
---|
87 | */
|
---|
88 | protected void uploadChangesIndividually(Collection<? extends OsmPrimitive> primitives, ProgressMonitor progressMonitor)
|
---|
89 | throws OsmTransferException {
|
---|
90 | try {
|
---|
91 | progressMonitor.beginTask(tr("Starting to upload with one request per primitive ..."));
|
---|
92 | progressMonitor.setTicksCount(primitives.size());
|
---|
93 | uploadStartTime = System.currentTimeMillis();
|
---|
94 | for (OsmPrimitive osm : primitives) {
|
---|
95 | int progress = progressMonitor.getTicks();
|
---|
96 | String time_left_str = timeLeft(progress, primitives.size());
|
---|
97 | String msg = "";
|
---|
98 | switch(OsmPrimitiveType.from(osm)) {
|
---|
99 | case NODE: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading node ''{4}'' (id: {5})"); break;
|
---|
100 | case WAY: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading way ''{4}'' (id: {5})"); break;
|
---|
101 | case RELATION: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading relation ''{4}'' (id: {5})"); break;
|
---|
102 | }
|
---|
103 | progressMonitor.subTask(
|
---|
104 | tr(msg,
|
---|
105 | Math.round(100.0*progress/primitives.size()),
|
---|
106 | progress,
|
---|
107 | primitives.size(),
|
---|
108 | time_left_str,
|
---|
109 | osm.getName() == null ? osm.getId() : osm.getName(),
|
---|
110 | osm.getId()));
|
---|
111 | makeApiRequest(osm, progressMonitor);
|
---|
112 | processed.add(osm);
|
---|
113 | progressMonitor.worked(1);
|
---|
114 | }
|
---|
115 | } catch (OsmTransferException e) {
|
---|
116 | throw e;
|
---|
117 | } catch (Exception e) {
|
---|
118 | throw new OsmTransferException(e);
|
---|
119 | } finally {
|
---|
120 | progressMonitor.finishTask();
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Upload all changes in one diff upload
|
---|
126 | *
|
---|
127 | * @param primitives the collection of primitives to upload
|
---|
128 | * @param progressMonitor the progress monitor
|
---|
129 | * @throws OsmTransferException if an exception occurs
|
---|
130 | */
|
---|
131 | protected void uploadChangesAsDiffUpload(Collection<? extends OsmPrimitive> primitives, ProgressMonitor progressMonitor)
|
---|
132 | throws OsmTransferException {
|
---|
133 | try {
|
---|
134 | progressMonitor.beginTask(tr("Starting to upload in one request ..."));
|
---|
135 | processed.addAll(api.uploadDiff(primitives, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)));
|
---|
136 | } catch (OsmTransferException e) {
|
---|
137 | throw e;
|
---|
138 | } finally {
|
---|
139 | progressMonitor.finishTask();
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Upload all changes in one diff upload
|
---|
145 | *
|
---|
146 | * @param primitives the collection of primitives to upload
|
---|
147 | * @param progressMonitor the progress monitor
|
---|
148 | * @param chunkSize the size of the individual upload chunks. > 0 required.
|
---|
149 | * @throws IllegalArgumentException if chunkSize <= 0
|
---|
150 | * @throws OsmTransferException if an exception occurs
|
---|
151 | */
|
---|
152 | protected void uploadChangesInChunks(Collection<? extends OsmPrimitive> primitives, ProgressMonitor progressMonitor, int chunkSize)
|
---|
153 | throws OsmTransferException, IllegalArgumentException {
|
---|
154 | if (chunkSize <= 0)
|
---|
155 | throw new IllegalArgumentException(tr("Value >0 expected for parameter ''{0}'', got {1}", "chunkSize", chunkSize));
|
---|
156 | try {
|
---|
157 | progressMonitor.beginTask(tr("Starting to upload in chunks..."));
|
---|
158 | List<OsmPrimitive> chunk = new ArrayList<>(chunkSize);
|
---|
159 | Iterator<? extends OsmPrimitive> it = primitives.iterator();
|
---|
160 | int numChunks = (int) Math.ceil((double) primitives.size() / (double) chunkSize);
|
---|
161 | int i = 0;
|
---|
162 | while (it.hasNext()) {
|
---|
163 | i++;
|
---|
164 | if (canceled) return;
|
---|
165 | int j = 0;
|
---|
166 | chunk.clear();
|
---|
167 | while (it.hasNext() && j < chunkSize) {
|
---|
168 | if (canceled) return;
|
---|
169 | j++;
|
---|
170 | chunk.add(it.next());
|
---|
171 | }
|
---|
172 | progressMonitor.setCustomText(
|
---|
173 | trn("({0}/{1}) Uploading {2} object...",
|
---|
174 | "({0}/{1}) Uploading {2} objects...",
|
---|
175 | chunk.size(), i, numChunks, chunk.size()));
|
---|
176 | processed.addAll(api.uploadDiff(chunk, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)));
|
---|
177 | }
|
---|
178 | } catch (OsmTransferException e) {
|
---|
179 | throw e;
|
---|
180 | } finally {
|
---|
181 | progressMonitor.finishTask();
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Send the dataset to the server.
|
---|
187 | *
|
---|
188 | * @param strategy the upload strategy. Must not be null.
|
---|
189 | * @param primitives list of objects to send
|
---|
190 | * @param changeset the changeset the data is uploaded to. Must not be null.
|
---|
191 | * @param monitor the progress monitor. If null, assumes {@link NullProgressMonitor#INSTANCE}
|
---|
192 | * @throws IllegalArgumentException if changeset is null
|
---|
193 | * @throws IllegalArgumentException if strategy is null
|
---|
194 | * @throws OsmTransferException if something goes wrong
|
---|
195 | */
|
---|
196 | public void uploadOsm(UploadStrategySpecification strategy, Collection<? extends OsmPrimitive> primitives,
|
---|
197 | Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
|
---|
198 | CheckParameterUtil.ensureParameterNotNull(changeset, "changeset");
|
---|
199 | processed = new LinkedList<>();
|
---|
200 | monitor = monitor == null ? NullProgressMonitor.INSTANCE : monitor;
|
---|
201 | monitor.beginTask(tr("Uploading data ..."));
|
---|
202 | try {
|
---|
203 | api.initialize(monitor);
|
---|
204 | // check whether we can use diff upload
|
---|
205 | if (changeset.getId() == 0) {
|
---|
206 | api.openChangeset(changeset, monitor.createSubTaskMonitor(0, false));
|
---|
207 | // update the user information
|
---|
208 | changeset.setUser(JosmUserIdentityManager.getInstance().asUser());
|
---|
209 | } else {
|
---|
210 | api.updateChangeset(changeset, monitor.createSubTaskMonitor(0, false));
|
---|
211 | }
|
---|
212 | api.setChangeset(changeset);
|
---|
213 | switch(strategy.getStrategy()) {
|
---|
214 | case SINGLE_REQUEST_STRATEGY:
|
---|
215 | uploadChangesAsDiffUpload(primitives, monitor.createSubTaskMonitor(0, false));
|
---|
216 | break;
|
---|
217 | case INDIVIDUAL_OBJECTS_STRATEGY:
|
---|
218 | uploadChangesIndividually(primitives, monitor.createSubTaskMonitor(0, false));
|
---|
219 | break;
|
---|
220 | case CHUNKED_DATASET_STRATEGY:
|
---|
221 | uploadChangesInChunks(primitives, monitor.createSubTaskMonitor(0, false), strategy.getChunkSize());
|
---|
222 | break;
|
---|
223 | }
|
---|
224 | } catch (OsmTransferException e) {
|
---|
225 | throw e;
|
---|
226 | } finally {
|
---|
227 | executePostprocessors(monitor);
|
---|
228 | monitor.finishTask();
|
---|
229 | api.setChangeset(null);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | void makeApiRequest(OsmPrimitive osm, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
234 | if (osm.isDeleted()) {
|
---|
235 | api.deletePrimitive(osm, progressMonitor);
|
---|
236 | } else if (osm.isNew()) {
|
---|
237 | api.createPrimitive(osm, progressMonitor);
|
---|
238 | } else {
|
---|
239 | api.modifyPrimitive(osm, progressMonitor);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | public void cancel() {
|
---|
244 | this.canceled = true;
|
---|
245 | if (api != null) {
|
---|
246 | api.cancel();
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Replies the collection of successfully processed primitives
|
---|
252 | *
|
---|
253 | * @return the collection of successfully processed primitives
|
---|
254 | */
|
---|
255 | public Collection<OsmPrimitive> getProcessedPrimitives() {
|
---|
256 | return processed;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Calls all registered upload postprocessors.
|
---|
261 | * @param pm progress monitor
|
---|
262 | */
|
---|
263 | public void executePostprocessors(ProgressMonitor pm) {
|
---|
264 | if (postprocessors != null) {
|
---|
265 | for (OsmServerWritePostprocessor pp : postprocessors) {
|
---|
266 | pp.postprocessUploadedPrimitives(processed, pm);
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|