// Path: plague-tracker/src/main/java/es/tid/fiware/livedemoapp/cosmos/plaguetracker/hive/HiveConnection.java
// public final class HiveConnection {
//     private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
//     
//     /**
//      * Constructor. It is private since this is an utility class.
//      */
//     private HiveConnection() {
//     } // getConnection
//     
//     /**
//      * 
//      */
//     public static Connection getConnection(String ip, String port) {
//         try {
//             Class.forName(driverName);
//         } catch (ClassNotFoundException e) {
//             // TODO Auto-generated catch block
//             e.printStackTrace();
//             System.out.println(e.getMessage());
//             
//             System.exit(1);
//         } // try catch
//         
//         try {
//             return DriverManager.getConnection("jdbc:hive://" + ip + ":" + port
//                     + "/default?user=user&password=password");
//         } catch (SQLException ex) {
//             Logger.getLogger(HiveConnection.class.getName()).log(Level.SEVERE, null, ex);
//             return null;
//         } // catch
//     } // getConnection
//     
// } // getConnection
// 
// Path: plague-tracker/src/main/java/es/tid/fiware/livedemoapp/cosmos/plaguetracker/servlets/GeolocationDictionary.java
// public class Coordinates {
//         
//     private String lat;
//     private String lng;
//         
//     public String getLat() {
//         return lat;
//     } // getLat
//         
//     public String getLng() {
//         return lng;
//     } // gertLng
//         
// } // Coordiantes


import com.google.gson.Gson;
import es.tid.fiware.livedemoapp.cosmos.plaguetracker.hive.HiveConnection;
import es.tid.fiware.livedemoapp.cosmos.plaguetracker.servlets.GeolocationDictionary.Coordinates;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
     * @param type Plague type
     * @return A json string containing the hits per neighbourhood
     */
    private String getInfectionLevelsPerNeighbourhood(String type) {
        GeolocationDictionary geolocations = loadCoordinates();
        
        // query the Cosmos cluster
        Connection con = HiveConnection.getConnection("130.206.80.46", "10000");
        
        try {
            Statement stmt = con.createStatement();
            ResultSet res = stmt.executeQuery(
                "select neighbourhood,count(*) from malaga_plagues where type='" + type + "' group by neighbourhood");
            String s = "{\"infection_levels\": [";
            boolean empty = true;
        
            // iterate on the result in order to create a json string
            while (res.next()) {
                String neighbourhood = res.getString(1);
                String count = res.getString(2);
                ArrayList<Coordinates> area = geolocations.getArea(neighbourhood);
                
                if (area == null) {
                    continue;
                } // if
                
                empty = false;
                s += "{\"count\": \"" + count + "\", \"area\": [";
                
                for (int i = 0; i < area.size(); i++) {
// Path: plague-tracker/src/main/java/es/tid/fiware/livedemoapp/cosmos/plaguetracker/hive/HiveConnection.java
// public final class HiveConnection {
//     private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
//     
//     /**
//      * Constructor. It is private since this is an utility class.
//      */
//     private HiveConnection() {
//     } // getConnection
//     
//     /**
//      * 
//      */
//     public static Connection getConnection(String ip, String port) {
//         try {
//             Class.forName(driverName);
//         } catch (ClassNotFoundException e) {
//             // TODO Auto-generated catch block
//             e.printStackTrace();
//             System.out.println(e.getMessage());
//             
//             System.exit(1);
//         } // try catch
//         
//         try {
//             return DriverManager.getConnection("jdbc:hive://" + ip + ":" + port
//                     + "/default?user=user&password=password");
//         } catch (SQLException ex) {
//             Logger.getLogger(HiveConnection.class.getName()).log(Level.SEVERE, null, ex);
//             return null;
//         } // catch
//     } // getConnection
//     
// } // getConnection
// 
// Path: plague-tracker/src/main/java/es/tid/fiware/livedemoapp/cosmos/plaguetracker/servlets/GeolocationDictionary.java
// public class Coordinates {
//         
//     private String lat;
//     private String lng;
//         
//     public String getLat() {
//         return lat;
//     } // getLat
//         
//     public String getLng() {
//         return lng;
//     } // gertLng
//         
// } // Coordiantes


// Path: plague-tracker/src/main/java/es/tid/fiware/livedemoapp/cosmos/plaguetracker/servlets/CommandsMgr.java
import com.google.gson.Gson;
import es.tid.fiware.livedemoapp.cosmos.plaguetracker.hive.HiveConnection;
import es.tid.fiware.livedemoapp.cosmos.plaguetracker.servlets.GeolocationDictionary.Coordinates;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

     * @param type Plague type
     * @return A json string containing the hits per neighbourhood
     */
    private String getInfectionLevelsPerNeighbourhood(String type) {
        GeolocationDictionary geolocations = loadCoordinates();
        
        // query the Cosmos cluster
        Connection con = HiveConnection.getConnection("130.206.80.46", "10000");
        
        try {
            Statement stmt = con.createStatement();
            ResultSet res = stmt.executeQuery(
                "select neighbourhood,count(*) from malaga_plagues where type='" + type + "' group by neighbourhood");
            String s = "{\"infection_levels\": [";
            boolean empty = true;
        
            // iterate on the result in order to create a json string
            while (res.next()) {
                String neighbourhood = res.getString(1);
                String count = res.getString(2);
                ArrayList<Coordinates> area = geolocations.getArea(neighbourhood);
                
                if (area == null) {
                    continue;
                } // if
                
                empty = false;
                s += "{\"count\": \"" + count + "\", \"area\": [";
                
                for (int i = 0; i < area.size(); i++) {
                    Coordinates coords = area.get(i);