Search in sources :

Example 1 with TeacherDaoException

use of com.remswork.project.alice.dao.exception.TeacherDaoException in project classify-system by anverliedoit.

the class TeacherDaoImpl method getTeacherList.

@Override
public List<Teacher> getTeacherList() throws TeacherException {
    final List<Teacher> teacherList = new ArrayList<>();
    Query query = null;
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        query = session.createQuery("from Teacher");
        for (Object teacherObj : query.list()) teacherList.add((Teacher) teacherObj);
        session.getTransaction().commit();
        session.close();
        return teacherList;
    } catch (TeacherDaoException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) Query(org.hibernate.Query) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) ArrayList(java.util.ArrayList) Session(org.hibernate.Session)

Example 2 with TeacherDaoException

use of com.remswork.project.alice.dao.exception.TeacherDaoException in project classify-system by anverliedoit.

the class TeacherDaoImpl method addTeacher.

@Override
public Teacher addTeacher(Teacher teacher, long departmentId) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (teacher == null)
            throw new TeacherDaoException("You tried to add teacher with a null value");
        if (teacher.getFirstName() == null)
            throw new TeacherDaoException("Teacher's first name is required");
        if (teacher.getFirstName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty first name");
        if (teacher.getLastName() == null)
            throw new TeacherDaoException("Teacher's last name is required");
        if (teacher.getLastName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty last name");
        if (teacher.getMiddleName() == null)
            throw new TeacherDaoException("Teacher's middle name is required");
        if (teacher.getMiddleName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty middle name");
        if (teacher.getEmail() == null)
            throw new TeacherDaoException("Teacher's email is required");
        if (teacher.getEmail().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty email");
        if (departmentId > 0) {
            Department department = departmentDao.getDepartmentById(departmentId);
            teacher.setDepartment(department);
        }
        UserDetail userDetail = new UserDetail();
        userDetail.setIsEnabled(true);
        userDetail.setRegistrationDate(new Date().now().toString());
        userDetail.setUsername(teacher.getEmail());
        userDetail.setPassword((teacher.getFirstName() + teacher.getLastName() + "123").toLowerCase());
        userDetail.setUserType(UserDetail.USER_TEACHER);
        teacher.setUserDetail(userDetail);
        teacher = (Teacher) session.merge(teacher);
        session.getTransaction().commit();
        session.close();
        return teacher;
    } catch (TeacherDaoException | DepartmentException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) DepartmentException(com.remswork.project.alice.exception.DepartmentException) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) Date(com.remswork.project.alice.model.support.Date) Session(org.hibernate.Session)

Example 3 with TeacherDaoException

use of com.remswork.project.alice.dao.exception.TeacherDaoException in project classify-system by anverliedoit.

the class TeacherDaoImpl method addTeacher.

@Override
public Teacher addTeacher(Teacher teacher) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (teacher == null)
            throw new TeacherDaoException("You tried to add teacher with a null value");
        if (teacher.getFirstName() == null)
            throw new TeacherDaoException("Teacher's first name is required");
        if (teacher.getFirstName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty first name");
        if (teacher.getLastName() == null)
            throw new TeacherDaoException("Teacher's last name is required");
        if (teacher.getLastName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty last name");
        if (teacher.getMiddleName() == null)
            throw new TeacherDaoException("Teacher's middle name is required");
        if (teacher.getMiddleName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty middle name");
        if (teacher.getEmail() == null)
            throw new TeacherDaoException("Teacher's email is required");
        if (teacher.getEmail().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty email");
        if (teacher.getDepartment() != null) {
            Department department = teacher.getDepartment();
            if (department.getName() == null)
                throw new TeacherDaoException("Teacher's department name is required");
            if (department.getName().trim().equals(""))
                throw new TeacherDaoException("Teacher's department can't have an empty name");
            if (department.getDescription() == null)
                throw new TeacherDaoException("Teacher's department description is required");
            if (department.getDescription().trim().equals(""))
                throw new TeacherDaoException("Teacher's department can't have an empty description");
        }
        UserDetail userDetail = new UserDetail();
        userDetail.setIsEnabled(true);
        userDetail.setRegistrationDate(new Date().now().toString());
        userDetail.setUsername(teacher.getEmail());
        userDetail.setPassword((teacher.getFirstName() + teacher.getLastName() + "123").toLowerCase());
        userDetail.setUserType(UserDetail.USER_TEACHER);
        teacher.setUserDetail(userDetail);
        session.persist(teacher);
        session.getTransaction().commit();
        session.close();
        return teacher;
    } catch (TeacherDaoException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) Date(com.remswork.project.alice.model.support.Date) Session(org.hibernate.Session)

Example 4 with TeacherDaoException

use of com.remswork.project.alice.dao.exception.TeacherDaoException in project classify-system by anverliedoit.

the class TeacherDaoImpl method updateTeacherById.

@Override
public Teacher updateTeacherById(long id, Teacher newTeacher, long departmentId) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (newTeacher == null)
            newTeacher = new Teacher();
        Teacher teacher = session.get(Teacher.class, id);
        if (teacher == null)
            throw new TeacherDaoException("Teacher with id : " + id + " does not exist.");
        if (!(newTeacher.getFirstName() != null ? newTeacher.getFirstName() : "").trim().isEmpty())
            teacher.setFirstName(newTeacher.getFirstName());
        if (!(newTeacher.getLastName() != null ? newTeacher.getLastName() : "").trim().isEmpty())
            teacher.setLastName(newTeacher.getLastName());
        if (!(newTeacher.getEmail() != null ? newTeacher.getEmail() : "").trim().isEmpty())
            teacher.setEmail(newTeacher.getEmail());
        if (!(newTeacher.getMiddleName() != null ? newTeacher.getMiddleName() : "").trim().isEmpty())
            teacher.setMiddleName(newTeacher.getMiddleName());
        if (departmentId > 0) {
            Department department = departmentDao.getDepartmentById(departmentId);
            if (department.getId() == (teacher.getDepartment() != null ? teacher.getDepartment().getId() : 0))
                throw new TeacherDaoException("Can't update teacher's department with same department");
            teacher.setDepartment(department);
            teacher = (Teacher) session.merge(teacher);
        }
        session.getTransaction().commit();
        session.close();
        return teacher;
    } catch (TeacherDaoException | DepartmentException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) DepartmentException(com.remswork.project.alice.exception.DepartmentException) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) Session(org.hibernate.Session)

Example 5 with TeacherDaoException

use of com.remswork.project.alice.dao.exception.TeacherDaoException in project classify-system by anverliedoit.

the class TeacherDaoImpl method getTeacherById.

@Override
public Teacher getTeacherById(long id) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Teacher teacher = session.get(Teacher.class, id);
        if (teacher == null)
            throw new TeacherDaoException("Teacher with id : " + id + " does not exist");
        session.getTransaction().commit();
        session.close();
        return teacher;
    } catch (TeacherDaoException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) Session(org.hibernate.Session)

Aggregations

TeacherDaoException (com.remswork.project.alice.dao.exception.TeacherDaoException)6 TeacherException (com.remswork.project.alice.exception.TeacherException)6 Session (org.hibernate.Session)6 DepartmentException (com.remswork.project.alice.exception.DepartmentException)2 Date (com.remswork.project.alice.model.support.Date)2 Query (org.hibernate.Query)2 Class (com.remswork.project.alice.model.Class)1 ArrayList (java.util.ArrayList)1