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

Last change on this file since 2598 was 2598, checked in by Gubaer, 14 years ago

comment to follow in a later commit
Have to break up a commit because of SSL problem in SVN (filed a ticket - #4093)

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