Example usage for io.vertx.core.json JsonObject getString

List of usage examples for io.vertx.core.json JsonObject getString

Introduction

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

Prototype

public String getString(String key) 

Source Link

Document

Get the string value with the specified key, special cases are addressed for extended JSON types Instant , byte[] and Enum which can be converted to String.

Usage

From source file:com.funmix.service.LineServiceImpl.java

@Override
public Future<JsonObject> getAll(JsonObject params) {
    Future<JsonObject> result = Future.future();
    StringBuffer sqlCount = new StringBuffer(SQL_QUERY_COUNT);
    StringBuffer sqlQuery = new StringBuffer(SQL_QUERY_ALL);
    StringBuffer order = new StringBuffer();
    StringBuffer where = new StringBuffer();
    JsonArray queryparams = new JsonArray();
    JsonArray countparams = new JsonArray();
    params.forEach(r -> {//from   w  w  w . j a  v  a  2s . com
        String key = r.getKey();
        if (key.equals("orderby")) {
            order.append(" order by ").append(r.getValue());
        } else if (key.equals("page")) {
        } else if (key.equals("page_size")) {
        } else {
            if (where.length() == 0)
                where.append(" where ");
            else
                where.append(" and ");
            where.append(key).append(" like ? ");
            queryparams.add("%" + r.getValue() + "%");
            countparams.add("%" + r.getValue() + "%");
        }
    });

    if (where.length() > 0) {
        sqlCount.append(where);
        sqlQuery.append(where);
    }
    JsonObject jrs = new JsonObject();
    client.getConnection(connHandler(result, connection -> {
        log.info(sqlCount);
        if (countparams.size() == 0)
            connection.query(sqlCount.toString(), r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        else
            connection.queryWithParams(sqlCount.toString(), countparams, r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        if (order.length() > 0) {
            sqlQuery.append(order);
            queryparams.add(params.getString("orderby"));
        }
        if (params.getValue("page") == null) {
            params.put("page", "0");
            params.put("page_size", "" + Integer.MAX_VALUE);
        }
        sqlQuery.append(" limit ?,? ");
        log.info(sqlQuery);
        int page = Integer.parseInt(params.getString("page"));
        int limit = Integer.parseInt(params.getString("page_size"));
        queryparams.add(Utils.calcPage(page, limit)).add(limit);
        connection.queryWithParams(sqlQuery.toString(), queryparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                log.info(r.result().getRows().toString());
                jrs.put("data", r.result().getRows().stream().map(Line::new).collect(Collectors.toList()));
                jrs.put("per_page", limit);
                jrs.put("current_page", page);
            }
            result.complete(new JsonObject().put("status", 200).put("data", new JsonObject().put("list", jrs)));
        });
        connection.close();

    }));
    return result;
}

From source file:com.funmix.service.LineServiceImpl.java

@Override
public Future<Optional<Line>> insert(JsonObject json) {
    log.info(json.toString());/*from  www  .  j av  a 2s . c  om*/
    Future<Optional<Line>> result = Future.future();
    JsonArray params = new JsonArray();
    // licenseplate,drivername,startaddr,starttime,endtime,endaddr,linename,path
    params.add(json.getString("licenseplate"));
    params.add(json.getString("drivername"));
    params.add(json.getString("startaddr"));
    params.add(json.getString("starttime"));
    params.add(json.getString("endtime"));
    params.add(json.getString("endaddr"));
    params.add(json.getString("linename"));
    params.add(json.getJsonArray("path").toString());
    StringBuffer updateOrder = new StringBuffer("update torder set status=2 where orderno in (");
    JsonArray setparams = new JsonArray();
    if (json.getValue("orderlist") != null) {
        JsonArray ja = json.getJsonArray("orderlist");
        ja.forEach(r -> {
            updateOrder.append("?,");
            setparams.add(r);
        });
        updateOrder.deleteCharAt(updateOrder.length() - 1).append(")");
    }
    log.info(setparams.toString());
    log.info(params.toString());
    client.getConnection(connHandler(result, connection -> {
        connection.updateWithParams(updateOrder.toString(), setparams, ru -> {
            if (ru.failed()) {
                result.fail(ru.cause());
                log.info(ru.cause());
            } else {
                connection.updateWithParams(SQL_INSERT, params, r -> {
                    if (r.failed()) {
                        result.fail(r.cause());
                        log.info(r.cause());
                    } else {
                        UpdateResult urs = r.result();
                        params.clear();
                        params.add(urs.getKeys().getInteger(0));
                        log.info(urs.getKeys().getInteger(0));
                        connection.queryWithParams(SQL_QUERY, params, rs -> {
                            if (rs.failed()) {
                                result.fail(rs.cause());
                                log.info(rs.cause());
                            } else {
                                List<JsonObject> list = rs.result().getRows();
                                if (list == null || list.isEmpty()) {
                                    result.complete(Optional.empty());
                                } else {
                                    result.complete(Optional.of(new Line(list.get(0))));
                                }
                            }
                        });
                    }
                    connection.close();
                });
            }
        });

    }));
    return result;
}

