package be.peopleware.theopenmoviedb.servlet;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import be.peopleware.theopenmoviedb.Util;
import be.peopleware.theopenmoviedb.model.Movie;

public class MovieServlet extends HttpServlet {
	
	private static final long serialVersionUID = 4442857337491447538L;

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json; charset=UTF-8");
		ServletOutputStream out = response.getOutputStream();
		OutputStreamWriter writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));
		
		String queryId = request.getParameter("id");
		String queryName = request.getParameter("name");
		
		if (queryId != null && queryId.length() > 0) {
			handleIdRequest(writer, queryId);
		} else if (queryName != null && queryName.length() >= 3) {
			handleNameRequest(writer, queryName);
		} else {
			handleEmptyRequest(writer);
		}
		
		writer.close();
		
	}
	
	private void handleIdRequest(OutputStreamWriter writer, String queryId) throws IOException {

		Movie movie = Util.getMovie(queryId);
		if (movie != null) {
			JSONArray movieList = new JSONArray();
			movieList.put(new JSONObject().put("name", movie.getName())
					                      .put("movieId", movie.getId()));
			JSONObject result = new JSONObject();
			result.put("identifier", "movieId");
			result.put("items", movieList);
			
			writer.write(result.toString());
		} else {
			handleEmptyRequest(writer);
		}
		
	}
	
	private void handleNameRequest(OutputStreamWriter writer, String queryName) throws IOException {
		List<Movie> movies = Util.getMovies(queryName);
		
		JSONArray movieList = new JSONArray();
		for (Movie movie : movies) {
			movieList.put(new JSONObject().put("name", movie.getName())
					                      .put("movieId", movie.getId()));
		}
		
		JSONObject result = new JSONObject();
		result.put("identifier", "movieId");
		result.put("items", movieList);
		
		writer.write(result.toString());
	}
	
	private void handleEmptyRequest(OutputStreamWriter writer) throws IOException {
		JSONObject result = new JSONObject();
		result.put("identifier", "movieId");
		result.put("items", new JSONArray());
		
		writer.write(result.toString());
	}

}

@@ -1658,16 +1658,20 @@
 Id()));%0A
+%09%09%09%0A
 %09%09%09JSONO
@@ -1689,89 +1689,21 @@
 t = 
-new JSONObject();%0A%09%09%09result.put(%22identifier%22, %22movieId%22);%0A%09%09%09result.put(%22items%22, 
+createResult(
 movi
@@ -1710,19 +1710,16 @@
 eList);%0A
-%09%09%09
 %0A%09%09%09writ
@@ -2180,87 +2180,21 @@
 t = 
-new JSONObject();%0A%09%09result.put(%22identifier%22, %22movieId%22);%0A%09%09result.put(%22items%22, 
+createResult(
 movi
@@ -2325,16 +2325,168 @@
 ption %7B%0A
+%09%09JSONObject result = createResult(new JSONArray());%0A%09%09%0A%09%09writer.write(result.toString());%0A%09%7D%0A%09%0A%09private JSONObject createResult(JSONArray movieList) %7B%0A
 %09%09JSONOb
@@ -2582,62 +2582,35 @@
 s%22, 
-new JSONArray());%0A%09%09%0A%09%09writer.write(result.toString())
+movieList);%0A%09%09return result
 ;%0A%09%7D

Refactor common code