Search in sources :

Example 6 with SubjectDaoException

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

the class SubjectDaoImpl method getSubjectListByTeacherId.

@Override
public List<Subject> getSubjectListByTeacherId(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";
        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 7 with SubjectDaoException

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

the class SubjectDaoImpl method updateSubjectById.

@Override
public Subject updateSubjectById(long id, Subject newSubject) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Subject subject = session.get(Subject.class, id);
        if (newSubject == null)
            throw new SubjectDaoException("You tried to update subject with a null value");
        if (subject == null)
            throw new SubjectDaoException("SubjectDto with id : " + id + " does not exist");
        if (!(newSubject.getName() != null ? newSubject.getName() : "").trim().isEmpty())
            subject.setName(newSubject.getName());
        if (!(newSubject.getDescription() != null ? newSubject.getDescription() : "").trim().isEmpty())
            subject.setDescription(newSubject.getDescription());
        if (!(newSubject.getCode() != null ? newSubject.getCode() : "").trim().isEmpty())
            subject.setCode(newSubject.getCode());
        if (newSubject.getUnit() != 0) {
            if (newSubject.getUnit() < 0)
                throw new SubjectDaoException("SubjectDto's unit is invalid");
            subject.setUnit(newSubject.getUnit());
        }
        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 8 with SubjectDaoException

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

the class SubjectDaoImpl method getSubjectByClassAndTeacherId.

@Override
public Subject getSubjectByClassAndTeacherId(long classId, long teacherId) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        String hql = "from Class as c join c.subject as su join c.teacher as t where c.id = :classId and " + "t.id = :teacherId";
        Query query = session.createQuery(hql);
        query.setParameter("classId", classId);
        query.setParameter("teacherId", teacherId);
        if (query.list().size() < 1)
            throw new SubjectDaoException("No subject found with teacherId : " + teacherId);
        Subject subject = (Subject) ((Object[]) query.list().get(0))[1];
        session.getTransaction().commit();
        session.close();
        return subject;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Example 9 with SubjectDaoException

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

the class SubjectDaoImpl method getSubjectByScheduleId.

@Override
public Subject getSubjectByScheduleId(long scheduleId) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        String hql = "from Class as c join c.subject as su join c.scheduleList as sc where sc.id = :scheduleId";
        Query query = session.createQuery(hql);
        query.setParameter("scheduleId", scheduleId);
        if (query.list().size() < 1)
            throw new SubjectDaoException("No subject found with scheduleId : " + scheduleId);
        Subject subject = (Subject) ((Object[]) query.list().get(0))[1];
        session.getTransaction().commit();
        session.close();
        return subject;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Example 10 with SubjectDaoException

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

the class SubjectDaoImpl method deleteSubjectById.

@Override
public Subject deleteSubjectById(long id) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        String[] table = new String[2];
        table[0] = Class.class.getSimpleName();
        table[1] = Formula.class.getSimpleName();
        Subject subject = session.get(Subject.class, id);
        if (subject == null)
            throw new SubjectDaoException("SubjectDto with id : " + id + " does not exist");
        for (String cell : table) {
            String hql = "delete from ".concat(cell).concat(" as a where a.subject.id = :subjectId");
            Query query = session.createQuery(hql);
            query.setParameter("subjectId", id);
            query.executeUpdate();
        }
        session.delete(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) Query(org.hibernate.Query) SubjectException(com.remswork.project.alice.exception.SubjectException) Class(com.remswork.project.alice.model.Class) 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