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

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

Introduction

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

Prototype

public JsonObject put(String key, Object value) 

Source Link

Document

Put an Object into the JSON object with the specified key.

Usage

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 .java 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 {
                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.github.edgar615.util.vertx.wheel.KeepaliveOptionsConverter.java

License:Apache License

public static void toJson(KeepaliveOptions obj, JsonObject json) {
    if (obj.getDisConnAddress() != null) {
        json.put("disConnAddress", obj.getDisConnAddress());
    }//from  w ww  .j  a  va 2  s  .c om
    if (obj.getFirstConnAddress() != null) {
        json.put("firstConnAddress", obj.getFirstConnAddress());
    }
    json.put("interval", obj.getInterval());
    json.put("step", obj.getStep());
}

From source file:com.github.edgar615.util.vertx.wheel.TimerWheelOptionsConverter.java

License:Apache License

public static void toJson(TimerWheelOptions obj, JsonObject json) {
    if (obj.getAnnounceAddress() != null) {
        json.put("announceAddress", obj.getAnnounceAddress());
    }/*w w  w .  j a  v  a  2 s .  c om*/
    if (obj.getCancelAddress() != null) {
        json.put("cancelAddress", obj.getCancelAddress());
    }
    json.put("interval", obj.getInterval());
}

From source file:com.github.ithildir.airbot.model.LocationConverter.java

License:Apache License

public static void toJson(Location obj, JsonObject json) {
    if (obj.getCountry() != null) {
        json.put("country", obj.getCountry());
    }// w w w  . jav a  2  s. co  m
    json.put("latitude", obj.getLatitude());
    json.put("longitude", obj.getLongitude());
}

From source file:com.github.ithildir.airbot.model.MeasurementConverter.java

License:Apache License

public static void toJson(Measurement obj, JsonObject json) {
    json.put("aqi", obj.getAqi());
    if (obj.getCity() != null) {
        json.put("city", obj.getCity());
    }/* www . ja  va2s.co m*/
    if (obj.getComments() != null) {
        json.put("comments", obj.getComments());
    }
    if (obj.getMainPollutant() != null) {
        json.put("mainPollutant", obj.getMainPollutant());
    }
    json.put("time", obj.getTime());
    if (obj.getValues() != null) {
        JsonObject map = new JsonObject();
        obj.getValues().forEach((key, value) -> map.put(key, value));
        json.put("values", map);
    }
}

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

License:Open Source License

public SingleUserAuthProvider(String username, String password) {
    _username = Objects.requireNonNull(username);
    _password = Objects.requireNonNull(password);

    _user = new AbstractUser() {

        @Override// w  w  w .  j  a v  a 2 s .  c o  m
        public JsonObject principal() {
            JsonObject jsonObject = new JsonObject();

            jsonObject.put("username", _username);

            return jsonObject;
        }

        @Override
        public void setAuthProvider(AuthProvider authProvider) {

            // Nothing to do

        }

        @Override
        protected void doIsPermitted(String permission, Handler<AsyncResult<Boolean>> handler) {

            handler.handle(Future.succeededFuture(Boolean.TRUE));
        }

    };
}

From source file:com.github.ithildir.airbot.service.GeoServiceVertxEBProxy.java

License:Apache License

public void getLocationByCoordinates(double latitude, double longitude,
        Handler<AsyncResult<Location>> handler) {
    if (closed) {
        handler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;//from w ww  .  j  a  v a 2 s .  c  o  m
    }
    JsonObject _json = new JsonObject();
    _json.put("latitude", latitude);
    _json.put("longitude", longitude);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "getLocationByCoordinates");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
        } else {
            handler.handle(Future
                    .succeededFuture(res.result().body() == null ? null : new Location(res.result().body())));
        }
    });
}

From source file:com.github.ithildir.airbot.service.GeoServiceVertxEBProxy.java

License:Apache License

public void getLocationByQuery(String query, Handler<AsyncResult<Location>> handler) {
    if (closed) {
        handler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*from w ww .  j ava2 s .  c  om*/
    }
    JsonObject _json = new JsonObject();
    _json.put("query", query);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "getLocationByQuery");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
        } else {
            handler.handle(Future
                    .succeededFuture(res.result().body() == null ? null : new Location(res.result().body())));
        }
    });
}

From source file:com.github.ithildir.airbot.service.MeasurementServiceVertxEBProxy.java

License:Apache License

public void getMeasurement(double latitude, double longitude, Handler<AsyncResult<Measurement>> handler) {
    if (closed) {
        handler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*  w w w . ja  v a  2s  .c  o m*/
    }
    JsonObject _json = new JsonObject();
    _json.put("latitude", latitude);
    _json.put("longitude", longitude);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "getMeasurement");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
        } else {
            handler.handle(Future.succeededFuture(
                    res.result().body() == null ? null : new Measurement(res.result().body())));
        }
    });
}

From source file:com.github.ithildir.airbot.service.UserServiceVertxEBProxy.java

License:Apache License

public void getUserLocation(String userId, Handler<AsyncResult<Location>> handler) {
    if (closed) {
        handler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;//from  w  w w.ja  v  a 2 s.  c  om
    }
    JsonObject _json = new JsonObject();
    _json.put("userId", userId);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "getUserLocation");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
        } else {
            handler.handle(Future
                    .succeededFuture(res.result().body() == null ? null : new Location(res.result().body())));
        }
    });
}