Example usage for io.vertx.core.json JsonArray JsonArray

List of usage examples for io.vertx.core.json JsonArray JsonArray

Introduction

In this page you can find the example usage for io.vertx.core.json JsonArray JsonArray.

Prototype

public JsonArray() 

Source Link

Document

Create an empty instance

Usage

From source file:com.nasa.ElasticSearchAdapter.java

public JsonObject getSymptoms(String word) throws IOException {
    JsonObject result = new JsonObject();
    JsonArray matchesArr = new JsonArray();
    String data = "{\n" + "    \"size\": 0,\n" + "    \"aggs\" : {\n" + "        \"langs\" : {\n"
            + "            \"terms\" : { \"field\" : \"symptoms.name\" }\n" + "        }\n" + "    }\n" + "}";
    JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data);

    JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets");

    for (Object item : items) {
        JsonObject obj = (JsonObject) item;
        String key = obj.getString("key");
        if (key != null && obj.getString("key").startsWith(word)) {
            matchesArr.add(key);//w  w w  . j av a 2  s  . com
        }
    }

    result.put("symptoms", matchesArr);

    return result;
}

From source file:com.nasa.ElasticSearchAdapter.java

public JsonObject getConditions(String word) throws IOException {
    JsonObject result = new JsonObject();
    JsonArray matchesArr = new JsonArray();
    String data = "{\n" + "    \"size\": 0,\n" + "    \"aggs\" : {\n" + "        \"langs\" : {\n"
            + "            \"terms\" : { \"field\" : \"condition\" }\n" + "        }\n" + "    }\n" + "}";
    JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data);

    JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets");

    for (Object item : items) {
        JsonObject obj = (JsonObject) item;
        String key = obj.getString("key");
        if (key != null && obj.getString("key").startsWith(word)) {
            matchesArr.add(key);//from   w w  w  . jav a  2 s.  c  o m
        }
    }

    result.put("conditions", matchesArr);

    return result;
}

From source file:com.nasa.ElasticSearchAdapter.java

public JsonArray getCheck(Float lat, Float lon) throws IOException {
    JsonArray matchesArr = new JsonArray();
    String data = "{\n" + "    \"size\": 0,\n" + "   \"query\": {\n" + "      \"filtered\": {\n"
            + "         \"query\": {\n" + "            \"match_all\": {}\n" + "         },\n"
            + "         \"filter\": {\n" + "            \"geo_distance\": {\n"
            + "               \"distance\": \"100km\",\n" + "               \"location\": {\n"
            + "                  \"lat\": " + lat + ",\n" + "                  \"lon\": " + lon + "\n"
            + "               }\n" + "            }\n" + "         }\n" + "      }\n" + "   },\n"
            + "   \"aggs\": {\n" + "      \"langs\": {\n" + "         \"terms\": {\n"
            + "            \"field\": \"condition\"\n" + "         }\n" + "      }\n" + "   }\n" + "}";
    JsonObject response = doPost(ES_URL + "/healthy-travel/feedback/_search", data);

    JsonArray items = response.getJsonObject("aggregations").getJsonObject("langs").getJsonArray("buckets");

    for (Object item : items) {
        JsonObject obj = (JsonObject) item;
        String key = obj.getString("key");
        int value = obj.getInteger("doc_count");

        if (key != null && value > 0) {
            JsonObject cond = new JsonObject();
            cond.put("name", key);
            cond.put("reported_cases", value);
            matchesArr.add(cond);/*from  ww  w. j a  v  a2s  .c  om*/
        }
    }

    return matchesArr;
}

From source file:com.nasa.ESWorkerVerticle.java

