CollegeServiceImpl.java

package in.co.sunrays.service;

import in.co.sunrays.dao.CollegeDAOInt;

import in.co.sunrays.dto.CollegeDTO;

import in.co.sunrays.exception.ApplicationException;

import in.co.sunrays.exception.DuplicateRecordException;

import java.util.List;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

/**

* Service class contains College module business logics. It does transactional

* handling with help of Spring AOP.

*

* @author SUNRAYS Developer

* @version 1.0

* @Copyright (c) SUNRAYS Technologies

*/

@Service("collegeService")

public class CollegeServiceImpl implements CollegeServiceInt {

private static Logger log = Logger.getLogger(CollegeServiceImpl.class);

@Autowired

private CollegeDAOInt dao;

/**

* Adds a College.

*/

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)

public long add(CollegeDTO dto) {

CollegeDTO duplicateDTO = get(dto.getName());

if (duplicateDTO != null) {

throw new DuplicateRecordException("College is already exist.");

}

long pk = 0;

try {

pk = dao.add(dto);

} catch (Exception e) {

throw new ApplicationException("Exception in College Add "

+ e.getMessage());

}

return pk;

}

/**

* Updates College

*/

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)

public void update(CollegeDTO dto) {

CollegeDTO duplicateDTO = get(dto.getName());

if (duplicateDTO != null && duplicateDTO.getId() != dto.getId()) {

throw new DuplicateRecordException("College is already exist.");

}

try {

dao.update(dto);

} catch (Exception e) {

log.error(e);

throw new ApplicationException("Exception in College update "

+ e.getMessage());

}

}

/**

* Deletes a College

*/

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)

public CollegeDTO delete(long id) {

CollegeDTO dto = null;

try {

dto = dao.delete(id);

} catch (Exception e) {

throw new ApplicationException("Exception in college Delete"

+ e.getMessage());

}

return dto;

}

/**

* Search a College. It applies pagination.

*/

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)

public List search(CollegeDTO dto, int pageNo, int pageSize) {

return dao.search(dto, pageNo, pageSize);

}

/**

* Finds a College by its ID

*/

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)

public CollegeDTO get(long id) {

return dao.findByPK(id);

}

public CollegeDTO get(String name) {

return dao.findByName(name);

}

}