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

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

new: replaced global conflict list by conflict list per layer, similar to datasets

  • Property svn:eol-style set to native
File size: 5.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.tr;
5
6import java.util.Collection;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.logging.Logger;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.actions.UploadAction;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
15
16/**
17 * Class that uploads all changes to the osm server.
18 *
19 * This is done like this: - All objects with id = 0 are uploaded as new, except
20 * those in deleted, which are ignored - All objects in deleted list are
21 * deleted. - All remaining objects with modified flag set are updated.
22 */
23public class OsmServerWriter {
24 static private final Logger logger = Logger.getLogger(OsmServerWriter.class.getName());
25
26 /**
27 * This list contains all successfully processed objects. The caller of
28 * upload* has to check this after the call and update its dataset.
29 *
30 * If a server connection error occurs, this may contain fewer entries
31 * than where passed in the list to upload*.
32 */
33 public Collection<OsmPrimitive> processed;
34
35 private OsmApi api = OsmApi.getOsmApi();
36
37 private static final int MSECS_PER_SECOND = 1000;
38 private static final int SECONDS_PER_MINUTE = 60;
39 private static final int MSECS_PER_MINUTE = MSECS_PER_SECOND * SECONDS_PER_MINUTE;
40
41 long uploadStartTime;
42
43 public String timeLeft(int progress, int list_size) {
44 long now = System.currentTimeMillis();
45 long elapsed = now - uploadStartTime;
46 if (elapsed == 0) {
47 elapsed = 1;
48 }
49 float uploads_per_ms = (float)progress / elapsed;
50 float uploads_left = list_size - progress;
51 int ms_left = (int)(uploads_left / uploads_per_ms);
52 int minutes_left = ms_left / MSECS_PER_MINUTE;
53 int seconds_left = (ms_left / MSECS_PER_SECOND) % SECONDS_PER_MINUTE ;
54 String time_left_str = Integer.toString(minutes_left) + ":";
55 if (seconds_left < 10) {
56 time_left_str += "0";
57 }
58 time_left_str += Integer.toString(seconds_left);
59 return time_left_str;
60 }
61
62 /**
63 * retrieves the most recent changeset comment from the preferences
64 *
65 * @return the most recent changeset comment
66 */
67 protected String getChangesetComment() {
68 String cmt = "";
69 List<String> history = new LinkedList<String>(
70 Main.pref.getCollection(UploadAction.HISTORY_KEY, new LinkedList<String>()));
71 if(history.size() > 0) {
72 cmt = history.get(0);
73 }
74 return cmt;
75 }
76
77 /**
78 * Send the dataset to the server.
79 *
80 * @param apiVersion version of the data set
81 * @param primitives list of objects to send
82 */
83 public void uploadOsm(String apiVersion, Collection<OsmPrimitive> primitives) throws OsmTransferException {
84 processed = new LinkedList<OsmPrimitive>();
85
86 api.initialize();
87
88 Main.pleaseWaitDlg.progress.setMaximum(primitives.size());
89 Main.pleaseWaitDlg.progress.setValue(0);
90
91 // check whether we can use changeset
92 //
93 boolean canUseChangeset = api.hasChangesetSupport();
94 boolean useChangeset = Main.pref.getBoolean("osm-server.atomic-upload", apiVersion.compareTo("0.6")>=0);
95 if (useChangeset && ! canUseChangeset) {
96 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));
97 useChangeset = false;
98 }
99
100 if (useChangeset) {
101 // upload everything in one changeset
102 //
103 try {
104 api.createChangeset(getChangesetComment());
105 processed.addAll(api.uploadDiff(primitives));
106 } catch(OsmTransferException e) {
107 throw e;
108 } finally {
109 try {
110 if (canUseChangeset) {
111 api.stopChangeset();
112 }
113 } catch (Exception ee) {
114 ee.printStackTrace();
115 // ignore nested exception
116 }
117 }
118 } else {
119 // upload changes individually (90% of code is for the status display...)
120 //
121 api.createChangeset(getChangesetComment());
122 NameVisitor v = new NameVisitor();
123 uploadStartTime = System.currentTimeMillis();
124 for (OsmPrimitive osm : primitives) {
125 osm.visit(v);
126 int progress = Main.pleaseWaitDlg.progress.getValue();
127 String time_left_str = timeLeft(progress, primitives.size());
128 Main.pleaseWaitDlg.currentAction.setText(
129 tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})",
130 Math.round(100.0*progress/primitives.size()), progress,
131 primitives.size(), time_left_str, tr(v.className), v.name, osm.id));
132 makeApiRequest(osm);
133 processed.add(osm);
134 Main.pleaseWaitDlg.progress.setValue(progress+1);
135 }
136 api.stopChangeset();
137 }
138 }
139
140 void makeApiRequest(OsmPrimitive osm) throws OsmTransferException {
141 if (osm.deleted) {
142 api.deletePrimitive(osm);
143 } else if (osm.id == 0) {
144 api.createPrimitive(osm);
145 } else {
146 api.modifyPrimitive(osm);
147 }
148 }
149
150 public void disconnectActiveConnection() {
151 if (api != null && api.activeConnection != null) {
152 api.activeConnection.disconnect();
153 }
154 }
155}
Note: See TracBrowser for help on using the repository browser.