From 015c8c74643249a2fc39c838e8e99a87ca9dd974 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Mon, 24 Feb 2020 17:14:11 -0700 Subject: [PATCH] Improve handling of third party sources Signed-off-by: Taylor Smock --- .../mapwithai/MapWithAIPreferences.java | 11 +++++--- .../mapwithai/backend/DataAvailability.java | 25 ++++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferences.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferences.java index f84465c..b26786f 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferences.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferences.java @@ -78,10 +78,13 @@ public class MapWithAIPreferences implements SubPreferenceSetting { } private static void fillMapWithAIURLTableDisplayData(List list) { - MapWithAIPreferenceHelper.getMapWithAIURLs() - .forEach(entry -> list.add(new DataUrl(entry.get(SOURCE), entry.get("url"), - Boolean.valueOf(entry.getOrDefault("enabled", "false")), - entry.getOrDefault("parameters", "[]")))); + List> entries = MapWithAIPreferenceHelper.getMapWithAIURLs(); + if (list.isEmpty()) { + DataAvailability.populatePreferences(); + entries = MapWithAIPreferenceHelper.getMapWithAIURLs(); + } + entries.forEach(entry -> list.add(new DataUrl(entry.get(SOURCE), entry.get("url"), + Boolean.valueOf(entry.getOrDefault("enabled", "false")), entry.getOrDefault("parameters", "[]")))); } @Override diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/DataAvailability.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/DataAvailability.java index 50f1254..6e09df4 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/DataAvailability.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/DataAvailability.java @@ -65,6 +65,27 @@ public class DataAvailability { } } + /** + * Populate preferences with the default values + */ + public static void populatePreferences() { + try (CachedFile jsonFile = new CachedFile(DEFAULT_SERVER_URL); + JsonParser jsonParser = Json.createParser(jsonFile.getContentReader());) { + jsonFile.setMaxAge(604_800); + jsonParser.next(); + JsonObject jsonObject = jsonParser.getObject(); + for (Entry entry : jsonObject.entrySet()) { + JsonValue parameters = entry.getValue().asJsonObject().getJsonArray("parameters"); + DataUrl url = new DataUrl(entry.getKey(), entry.getValue().asJsonObject().getString("url", ""), false, + parameters == null ? "[]" : parameters.toString()); + boolean enabled = entry.getValue().asJsonObject().getBoolean("default", false); + MapWithAIPreferenceHelper.setMapWithAIUrl(url, enabled, true); + } + } catch (JsonException | IOException e) { + Logging.debug(e); + } + } + /** * Initialize the class */ @@ -77,9 +98,7 @@ public class DataAvailability { boolean initializePreferences = MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty(); for (Entry entry : jsonObject.entrySet()) { if (initializePreferences) { - DataUrl url = new DataUrl(entry.getKey(), entry.getValue().asJsonObject().getString("url", ""), - false, entry.getValue().asJsonObject().getJsonArray("parameters").toString()); - MapWithAIPreferenceHelper.setMapWithAIUrl(url, false, true); + populatePreferences(); } Logging.debug("{0}: {1}", entry.getKey(), entry.getValue()); if (JsonValue.ValueType.OBJECT.equals(entry.getValue().getValueType())