package flex.spring.samples.company;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class IbatisCompanyDAO extends SqlMapClientDaoSupport implements ICompanyDAO {

	private static final String COMPANY_NS = "Company.";
	
	public Company create(Company item) {
		Integer id = (Integer) getSqlMapClientTemplate().insert(COMPANY_NS+"create", item);
		item.setId(id);
		return item;
	}

	@SuppressWarnings("unchecked")
	public List<Company> findAll() {
		return getSqlMapClientTemplate().queryForList(COMPANY_NS+"findAll");
	}

	public Company findById(int id) {
		return (Company) getSqlMapClientTemplate().queryForObject(COMPANY_NS+"findById", id);
	}

	@SuppressWarnings("unchecked")
	public List<Company> findByName(String name) {
		return getSqlMapClientTemplate().queryForList(COMPANY_NS+"findByName", "%" + name.toUpperCase() + "%");
	}

	public boolean remove(Company item) {
		int count = getSqlMapClientTemplate().delete(COMPANY_NS+"remove", item);
		return (count == 1);
	}

	public boolean update(Company item) {
		int count = getSqlMapClientTemplate().update(COMPANY_NS+"update", item);
		return (count == 1);
	}

}
