Search in sources :

Example 6 with ClassDaoException

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

the class ClassDaoImpl method addClass.

@Override
public Class addClass(Class _class, long teacherId, long subjectId, long sectionId) throws ClassException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (_class == null)
            throw new ClassDaoException("You tried to add class with a null value");
        final Query query = session.createQuery("from Class as a join a.subject as b join a.section as c " + "join a.teacher as d where b.id=:subjectId and c.id=:sectionId and d.id=:teacherId");
        query.setParameter("subjectId", subjectId);
        query.setParameter("sectionId", sectionId);
        query.setParameter("teacherId", teacherId);
        if (query.list().size() > 0) {
            throw new ClassDaoException("Unable to add a class. Something might wrong to your inputs");
        }
        _class.setTeacher(null);
        _class.setSubject(null);
        _class.setSection(null);
        if (teacherId != 0) {
            Teacher teacher = teacherDao.getTeacherById(teacherId);
            _class.setTeacher(teacher);
        }
        if (subjectId != 0) {
            Subject subject = subjectDao.getSubjectById(subjectId);
            _class.setSubject(subject);
        }
        if (sectionId != 0) {
            Section section = sectionDao.getSectionById(sectionId);
            _class.setSection(section);
        }
        _class = (Class) session.merge(_class);
        session.getTransaction().commit();
        session.close();
        return _class;
    } catch (ClassDaoException | TeacherException | SubjectException | SectionException e) {
        session.close();
        throw new ClassException(e.getMessage());
    }
}
Also used : ClassDaoException(com.remswork.project.alice.dao.exception.ClassDaoException) Query(org.hibernate.Query) Session(org.hibernate.Session)

Example 7 with ClassDaoException

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

the class ClassDaoImpl method getStudentById.

@Override
public Student getStudentById(long classId, long id) throws ClassException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Class _class = session.get(Class.class, classId);
        Student student = null;
        if (_class == null)
            throw new ClassDaoException("Class with id : " + classId + " does not exist");
        for (Student s : _class.getStudentList()) {
            if (s.getId() == id)
                student = s;
        }
        if (student == null)
            throw new ClassDaoException("Class's student with id : " + id + " does not exist");
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (ClassDaoException e) {
        session.close();
        throw new ClassException(e.getMessage());
    }
}
Also used : ClassDaoException(com.remswork.project.alice.dao.exception.ClassDaoException) Class(com.remswork.project.alice.model.Class) Session(org.hibernate.Session)

Example 8 with ClassDaoException

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

the class ClassDaoImpl method deleteClassById.

