Search in sources :

Example 6 with StudentDaoException

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

the class StudentDaoImpl method updateStudentById.

@Override
public Student updateStudentById(long id, Student newStudent, long sectionId) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Student student = session.get(Student.class, id);
        if (student == null)
            throw new StudentDaoException("Student with id : " + id + " does not exist.");
        if (newStudent == null)
            newStudent = new Student();
        if (newStudent.getStudentNumber() < 1)
            student.setStudentNumber(newStudent.getStudentNumber());
        if (!(newStudent.getFirstName() != null ? newStudent.getFirstName() : "").trim().isEmpty())
            student.setFirstName(newStudent.getFirstName());
        if (!(newStudent.getLastName() != null ? newStudent.getLastName() : "").trim().isEmpty())
            student.setLastName(newStudent.getLastName());
        if (!(newStudent.getMiddleName() != null ? newStudent.getMiddleName() : "").trim().isEmpty())
            student.setMiddleName(newStudent.getMiddleName());
        if (!(newStudent.getGender() != null ? newStudent.getGender() : "").trim().isEmpty()) {
            if (!(newStudent.getGender().trim().equalsIgnoreCase("Male") || newStudent.getGender().trim().equalsIgnoreCase("Female")))
                throw new StudentDaoException("Student's gender is invalid");
            student.setGender(newStudent.getGender());
        }
        if (newStudent.getAge() != 0) {
            if (newStudent.getAge() <= 14 || newStudent.getAge() > 60)
                throw new StudentDaoException("Student's age is invalid. " + "The minimum age is 14 and the maximum age is 60");
            student.setAge(newStudent.getAge());
        }
        if (sectionId > 0) {
            Section section = sectionDao.getSectionById(sectionId);
            if (section.getId() == (student.getSection() != null ? student.getSection().getId() : 0))
                throw new StudentDaoException("Can't update student's section with same section");
            student.setSection(section);
            student = (Student) session.merge(student);
        }
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException | SectionException e) {
        session.close();
        throw new StudentException(e.getMessage());
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) StudentDaoException(com.remswork.project.alice.dao.exception.StudentDaoException) SectionException(com.remswork.project.alice.exception.SectionException) Session(org.hibernate.Session)

Aggregations

StudentDaoException (com.remswork.project.alice.dao.exception.StudentDaoException)6 StudentException (com.remswork.project.alice.exception.StudentException)6 Session (org.hibernate.Session)6 Query (org.hibernate.Query)3 SectionException (com.remswork.project.alice.exception.SectionException)2 Class (com.remswork.project.alice.model.Class)1 ArrayList (java.util.ArrayList)1