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

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

fixed #3684: Add "chunked" upload mode
Removed support for API "0.5" when uploading (there are still 0.5-files around, but I'm not aware of any 0.5-servers)

File size: 4.9 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 api.initialize(NullProgressMonitor.INSTANCE);
26 long maxNodes = 0;
27 if (api.getCapabilities().isDefined("waynodes", "maximum")) {
28 maxNodes = api.getCapabilities().getLong("waynodes","maximum");
29 }
30 long maxElements = 0;
31 if (api.getCapabilities().isDefined("changesets", "maximum_elements")) {
32 maxElements = api.getCapabilities().getLong("changesets", "maximum_elements");
33 }
34
35 if (maxNodes > 0) {
36 if( !checkMaxNodes(apiData.getPrimitivesToAdd(), maxNodes))
37 return false;
38 if( !checkMaxNodes(apiData.getPrimitivesToUpdate(), maxNodes))
39 return false;
40 if( !checkMaxNodes(apiData.getPrimitivesToDelete(), maxNodes))
41 return false;
42 }
43
44 if (maxElements > 0) {
45 int total = 0;
46 total = apiData.getPrimitivesToAdd().size() + apiData.getPrimitivesToUpdate().size() + apiData.getPrimitivesToDelete().size();
47 if(total > maxElements) {
48 JOptionPane.showMessageDialog(
49 Main.parent,
50 tr("Current number of changes exceeds the max. number of changes, current is {0}, max is {1}",
51 total,
52 maxElements
53 ),
54 tr("API Capabilities Violation"),
55 JOptionPane.ERROR_MESSAGE
56 );
57 return false;
58 }
59 }
60 } catch (OsmApiInitializationException e) {
61 ExceptionDialogUtil.explainOsmTransferException(e);
62 return false;
63 }
64 return true;
65 }
66
67 private boolean checkMaxNodes(Collection<OsmPrimitive> primitives, long maxNodes) {
68 for (OsmPrimitive osmPrimitive : primitives) {
69 for (Entry<String,String> e : osmPrimitive.entrySet()) {
70 if(e.getValue().length() > 255) {
71 if (osmPrimitive.isDeleted()) {
72 // if OsmPrimitive is going to be deleted we automatically shorten the
73 // value
74 System.out.println(
75 tr("Warning: automatically truncating value of tag ''{0}'' on deleted object {1}",
76 e.getKey(),
77 Long.toString(osmPrimitive.getId())
78 )
79 );
80 osmPrimitive.put(e.getKey(), e.getValue().substring(0, 255));
81 continue;
82 }
83 JOptionPane.showMessageDialog(Main.parent,
84 tr("Length of value for tag ''{0}'' on object {1} exceeds the max. allowed length {2}. Values length is {3}.",
85 e.getKey(), Long.toString(osmPrimitive.getId()), 255, e.getValue().length()
86 ),
87 tr("Precondition Violation"),
88 JOptionPane.ERROR_MESSAGE
89 );
90 Main.main.getCurrentDataSet().setSelected(Collections.singleton(osmPrimitive));
91 return false;
92 }
93 }
94
95 if (osmPrimitive instanceof Way &&
96 ((Way)osmPrimitive).getNodesCount() > maxNodes) {
97 JOptionPane.showMessageDialog(
98 Main.parent,
99 tr("{0} nodes in way {1} exceed the max. allowed number of nodes {2}",
100 ((Way)osmPrimitive).getNodesCount(),
101 Long.toString(osmPrimitive.getId()),
102 maxNodes
103 ),
104 tr("API Capabilities Violation"),
105 JOptionPane.ERROR_MESSAGE
106 );
107 Main.main.getCurrentDataSet().setSelected(Collections.singleton(osmPrimitive));
108 return false;
109 }
110 }
111 return true;
112 }
113}
Note: See TracBrowser for help on using the repository browser.