I am trying to create an application where I need to know where to put the logic. Here is the code
HomeController.java
@Controller
public class HomeController {
private EmployeeService employeeService;
@Autowired(required=true)
@Qualifier(value="employeeService")
public void setEmployeeService(EmployeeService employeeService)
{
this.employeeService = employeeService;
}
@RequestMapping("/login")
public ModelAndView loginWorld(@ModelAttribute("loginForm") Employee employee, ModelMap model){
String result="", returnPage="";
if(this.employeeService.validateLogin(employee.getUsername(), employee.getPassword())){
result = "Welcome " + employee.getUsername();
returnPage = "home";
}
else{
result = "<BR>Login Failure";
returnPage = "index";
}
model.addAttribute("result", result);
return new ModelAndView(returnPage, model);
}
}
EmployeeServiceImpl.java
@Service
public class EmployeeServiceImpl implements EmployeeService {
/* (non-Javadoc)
* @see com.smart.service.EmployeeService#validateLogin(java.lang.String, java.lang.String)
*/
EmployeeDAO employeeDAO;
public void setEmployeeDAO(EmployeeDAO employeeDAO)
{
this.employeeDAO = employeeDAO;
}
@Override
public boolean validateLogin(String username, String password) {
// TODO Auto-generated method stub
return this.employeeDAO.validateLogin(username, password);
}
}
EmployeeDAOImpl.java
@Transactional(value="myTransactionManager")
public class EmployeeDaoImpl implements EmployeeDAO {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@Override
public boolean validateLogin(String username, String password) {
return (long)this.sessionFactory.getCurrentSession().createQuery("SELECT COUNT(*) FROM EmployeeInfo WHERE username = :username AND password = :password")
.setString("username", username)
.setString("password", password).uniqueResult() > 0;
}
}
I would like to know where to put the logic of the code, i.e. in Controller class, Service class or in the model class.
In the above code, I put validateLogin()
to validate user in the controller class. I also want to put some more logic like checking if the data got from the textbox is null or If the user trying to login already logged in i.e. session already exist.
In which file do I put these logics?