1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.upload;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.io.NetworkManager;
|
---|
20 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
21 | import org.openstreetmap.josm.io.OsmApi;
|
---|
22 | import org.openstreetmap.josm.io.OsmApiInitializationException;
|
---|
23 | import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
---|
24 | import org.openstreetmap.josm.tools.Logging;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Checks certain basic conditions, that are listed in the OSM API
|
---|
28 | * {@link org.openstreetmap.josm.io.Capabilities}.
|
---|
29 | */
|
---|
30 | public class ApiPreconditionCheckerHook implements UploadHook {
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public boolean checkUpload(APIDataSet apiData) {
|
---|
34 | OsmApi api = OsmApi.getOsmApi();
|
---|
35 | try {
|
---|
36 | if (NetworkManager.isOffline(OnlineResource.OSM_API)) {
|
---|
37 | return false;
|
---|
38 | }
|
---|
39 | // FIXME: this should run asynchronously and a progress monitor
|
---|
40 | // should be displayed.
|
---|
41 | api.initialize(NullProgressMonitor.INSTANCE);
|
---|
42 | long maxNodes = api.getCapabilities().getMaxWayNodes();
|
---|
43 | if (maxNodes > 0) {
|
---|
44 | if (!checkMaxNodes(apiData.getPrimitivesToAdd(), maxNodes))
|
---|
45 | return false;
|
---|
46 | if (!checkMaxNodes(apiData.getPrimitivesToUpdate(), maxNodes))
|
---|
47 | return false;
|
---|
48 | if (!checkMaxNodes(apiData.getPrimitivesToDelete(), maxNodes))
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | } catch (OsmTransferCanceledException e) {
|
---|
52 | Logging.trace(e);
|
---|
53 | return false;
|
---|
54 | } catch (OsmApiInitializationException e) {
|
---|
55 | ExceptionDialogUtil.explainOsmTransferException(e);
|
---|
56 | return false;
|
---|
57 | }
|
---|
58 | return true;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static boolean checkMaxNodes(Collection<OsmPrimitive> primitives, long maxNodes) {
|
---|
62 | for (OsmPrimitive osmPrimitive : primitives) {
|
---|
63 | for (Map.Entry<String, String> entry: osmPrimitive.getKeys().entrySet()) {
|
---|
64 | String key = entry.getKey();
|
---|
65 | String value = entry.getValue();
|
---|
66 | if (key.length() > Tagged.MAX_TAG_LENGTH) {
|
---|
67 | if (osmPrimitive.isDeleted()) {
|
---|
68 | // if OsmPrimitive is going to be deleted we automatically shorten the value
|
---|
69 | Logging.warn(
|
---|
70 | tr("Automatically truncating value of tag ''{0}'' on deleted object {1}",
|
---|
71 | key,
|
---|
72 | Long.toString(osmPrimitive.getId())
|
---|
73 | )
|
---|
74 | );
|
---|
75 | osmPrimitive.put(key, value.substring(0, Tagged.MAX_TAG_LENGTH));
|
---|
76 | continue;
|
---|
77 | }
|
---|
78 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
---|
79 | tr("Length of value for tag ''{0}'' on object {1} exceeds the max. allowed length {2}. Values length is {3}.",
|
---|
80 | key, Long.toString(osmPrimitive.getId()), Tagged.MAX_TAG_LENGTH, value.length()
|
---|
81 | ),
|
---|
82 | tr("Precondition violation"),
|
---|
83 | JOptionPane.ERROR_MESSAGE
|
---|
84 | );
|
---|
85 | MainApplication.getLayerManager().getEditDataSet().setSelected(Collections.singleton(osmPrimitive));
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (osmPrimitive instanceof Way &&
|
---|
91 | ((Way) osmPrimitive).getNodesCount() > maxNodes) {
|
---|
92 | JOptionPane.showMessageDialog(
|
---|
93 | MainApplication.getMainFrame(),
|
---|
94 | tr("{0} nodes in way {1} exceed the max. allowed number of nodes {2}",
|
---|
95 | ((Way) osmPrimitive).getNodesCount(),
|
---|
96 | Long.toString(osmPrimitive.getId()),
|
---|
97 | maxNodes
|
---|
98 | ),
|
---|
99 | tr("API Capabilities Violation"),
|
---|
100 | JOptionPane.ERROR_MESSAGE
|
---|
101 | );
|
---|
102 | MainApplication.getLayerManager().getEditDataSet().setSelected(Collections.singleton(osmPrimitive));
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return true;
|
---|
107 | }
|
---|
108 | }
|
---|