package in.co.sunrays.proj1.service;
import in.co.sunrays.proj1.dao.CollegeDAOInt; import in.co.sunrays.proj1.dto.CollegeDTO; import in.co.sunrays.proj1.exception.ApplicationException; import in.co.sunrays.proj1.exception.DatabaseException; import in.co.sunrays.proj1.exception.DuplicateRecordException; import java.util.List; import org.apache.log4j.Logger; /** * Spring Implementation of College Service * * @author SUNRAYS Technologies * @version 1.0 * @Copyright (c) SUNRAYS Technologies */ public class CollegeServiceSpringImpl implements CollegeServiceInt { /** * Logger */ private static Logger log = Logger .getLogger(CollegeServiceSpringImpl.class); /** * Get CollegeDAO Instance from Factory of DAO */ private CollegeDAOInt dao; /** * Injects DAO object * * @param dao */ public void setDao(CollegeDAOInt dao) { this.dao = dao; } /** * Add a College * * @param dto * @throws ApplicationException * @throws DuplicateRecordException * : throws when College is already exists */ @Override public long add(CollegeDTO dto) throws ApplicationException, DuplicateRecordException { log.debug("Service add Started"); long pk = 0; try { CollegeDTO dtoExist = dao.findByName(dto.getName()); if (dtoExist != null) { throw new DuplicateRecordException("Name already exists"); } pk = dao.add(dto); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service add End"); return pk; } /** * Update a College * * @param dto * @throws ApplicationException * @throws DuplicateRecordException * : throws when updated College is already exists */ @Override public void update(CollegeDTO dto) throws ApplicationException, DuplicateRecordException { log.debug("Service update Started"); try { CollegeDTO dtoExist = dao.findByName(dto.getName()); // Check if updated College is already exists if (dtoExist != null && dtoExist.getId() != dto.getId()) { throw new DuplicateRecordException("Duplicate Roll Number"); } dao.update(dto); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service update End"); } /** * Delete a College * * @param dto * @throws ApplicationException */ @Override public void delete(CollegeDTO dto) throws ApplicationException { log.debug("Service delete Started"); try { CollegeDTO dtoExist = findByPK(dto.getId()); if (dtoExist == null) { throw new ApplicationException("College does not exist"); } dao.delete(dto); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service delete End"); } /** * Find College by Name * * @param name * : get parameter * @return dto * @throws ApplicationException */ @Override public CollegeDTO findByName(String name) throws ApplicationException { log.debug("Service findByName Started"); CollegeDTO dto; try { dto = dao.findByName(name); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service findByName End"); return dto; } /** * Find College by PK * * @param pk * : get parameter * @return dto * @throws ApplicationException */ @Override public CollegeDTO findByPK(long pk) throws ApplicationException { log.debug("Service findByPK Started"); CollegeDTO dto; try { dto = dao.findByPK(pk); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service findByPK End"); return dto; } /** * Search Colleges with pagination * * @return list : List of Colleges * @param dto * : Search Parameters * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws ApplicationException */ @Override public List search(CollegeDTO dto, int pageNo, int pageSize) throws ApplicationException { log.debug("Service search Started"); List list; try { list = dao.search(dto, pageNo, pageSize); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service search End"); return list; } /** * Search Colleges * * @return list : List of Colleges * @param dto * : Search Parameters * @throws ApplicationException */ @Override public List search(CollegeDTO dto) throws ApplicationException { log.debug("Service search Started"); List list; try { list = dao.search(dto); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service search End"); return list; } /** * Get List of Colleges * * @return list : List of Colleges * @throws ApplicationException */ public List list() throws ApplicationException { log.debug("Service list Started"); List list; try { list = dao.list(); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service list End"); return list; } /** * Get List of Colleges with pagination * * @return list : List of Colleges * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws ApplicationException */ public List list(int pageNo, int pageSize) throws ApplicationException { log.debug("Service list Started"); List list; try { list = dao.list(pageNo, pageSize); } catch (DatabaseException e) { log.error("Application Exception..", e); throw new ApplicationException("Database Exception"); } log.debug("Service list End"); return list; } } |