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

Last change on this file since 1575 was 1575, checked in by stoecker, 15 years ago

close #2443 - patch by Henrik Niehaus - History for commit dialog

  • Property svn:eol-style set to native
File size: 5.3 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;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.UploadAction;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
16import org.openstreetmap.josm.gui.historycombobox.JHistoryComboBox;
17import org.openstreetmap.josm.gui.historycombobox.StringUtils;
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 */
26public class OsmServerWriter {
27
28 /**
29 * This list contains all successfully processed objects. The caller of
30 * upload* has to check this after the call and update its dataset.
31 *
32 * If a server connection error occurs, this may contain fewer entries
33 * than where passed in the list to upload*.
34 */
35 public Collection<OsmPrimitive> processed;
36
37
38 private OsmApi api = new OsmApi();
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 float uploads_per_ms = (float)progress / elapsed;
52 float uploads_left = list_size - progress;
53 int ms_left = (int)(uploads_left / uploads_per_ms);
54 int minutes_left = ms_left / MSECS_PER_MINUTE;
55 int seconds_left = (ms_left / MSECS_PER_SECOND) % SECONDS_PER_MINUTE ;
56 String time_left_str = Integer.toString(minutes_left) + ":";
57 if (seconds_left < 10)
58 time_left_str += "0";
59 time_left_str += Integer.toString(seconds_left);
60 return time_left_str;
61 }
62
63 /**
64 * Send the dataset to the server.
65 * @param the_version version of the data set
66 * @param list list of objects to send
67 */
68 public void uploadOsm(String the_version, Collection<OsmPrimitive> list) {
69 processed = new LinkedList<OsmPrimitive>();
70 api.initialize();
71
72 Main.pleaseWaitDlg.progress.setMaximum(list.size());
73 Main.pleaseWaitDlg.progress.setValue(0);
74
75 boolean useChangesets = api.hasChangesetSupport();
76
77 // controls whether or not we try and upload the whole bunch in one go
78 boolean useDiffUploads = Main.pref.getBoolean("osm-server.atomic-upload",
79 "0.6".equals(api.getVersion()));
80
81 // create changeset if required
82 try {
83 if (useChangesets) {
84 // add the last entered comment to the changeset
85 String cmt = "";
86 List<String> history = StringUtils.stringToList(Main.pref.get(UploadAction.HISTORY_KEY), JHistoryComboBox.DELIM);
87 if(history.size() > 0) {
88 cmt = history.get(0);
89 }
90 api.createChangeset(cmt);
91 }
92 } catch (OsmTransferException ex) {
93 dealWithTransferException(ex);
94 return;
95 }
96
97 try {
98 if (useDiffUploads) {
99 // all in one go
100 processed.addAll(api.uploadDiff(list));
101 } else {
102 // upload changes individually (90% of code is for the status display...)
103 NameVisitor v = new NameVisitor();
104 uploadStartTime = System.currentTimeMillis();
105 for (OsmPrimitive osm : list) {
106 osm.visit(v);
107 int progress = Main.pleaseWaitDlg.progress.getValue();
108 String time_left_str = timeLeft(progress, list.size());
109 Main.pleaseWaitDlg.currentAction.setText(
110 tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})",
111 Math.round(100.0*progress/list.size()), progress,
112 list.size(), time_left_str, tr(v.className), v.name, osm.id));
113 makeApiRequest(osm);
114 processed.add(osm);
115 Main.pleaseWaitDlg.progress.setValue(progress+1);
116 }
117 }
118 if (useChangesets) api.stopChangeset();
119 } catch (OsmTransferException e) {
120 try {
121 if (useChangesets) api.stopChangeset();
122 } catch (Exception ee) {
123 // ignore nested exception
124 }
125 dealWithTransferException(e);
126 }
127 }
128
129 void makeApiRequest(OsmPrimitive osm) throws OsmTransferException {
130 if (osm.deleted) {
131 api.deletePrimitive(osm);
132 } else if (osm.id == 0) {
133 api.createPrimitive(osm);
134 } else {
135 api.modifyPrimitive(osm);
136 }
137 }
138
139 private void dealWithTransferException (OsmTransferException e) {
140 JOptionPane.showMessageDialog(Main.parent,
141 /* tr("Error during upload: ") + */ e.getMessage());
142 }
143}
Note: See TracBrowser for help on using the repository browser.