@Override
public void start() throws Exception {

    EventBus eb = vertx.eventBus();//from w w w .  j a  v a 2 s.c  om

    eb.consumer("bus.symptoms").handler(message -> {

        String text = ((JsonObject) message.body()).getString("text");

        JsonObject result = null;

        try {
            if (text != null) {
                result = es.getSymptoms(text);
            }
        } catch (Exception ex) {
            result = new JsonObject();
            result.put("symptoms", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.conditions").handler(message -> {

        String text = ((JsonObject) message.body()).getString("text");

        JsonObject result = null;

        try {
            if (text != null) {
                result = es.getConditions(text);
            }
        } catch (Exception ex) {
            result = new JsonObject();
            result.put("conditions", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.map.weather").handler(message -> {

        String lat = ((JsonObject) message.body()).getString("lat");
        String lon = ((JsonObject) message.body()).getString("lon");
        String zoom = ((JsonObject) message.body()).getString("zoom");
        String count = ((JsonObject) message.body()).getString("count");

        JsonObject result = null;

        try {
            if (lat != null && lon != null && zoom != null) {
                result = Transformer.weatherToMap(es.getMap(lat, lon, zoom, count));
            } else {
                throw new IllegalArgumentException("incorrect weather params: " + message.body().toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result = new JsonObject();
            result.put("type", "FeatureCollection");
            result.put("features", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.map.pollution").handler(message -> {

        String lat = ((JsonObject) message.body()).getString("lat");
        String lon = ((JsonObject) message.body()).getString("lon");
        String zoom = ((JsonObject) message.body()).getString("zoom");
        String count = ((JsonObject) message.body()).getString("count");

        System.out.println("Worker: " + message.body());

        JsonObject result = null;

        try {
            if (lat != null && lon != null && zoom != null) {
                result = Transformer.pollutionToMap(es.getMap(lat, lon, zoom, count));
            } else {
                throw new IllegalArgumentException("incorrect pollution params: " + message.body().toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result = new JsonObject();
            result.put("type", "FeatureCollection");
            result.put("features", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.map.condition").handler(message -> {

        String lat = ((JsonObject) message.body()).getString("lat");
        String lon = ((JsonObject) message.body()).getString("lon");
        String zoom = ((JsonObject) message.body()).getString("zoom");
        String count = ((JsonObject) message.body()).getString("count");

        JsonObject result = null;

        try {
            if (lat != null && lon != null && zoom != null) {
                result = Transformer.conditionToMap(es.getMap(lat, lon, zoom, count));
            } else {
                throw new IllegalArgumentException("incorrect condition params: " + message.body().toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result = new JsonObject();
            result.put("type", "FeatureCollection");
            result.put("features", new JsonArray());
        }

        message.reply(result);
    });

    eb.consumer("bus.check").handler(message -> {

        Float lat = Float.parseFloat(((JsonObject) message.body()).getString("lat"));
        Float lon = Float.parseFloat(((JsonObject) message.body()).getString("lon"));

        JsonObject result = new JsonObject();

        Gson gson = new Gson();

        try {
            JsonArray matchesArr = new JsonArray();

            if (lat != null && lon != null) {
                matchesArr = es.getCheck(lat, lon);
                result.put("conditions", matchesArr);
            }

            OtherOmwClient owm = new OtherOmwClient();
            BreezoMeterClient bm = new BreezoMeterClient();

            JsonObject weatherJ = owm.currentWeather(lat, lon);
            JsonObject pollutionJ = bm.currentAirQualityAtPoint(lat, lon);

            Weather weather = owm.toWeather(weatherJ);
            if (weather != null) {
                result.put("weather", new JsonObject(gson.toJson(weather)));
            }

            Pollution pollution = bm.toPollution(pollutionJ);
            if (pollution != null) {
                result.put("pollution", new JsonObject(gson.toJson(pollution)));
            }

        } catch (Exception ex) {
            result = new JsonObject();
            result.put("conditions", new JsonArray());
            result.put("weather", new JsonObject());
            result.put("pollution", new JsonObject());
        }

        message.reply(result);
    });

    eb.consumer("bus.feedback").handler(message -> {

        JsonObject userFeedback = (JsonObject) message.body();

        JsonObject result = new JsonObject();

        try {
            Gson gson = new Gson();
            Feedback feedback = gson.fromJson(userFeedback.toString(), Feedback.class);

            float lat = feedback.getLocation().getLat();
            float lon = feedback.getLocation().getLon();

            OtherOmwClient owm = new OtherOmwClient();
            JsonObject owmData = owm.currentWeather(lat, lon);
            feedback.setWeather(owm.toWeather(owmData));

            BreezoMeterClient bm = new BreezoMeterClient();
            JsonObject bmData = bm.currentAirQualityAtPoint(lat, lon);
            feedback.setPollution(bm.toPollution(bmData));

            result = es.postFeedback(new JsonObject(gson.toJson(feedback)));
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("success", false);
        }

        message.reply(result);
    });
}

From source file:com.nasa.Transformer.java

public static JsonObject weatherToMap(JsonObject jsonObject) {
    JsonObject result = new JsonObject();
    JsonArray featuresArr = new JsonArray();

    JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits");

    int idx = 0;//from   ww  w  .  j a v a  2s  .com
    for (Object hit : items) {
        JsonObject source = ((JsonObject) hit).getJsonObject("_source");

        JsonObject feature = new JsonObject();
        feature.put("type", "Feature");
        feature.put("id", idx);

        JsonObject properties = new JsonObject();
        float mag = source.getJsonObject("weather").getFloat("temp");
        properties.put("mag", mag);

        feature.put("properties", properties);

        JsonObject geometry = new JsonObject();
        geometry.put("type", "Point");

        JsonArray coordinates = new JsonArray();
        Float lon = source.getJsonObject("location").getFloat("lon");
        Float lat = source.getJsonObject("location").getFloat("lat");
        coordinates.add(lon);
        coordinates.add(lat);

        geometry.put("coordinates", coordinates);
        feature.put("geometry", geometry);

        featuresArr.add(feature);

        idx++;
    }

    result.put("type", "FeatureCollection");
    result.put("features", featuresArr);

    return result;
}

From source file:com.nasa.Transformer.java

public static JsonObject pollutionToMap(JsonObject jsonObject) {
    JsonObject result = new JsonObject();
    JsonArray featuresArr = new JsonArray();

    JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits");

    int idx = 0;/*  ww w. j  a  va2  s. co m*/
    for (Object hit : items) {
        JsonObject source = ((JsonObject) hit).getJsonObject("_source");

        JsonObject feature = new JsonObject();
        feature.put("type", "Feature");
        feature.put("id", idx);

        JsonObject properties = new JsonObject();
        float mag = source.getJsonObject("pollution").getFloat("aqi");
        properties.put("mag", mag);

        feature.put("properties", properties);

        JsonObject geometry = new JsonObject();
        geometry.put("type", "Point");

        JsonArray coordinates = new JsonArray();
        Float lon = source.getJsonObject("location").getFloat("lon");
        Float lat = source.getJsonObject("location").getFloat("lat");
        coordinates.add(lon);
        coordinates.add(lat);

        geometry.put("coordinates", coordinates);
        feature.put("geometry", geometry);

        featuresArr.add(feature);

        idx++;
    }

    result.put("type", "FeatureCollection");
    result.put("features", featuresArr);

    return result;
}

From source file:com.nasa.Transformer.java

public static JsonObject conditionToMap(JsonObject jsonObject) {

    //        private String conditionsMap = "{\n"
    //            + "  \"Diseases\": [\n"
    //            + "    {\n"
    //            + "      \"lat\": 46.77029284,\n"
    //            + "      \"lng\": 23.57641889,\n"
    //            + "      \"type\": \"something good\",\n"
    //            + "      \"description\": \"good\",\n"
    //            + "      \"rating\": 1.23\n"
    //            + "    },\n"
    //            + "    {\n"
    //            + "      \"lat\": 46.78185201,\n"
    //            + "      \"lng\": 23.68522613,\n"
    //            + "      \"type\": \"something not bad\",\n"
    //            + "      \"description\": \"not bad\",\n"
    //            + "      \"rating\": 3.99\n"
    //            + "    },\n"
    //            + "    {\n"
    //            + "      \"lat\": 46.7558097,\n"
    //            + "      \"lng\": 23.5940353,\n"
    //            + "      \"type\": \"something bad\",\n"
    //            + "      \"description\": \"bad\",\n"
    //            + "      \"rating\": 4.75\n"
    //            + "    }\n"
    //            + "  ]\n"
    //            + "}";

    JsonObject result = new JsonObject();
    JsonArray featuresArr = new JsonArray();

    JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits");

    for (Object hit : items) {
        JsonObject source = ((JsonObject) hit).getJsonObject("_source");

        JsonObject feature = new JsonObject();

        Float lon = source.getJsonObject("location").getFloat("lon");
        Float lat = source.getJsonObject("location").getFloat("lat");
        feature.put("lat", lat);
        feature.put("lng", lon);

        feature.put("type", source.getString("condition"));

        String symptoms = "";
        int ratingSum = 0;
        int ratingCount = 0;

        JsonArray symptomsJ = source.getJsonArray("symptoms");

        for (Object sym : symptomsJ) {
            JsonObject symJ = (JsonObject) sym;
            symptoms += symJ.getString("name") + ", ";
            ratingSum += symJ.getInteger("rating");
            ratingCount++;/* w w w .j av a  2s  . c o m*/
        }

        feature.put("description", symptoms);

        if (ratingCount > 0) {
            feature.put("rating", ratingSum / ratingCount);
        } else {
            feature.put("rating", 0);
        }

        featuresArr.add(feature);
    }

    result.put("Diseases", featuresArr);

    return result;
}

From source file:de.bischinger.anotherblog.RestVerticle.java

License:Open Source License

private void handleListBlogs(RoutingContext routingContext) {
    JsonArray arr = new JsonArray();

    vertx.executeBlocking(future -> {
        SearchResponse searchResponse = client.prepareSearch(indexName).execute().actionGet();
        future.complete(searchResponse);
    }, result -> {//  w  w  w.  j av  a  2 s  . c o m
        SearchResponse searchResponse = (SearchResponse) result.result();
        searchResponse.getHits().forEach(hit -> arr.add(hit.getSourceAsString()));
        routingContext.response().putHeader("content-type", "application/json").end(arr.encodePrettily());
    });
}

From source file:de.braintags.netrelay.controller.api.DataTablesController.java

License:Open Source License

private JsonObject createJsonObject(IMapper mapper, List<IStoreObject<?, ?>> selection,
        DataTableLinkDescriptor descr, long completeCount, long tableCount) {
    LOGGER.info("tableCount: " + tableCount + ", completeCount: " + completeCount);
    JsonObject json = new JsonObject();
    json.put("recordsTotal", tableCount);
    json.put("recordsFiltered", completeCount);
    JsonArray resArray = new JsonArray();
    json.put("data", resArray);
    for (IStoreObject<?, ?> ob : selection) {
        resArray.add(handleObject(mapper, ob, descr));
    }/*from   w  ww. ja  va2 s .c o  m*/
    return json;
}

From source file:de.braintags.netrelay.controller.api.DataTablesController.java

License:Open Source License

private JsonArray handleObject(IMapper mapper, IStoreObject<?, ?> sto, DataTableLinkDescriptor descr) {
    JsonArray json = new JsonArray();
    for (ColDef colDef : descr.getColumns()) {
        if (colDef != null && colDef.name != null && colDef.name.hashCode() != 0) {
            IField field = mapper.getField(colDef.name);
            Objects.requireNonNull(field, "Could not find defined field for '" + colDef.name + "'");
            Object value = sto.get(field);
            json.add(value == null ? "" : value);
        } else {//from   ww  w  .j a v a  2  s. com
            json.add("");
        }
    }
    return json;
}