Database service implementation
The service implementation is a straightforward port of the previous WikiDatabaseVerticle
class code. The essential difference is the support of the asynchronous result handler in the constructor (to report the initialization outcome) and in the service methods (to report the operation success).
The class code is the following:
class WikiDatabaseServiceImpl implements WikiDatabaseService {
private static final Logger LOGGER = LoggerFactory.getLogger(WikiDatabaseServiceImpl.class);
private final HashMap<SqlQuery, String> sqlQueries;
private final JDBCClient dbClient;
WikiDatabaseServiceImpl(JDBCClient dbClient, HashMap<SqlQuery, String> sqlQueries, Handler<AsyncResult<WikiDatabaseService>> readyHandler) {
this.dbClient = dbClient;
this.sqlQueries = sqlQueries;
dbClient.getConnection(ar -> {
if (ar.failed()) {
LOGGER.error("Could not open a database connection", ar.cause());
readyHandler.handle(Future.failedFuture(ar.cause()));
} else {
SQLConnection connection = ar.result();
connection.execute(sqlQueries.get(SqlQuery.CREATE_PAGES_TABLE), create -> {
connection.close();
if (create.failed()) {
LOGGER.error("Database preparation error", create.cause());
readyHandler.handle(Future.failedFuture(create.cause()));
} else {
readyHandler.handle(Future.succeededFuture(this));
}
});
}
});
}
@Override
public WikiDatabaseService fetchAllPages(Handler<AsyncResult<JsonArray>> resultHandler) {
dbClient.query(sqlQueries.get(SqlQuery.ALL_PAGES), res -> {
if (res.succeeded()) {
JsonArray pages = new JsonArray(res.result()
.getResults()
.stream()
.map(json -> json.getString(0))
.sorted()
.collect(Collectors.toList()));
resultHandler.handle(Future.succeededFuture(pages));
} else {
LOGGER.error("Database query error", res.cause());
resultHandler.handle(Future.failedFuture(res.cause()));
}
});
return this;
}
@Override
public WikiDatabaseService fetchPage(String name, Handler<AsyncResult<JsonObject>> resultHandler) {
dbClient.queryWithParams(sqlQueries.get(SqlQuery.GET_PAGE), new JsonArray().add(name), fetch -> {
if (fetch.succeeded()) {
JsonObject response = new JsonObject();
ResultSet resultSet = fetch.result();
if (resultSet.getNumRows() == 0) {
response.put("found", false);
} else {
response.put("found", true);
JsonArray row = resultSet.getResults().get(0);
response.put("id", row.getInteger(0));
response.put("rawContent", row.getString(1));
}
resultHandler.handle(Future.succeededFuture(response));
} else {
LOGGER.error("Database query error", fetch.cause());
resultHandler.handle(Future.failedFuture(fetch.cause()));
}
});
return this;
}
@Override
public WikiDatabaseService createPage(String title, String markdown, Handler<AsyncResult<Void>> resultHandler) {
JsonArray data = new JsonArray().add(title).add(markdown);
dbClient.updateWithParams(sqlQueries.get(SqlQuery.CREATE_PAGE), data, res -> {
if (res.succeeded()) {
resultHandler.handle(Future.succeededFuture());
} else {
LOGGER.error("Database query error", res.cause());
resultHandler.handle(Future.failedFuture(res.cause()));
}
});
return this;
}
@Override
public WikiDatabaseService savePage(int id, String markdown, Handler<AsyncResult<Void>> resultHandler) {
JsonArray data = new JsonArray().add(markdown).add(id);
dbClient.updateWithParams(sqlQueries.get(SqlQuery.SAVE_PAGE), data, res -> {
if (res.succeeded()) {
resultHandler.handle(Future.succeededFuture());
} else {
LOGGER.error("Database query error", res.cause());
resultHandler.handle(Future.failedFuture(res.cause()));
}
});
return this;
}
@Override
public WikiDatabaseService deletePage(int id, Handler<AsyncResult<Void>> resultHandler) {
JsonArray data = new JsonArray().add(id);
dbClient.updateWithParams(sqlQueries.get(SqlQuery.DELETE_PAGE), data, res -> {
if (res.succeeded()) {
resultHandler.handle(Future.succeededFuture());
} else {
LOGGER.error("Database query error", res.cause());
resultHandler.handle(Future.failedFuture(res.cause()));
}
});
return this;
}
}
There is one last step required before the proxy code generation works: the service package needs to have a package-info.java
annotated to define a Vert.x module:
@ModuleGen(groupPackage = "io.vertx.guides.wiki.database", name = "wiki-database")
package io.vertx.guides.wiki.database;
import io.vertx.codegen.annotations.ModuleGen;