Search in sources :

Example 1 with SubjectDaoException

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

the class SubjectDaoImpl method getSubjectById.

@Override
public Subject getSubjectById(long id) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Subject subject = session.get(Subject.class, id);
        if (subject == null)
            throw new SubjectDaoException("SubjectDto with id : " + id + " does not exist");
        session.getTransaction().commit();
        session.close();
        return subject;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Example 2 with SubjectDaoException

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

the class SubjectDaoImpl method getSubjectList.

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

Example 3 with SubjectDaoException

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

the class SubjectDaoImpl method addSubject.

@Override
public Subject addSubject(Subject subject) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (subject == null)
            throw new SubjectDaoException("You tried to add subject with a null value");
        if (subject.getName() == null)
            throw new SubjectDaoException("SubjectDto's name is required");
        if (subject.getName().trim().equals(""))
            throw new SubjectDaoException("SubjectDto can't have an empty name");
        if (subject.getDescription() == null)
            throw new SubjectDaoException("SubjectDto's description is required");
        if (subject.getDescription().trim().equals(""))
            throw new SubjectDaoException("SubjectDto can't have an empty description");
        if (subject.getCode() == null)
            throw new SubjectDaoException("SubjectDto's code is required");
        if (subject.getCode().trim().equals(""))
            throw new SubjectDaoException("SubjectDto can't have an empty code");
        if (subject.getUnit() == 0)
            throw new SubjectDaoException("SubjectDto's unit is required");
        if (subject.getUnit() < 0)
            throw new SubjectDaoException("SubjectDto's unit is invalid");
        session.persist(subject);
        session.getTransaction().commit();
        session.close();
        return subject;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Example 4 with SubjectDaoException

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

the class SubjectDaoImpl method getSubjectListByTeacherIdUnique.

public List<Subject> getSubjectListByTeacherIdUnique(long teacherId) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        List<Subject> subjectList = new ArrayList<>();
        String hql = "from Class as c join c.subject as s join c.teacher as t where t.id = :teacherId group by s.id";
        Query query = session.createQuery(hql);
        query.setParameter("teacherId", teacherId);
        for (Object list : query.list()) {
            Object[] row = (Object[]) list;
            subjectList.add((Subject) row[1]);
        }
        session.getTransaction().commit();
        session.close();
        return subjectList;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) ArrayList(java.util.ArrayList) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Example 5 with SubjectDaoException

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

the class SubjectDaoImpl method getSubjectListByStudentId.

@Override
public List<Subject> getSubjectListByStudentId(long studentId) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        List<Subject> subjectList = new ArrayList<>();
        String hql = "from Class as c join c.subject as su join c.studentList as st where st.id = :studentId";
        Query query = session.createQuery(hql);
        query.setParameter("studentId", studentId);
        for (Object list : query.list()) {
            Object[] row = (Object[]) list;
            subjectList.add((Subject) row[1]);
        }
        session.getTransaction().commit();
        session.close();
        return subjectList;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) ArrayList(java.util.ArrayList) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Aggregations

SubjectDaoException (com.remswork.project.alice.dao.exception.SubjectDaoException)10 SubjectException (com.remswork.project.alice.exception.SubjectException)10 Session (org.hibernate.Session)10 Query (org.hibernate.Query)7 ArrayList (java.util.ArrayList)4 Class (com.remswork.project.alice.model.Class)1