List of usage examples for io.vertx.core Future future
future
From source file:com.funmix.service.LineServiceImpl.java
@Override public Future<Boolean> update(JsonObject params) { Future<Boolean> result = Future.future(); int id = params.getInteger("id"); JsonArray setparams = new JsonArray(); StringBuffer sqlSet = new StringBuffer(); params.forEach(r -> {/* w w w. j ava2 s. c o m*/ if (!r.getKey().equals("id")) { sqlSet.append(r.getKey()).append("=?,"); setparams.add(r.getValue()); } }); if (sqlSet.length() > 0) { sqlSet.deleteCharAt(sqlSet.length() - 1); } else { sqlSet.append("status=abs(status-1)"); } sqlSet.append(" where id = ?"); setparams.add(id); String sql = SQL_UPDATE + sqlSet.toString(); log.info(sql); log.info(params.toString()); client.getConnection(connHandler(result, connection -> { connection.updateWithParams(sql, setparams, r -> { if (r.failed()) { result.fail(r.cause()); } else { result.complete(true); } 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 -> {/* www . ja v a 2 s .c om*/ 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>> getOne(String id) { Future<Optional<Truck>> result = Future.future(); client.getConnection(connHandler(result, connection -> { connection.queryWithParams(SQL_QUERY, new JsonArray().add(id), r -> { if (r.failed()) { result.fail(r.cause());//from ww w .ja v a 2 s .c o m } else { List<JsonObject> list = r.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.funmix.service.TruckServiceImpl.java
@Override public Future<Optional<Truck>> insert(JsonObject json) { log.info(json.toString());/* w w w. j a v a 2 s . c o m*/ 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<String> _deployVerticle(Class<? extends Verticle> clazz) { Future<String> future = Future.future(); vertx.deployVerticle(clazz.getName(), future); return future; }
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);/*w ww . j av a2 s.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.BaseMeasurementServiceVerticle.java
License:Open Source License
private Future<Void> _init() { Future<Void> future = Future.future(); MeasurementService measurementService = ProxyHelper.createProxy(MeasurementService.class, vertx, getAddress());//from ww w . j a v a 2 s .co m measurementService.init(future); return future; }
From source file:com.github.ithildir.airbot.server.api.ai.AirQualityApiAiFulfillmentBuilder.java
License:Open Source License
private Future<Fulfillment> _buildLocationFulfillment(String locationString, Locale locale) { Future<Location> locationFuture = Future.future(); _geoService.getLocationByQuery(locationString, locationFuture); return locationFuture.compose(location -> _buildFulfillment(location, locationString, locale)); }
From source file:com.github.ithildir.airbot.server.api.ai.AirQualityApiAiFulfillmentBuilder.java
License:Open Source License
private Future<Fulfillment> _buildUserFulfillment(Locale locale, AIResponse aiResponse, JsonObject responseJsonObject) { String userId = aiResponse.getSessionId(); Future<Location> locationFuture = _getReponseLocation(responseJsonObject); locationFuture = locationFuture.compose(location -> { if (location != null) { return Future.succeededFuture(location); }/*w w w . j av a 2 s.co m*/ Future<Location> future = Future.future(); _userService.getUserLocation(userId, future); return future; }); return locationFuture.compose(location -> { if (location == null) { Fulfillment fulfillment = ApiAiUtil.buildGooglePermissionFulfillment( "in-order-to-get-information-about-the-air-" + "quality-around-you", "DEVICE_PRECISE_LOCATION", locale); return Future.succeededFuture(fulfillment); } _updateUserLocation(userId, location); return _buildFulfillment(location, null, locale); }); }
From source file:com.github.ithildir.airbot.server.api.ai.AirQualityApiAiFulfillmentBuilder.java
License:Open Source License
private Future<Location> _getReponseLocation(JsonObject responseJsonObject) { double[] coordinates = ApiAiUtil.getResponseCoordinates(responseJsonObject); if (coordinates == null) { return Future.succeededFuture(null); }//from ww w. j a va 2 s . co m Future<Location> future = Future.future(); _geoService.getLocationByCoordinates(coordinates[0], coordinates[1], future); return future; }