Search in sources :

Example 1 with StudentDaoException

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

the class StudentDaoImpl method addStudent.

@Override
public Student addStudent(Student student, long sectionId) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (student == null)
            throw new StudentDaoException("You tried to add student with a null value");
        if (student.getStudentNumber() == 0)
            throw new StudentDaoException("Student's studentnumber is required");
        if (student.getFirstName() == null)
            throw new StudentDaoException("Student's first name is required");
        if (student.getFirstName().trim().equals(""))
            throw new StudentDaoException("Student can't have an empty first name");
        if (student.getLastName() == null)
            throw new StudentDaoException("Student's last name is required");
        if (student.getLastName().trim().equals(""))
            throw new StudentDaoException("Student can't have an empty last name");
        if (student.getMiddleName() == null)
            throw new StudentDaoException("Student's middle name is required");
        if (student.getMiddleName().trim().equals(""))
            throw new StudentDaoException("Student can't have an empty middle name");
        if (student.getGender() == null)
            throw new StudentDaoException("Student's gender is required");
        if (student.getGender().trim().equals(""))
            throw new StudentDaoException("Student can't have an empty gender");
        if (!(student.getGender().trim().equalsIgnoreCase("Male") || student.getGender().trim().equalsIgnoreCase("Female")))
            throw new StudentDaoException("Student's gender is invalid");
        if (student.getAge() == 0)
            throw new StudentDaoException("Student's age is required");
        if (student.getAge() <= 14 || student.getAge() > 60)
            throw new StudentDaoException("Student's age is invalid. " + "The minimum age is 14 and the maximum age is 60");
        if (sectionId > 0) {
            Section section = sectionDao.getSectionById(sectionId);
            student.setSection(section);
        } else
            student.setSection(null);
        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)

Example 2 with StudentDaoException

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

the class StudentDaoImpl method deleteStudentById.

@Override
public Student deleteStudentById(long id) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Student student = session.get(Student.class, id);
        String[] table = new String[8];
        table[0] = ActivityResult.class.getSimpleName();
        table[1] = AssignmentResult.class.getSimpleName();
        table[2] = AttendanceResult.class.getSimpleName();
        table[3] = ExamResult.class.getSimpleName();
        table[4] = ProjectResult.class.getSimpleName();
        table[5] = QuizResult.class.getSimpleName();
        table[6] = RecitationResult.class.getSimpleName();
        table[7] = Grade.class.getSimpleName();
        if (student == null)
            throw new StudentDaoException("Student with id : " + id + " does not exist.");
        for (String cell : table) {
            String hql = "delete from ".concat(cell).concat(" where student.id = :studentId");
            Query query = session.createQuery(hql);
            query.setParameter("studentId", id);
            query.executeUpdate();
        }
        Query classQuery = session.createQuery("from Class");
        for (Object classObj : classQuery.list()) {
            Class _class = (Class) classObj;
            _class = session.get(Class.class, _class.getId());
            for (Student s : _class.getStudentList()) {
                if (s == null)
                    continue;
                if (s.getId() == id) {
                    _class.getStudentList().remove(s);
                    break;
                }
            }
        }
        String hql = "";
        student.setSection(null);
        session.delete(student);
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException e) {
        session.close();
        throw new StudentException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) StudentException(com.remswork.project.alice.exception.StudentException) Class(com.remswork.project.alice.model.Class) StudentDaoException(com.remswork.project.alice.dao.exception.StudentDaoException) Session(org.hibernate.Session)

Example 3 with StudentDaoException

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

the class StudentDaoImpl method getStudentBySN.

public Student getStudentBySN(long sn) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Query query = session.createQuery("from Student as s where s.studentNumber=:sn");
        query.setParameter("sn", sn);
        Student student = null;
        if (query.list().size() > 0) {
            student = (Student) query.list().get(0);
        }
        if (student == null)
            throw new StudentDaoException("Student with sn : " + sn + " does not exist");
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException e) {
        session.close();
        throw new StudentException(e.getMessage());
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) Query(org.hibernate.Query) StudentDaoException(com.remswork.project.alice.dao.exception.StudentDaoException) Session(org.hibernate.Session)

Example 4 with StudentDaoException

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

the class StudentDaoImpl method getStudentById.

@Override
public Student getStudentById(long id) 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");
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException 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) Session(org.hibernate.Session)

Example 5 with StudentDaoException

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

the class StudentDaoImpl method getStudentList.

@Override
public List<Student> getStudentList() throws StudentException {
    List<Student> studentList = new ArrayList<>();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Query query = session.createQuery("from Student");
        for (Object studentObj : query.list()) studentList.add((Student) studentObj);
        session.getTransaction().commit();
        session.close();
        return studentList;
    } catch (StudentDaoException e) {
        session.close();
        throw new StudentException(e.getMessage());
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) Query(org.hibernate.Query) ArrayList(java.util.ArrayList) StudentDaoException(com.remswork.project.alice.dao.exception.StudentDaoException) 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