Search in sources :

Example 1 with ClassDaoException

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

the class ClassScheduleListResource method addScheduleById.

@POST
public Response addScheduleById(@PathParam("classId") long classId) {
    try {
        ClassScheduleListResourceLinks resourceLinks = new ClassScheduleListResourceLinks(uriInfo);
        if (scheduleId == 0)
            throw new ClassDaoException("Query param : scheduleId is required");
        Schedule schedule = classService.addScheduleById(classId, scheduleId);
        schedule.addLink(resourceLinks.self(classId, scheduleId));
        return Response.status(Response.Status.OK).entity(schedule).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 : ClassScheduleListResourceLinks(com.remswork.project.alice.resource.links.ClassScheduleListResourceLinks) ClassDaoException(com.remswork.project.alice.dao.exception.ClassDaoException) Message(com.remswork.project.alice.model.support.Message) Schedule(com.remswork.project.alice.model.Schedule) ClassException(com.remswork.project.alice.exception.ClassException)

Example 2 with ClassDaoException

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

the class ScheduleDaoImpl method getScheduleListByTeacherId.

@Override
public Set<Schedule> getScheduleListByTeacherId(long teacherId) throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Set<Schedule> scheduleSet = new HashSet<>();
        String hql = "from Class as c join c.scheduleList as s join c.teacher as t where t.id = :teacherId";
        Query query = session.createQuery(hql);
        query.setParameter("teacherId", teacherId);
        for (Object list : query.list()) {
            Object[] row = (Object[]) list;
            scheduleSet.add((Schedule) row[1]);
        }
        session.getTransaction().commit();
        session.close();
        return scheduleSet;
    } catch (ClassDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) ClassDaoException(com.remswork.project.alice.dao.exception.ClassDaoException) Schedule(com.remswork.project.alice.model.Schedule) ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) HashSet(java.util.HashSet)

Example 3 with ClassDaoException

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

the class ClassDaoImpl method getClassById.

@Override
public Class getClassById(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");
        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) Class(com.remswork.project.alice.model.Class) Session(org.hibernate.Session)

Example 4 with ClassDaoException

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

the class ClassDaoImpl method updateClassById.

@Override
public Class updateClassById(long id, Class newClass, long teacherId, long subjectId, long sectionId) 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");
        if (teacherId != 0) {
            Teacher teacher = teacherDao.getTeacherById(teacherId);
            if ((_class.getTeacher() != null ? _class.getTeacher().getId() : 0) == teacher.getId())
                throw new ClassDaoException("Can't update class's teacher with same teacher");
            _class.setTeacher(teacher);
        }
        if (subjectId != 0) {
            Subject subject = subjectDao.getSubjectById(subjectId);
            if ((_class.getSubject() != null ? _class.getSubject().getId() : 0) == subject.getId())
                throw new ClassDaoException("Can't update class's subject with same subject");
            _class.setSubject(subject);
        }
        if (sectionId != 0) {
            Section section = sectionDao.getSectionById(sectionId);
            if ((_class.getSection() != null ? _class.getSection().getId() : 0) == section.getId())
                throw new ClassDaoException("Can't update class's section with same section");
            _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) Class(com.remswork.project.alice.model.Class) Session(org.hibernate.Session)

Example 5 with ClassDaoException

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

the class ClassDaoImpl method deleteScheduleById.

@Override
public Schedule deleteScheduleById(long classId, long id) throws ClassException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Class _class = session.get(Class.class, classId);
        Schedule schedule = null;
        if (_class == null)
            throw new ClassDaoException("Class with id : " + classId + " does not exist");
        for (Schedule s : _class.getScheduleList()) {
            if (s.getId() == id) {
                _class.getScheduleList().remove(s);
                schedule = s;
                break;
            }
        }
        if (schedule == null)
            throw new ClassDaoException("Class's schedule with id : " + id + " does not exist");
        session.getTransaction().commit();
        session.close();
        return schedule;
    } 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)

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