[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[626] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.util.Collection;
|
---|
| 7 | import java.util.LinkedList;
|
---|
[1575] | 8 | import java.util.List;
|
---|
[1523] | 9 |
|
---|
[1559] | 10 | import javax.swing.JOptionPane;
|
---|
| 11 |
|
---|
[626] | 12 | import org.openstreetmap.josm.Main;
|
---|
[1546] | 13 | import org.openstreetmap.josm.actions.UploadAction;
|
---|
[626] | 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
[1575] | 16 | import org.openstreetmap.josm.gui.historycombobox.JHistoryComboBox;
|
---|
| 17 | import org.openstreetmap.josm.gui.historycombobox.StringUtils;
|
---|
[626] | 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 | */
|
---|
[1523] | 26 | public class OsmServerWriter {
|
---|
[626] | 27 |
|
---|
[1137] | 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;
|
---|
[1546] | 36 |
|
---|
[626] | 37 |
|
---|
[1523] | 38 | private OsmApi api = new OsmApi();
|
---|
| 39 |
|
---|
[1137] | 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;
|
---|
[529] | 43 |
|
---|
[1137] | 44 | long uploadStartTime;
|
---|
[1169] | 45 |
|
---|
[1137] | 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 | }
|
---|
[1169] | 62 |
|
---|
[1523] | 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) {
|
---|
[1137] | 69 | processed = new LinkedList<OsmPrimitive>();
|
---|
[1523] | 70 | api.initialize();
|
---|
[626] | 71 |
|
---|
[1137] | 72 | Main.pleaseWaitDlg.progress.setMaximum(list.size());
|
---|
| 73 | Main.pleaseWaitDlg.progress.setValue(0);
|
---|
[1169] | 74 |
|
---|
[1523] | 75 | boolean useChangesets = api.hasChangesetSupport();
|
---|
| 76 |
|
---|
| 77 | // controls whether or not we try and upload the whole bunch in one go
|
---|
[1169] | 78 | boolean useDiffUploads = Main.pref.getBoolean("osm-server.atomic-upload",
|
---|
[1523] | 79 | "0.6".equals(api.getVersion()));
|
---|
[1169] | 80 |
|
---|
[1523] | 81 | // create changeset if required
|
---|
[1137] | 82 | try {
|
---|
[1575] | 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 | }
|
---|
[1523] | 92 | } catch (OsmTransferException ex) {
|
---|
[1137] | 93 | dealWithTransferException(ex);
|
---|
| 94 | return;
|
---|
| 95 | }
|
---|
[1169] | 96 |
|
---|
[1137] | 97 | try {
|
---|
[1071] | 98 | if (useDiffUploads) {
|
---|
[1523] | 99 | // all in one go
|
---|
| 100 | processed.addAll(api.uploadDiff(list));
|
---|
[1071] | 101 | } else {
|
---|
[1523] | 102 | // upload changes individually (90% of code is for the status display...)
|
---|
[1071] | 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());
|
---|
[1338] | 109 | Main.pleaseWaitDlg.currentAction.setText(
|
---|
[1523] | 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);
|
---|
[1071] | 115 | Main.pleaseWaitDlg.progress.setValue(progress+1);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
[1523] | 118 | if (useChangesets) api.stopChangeset();
|
---|
| 119 | } catch (OsmTransferException e) {
|
---|
[1137] | 120 | try {
|
---|
[1523] | 121 | if (useChangesets) api.stopChangeset();
|
---|
| 122 | } catch (Exception ee) {
|
---|
| 123 | // ignore nested exception
|
---|
[1137] | 124 | }
|
---|
| 125 | dealWithTransferException(e);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
[1169] | 128 |
|
---|
[1523] | 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);
|
---|
[1137] | 134 | } else {
|
---|
[1523] | 135 | api.modifyPrimitive(osm);
|
---|
[1137] | 136 | }
|
---|
| 137 | }
|
---|
[624] | 138 |
|
---|
[1137] | 139 | private void dealWithTransferException (OsmTransferException e) {
|
---|
[1559] | 140 | JOptionPane.showMessageDialog(Main.parent,
|
---|
| 141 | /* tr("Error during upload: ") + */ e.getMessage());
|
---|
[1137] | 142 | }
|
---|
[626] | 143 | }
|
---|