1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.HashSet;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
14 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
15 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
16 | import org.openstreetmap.josm.gui.io.DownloadPrimitivesWithReferrersTask;
|
---|
17 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
18 | import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
|
---|
19 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
20 | import org.openstreetmap.josm.tools.Logging;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Loads OSM primitives using their ID
|
---|
24 | * similar to the "Download object" dialog (@see DownloadPrimitiveAction}.
|
---|
25 | * For instance, {@code /load_object?objects=n1,w2,r3[&new_layer=false&relation_members=true]}.
|
---|
26 | */
|
---|
27 | public class LoadObjectHandler extends RequestHandler {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The remote control command name used to load objects using their ID.
|
---|
31 | */
|
---|
32 | public static final String command = "load_object";
|
---|
33 |
|
---|
34 | private final List<PrimitiveId> ps = new LinkedList<>();
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public String[] getMandatoryParams() {
|
---|
38 | return new String[]{"objects"};
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public String[] getOptionalParams() {
|
---|
43 | return new String[] {"new_layer", "layer_name", "layer_locked", "download_policy", "upload_policy",
|
---|
44 | "addtags", "relation_members", "referrers"};
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public String getUsage() {
|
---|
49 | return "downloads the specified objects from the server";
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public String[] getUsageExamples() {
|
---|
54 | return new String[] {"/load_object?new_layer=true&objects=w106159509",
|
---|
55 | "/load_object?new_layer=true&objects=r2263653&relation_members=true",
|
---|
56 | "/load_object?objects=n100000&referrers=false"
|
---|
57 | };
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException {
|
---|
62 | if (!PermissionPrefWithDefault.LOAD_DATA.isAllowed()) {
|
---|
63 | Logging.info("RemoteControl: download forbidden by preferences");
|
---|
64 | }
|
---|
65 | if (!ps.isEmpty()) {
|
---|
66 | final boolean newLayer = getDownloadParams().isNewLayer();
|
---|
67 | final boolean relationMembers = Boolean.parseBoolean(args.get("relation_members"));
|
---|
68 | final boolean referrers = Boolean.parseBoolean(args.get("referrers"));
|
---|
69 | final DownloadPrimitivesWithReferrersTask task = new DownloadPrimitivesWithReferrersTask(
|
---|
70 | newLayer, ps, referrers, relationMembers, args.get("layer_name"), null);
|
---|
71 | MainApplication.worker.submit(task);
|
---|
72 | MainApplication.worker.submit(() -> {
|
---|
73 | final List<PrimitiveId> downloaded = task.getDownloadedId();
|
---|
74 | final DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
---|
75 | if (downloaded != null) {
|
---|
76 | GuiHelper.runInEDT(() -> ds.setSelected(downloaded));
|
---|
77 | Collection<OsmPrimitive> downlPrim = new HashSet<>();
|
---|
78 | for (PrimitiveId id : downloaded) {
|
---|
79 | downlPrim.add(ds.getPrimitiveById(id));
|
---|
80 | }
|
---|
81 | AddTagsDialog.addTags(args, sender, downlPrim);
|
---|
82 | }
|
---|
83 | ps.clear();
|
---|
84 | });
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public String getPermissionMessage() {
|
---|
90 | return tr("Remote Control has been asked to load objects (specified by their id) from the API.");
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public PermissionPrefWithDefault getPermissionPref() {
|
---|
95 | return PermissionPrefWithDefault.LOAD_DATA;
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
---|
100 | validateDownloadParams();
|
---|
101 | ps.clear();
|
---|
102 | for (String i : splitArg("objects", SPLITTER_COMMA)) {
|
---|
103 | if (!i.isEmpty()) {
|
---|
104 | try {
|
---|
105 | ps.add(SimplePrimitiveId.fromString(i));
|
---|
106 | } catch (IllegalArgumentException e) {
|
---|
107 | Logging.log(Logging.LEVEL_WARN, "RemoteControl: invalid selection '"+i+"' ignored.", e);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|