/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package data;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.sql.*;


/**
 *
 * @author Administrator
 */
public class GetCinemasList extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException{
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String currentDate = DateUtil.currentDate_YYYYMMDD();
        String selectedCity = request.getParameter("city");
        String sqlStatement = "";
        String extraCondition = "";
        String result = "";

        try
        {
            ResourceBundle config = ResourceBundle.getBundle("config");
            // get a DB connection
            String dbURL = config.getString("dbURL");
            String username = config.getString("username");
            String password = config.getString("password");

            Connection connection = DriverManager.getConnection(dbURL, username, password);
            Statement statement = connection.createStatement();

            StringBuffer jsonSB = new StringBuffer();

            ResourceBundle dataSQLQueries = ResourceBundle.getBundle("data.SQLQueries");

            sqlStatement = dataSQLQueries.getString("CinemaLookup.query.cinemas");
            extraCondition = dataSQLQueries.getString("Condition.city") + SQLUtil.addQuotes(selectedCity) + dataSQLQueries.getString("Condition.date") + SQLUtil.addQuotes(currentDate);
            ResultSet resultSet = statement.executeQuery(sqlStatement + extraCondition);

            jsonSB.append("\"status\" : \"success\", \n");
            jsonSB.append("\"cinemas\" : [\n");

            while (resultSet.next())
            {
                jsonSB.append("{\"name\" : \"" + resultSet.getString(1) + "\", \"recognition\" : \"" + resultSet.getString(2) + "\"}");
                if(!resultSet.isLast())
                    jsonSB.append(",\n");
            }
            jsonSB.append("\n]\n");
            result = XMLUtil.getFormat(jsonSB.toString());
            out.println(result);
            resultSet.close();
            statement.close();
            connection.close();
            out.close();
        }
        catch(SQLException e)
        {
             out.println(XMLUtil.getFormat(XMLUtil.errorHandler(e.getMessage())));
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            //Logger.getLogger(GetFilmsList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException  {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            //Logger.getLogger(GetFilmsList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 }
