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

Last change on this file since 2115 was 2115, checked in by Gubaer, 15 years ago

new: reading open changesets from the server
new: reading user info from the server
new: any open changeset can be used when uploading
new: generic dialog for closing changesets
fixed #3427: JOSM can't keep many changesets open at once
fixed #3408: Allow continuing opened changeset even after restarting JOSM
fixed #3476: Default selection in upload dialog should be different for unclosed changesets. (Upload dialog now looks different)

  • Property svn:eol-style set to native
File size: 8.7 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.Collection;
8import java.util.LinkedList;
9import java.util.logging.Logger;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.Changeset;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
15import org.openstreetmap.josm.gui.progress.ProgressMonitor;
16
17/**
18 * Class that uploads all changes to the osm server.
19 *
20 * This is done like this: - All objects with id = 0 are uploaded as new, except
21 * those in deleted, which are ignored - All objects in deleted list are
22 * deleted. - All remaining objects with modified flag set are updated.
23 */
24public class OsmServerWriter {
25 static private final Logger logger = Logger.getLogger(OsmServerWriter.class.getName());
26
27 /**
28 * This list contains all successfully processed objects. The caller of
29 * upload* has to check this after the call and update its dataset.
30 *
31 * If a server connection error occurs, this may contain fewer entries
32 * than where passed in the list to upload*.
33 */
34 private Collection<OsmPrimitive> processed;
35
36 private OsmApi api = OsmApi.getOsmApi();
37
38 private static final int MSECS_PER_SECOND = 1000;
39 private static final int SECONDS_PER_MINUTE = 60;
40 private static final int MSECS_PER_MINUTE = MSECS_PER_SECOND * SECONDS_PER_MINUTE;
41
42 long uploadStartTime;
43
44 public String timeLeft(int progress, int list_size) {
45 long now = System.currentTimeMillis();
46 long elapsed = now - uploadStartTime;
47 if (elapsed == 0) {
48 elapsed = 1;
49 }
50 float uploads_per_ms = (float)progress / elapsed;
51 float uploads_left = list_size - progress;
52 int ms_left = (int)(uploads_left / uploads_per_ms);
53 int minutes_left = ms_left / MSECS_PER_MINUTE;
54 int seconds_left = (ms_left / MSECS_PER_SECOND) % SECONDS_PER_MINUTE ;
55 String time_left_str = Integer.toString(minutes_left) + ":";
56 if (seconds_left < 10) {
57 time_left_str += "0";
58 }
59 time_left_str += Integer.toString(seconds_left);
60 return time_left_str;
61 }
62
63 /**
64 * Uploads the changes individually. Invokes one API call per uploaded primitmive.
65 *
66 * @param primitives the collection of primitives to upload
67 * @param progressMonitor the progress monitor
68 * @throws OsmTransferException thrown if an exception occurs
69 */
70 protected void uploadChangesIndividually(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
71 try {
72 progressMonitor.beginTask(tr("Starting to upload with one request per primitive ..."));
73 progressMonitor.setTicksCount(primitives.size());
74 uploadStartTime = System.currentTimeMillis();
75 for (OsmPrimitive osm : primitives) {
76 int progress = progressMonitor.getTicks();
77 String time_left_str = timeLeft(progress, primitives.size());
78 String msg = "";
79 switch(OsmPrimitiveType.from(osm)) {
80 case NODE: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading node ''{4}'' (id: {5})"); break;
81 case WAY: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading way ''{4}'' (id: {5})"); break;
82 case RELATION: msg = marktr("{0}% ({1}/{2}), {3} left. Uploading relation ''{4}'' (id: {5})"); break;
83 }
84 progressMonitor.subTask(
85 tr(msg,
86 Math.round(100.0*progress/primitives.size()),
87 progress,
88 primitives.size(),
89 time_left_str,
90 osm.getName() == null ? osm.getId() : osm.getName(),
91 osm.getId()));
92 makeApiRequest(osm,progressMonitor);
93 processed.add(osm);
94 progressMonitor.worked(1);
95 }
96 } catch(OsmTransferException e) {
97 throw e;
98 } catch(Exception e) {
99 throw new OsmTransferException(e);
100 } finally {
101 progressMonitor.finishTask();
102 }
103 }
104
105 /**
106 * Upload all changes in one diff upload
107 *
108 * @param primitives the collection of primitives to upload
109 * @param progressMonitor the progress monitor
110 * @throws OsmTransferException thrown if an exception occurs
111 */
112 protected void uploadChangesAsDiffUpload(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
113 // upload everything in one changeset
114 //
115 try {
116 progressMonitor.beginTask(tr("Starting to upload in one request ..."));
117 processed.addAll(api.uploadDiff(primitives, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)));
118 } catch(OsmTransferException e) {
119 throw e;
120 } catch(Exception e) {
121 throw new OsmTransferException(e);
122 } finally {
123 progressMonitor.finishTask();
124 }
125 }
126
127 /**
128 * Send the dataset to the server.
129 *
130 * @param apiVersion version of the data set
131 * @param primitives list of objects to send
132 */
133 public void uploadOsm(String apiVersion, Collection<OsmPrimitive> primitives, Changeset changeset, boolean closeChangesetAfterUpload, ProgressMonitor progressMonitor) throws OsmTransferException {
134 processed = new LinkedList<OsmPrimitive>();
135 progressMonitor.beginTask(tr("Uploading data ..."));
136 api.initialize(progressMonitor);
137 try {
138 // check whether we can use diff upload
139 //
140 boolean casUseDiffUploads = api.hasSupportForDiffUploads();
141 if (apiVersion == null) {
142 System.out.println(tr("WARNING: no API version defined for data to upload. Falling back to version 0.6"));
143 apiVersion = "0.6";
144 }
145 boolean useDiffUpload = Main.pref.getBoolean("osm-server.atomic-upload", apiVersion.compareTo("0.6")>=0);
146 if (useDiffUpload && ! casUseDiffUploads) {
147 System.out.println(tr("WARNING: preference ''{0}'' or api version ''{1}'' of dataset requires to use diff uploads, but API is not able to handle them. Ignoring diff upload.", "osm-server.atomic-upload", apiVersion));
148 useDiffUpload = false;
149 }
150 if (changeset == null) {
151 changeset = new Changeset();
152 }
153 if (changeset.getId() == 0) {
154 api.openChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
155 } else {
156 api.updateChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
157 }
158 api.setChangeset(changeset);
159 if (useDiffUpload) {
160 uploadChangesAsDiffUpload(primitives,progressMonitor.createSubTaskMonitor(0,false));
161 } else {
162 uploadChangesIndividually(primitives,progressMonitor.createSubTaskMonitor(0,false));
163 }
164 } catch(OsmTransferException e) {
165 throw e;
166 } catch(Exception e) {
167 throw new OsmTransferException(e);
168 } finally {
169 try {
170 if (closeChangesetAfterUpload && api.getChangeset() != null && api.getChangeset().getId() > 0) {
171 api.closeChangeset(changeset,progressMonitor.createSubTaskMonitor(0, false));
172 api.setChangeset(null);
173 }
174 } catch (Exception ee) {
175 OsmChangesetCloseException closeException = new OsmChangesetCloseException(ee);
176 closeException.setChangeset(api.getChangeset());
177 throw closeException;
178 } finally {
179 progressMonitor.finishTask();
180 }
181 }
182 }
183
184 void makeApiRequest(OsmPrimitive osm, ProgressMonitor progressMonitor) throws OsmTransferException {
185 if (osm.isDeleted()) {
186 api.deletePrimitive(osm, progressMonitor);
187 } else if (osm.getId() == 0) {
188 api.createPrimitive(osm, progressMonitor);
189 } else {
190 api.modifyPrimitive(osm,progressMonitor);
191 }
192 }
193
194 public void cancel() {
195 if (api != null) {
196 api.cancel();
197 }
198 }
199
200 /**
201 * Replies the collection of successfully processed primitives
202 *
203 * @return the collection of successfully processed primitives
204 */
205 public Collection<OsmPrimitive> getProcessedPrimitives() {
206 return processed;
207 }
208}
Note: See TracBrowser for help on using the repository browser.