package main.java.meterstanden.domain;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.google.gson.Gson;

import main.java.meterstanden.hibernate.HibernateUtil;

/**
 * Servlet implementation class Util
 */
@WebServlet(
		description = "REST interface for meterstanden.",
		urlPatterns = {
				"/Util",
				"/util",
				"/UTIL" 
		}
	)
public class Util extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(Util.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Util() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Get the distinct jaren for the maandverbruiken
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("q").equalsIgnoreCase("maandverbruikjaar"))
		{
			log.debug("getting maandverbruik_jaar");
			List<?> rl = getDistinctJaren();
			List<HashMap<String, Integer>> map = maanverbruikJaarToHashMap(rl);
			
			Gson gson = new Gson();
			response.getWriter().append(gson.toJson(map));
		} else {
			response.getWriter().append("Missing paramters.");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	/**************************************************************************
	 * PRIVATE METHODS
	**************************************************************************/
	
	/**
	 * Get a list of the distinct jaren of maandverbruiken.
	 * 
	 * @return
	 */
	private List<?> getDistinctJaren(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		StringBuffer hql = new StringBuffer();
		hql.append("select distinct m.jaar as jaar from Maandverbruik m");
		hql.append(" order by m.jaar desc");
		
		Query q = session.createQuery(hql.toString());
		List<?> rl = q.getResultList();
		session.close();
		return rl;
	}
	
	/**
	 * Make a hashmap of the jaren
	 * 
	 * @param rl the resultlist with the distinct jaren
	 * @return a hashmap <String, Integer> as key-value pare for "jaar" = xxxx
	 */
	private List<HashMap<String, Integer>> maanverbruikJaarToHashMap(List<?> rl){
		List<HashMap<String, Integer>> map = new ArrayList<HashMap<String, Integer>>();
		
		Iterator<?> rl_i = rl.iterator();
		while (rl_i.hasNext()){
			int jaar = (Integer)rl_i.next();
			HashMap<String, Integer> hm = new HashMap<String, Integer>();
			hm.put("jaar", jaar);
			map.add(hm);
		}
		
		return map;
	}

}

@@ -1390,13 +1390,10 @@
 r%22))
-%0A%09%09
 %7B%0A
+
 %09%09%09l
@@ -1570,16 +1570,16 @@
 Gson();%0A
-
 %09%09%09respo
@@ -1620,16 +1620,449 @@
 (map));%0A
+%09%09%7D else if (request.getParameter(%22q%22).equalsIgnoreCase(%22jaarverbruik%22))%7B%0A%09%09%09log.debug(%22Getting the jaarverbruik.%22);%0A%09%09%09Session session = HibernateUtil.getSessionFactory().openSession();%0A%09%09%09StringBuilder hql = new StringBuilder();%0A%09%09%09hql.append(%22from Jaarverbruik%22);%0A%09%09%09Query q = session.createQuery(hql.toString());%0A%09%09%09List%3C?%3E rl = q.getResultList();%0A%09%09%09%0A%09%09%09Gson gson = new Gson();%0A%09%09%09response.getWriter().append(gson.toJson(rl));%0A%0A
 %09%09%7D else

Add endpoint for jaaroverzicht