From source file:com.funmix.service.TruckServiceImpl.java

@Override
public Future<JsonObject> getAll(JsonObject params) {
    Future<JsonObject> result = Future.future();
    StringBuffer sqlCount = new StringBuffer(SQL_QUERY_COUNT);
    StringBuffer sqlQuery = new StringBuffer(SQL_QUERY_ALL);
    StringBuffer order = new StringBuffer();
    StringBuffer where = new StringBuffer();
    JsonArray queryparams = new JsonArray();
    JsonArray countparams = new JsonArray();
    params.forEach(r -> {// w ww. j a  v  a 2s .  co m
        String key = r.getKey();
        if (key.equals("orderby")) {
            order.append(" order by ").append(r.getValue());
        } else if (key.equals("page")) {
        } else if (key.equals("page_size")) {
        } else {
            if (where.length() == 0)
                where.append(" where ");
            else
                where.append(" and ");
            where.append(key).append(" like ? ");
            queryparams.add("%" + r.getValue() + "%");
            countparams.add("%" + r.getValue() + "%");
        }
    });

    if (where.length() > 0) {
        sqlCount.append(where);
        sqlQuery.append(where);
    }
    JsonObject jrs = new JsonObject();
    client.getConnection(connHandler(result, connection -> {
        log.info(sqlCount);
        if (countparams.size() == 0)
            connection.query(sqlCount.toString(), r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        else
            connection.queryWithParams(sqlCount.toString(), countparams, r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        if (order.length() > 0) {
            sqlQuery.append(order);
            queryparams.add(params.getString("orderby"));
        }
        if (params.getValue("page") == null) {
            params.put("page", "0");
            params.put("page_size", "" + Integer.MAX_VALUE);
        }
        sqlQuery.append(" limit ?,? ");
        log.info(sqlQuery);
        int page = Integer.parseInt(params.getString("page"));
        int limit = Integer.parseInt(params.getString("page_size"));
        queryparams.add(Utils.calcPage(page, limit)).add(limit);
        connection.queryWithParams(sqlQuery.toString(), queryparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                jrs.put("data", r.result().getRows().stream().map(Truck::new).collect(Collectors.toList()));
                jrs.put("per_page", limit);
                jrs.put("current_page", page);
            }
            result.complete(new JsonObject().put("status", 200).put("data", new JsonObject().put("list", jrs)));
        });
        connection.close();

    }));
    return result;
}

From source file:com.funmix.service.TruckServiceImpl.java

@Override
public Future<Optional<Truck>> insert(JsonObject json) {
    log.info(json.toString());/*from w ww  .jav a  2s.  c om*/
    Future<Optional<Truck>> result = Future.future();
    JsonArray params = new JsonArray();
    //licenseplate,truck_type,tonnage,volume_length,,volume_width,volume_height,status
    params.add(json.getString("licenseplate"));
    params.add(json.getString("truck_type"));
    params.add(json.getDouble("tonnage"));
    params.add(json.getDouble("volume_length"));
    params.add(json.getDouble("volume_width"));
    params.add(json.getDouble("volume_height"));
    log.info(params.toString());
    client.getConnection(connHandler(result, connection -> {
        connection.updateWithParams(SQL_INSERT, params, r -> {
            if (r.failed()) {
                result.fail(r.cause());
                log.info(r.cause());
            } else {
                UpdateResult urs = r.result();
                params.clear();
                params.add(urs.getKeys().getInteger(0));
                log.info(urs.getKeys().getInteger(0));
                connection.queryWithParams(SQL_QUERY, params, rs -> {
                    if (rs.failed()) {
                        result.fail(rs.cause());
                        log.info(rs.cause());
                    } else {
                        List<JsonObject> list = rs.result().getRows();
                        if (list == null || list.isEmpty()) {
                            result.complete(Optional.empty());
                        } else {
                            result.complete(Optional.of(new Truck(list.get(0))));
                        }
                    }
                });
            }
            connection.close();
        });
    }));
    return result;
}

From source file:com.github.ithildir.airbot.AirBotVerticle.java

License:Open Source License

private Future<HttpServer> _startHttpServer(JsonObject configJsonObject) {
    Future<HttpServer> future = Future.future();

    HttpServer httpServer = vertx.createHttpServer();

    Router router = Router.router(vertx);

    Route authHandlerRoute = router.route();

    String username = configJsonObject.getString(ConfigKeys.USERNAME);
    String password = configJsonObject.getString(ConfigKeys.PASSWORD);

    AuthProvider authProvider = new SingleUserAuthProvider(username, password);

    authHandlerRoute.handler(BasicAuthHandler.create(authProvider));

    Route bodyHandlerRoute = router.route();

    bodyHandlerRoute.handler(BodyHandler.create());

    _addHttpRouteApiAi(router);/*from  www  .  ja  v a  2s.  c o m*/

    httpServer.requestHandler(router::accept);

    int port = configJsonObject.getInteger(ConfigKeys.PORT, _DEFAULT_PORT);

    httpServer.listen(port, future);

    return future;
}

