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