source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java@ 2702

Last change on this file since 2702 was 2599, checked in by Gubaer, 14 years ago

fixed #4130: Chunked upload mode counter is wrong
fixed #4118: Upload dialog too complicated
fixed #4129: Hide the new "Upload data in one request/chunks/individually" behind an expanding "Upload method" box
fixed #2075: API 0.6: don't upload more than 50K edits at once
fixed #4044: Huge uploads never end [should be solved with chunked upload mode]
fixed #4110: Upload dialog spacing wrong
fixed #3386: Upload dialog has empty areas when the changeset doesn't include all of add/modify/delete operations
see #3369: bulk import helper [JOSM now supports multi changesets uploads]

See online help for more details.

Completes r2598

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