/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jsf.entity;
import entity.Courses;
import entity.Enrollment;
import entity.User;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.context.FacesContext;
import jpa.controllers.EnrollmentJpaController;
import jpa.controllers.UserJpaController;
import jsf.util.JsfUtil;
/**
*
* @author atap
*/
public class UserController {
private User user = null;
private UserJpaController jpaController = null;
private UserConverter converter = null;
private CoursesController courseController = null;
private List<Courses> courseList = null;
public UserController() {
FacesContext context = FacesContext.getCurrentInstance();
jpaController = (UserJpaController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "userJpa");
converter = new UserConverter();
courseController = new CoursesController();
courseList = new ArrayList<Courses>();
}
public User getUser() {
if(user == null) {
user = (User)JsfUtil.getObjectFromRequestParameter("jsfcrud.currentUser", converter, null);
}
if(user == null) {
user = new User();
}
return user;
}
public Courses getEnrolledCourse() {
return course;
}
public String create() {
try {
user.setUserType('s');
user.setSysdate(new Date());
jpaController.create(user);
JsfUtil.addSuccessMessage("User was successfully created.");
} catch (Exception e) {
JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
return null;
}
return null;
}
public String updateUser() {
try {
jpaController.edit(user);
JsfUtil.addSuccessMessage("User information was successfully updated.");
} catch (Exception e) {
JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
return null;
}
return null;
}
public String validateUser()
{
String userEmail = user.getEmail();
String userPassword = user.getPassword();
user = jpaController.loginUser(userEmail, userPassword);
if(user != null)
return "loginUser";
else {
JsfUtil.addErrorMessage("User does not exist");
return "";
}
}
public Courses course;
public void setactionUser(Courses course)
{
this.course = course;
}
public String enrollCoursePage()
{
return "courseDetail";
}
public void enrollCourse()
{
Enrollment enrollment = new Enrollment();
enrollment.setUserId(user);
enrollment.setCourseId(course);
enrollment.setSysdate(new Date());
try {
EnrollmentJpaController jpaController = new EnrollmentJpaController();
jpaController.create(enrollment);
JsfUtil.addSuccessMessage("User successfully enrolled course.");
}
catch (Exception e) {
JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
}
}
public List<Courses> getEnrolledCourses() {
List<Enrollment> enrollList = user.getEnrollmentList();
courseList.clear();
for (Enrollment course : enrollList) {
courseList.add(course.getCourseId());
}
return courseList;
}
public String logOffUser()
{
return "";
//user = null;
//System.out.print("logged off");
//return "logoff";
}
}