package flex.samples.ibatis;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.sqlmap.client.SqlMapClient;

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

public class RestaurantDAO {

    private SqlMapClient sqlMap;

    public RestaurantDAO() throws IOException {
        Reader reader = Resources.getResourceAsReader("sqlmap-config.xml");
        sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
    }

    public List getRestaurantList() throws Exception {
        return sqlMap.queryForList("getRestaurantList", null);
    }

    public RestaurantVO getRestaurant(int restaurantId) throws Exception {
        return (RestaurantVO) sqlMap.queryForObject("getRestaurantList", new Integer(restaurantId));
    }

    public int create(RestaurantVO restaurantVO) throws Exception  {
        return ( (Integer) sqlMap.insert("insertRestaurant", restaurantVO) ).intValue();
    }

    public void update(RestaurantVO restaurantVO) throws Exception {
        sqlMap.update("updateRestaurant", restaurantVO);
    }

    public void remove(RestaurantVO restaurantVO) throws Exception  {
        sqlMap.delete("deleteRestaurant", restaurantVO);
    }

}
