Search in sources :

Example 1 with DepartmentDaoException

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

the class DepartmentDaoImpl method updateDepartmentById.

@Override
public Department updateDepartmentById(long id, Department newDepartment) throws DepartmentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Department department = session.get(Department.class, id);
        if (department == null)
            throw new DepartmentDaoException("Department with id : " + id + " does not exist.");
        if (newDepartment == null)
            throw new DepartmentDaoException("You tried to update department with a null value");
        if (!(newDepartment.getName() != null ? newDepartment.getName().trim() : "").isEmpty())
            department.setName(newDepartment.getName());
        if (!(newDepartment.getDescription() != null ? newDepartment.getDescription().trim() : "").isEmpty())
            department.setDescription(newDepartment.getDescription());
        session.getTransaction().commit();
        session.close();
        return department;
    } catch (DepartmentDaoException e) {
        session.close();
        throw new DepartmentException(e.getMessage());
    }
}
Also used : Department(com.remswork.project.alice.model.Department) DepartmentException(com.remswork.project.alice.exception.DepartmentException) DepartmentDaoException(com.remswork.project.alice.dao.exception.DepartmentDaoException) Session(org.hibernate.Session)

Example 2 with DepartmentDaoException

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

the class DepartmentDaoImpl method getDepartmentList.

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

Example 3 with DepartmentDaoException

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

the class DepartmentDaoImpl method addDepartment.

@Override
public Department addDepartment(Department department) throws DepartmentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (department == null)
            throw new DepartmentDaoException("You tried to add department with a null value");
        if (department.getName() == null)
            throw new DepartmentDaoException("Department's name is required");
        if (department.getName().trim().equals(""))
            throw new DepartmentDaoException("Department can't have an empty name");
        if (department.getDescription() == null)
            throw new DepartmentDaoException("Department's description is required");
        if (department.getDescription().trim().equals(""))
            throw new DepartmentDaoException("Department can't have an empty description");
        session.save(department);
        session.getTransaction().commit();
        session.close();
        return department;
    } catch (DepartmentDaoException e) {
        session.close();
        throw new DepartmentException(e.getMessage());
    }
}
Also used : DepartmentException(com.remswork.project.alice.exception.DepartmentException) DepartmentDaoException(com.remswork.project.alice.dao.exception.DepartmentDaoException) Session(org.hibernate.Session)

Example 4 with DepartmentDaoException

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

the class DepartmentDaoImpl method getDepartmentById.

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

Example 5 with DepartmentDaoException

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

the class DepartmentDaoImpl method deleteDepartmentById.

@Override
public Department deleteDepartmentById(long id) throws DepartmentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Department department = session.get(Department.class, id);
        if (department == null)
            throw new DepartmentDaoException("Department with id : " + id + " does not exist.");
        // to avoid the constraints restriction we meed to remove department from the teacher
        // that having the department
        Query teacherQuery = session.createQuery("from Teacher");
        for (Object teacherObj : teacherQuery.list()) {
            Teacher teacher = (Teacher) teacherObj;
            if (teacher.getDepartment() == null)
                continue;
            if (teacher.getDepartment().equals(department) || (teacher.getDepartment() != null ? teacher.getDepartment().getId() : 0) == department.getId()) {
                teacher.setDepartment(null);
            }
        }
        // to avoid the constraints restriction we meed to delete the section that having the department
        Query sectionQuery = session.createQuery("from Section");
        for (Object sectionObj : sectionQuery.list()) {
            Section section = (Section) sectionObj;
            if (section.getDepartment() == null)
                continue;
            if (section.getDepartment().equals(department) || (section.getDepartment() != null ? section.getDepartment().getId() : 0) == department.getId()) {
                session.delete(section);
            }
        }
        // to avoid the constraints restriction we meed to remove department from the class's teacher and section
        // that having the department
        Query classQuery = session.createQuery("from Class");
        for (Object classObj : classQuery.list()) {
            Class _class = (Class) classObj;
            Teacher teacher = _class.getTeacher();
            Section section = _class.getSection();
            if (teacher != null) {
                if (teacher.getDepartment() != null) {
                    if (teacher.getDepartment().equals(department) || (teacher.getDepartment() != null ? teacher.getDepartment().getId() : 0) == department.getId()) {
                        teacher.setDepartment(null);
                    }
                }
            }
            if (section != null) {
                if (section.getDepartment() != null) {
                    if (section.getDepartment().equals(department) || (section.getDepartment() != null ? section.getDepartment().getId() : 0) == department.getId()) {
                        _class.setSection(null);
                    }
                }
            }
        }
        session.delete(department);
        session.getTransaction().commit();
        session.close();
        return department;
    } catch (DepartmentDaoException e) {
        session.close();
        throw new DepartmentException(e.getMessage());
    }
}
Also used : Department(com.remswork.project.alice.model.Department) Query(org.hibernate.Query) DepartmentException(com.remswork.project.alice.exception.DepartmentException) Teacher(com.remswork.project.alice.model.Teacher) Class(com.remswork.project.alice.model.Class) DepartmentDaoException(com.remswork.project.alice.dao.exception.DepartmentDaoException) Section(com.remswork.project.alice.model.Section) Session(org.hibernate.Session)

Aggregations

DepartmentDaoException (com.remswork.project.alice.dao.exception.DepartmentDaoException)5 DepartmentException (com.remswork.project.alice.exception.DepartmentException)5 Session (org.hibernate.Session)5 Department (com.remswork.project.alice.model.Department)4 Query (org.hibernate.Query)2 Class (com.remswork.project.alice.model.Class)1 Section (com.remswork.project.alice.model.Section)1 Teacher (com.remswork.project.alice.model.Teacher)1 ArrayList (java.util.ArrayList)1