package com.javahonk;


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

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

/**
 * @author Java Honk
 * Servlet implementation class ReportServlet
 */
@WebServlet("/CSVReportServlet")
public class CSVReportServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CSVReportServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request,
	 *  HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) 
					throws ServletException, IOException {

		response.setContentType("text/csv");
		String reportName =  "GenerateCSV_Report_"
				+System.currentTimeMillis()+".csv";		
		response.setHeader("Content-disposition", "attachment; " +
				"filename=" + reportName);	 
		
		ArrayList<String> rows = new ArrayList<String>();
        rows.add("First Name,LastName,Test");       
        rows.add("\n");

        for (int i = 0; i < 5; i++) {
            rows.add("Java,Honk,Success");
            rows.add("\n");
        }

		Iterator<String> iter = rows.iterator();
		while (iter.hasNext()){
			String outputString = (String) iter.next();
			response.getOutputStream().print(outputString);
		}
		
		response.getOutputStream().flush();
		
	}

}
