EMPLOYEE BEAN
package com.accenture.lkm.business.bean;
import java.util.Date;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
public class EmployeeBean {
// TO-DO - Add the validation for the following fields
/*
* empName,empDor,candName,candSkill,candLevel should not be empty Length of the
* employee name should be between 3 and 7 empDor date format should be
* "dd-MMM-yyyy"
*/
private int empId;
@NotEmpty
@Size(min = 3, max = 7, message = "Employee name should be between 3 and 7")
private String empName;
@NotNull
@DateTimeFormat(pattern = "dd-MMM-yyyy")
private Date empDor;
@NotEmpty
private String candName;
@NotEmpty
private String candSkill;
@NotEmpty
private String candLevel;
private Double referralBonus;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Date getEmpDor() {
return empDor;
}
public void setEmpDor(Date empDor) {
this.empDor = empDor;
}
public String getCandName() {
return candName;
}
public void setCandName(String candName) {
this.candName = candName;
}
public String getCandSkill() {
return candSkill;
}
public void setCandSkill(String candSkill) {
this.candSkill = candSkill;
}
public String getCandLevel() {
return candLevel;
}
public void setCandLevel(String candLevel) {
this.candLevel = candLevel;
}
public Double getReferralBonus() {
return referralBonus;
}
public void setReferralBonus(Double referralBonus) {
this.referralBonus = referralBonus;
}
}
EMPLOYEE WRAPPER
package com.accenture.lkm.dao;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.accenture.lkm.business.bean.EmployeeBean;
import com.accenture.lkm.entity.EmployeeEntity;
import com.accenture.lkm.entity.SkillSetEntity;
import com.accenture.lkm.exceptions.SkillLevelMismatchException;
@Repository
@Transactional(value = "txManager")
public class EmpDaoWrapper {
@Autowired
private EmployeeDAO employeeDAO;
@Autowired
private SkillSetDAO skillSetDAO;
public void addEmp(EmployeeBean bean) throws Exception {
double bonus = checkLevel(bean);
// TO-DO - Include statements to save the data into database
if (bonus != 0)
{
EmployeeEntity toEntity = convertBeanToEntity(bean);
toEntity.setReferralBonus(bonus);
toEntity = employeeDAO.save(toEntity);
}
else
{
throw new SkillLevelMismatchException("Skill level mismatch Exception");
}
}
public double checkLevel(EmployeeBean bean) {
SkillSetEntity skillSet = skillSetDAO.getSkillSet(bean.getCandSkill(),
bean.getCandLevel());
if (skillSet != null)
return skillSet.getBonus();
return 0;
}
public List<String> getSkills() {
return skillSetDAO.getSkills();
}
public List<EmployeeBean> getEmployeeDetails(Date fromDate, Date toDate) {
List<EmployeeEntity> employeeDetails = employeeDAO.getEmployeeDetails(fromDate,
toDate);
List<EmployeeBean> beans = new ArrayList<>();
employeeDetails.forEach(employeeEntity -> {
EmployeeBean toBean = convertEntityToBean(employeeEntity);
beans.add(toBean);
});
return beans;
}
// Utility Methods.......
public static EmployeeBean convertEntityToBean(EmployeeEntity entity) {
EmployeeBean employeeBean = new EmployeeBean();
BeanUtils.copyProperties(entity, employeeBean);
return employeeBean;
}
public static EmployeeEntity convertBeanToEntity(EmployeeBean bean) {
EmployeeEntity entity = new EmployeeEntity();
BeanUtils.copyProperties(bean, entity);
return entity;
}
}
EmployeeServiceImpl
package com.accenture.lkm.service;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.accenture.lkm.business.bean.EmployeeBean;
import com.accenture.lkm.dao.EmpDaoWrapper;
import com.accenture.lkm.exceptions.SkillLevelMismatchException;
@Service
public class EmployeeServiceImpl implements EmpService {
@Autowired
EmpDaoWrapper edao;
@Override
public void addEmp(EmployeeBean bean) throws Exception {
// TO-DO - Invoke the appropriate DAO method here
edao.addEmp(bean);
}
@Override
public List<String> getSkills() {
return edao.getSkills();
}
@Override
public List<EmployeeBean> getEmployeeDetails(Date fromDate, Date toDate) {
return edao.getEmployeeDetails(fromDate, toDate);
}
No comments:
Post a Comment