Search in sources :

Example 16 with ClassDaoException

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

the class ClassDaoImpl method getStudentList.

@Override
public Set<Student> getStudentList(long classId) 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");
        Set<Student> studentSet = _class.getStudentList();
        session.getTransaction().commit();
        session.close();
        return studentSet;
    } 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 17 with ClassDaoException

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

the class ClassDaoImpl method deleteStudentById.

@Override
public Student deleteStudentById(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) {
                _class.getStudentList().remove(s);
                student = s;
                break;
            }
        }
        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 18 with ClassDaoException

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

the class ClassDaoImpl method getClassListByStudentId.

@Override
public List<Class> getClassListByStudentId(long studentId) throws ClassException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        List<Class> classList = new ArrayList<>();
        Query query = session.createQuery("from Class as c join c.studentList as st where st.id = :studentId");
        query.setParameter("studentId", studentId);
        for (Object classObject : query.list()) classList.add((Class) ((Object[]) classObject)[0]);
        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)

Example 19 with ClassDaoException

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

the class ClassStudentListResource method addStudentById.

@POST
public Response addStudentById(@PathParam("classId") long classId) {
    try {
        ClassStudentListResourceLinks resourceLinks = new ClassStudentListResourceLinks(uriInfo);
        if (studentId == 0)
            throw new ClassDaoException("Query param : studentId is required");
        Student student = classService.addStudentById(classId, studentId);
        student.addLink(resourceLinks.self(classId, studentId));
        return Response.status(Response.Status.OK).entity(student).build();
    } catch (ClassException | ClassDaoException e) {
        e.printStackTrace();
        Message message = new Message(400, "Bad Request", e.getMessage());
        return Response.status(Response.Status.BAD_REQUEST).entity(message).build();
    }
}
Also used : ClassStudentListResourceLinks(com.remswork.project.alice.resource.links.ClassStudentListResourceLinks) ClassDaoException(com.remswork.project.alice.dao.exception.ClassDaoException) Message(com.remswork.project.alice.model.support.Message) ClassException(com.remswork.project.alice.exception.ClassException) Student(com.remswork.project.alice.model.Student)

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