package in.co.sunrays.proj1.dao;
import in.co.sunrays.proj1.dto.StudentDTO; import in.co.sunrays.proj1.exception.DatabaseException; import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; /** * JDBC Implementation of StudentDAO * * @author SUNRAYS Technologies * @version 1.0 * @Copyright (c) SUNRAYS Technologies */ public class StudentDAOJDBCImpl extends JdbcDaoSupport implements StudentDAOInt { /** * Logger */ private static Logger log = Logger.getLogger(StudentDAOJDBCImpl.class); /** * Find next PK for Student * * @throws DatabaseException */ public long nextPK() throws DatabaseException { log.debug("DAO nextPK Started"); long pk = 0; try { pk = getJdbcTemplate().queryForLong("SELECT MAX(ID) FROM STUDENT"); } catch (Exception e) { log.error("Database Exception..", e); throw new DatabaseException("Exception : Exception in getting PK"); } log.debug("DAO nextPK End"); return pk + 1; } /** * Add a Student * * @param dto * @throws DatabaseException */ public long add(StudentDTO dto) throws DatabaseException { log.debug("DAO add Started"); long pk = nextPK(); try { Object[] values = new Object[] { pk, dto.getCollegeId(), dto.getCollegeName(), dto.getFirstName(), dto.getLastName(), new Date(dto.getDob().getTime()), dto.getMobileNo(), dto.getEmail(), dto.getCreatedBy(), dto.getModifiedBy(), dto.getCreatedDatetime(), dto.getModifiedDatetime() }; getJdbcTemplate().update( "INSERT INTO STUDENT VALUES(?,?,?,?,?,?,?,?,?,?,?,?)", values); } catch (Exception e) { log.error("Database Exception..", e); throw new DatabaseException("Exception : Exception in add Student"); } log.debug("DAO add End"); return pk; } /** * Update a Student * * @param dto * @throws DatabaseException */ @Override public void update(StudentDTO dto) throws DatabaseException { log.debug("DAO update Started"); try { Object[] values = new Object[] { dto.getCollegeId(), dto.getCollegeName(), dto.getFirstName(), dto.getLastName(), new Date(dto.getDob().getTime()), dto.getMobileNo(), dto.getEmail(), dto.getCreatedBy(), dto.getModifiedBy(), dto.getCreatedDatetime(), dto.getModifiedDatetime(), dto.getId() }; getJdbcTemplate() .update("UPDATE STUDENT SET COLLEGE_ID=?,COLLEGE_NAME=?,FIRST_NAME=?,LAST_NAME=?,DATE_OF_BIRTH=?,MOBILE_NO=?,EMAIL=?,CREATED_BY=?,MODIFIED_BY=?,CREATED_DATETIME=?,MODIFIED_DATETIME=? WHERE ID=?", values); } catch (Exception e) { log.error("Database Exception..", e); throw new DatabaseException("Exception in updating Student"); } log.debug("DAO update End"); } /** * Delete a Student * * @param dto * @throws DatabaseException */ public void delete(StudentDTO dto) throws DatabaseException { log.debug("DAO delete Started"); try { getJdbcTemplate().update("DELETE FROM STUDENT WHERE ID=?", new Object[] { dto.getId() }); } catch (Exception e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in delete Student"); } log.debug("DAO delete Started"); } /** * Find Student by Email * * @param email * : get parameter * @return dto * @throws DatabaseException */ @Override public StudentDTO findByEmail(String email) throws DatabaseException { log.debug("DAO findByEmail Started"); StudentDTO dto = null; try { dto = (StudentDTO) getJdbcTemplate().queryForObject( "SELECT * FROM STUDENT WHERE EMAIL=?", new Object[] { email }, new StudentMapper()); } catch (Exception e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting Student by Email"); } log.debug("DAO findByEmail End"); return dto; } /** * Find Student by PK * * @param pk * : get parameter * @return dto * @throws DatabaseException */ @Override public StudentDTO findByPK(long pk) throws DatabaseException { log.debug("DAO findByPK Started"); StudentDTO dto = null; try { dto = (StudentDTO) getJdbcTemplate().queryForObject( "SELECT * FROM STUDENT WHERE ID=?", new Object[] { pk }, new StudentMapper()); } catch (Exception e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting Student by pk"); } log.debug("DAO findByPK End"); return dto; } /** * Search Students with pagination * * @return list : List of Students * @param dto * : Search Parameters * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws DatabaseException */ @Override public List search(StudentDTO dto, int pageNo, int pageSize) throws DatabaseException { log.debug("DAO search Started"); StringBuffer sql = new StringBuffer("SELECT * FROM STUDENT WHERE 1=1"); if (dto.getId() > 0) { sql.append(" AND ID = " + dto.getId()); } if (dto.getFirstName() != null && dto.getFirstName().length() > 0) { sql.append(" AND FIRST_NAME LIKE '" + dto.getFirstName() + "%'"); } if (dto.getLastName() != null && dto.getLastName().length() > 0) { sql.append(" AND LAST_NAME LIKE '" + dto.getLastName() + "%'"); } if (dto.getDob() != null) { sql.append(" AND DATE_OF_BIRTH = " + dto.getDob()); } if (dto.getMobileNo() != null && dto.getMobileNo().length() > 0) { sql.append(" AND MOBILE_NO = '" + dto.getMobileNo() + "'"); } if (dto.getEmail() != null && dto.getEmail().length() > 0) { sql.append(" AND EMAIL LIKE '" + dto.getEmail() + "%'"); } // if page size is greater than zero then apply pagination if (pageSize > 0) { // Calculate start record index pageNo = (pageNo - 1) * pageSize; sql.append(" LIMIT " + pageNo + ", " + pageSize); } List list = new ArrayList(); try { list = getJdbcTemplate().query(sql.toString(), new StudentMapper()); } catch (Exception e) { log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in search Student"); } log.debug("DAO search End"); return list; } /** * Search Students * * @return list : List of Students * @param dto * : Search Parameters * @throws DatabaseException */ @Override public List search(StudentDTO dto) throws DatabaseException { return search(dto, 0, 0); } /** * Get List of Students * * @return list : List of Students * @throws DatabaseException */ @Override public List list() throws DatabaseException { return list(0, 0); } /** * Get List of Students with pagination * * @return list : List of Students * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws DatabaseException */ @Override public List list(int pageNo, int pageSize) throws DatabaseException { log.debug("DAO list Started"); StringBuffer sql = new StringBuffer("SELECT * FROM STUDENT"); // if page size is greater than zero then apply pagination if (pageSize > 0) { // Calculate start record index pageNo = (pageNo - 1) * pageSize; sql.append(" LIMIT " + pageNo + "," + pageSize); } List list = new ArrayList(); try { list = getJdbcTemplate().query(sql.toString(), new StudentMapper()); } catch (Exception e) { e.printStackTrace(); log.error("Database Exception..", e); throw new DatabaseException( "Exception : Exception in getting list of Student"); } log.debug("DAO list End"); return list; } class StudentMapper implements RowMapper { @Override public StudentDTO mapRow(ResultSet rs, int rowNum) throws SQLException { StudentDTO dto = new StudentDTO(); dto.setId(rs.getLong(1)); dto.setCollegeId(rs.getLong(2)); dto.setCollegeName(rs.getString(3)); dto.setFirstName(rs.getString(4)); dto.setLastName(rs.getString(5)); dto.setDob(rs.getDate(6)); dto.setMobileNo(rs.getString(7)); dto.setEmail(rs.getString(8)); dto.setCreatedBy(rs.getString(9)); dto.setModifiedBy(rs.getString(10)); dto.setCreatedDatetime(rs.getTimestamp(11)); dto.setModifiedDatetime(rs.getTimestamp(12)); return dto; } } } |