Improve handling of third party sources

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-02-24 17:14:11 -07:00
rodzic 9bbf637d31
commit 015c8c7464
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
2 zmienionych plików z 29 dodań i 7 usunięć

Wyświetl plik

@ -78,10 +78,13 @@ public class MapWithAIPreferences implements SubPreferenceSetting {
}
private static void fillMapWithAIURLTableDisplayData(List<DataUrl> 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<Map<String, String>> 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

Wyświetl plik

@ -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<String, JsonValue> 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<String, JsonValue> 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())