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