org.fao.fenix.wds.web.rest.faostat.GeoConverter.java Source code

Java tutorial

Introduction

Here is the source code for org.fao.fenix.wds.web.rest.faostat.GeoConverter.java

Source

/**
 *
 * FENIX (Food security and Early warning Network and Information Exchange)
 *
 * Copyright (c) 2011, by FAO of UN under the EC-FAO Food Security
 Information for Action Programme
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package org.fao.fenix.wds.web.rest.faostat;

import com.google.gson.Gson;
import org.fao.fenix.wds.core.bean.DatasourceBean;
import org.fao.fenix.wds.core.constant.DATASOURCE;
import org.fao.fenix.wds.core.datasource.DatasourcePool;
import org.fao.fenix.wds.core.exception.WDSExceptionStreamWriter;
import org.fao.fenix.wds.core.jdbc.JDBCIterable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.*;
import java.sql.SQLException;

/**
 * @author <a href="mailto:guido.barbaglia@fao.org">Guido Barbaglia</a>
 * @author <a href="mailto:guido.barbaglia@gmail.com">Guido Barbaglia</a>
 */
@Component
@Path("/geoconverter")
public class GeoConverter {

    @Autowired
    DatasourcePool datasourcePool;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{systemFrom}/{systemTo}/{code}")
    public Response convertCode(@PathParam("systemFrom") final String systemFrom,
            @PathParam("systemTo") final String systemTo, @PathParam("code") final String code) {

        // Initiate the stream
        StreamingOutput stream = new StreamingOutput() {

            @Override
            public void write(OutputStream os) throws IOException, WebApplicationException {

                // Initiate utilities
                Writer writer = new BufferedWriter(new OutputStreamWriter(os));
                Gson g = new Gson();

                // compute result
                DatasourceBean db = datasourcePool.getDatasource(DATASOURCE.FENIX.name());
                JDBCIterable it = new JDBCIterable();

                try {

                    // Query DB
                    it.query(db, buildSQL(systemFrom, systemTo, code));

                } catch (IllegalAccessException e) {
                    WDSExceptionStreamWriter.streamException(os,
                            ("Method 'convertCode' thrown an error: " + e.getMessage()));
                } catch (InstantiationException e) {
                    WDSExceptionStreamWriter.streamException(os,
                            ("Method 'convertCode' thrown an error: " + e.getMessage()));
                } catch (SQLException e) {
                    WDSExceptionStreamWriter.streamException(os,
                            ("Method 'convertCode' thrown an error: " + e.getMessage()));
                } catch (ClassNotFoundException e) {
                    WDSExceptionStreamWriter.streamException(os,
                            ("Method 'convertCode' thrown an error: " + e.getMessage()));
                } catch (Exception e) {
                    WDSExceptionStreamWriter.streamException(os,
                            ("Method 'getDomains' thrown an error: " + e.getMessage()));
                }

                // write the result of the query
                writer.write("[");
                while (it.hasNext()) {
                    writer.write(g.toJson(it.next()));
                    if (it.hasNext())
                        writer.write(",");
                }
                writer.write("]");

                // Convert and write the output on the stream
                writer.flush();

            }

        };

        /* Stream result */
        return Response.status(200).entity(stream).build();

    }

    private String buildSQL(String fromSystem, String toSystem, String code) {
        String select = (fromSystem == "FAOSTAT") ? "iso3_code" : "faost_code";
        String from = (toSystem == "FAOSTAT") ? "faost_code" : "iso3_code";
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT ").append(select).append(" FROM geo_conversion_table WHERE ");
        sb.append(from).append(" = '").append(code).append("' ");
        return sb.toString();
    }

}