package com.javahonk.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SpringMVCController {

	@RequestMapping(value="/helloWorld.web",
			method = RequestMethod.GET)
	public String printWelcome(ModelMap model,
			HttpServletRequest request) {

		model.addAttribute("message", "How to pass data");
		model.addAttribute("selected", "Select single, "
				+ "multiple or all check box");
		List<Person> personsList = new ArrayList<Person>();
		Person person2 = new Person();
		for (int i = 0; i <10; i++) {
			
			Person person = new Person();
			person.setDepartment("IT");
			person.setLocation("NY");
			person.setName("Java Honk"+i);
			person.setSalary("100");
			person.setZip("00038");
			personsList.add(person);
		}
		person2.setPersonsList(personsList);
		model.addAttribute("person",person2);
		request.getSession().setAttribute("personsList", 
				personsList);
		return "helloWorld";

	}
	

	
	@RequestMapping(value="/multiCheckbox.web",
			method = RequestMethod.POST)
	public String multiCheckbox(Person person3, 
			ModelMap model,HttpServletRequest request) {

		model.addAttribute("message", "How to pass data");
		model.addAttribute("selected", "Selected check box data");
		List<Person> personList = (List<Person>) 
				request.getSession().getAttribute("personsList");
		Person person2 = new Person();
		List<Person> selectedPersonList = new ArrayList<Person>();
		if (null != personList && personList.size() != 0) {
			for (Person person : personList) {
				List<String> selectedPerson = person3
						.getSelectedCheckBox();
				if (null != selectedPerson && selectedPerson
						.size()!=0) {
					for (String string : selectedPerson) {
						if (string.equalsIgnoreCase(
								person.getName())) {
							selectedPersonList.add(person);
						}
					}
				}
				
			}
		}
		
		
		person2.setPersonsList(selectedPersonList);
		model.addAttribute("person",person2);
		return "helloWorld";

	}

    
}