1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map.Entry;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.actions.UploadAction.UploadHook;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
18 | import org.openstreetmap.josm.io.OsmApi;
|
---|
19 | import org.openstreetmap.josm.io.OsmApiInitializationException;
|
---|
20 |
|
---|
21 | public class ApiPreconditionChecker implements UploadHook {
|
---|
22 |
|
---|
23 | public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update,
|
---|
24 | Collection<OsmPrimitive> delete) {
|
---|
25 | OsmApi api = OsmApi.getOsmApi();
|
---|
26 | try {
|
---|
27 | api.initialize();
|
---|
28 | long maxNodes = 0;
|
---|
29 | if (api.getCapabilities().isDefined("waynodes", "maximum")) {
|
---|
30 | maxNodes = api.getCapabilities().getLong("waynodes","maximum");
|
---|
31 | }
|
---|
32 | long maxElements = 0;
|
---|
33 | if (api.getCapabilities().isDefined("changesets", "maximum_elements")) {
|
---|
34 | maxElements = api.getCapabilities().getLong("changesets", "maximum_elements");
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (maxNodes > 0) {
|
---|
38 | if( !checkMaxNodes(add, maxNodes))
|
---|
39 | return false;
|
---|
40 | if( !checkMaxNodes(update, maxNodes))
|
---|
41 | return false;
|
---|
42 | if( !checkMaxNodes(delete, maxNodes))
|
---|
43 | return false;
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (maxElements > 0) {
|
---|
47 | int total = 0;
|
---|
48 | total = add.size() + update.size() + delete.size();
|
---|
49 | if(total > maxElements) {
|
---|
50 | OptionPaneUtil.showMessageDialog(
|
---|
51 | Main.parent,
|
---|
52 | tr("Current number of changes exceeds the max. number of changes, current is {0}, max is {1}",
|
---|
53 | total,
|
---|
54 | maxElements
|
---|
55 | ),
|
---|
56 | tr("API Capabilities Violation"),
|
---|
57 | JOptionPane.ERROR_MESSAGE
|
---|
58 | );
|
---|
59 | return false;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | } catch (OsmApiInitializationException e) {
|
---|
63 | e.printStackTrace();
|
---|
64 | OptionPaneUtil.showMessageDialog(
|
---|
65 | Main.parent,
|
---|
66 | tr("Failed to initialize API. Please try again later."),
|
---|
67 | tr("API initialization failed"),
|
---|
68 | JOptionPane.ERROR_MESSAGE
|
---|
69 | );
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | return true;
|
---|
73 | }
|
---|
74 |
|
---|
75 | private boolean checkMaxNodes(Collection<OsmPrimitive> add, long maxNodes) {
|
---|
76 | for (OsmPrimitive osmPrimitive : add) {
|
---|
77 | for (Entry<String,String> e : osmPrimitive.entrySet()) {
|
---|
78 | if(e.getValue().length() > 255) {
|
---|
79 | if (osmPrimitive.deleted) {
|
---|
80 | // if OsmPrimitive is going to be deleted we automatically shorten the
|
---|
81 | // value
|
---|
82 | System.out.println(
|
---|
83 | tr("Warning: automatically truncating value of tag ''{0}'' on deleted primitive {1}",
|
---|
84 | e.getKey(),
|
---|
85 | Long.toString(osmPrimitive.id)
|
---|
86 | )
|
---|
87 | );
|
---|
88 | osmPrimitive.put(e.getKey(), e.getValue().substring(0, 255));
|
---|
89 | continue;
|
---|
90 | }
|
---|
91 | OptionPaneUtil.showMessageDialog(Main.parent,
|
---|
92 | tr("Length of value for tag ''{0}'' on primitive {1} exceeds the max. allowed length {2}. Values length is {3}.",
|
---|
93 | e.getKey(), Long.toString(osmPrimitive.id), 255, e.getValue().length()
|
---|
94 | ),
|
---|
95 | tr("Precondition Violation"),
|
---|
96 | JOptionPane.ERROR_MESSAGE
|
---|
97 | );
|
---|
98 | List<OsmPrimitive> newNodes = new LinkedList<OsmPrimitive>();
|
---|
99 | newNodes.add(osmPrimitive);
|
---|
100 | Main.main.getCurrentDataSet().setSelected(newNodes);
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (osmPrimitive instanceof Way &&
|
---|
106 | ((Way)osmPrimitive).getNodesCount() > maxNodes) {
|
---|
107 | OptionPaneUtil.showMessageDialog(
|
---|
108 | Main.parent,
|
---|
109 | tr("{0} nodes in way {1} exceed the max. allowed number of nodes {2}",
|
---|
110 | ((Way)osmPrimitive).getNodesCount(),
|
---|
111 | Long.toString(osmPrimitive.id),
|
---|
112 | maxNodes
|
---|
113 | ),
|
---|
114 | tr("API Capabilities Violation"),
|
---|
115 | JOptionPane.ERROR_MESSAGE
|
---|
116 | );
|
---|
117 | List<OsmPrimitive> newNodes = new LinkedList<OsmPrimitive>();
|
---|
118 | newNodes.add(osmPrimitive);
|
---|
119 |
|
---|
120 | Main.main.getCurrentDataSet().setSelected(newNodes);
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 | }
|
---|