From source file:com.github.ithildir.airbot.GeoServiceVerticle.java

License:Open Source License

@Override
protected GeoService getServiceImpl(JsonObject configJsonObject) {
    String key = configJsonObject.getString(ConfigKeys.MAPQUEST_KEY);
    boolean open = configJsonObject.getBoolean(ConfigKeys.MAPQUEST_OPEN, _DEFAULT_MAPQUEST_OPEN);

    return new MapQuestGeoServiceImpl(vertx, key, open);
}

From source file:com.github.ithildir.airbot.server.SingleUserAuthProvider.java

License:Open Source License

@Override
public void authenticate(JsonObject authInfoJsonObject, Handler<AsyncResult<User>> handler) {

    String username = authInfoJsonObject.getString("username");

    if (username == null) {
        handler.handle(Future.failedFuture("Unable to get username in authentication info"));

        return;/*w  ww  . j a  va 2s . co m*/
    }

    String password = authInfoJsonObject.getString("password");

    if (password == null) {
        handler.handle(Future.failedFuture("Unable to get password in authentication info"));

        return;
    }

    if (!_username.equals(username) || !_password.equals(password)) {
        handler.handle(Future.failedFuture("Invalid username/password"));

        return;
    }

    handler.handle(Future.succeededFuture(_user));
}

From source file:com.github.ithildir.airbot.service.impl.MapQuestGeoServiceImpl.java

License:Open Source License

private Location _getLocation(JsonObject jsonObject) {
    JsonArray resultsJsonArray = jsonObject.getJsonArray("results");

    JsonObject resultJsonObject = resultsJsonArray.getJsonObject(0);

    JsonArray locationsJsonArray = resultJsonObject.getJsonArray("locations");

    JsonObject locationJsonObject = locationsJsonArray.getJsonObject(0);

    JsonObject latLngJsonObject = locationJsonObject.getJsonObject("latLng");

    double latitude = latLngJsonObject.getDouble("lat");
    double longitude = latLngJsonObject.getDouble("lng");

    String country = locationJsonObject.getString("adminArea1");

    return new Location(latitude, longitude, country);
}

From source file:com.github.ithildir.airbot.service.impl.WaqiMeasurementServiceImpl.java

License:Open Source License

private Measurement _getMeasurement(JsonObject jsonObject) {
    String status = jsonObject.getString("status");

    if (!"ok".equals(status)) {
        _logger.warn("Unable to use response {0}", jsonObject);

        return null;
    }/*from  ww  w  .jav a2 s . c o  m*/

    JsonObject dataJsonObject = jsonObject.getJsonObject("data");

    JsonObject cityJsonObject = dataJsonObject.getJsonObject("city");

    String city = cityJsonObject.getString("name");

    JsonObject timeJsonObject = dataJsonObject.getJsonObject("time");

    String dateTime = timeJsonObject.getString("s");
    String dateTimeOffset = timeJsonObject.getString("tz");

    String date = dateTime.substring(0, 10);
    String time = dateTime.substring(11);

    TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_OFFSET_DATE_TIME
            .parse(date + "T" + time + dateTimeOffset);

    Instant instant = Instant.from(temporalAccessor);

    int aqi = dataJsonObject.getInteger("aqi");
    String mainPollutant = dataJsonObject.getString("dominentpol");

    Map<String, Double> values = new HashMap<>();

    JsonObject valuesJsonObject = dataJsonObject.getJsonObject("iaqi");

    for (String pollutant : valuesJsonObject.fieldNames()) {
        JsonObject pollutantJsonObject = valuesJsonObject.getJsonObject(pollutant);

        double value = pollutantJsonObject.getDouble("v");

        values.put(pollutant, value);
    }

    return new Measurement(city, instant.toEpochMilli(), aqi, mainPollutant, values, null);
}

From source file:com.github.ithildir.airbot.service.impl.WaqiMeasurementServiceImpl.java

License:Open Source License

private <R, T> HttpResponse<T> _handleHttpResponse(AsyncResult<HttpResponse<T>> asyncResult,
        Handler<AsyncResult<R>> handler) {

    if (asyncResult.failed()) {
        handler.handle(Future.failedFuture(asyncResult.cause()));

        return null;
    }/*from  w w w  .  j  a va 2 s. c o m*/

    HttpResponse<T> httpResponse = asyncResult.result();

    int statusCode = httpResponse.statusCode();

    if (statusCode != HttpResponseStatus.OK.code()) {
        JsonObject jsonObject = httpResponse.bodyAsJsonObject();

        handler.handle(ServiceException.fail(statusCode, jsonObject.getString("message"), jsonObject));

        return null;
    }

    return httpResponse;
}