/**
 * @(#)ProjectDAOImpl.java
 * 
 * JFoxSOAF, Service-Oriented Application Framework
 * 
 * Copyright(c) JFoxSOAF Team
 * 
 * Licensed under the GNU LGPL, Version 2.1 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * For more information, please visit:
 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
 * http://www.huihoo.org/jfox/jfoxsoaf
 */

package org.huihoo.jfox.soaf.samples.jdbc.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.huihoo.jfox.soaf.exception.DAOException;
import org.huihoo.jfox.soaf.samples.jdbc.model.ProjectBean;
import org.huihoo.jfox.soaf.services.dao.AbstractJdbcDAO;
import org.huihoo.jfox.soaf.services.jdbc.DataSourceService;
import org.huihoo.jfox.soaf.services.persistence.JdbcService;

public class ProjectDAOImpl extends AbstractJdbcDAO {

	private JdbcService js;

	private DataSourceService dss;

	public ProjectDAOImpl() {
		js = getJdbcService();
		dss = getDataSourceService();
		js.setDataSource(dss.getDataSource(null));
	}

	private static String GET_PROJECT_BY_ID = "SELECT * FROM cn_project WHERE id='1'";

	private static String GET_PROJECTS = "SELECT * FROM cn_project";
	
	private static String COUNT_PROJECTS = "SELECT count(1) FROM cn_project";

	public ProjectBean getProjectById() throws DAOException {
		ResultSetHandler rsh = new BeanHandler(ProjectBean.class);
		return (ProjectBean) js.query(GET_PROJECT_BY_ID, rsh);
	}

	public List getProjects() throws DAOException {
		ResultSetHandler rsh = new BeanListHandler(ProjectBean.class);
		return (List) js.query(GET_PROJECTS, rsh);
	}

	public int countProject() throws DAOException {
		ResultSetHandler rsh = new MapHandler();
		Map map = (Map) js.query(COUNT_PROJECTS, rsh);
		Integer countNum = (Integer)map.get("count(1)");
		return countNum.intValue();				
	}

}