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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 4.2 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.progress.NullProgressMonitor;
17import org.openstreetmap.josm.io.OsmApi;
18import org.openstreetmap.josm.io.OsmApiInitializationException;
19import org.openstreetmap.josm.io.OsmTransferCanceledException;
20
21public class ApiPreconditionCheckerHook implements UploadHook {
22
23 @Override
24 public boolean checkUpload(APIDataSet apiData) {
25 OsmApi api = OsmApi.getOsmApi();
26 try {
27 // FIXME: this should run asynchronously and a progress monitor
28 // should be displayed.
29 api.initialize(NullProgressMonitor.INSTANCE);
30 long maxNodes = 0;
31 if (api.getCapabilities().isDefined("waynodes", "maximum")) {
32 maxNodes = api.getCapabilities().getLong("waynodes","maximum");
33 }
34 if (maxNodes > 0) {
35 if( !checkMaxNodes(apiData.getPrimitivesToAdd(), maxNodes))
36 return false;
37 if( !checkMaxNodes(apiData.getPrimitivesToUpdate(), maxNodes))
38 return false;
39 if( !checkMaxNodes(apiData.getPrimitivesToDelete(), maxNodes))
40 return false;
41 }
42 } catch(OsmTransferCanceledException e){
43 return false;
44 } catch (OsmApiInitializationException e) {
45 ExceptionDialogUtil.explainOsmTransferException(e);
46 return false;
47 }
48 return true;
49 }
50
51 private boolean checkMaxNodes(Collection<OsmPrimitive> primitives, long maxNodes) {
52 for (OsmPrimitive osmPrimitive : primitives) {
53 for (String key: osmPrimitive.keySet()) {
54 String value = osmPrimitive.get(key);
55 if(key.length() > 255) {
56 if (osmPrimitive.isDeleted()) {
57 // if OsmPrimitive is going to be deleted we automatically shorten the
58 // value
59 System.out.println(
60 tr("Warning: automatically truncating value of tag ''{0}'' on deleted object {1}",
61 key,
62 Long.toString(osmPrimitive.getId())
63 )
64 );
65 osmPrimitive.put(key, value.substring(0, 255));
66 continue;
67 }
68 JOptionPane.showMessageDialog(Main.parent,
69 tr("Length of value for tag ''{0}'' on object {1} exceeds the max. allowed length {2}. Values length is {3}.",
70 key, Long.toString(osmPrimitive.getId()), 255, value.length()
71 ),
72 tr("Precondition Violation"),
73 JOptionPane.ERROR_MESSAGE
74 );
75 Main.main.getCurrentDataSet().setSelected(Collections.singleton(osmPrimitive));
76 return false;
77 }
78 }
79
80 if (osmPrimitive instanceof Way &&
81 ((Way)osmPrimitive).getNodesCount() > maxNodes) {
82 JOptionPane.showMessageDialog(
83 Main.parent,
84 tr("{0} nodes in way {1} exceed the max. allowed number of nodes {2}",
85 ((Way)osmPrimitive).getNodesCount(),
86 Long.toString(osmPrimitive.getId()),
87 maxNodes
88 ),
89 tr("API Capabilities Violation"),
90 JOptionPane.ERROR_MESSAGE
91 );
92 Main.main.getCurrentDataSet().setSelected(Collections.singleton(osmPrimitive));
93 return false;
94 }
95 }
96 return true;
97 }
98}
Note: See TracBrowser for help on using the repository browser.