imbrobits.loosh.dao.ProjectDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for imbrobits.loosh.dao.ProjectDaoImpl.java

Source

/*
 * MIT License
 *
 * Copyright (c) 2016 Nhan Nguyen
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package imbrobits.loosh.dao;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.google.inject.Inject;
import imbrobits.loosh.models.Project;
import io.vertx.core.Handler;
import io.vertx.ext.asyncsql.AsyncSQLClient;
import io.vertx.ext.sql.ResultSet;
import io.vertx.ext.sql.SQLConnection;
import rx.Observable;

import java.io.IOException;
import java.util.List;

public class ProjectDaoImpl implements ProjectDao {

    private final AsyncSQLClient client;
    private final ObjectMapper om;

    @Inject
    public ProjectDaoImpl(AsyncSQLClient client, ObjectMapper om) {
        this.client = client;
        this.om = om;
    }

    @Override
    public void findAll(String query, final Handler<List<Project>> callback) {
        client.getConnection(event -> {
            if (event.succeeded()) {
                SQLConnection connection = event.result();
                connection.query(query, res -> {
                    if (res.succeeded()) {
                        callback.handle(mapResult(res.result()));
                    } else {
                        throw new RuntimeException(res.cause());
                    }
                    connection.close();
                });
            } else {
                throw new RuntimeException("Getting SQL connection failed", event.cause());
            }
        });
    }

    private List<Project> mapResult(ResultSet rs) {
        ObjectReader reader = om.readerFor(Project.class);
        return Observable.from(rs.getRows()).map(row -> {
            try {
                return (Project) reader.readValue(row.toString());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).toList().toBlocking().first();
    }
}