@Override
public Class deleteClassById(long id) throws ClassException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Class _class = session.get(Class.class, id);
        if (_class == null)
            throw new ClassDaoException("Class with id : " + id + " does not exist");
        _class.setTeacher(null);
        _class.setSubject(null);
        _class.setScheduleList(null);
        _class.setSection(null);
        _class.setStudentList(null);
        for (Object object : session.createQuery("from Activity where _class.id = '" + id + "'").list()) {
            Activity activity = (Activity) object;
            String hql = "delete from ActivityResult where activity.id = :activityId";
            Query query = session.createQuery(hql);
            query.setParameter("activityId", activity.getId());
            query.executeUpdate();
            session.delete(activity);
        }
        for (Object object : session.createQuery("from Assignment where _class.id = '" + id + "'").list()) {
            Assignment assignment = (Assignment) object;
            String hql = "delete from AssignmentResult where assignment.id = :assignmentId";
            Query query = session.createQuery(hql);
            query.setParameter("assignmentId", assignment.getId());
            query.executeUpdate();
            session.delete(assignment);
        }
        for (Object object : session.createQuery("from Attendance where _class.id = '" + id + "'").list()) {
            Attendance attendance = (Attendance) object;
            String hql = "delete from AttendanceResult where attendance.id = :attendanceId";
            Query query = session.createQuery(hql);
            query.setParameter("attendanceId", attendance.getId());
            query.executeUpdate();
            session.delete(attendance);
        }
        for (Object object : session.createQuery("from Exam where _class.id = '" + id + "'").list()) {
            Exam exam = (Exam) object;
            String hql = "delete from ExamResult where exam.id = :examId";
            Query query = session.createQuery(hql);
            query.setParameter("examId", exam.getId());
            query.executeUpdate();
            session.delete(exam);
        }
        for (Object object : session.createQuery("from Project where _class.id = '" + id + "'").list()) {
            Project project = (Project) object;
            String hql = "delete from ProjectResult where project.id = :projectId";
            Query query = session.createQuery(hql);
            query.setParameter("projectId", project.getId());
            query.executeUpdate();
            session.delete(project);
        }
        for (Object object : session.createQuery("from Quiz where _class.id = '" + id + "'").list()) {
            Quiz quiz = (Quiz) object;
            String hql = "delete from QuizResult where quiz.id = :quizId";
            Query query = session.createQuery(hql);
            query.setParameter("quizId", quiz.getId());
            query.executeUpdate();
            session.delete(quiz);
        }
        for (Object object : session.createQuery("from Recitation where _class.id = '" + id + "'").list()) {
            Recitation recitation = (Recitation) object;
            String hql = "delete from RecitationResult where recitation.id = :recitationId";
            Query query = session.createQuery(hql);
            query.setParameter("recitationId", recitation.getId());
            query.executeUpdate();
            session.delete(recitation);
        }
        String hql = "delete from Grade as G where G._class.id = :classId";
        Query query = session.createQuery(hql);
        query.setParameter("classId", id);
        query.executeUpdate();
        session.delete(_class);
        session.getTransaction().commit();
        session.close();
        return _class;
    } catch (ClassDaoException e) {
        session.close();
        throw new ClassException(e.getMessage());
    }
}
Also used : ClassDaoException(com.remswork.project.alice.dao.exception.ClassDaoException) Query(org.hibernate.Query) Class(com.remswork.project.alice.model.Class) Session(org.hibernate.Session)

Example 9 with ClassDaoException

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

the class ClassDaoImpl method addStudentById.

@Override
public Student addStudentById(long classId, long id) throws ClassException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Class _class = session.get(Class.class, classId);
        if (_class == null)
            throw new ClassDaoException("Class with id : " + classId + " does not exist");
        Student student = studentDao.getStudentById(id);
        for (Student s : _class.getStudentList()) {
            if (s.getId() == id)
                throw new ClassDaoException("Class's student with id : " + id + " already exist");
        }
        _class.getStudentList().add(student);
        session.merge(_class);
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (ClassDaoException | StudentException e) {
        session.close();
        throw new ClassException(e.getMessage());
    }
}
Also used : ClassDaoException(com.remswork.project.alice.dao.exception.ClassDaoException) Class(com.remswork.project.alice.model.Class) Session(org.hibernate.Session)

Example 10 with ClassDaoException

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

the class ClassDaoImpl method getClassList.

@Override
public List<Class> getClassList() throws ClassException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        List<Class> classList = new ArrayList<>();
        Query query = session.createQuery("from Class");
        for (Object classObject : query.list()) classList.add((Class) classObject);
        session.getTransaction().commit();
        session.close();
        return classList;
    } catch (ClassDaoException e) {
        session.close();
        throw new ClassException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) ClassDaoException(com.remswork.project.alice.dao.exception.ClassDaoException) ArrayList(java.util.ArrayList) Class(com.remswork.project.alice.model.Class) Session(org.hibernate.Session)

Aggregations

ClassDaoException (com.remswork.project.alice.dao.exception.ClassDaoException)19 Session (org.hibernate.Session)17 Class (com.remswork.project.alice.model.Class)15 Query (org.hibernate.Query)8 ArrayList (java.util.ArrayList)4 ClassException (com.remswork.project.alice.exception.ClassException)2 Schedule (com.remswork.project.alice.model.Schedule)2 Message (com.remswork.project.alice.model.support.Message)2 ScheduleException (com.remswork.project.alice.exception.ScheduleException)1 Student (com.remswork.project.alice.model.Student)1 ClassScheduleListResourceLinks (com.remswork.project.alice.resource.links.ClassScheduleListResourceLinks)1 ClassStudentListResourceLinks (com.remswork.project.alice.resource.links.ClassStudentListResourceLinks)1 HashSet (java.util.HashSet)1