source: josm/trunk/src/org/openstreetmap/josm/actions/upload/ApiPreconditionCheckerHook.java@ 12636

Last change on this file since 12636 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

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