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