package com.javahonk;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javahonk.dao.IPersonDAO;
import com.javahonk.di.bean.Person;

public class RowMapperTest {

    public static void main(String[] args) {
	
	ClassPathXmlApplicationContext applicationContext = 
		new ClassPathXmlApplicationContext(
			"spring\\application-config.xml");

	IPersonDAO personDAO = applicationContext.
		getBean("personDAO", IPersonDAO.class);
	
	//Using RowMapper to map rows run time
	List<Person> persons = personDAO.selectAllPerson();
	for (Person person : persons) {
	    System.out.println("Id: "+person.getId()
		    +" First Name: "+person.getFirst_Name()
		    +" Last Name: "+person.getLast_Name()
		    +" Street: "+person.getStreet_Name()
		    +" City: "+person.getCity()
		    +" State: "+person.getState()
		    +" Country: "+person.getCountry()
		    +" Row Number: "+person.getRowNumber());
	}
	
	//Using BeanPropertyRowMapper 
	persons = personDAO.selectPersonByName("Java");
	for (Person person : persons) {
	    System.out.println("Id: "+person.getId()
		+" First Name: "+person.getFirst_Name()
		+" Last Name: "+person.getLast_Name()
		+" Street: "+person.getStreet_Name()
		+" City: "+person.getCity()
		+" State: "+person.getState()
		+" Country: "+person.getCountry());
	}
	
	//Using Custom Person RowMapper class
	List <Person> persons2 = personDAO.
		selectPersonByNameUsingCustomRowMapper("Java");
	for (Person person2 : persons2) {
	    System.out.println("Id: "+person2.getId()
		    +" First Name: "+person2.getFirst_Name()
		    +" Last Name: "+person2.getLast_Name()
		    +" Street: "+person2.getStreet_Name()
		    +" City: "+person2.getCity()
		    +" State: "+person2.getState()
		    +" Country: "+person2.getCountry()
		    +" Row Number: "+person2.getRowNumber());
	    
	}
	
	//Find total person
	//Example 1
	Integer totalPerson = personDAO.findTotalPerson();
	System.out.println(totalPerson);
	
	//Find total person
	//Example 2
	totalPerson = personDAO.findTotalPersonUsingWhereClause();
	System.out.println(totalPerson);
	applicationContext.close();

    }

}
