package com.eyekabob;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class Main {
    private static final String API = "api";
    private static final String ARTIST = "artist";
    private static final String ADD_ARTIST = "addArtist";
    private static final String METHOD = "method";
    private static final String SEARCH = "search";
    private static final String QUERY = "query";
    private static final String GET = "GET";
    private static final String POST = "POST";

    public static JSONObject processRequest(HttpServletRequest request) {
        JSONObject result = null;

        try {
            result = new JSONObject();
            String method = request.getParameter(METHOD);
            String apiParam = request.getParameter(API);

            Connection conn = null;
            try {
                conn = getConnection();
            }
            catch (SQLException e) {
                result.put("error", "Could not establish a database connection. See server logs.");
                e.printStackTrace();
            }

            if (ARTIST.equals(apiParam)) {
                if (ADD_ARTIST.equals(method)) {
                    String genre = request.getParameter("genre");
                    String name = request.getParameter("name");
                    String url = request.getParameter("url");
                    String bio = request.getParameter("bio");
                    String query = "INSERT INTO artist (genre, name, url, bio) VALUES ('" + genre + "', '" + name + "', '" + url + "', '" + bio + "')";
                    Statement stmt = null;
                    try {
                        stmt = conn.createStatement();
                        stmt.executeUpdate(query);
                    }
                    catch (SQLException e ) {
                        result.put("error", e.getMessage());
                        e.printStackTrace();
                    }
                    finally {
                        if (stmt != null) {
                            try {
                                stmt.close();
                            }
                            catch (SQLException e) {
                                result.put("error", e.getMessage());
                                e.printStackTrace();
                            }
                        }
                    }
                }
                else if (SEARCH.equals(method)) {
                    String queryParam = request.getParameter(QUERY);
                    Statement stmt = null;
                    String query = "SELECT * FROM artist";// WHERE name LIKE '%" + queryParam + "%'";
                    try {
                        stmt = conn.createStatement();
                        ResultSet rs = stmt.executeQuery(query);
                        JSONArray artists = new JSONArray();
                        result.put("artists", artists);
                        while (rs.next()) {
                            String name = rs.getString("name");
                            JSONObject artist = new JSONObject();
                            artist.put("name", name);
                            artists.add(artist);
                        }
                    }
                    catch (SQLException e ) {
                        e.printStackTrace();
                    }
                    finally {
                        if (stmt != null) {
                            try {
                                stmt.close();
                            }
                            catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }

            result.put(API, apiParam);
            result.put(METHOD, method);
        }
        catch (JSONException e) {
            try {
                result.put("error", "JSONException occured: " + e.getMessage());
            }
            catch (JSONException ex) {
                // Totally boned. Can't do anything about it.
                ex.printStackTrace();
            }
            e.printStackTrace();
        }

        return result;
    }

    private static Connection getConnection() throws SQLException {
        Properties connectionProps = new Properties();
        connectionProps.put("user", "eyekabob");
        connectionProps.put("password", "privateeye");

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (InstantiationException e) {
            e.printStackTrace();
        }
        return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/eyekabob?autoReconnect=true", connectionProps);
    }
}

@@ -480,26 +480,26 @@
  String 
-ADD_
 ARTIST
+_ADD
  = %22addA
@@ -542,24 +542,31 @@
 ing 
-METHOD = %22method
+ARTIST_SEARCH = %22search
 %22;%0A 
@@ -596,32 +596,32 @@
  String 
-SEARCH = %22search
+METHOD = %22method
 %22;%0A    p
@@ -1399,18 +1399,18 @@
 if (
-ADD_
 ARTIST
+_ADD
 .equ
@@ -2681,16 +2681,23 @@
 lse if (
+ARTIST_
 SEARCH.e
@@ -2885,12 +2885,8 @@
 tist
-%22;//
  WHE
@@ -3158,83 +3158,222 @@
     
-result.put(%22artists%22, artists);%0A                        while (rs.next()) %7B
+while (rs.next()) %7B%0A                            JSONObject artist = new JSONObject();%0A%0A                            String name = rs.getString(%22name%22);%0A                            String mbid = rs.getString(%22mbid%22);
 %0A   
@@ -3396,35 +3396,36 @@
          String 
-nam
+genr
 e = rs.getString
@@ -3418,35 +3418,36 @@
 = rs.getString(%22
-nam
+genr
 e%22);%0A           
@@ -3467,46 +3467,105 @@
     
-JSONObject artist = new JSONObject(
+String bio = rs.getString(%22bio%22);%0A                            String url = rs.getString(%22url%22
 );%0A
+%0A
     
@@ -3652,48 +3652,319 @@
 tist
-s.add(artist);%0A                        %7D
+.put(%22mbid%22, mbid);%0A                            artist.put(%22genre%22, genre);%0A                            artist.put(%22bio%22, bio);%0A                            artist.put(%22url%22, url);%0A%0A                            artists.add(artist);%0A                        %7D%0A                        result.put(%22artists%22, artists);
 %0A   

modify artist search web service to return results