package net.mybluemix.servlet;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
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 net.mybluemix.dao.BaseDAO;
import net.mybluemix.dao.DepartmentDAO;
import net.mybluemix.dao.EmployeeDAO;
import net.mybluemix.entity.Department;
import net.mybluemix.entity.Employee;

/**
 * Servlet implementation class SimpleServlet
 */
@WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    EmployeeDAO edao = new EmployeeDAO();
    DepartmentDAO ddao = new DepartmentDAO();
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	    	
    	
    	this.createEmployees();
    	
    	this.listEmployees();

    	System.out.println(".. done");
    	
    	
    	response.setContentType("text/html");
        response.getWriter().print("Hello World!");
        

}



private void createEmployees() {
//int numOfEmployees = manager.createQuery("Select a From Employee a", Employee.class).getResultList().size();
//if (numOfEmployees == 0) {
	try {
    Department department = new Department("java");
    ddao.create(department);
    System.out.println("Saving dept");
    edao.create(new Employee("Jakab Gipsz",department));
    edao.create(new Employee("Captain Nemo",department));
	}
	catch(Exception e) {
		e.printStackTrace();
	}
//}
}

private void listEmployees() {
List<Employee> resultList = (List<Employee>) edao.createQuery("Select a From Employee a", Employee.class);
System.out.println("num of employess:" + resultList.size());
for (Employee next : resultList) {
    System.out.println("next employee: " + next);
}
}

}


@@ -1260,29 +1260,54 @@
 es();%0A    %09%0A
-  
+%09List%3CEmployee%3E resultList =
   
-%09
 this.listEmp
@@ -1315,16 +1315,131 @@
 oyees();
+%0A%09String listemployees = %22Employees %3CBR%3E%3CBR%3E%22;%0A%0A%09for (Employee e : resultList) %7B%0A    %09%09listemployees += e + %22%3CBR%3E%22%09
 %0A%0A    %09S
@@ -1562,22 +1562,21 @@
 int(
-%22Hello World!%22
+listemployees
 );%0A 
@@ -2070,20 +2070,30 @@
 private 
-void
+List%3CEmployee%3E
  listEmp
@@ -2214,155 +2214,26 @@
 s);%0A
-System.out.println(%22num of employess:%22 + resultList.size());%0Afor (Employee next : resultList) %7B%0A    System.out.println(%22next employee: %22 + next);%0A%7D
+return resultList;
 %0A%7D%0A%0A

Update SimpleServlet.java