Search in sources :

Example 26 with SectionException

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

the class SectionDaoImpl method updateSectionById.

@Override
public Section updateSectionById(long id, Section newSection, long departmentId) throws SectionException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Section section = session.get(Section.class, id);
        if (section == null)
            throw new SectionDaoException("Section with id : " + id + " does not exist");
        if (newSection == null)
            newSection = new Section();
        if (!(newSection.getName() != null ? newSection.getName() : "").trim().isEmpty())
            section.setName(newSection.getName().trim());
        if (departmentId > 0) {
            Department department = departmentDao.getDepartmentById(departmentId);
            if (department.getId() == (section.getDepartment() != null ? section.getDepartment().getId() : 0))
                throw new SectionDaoException("Can't update section's department with same department");
            section.setDepartment(department);
        }
        session.getTransaction().commit();
        session.close();
        return section;
    } catch (SectionDaoException | DepartmentException e) {
        session.close();
        throw new SectionException(e.getMessage());
    }
}
Also used : Department(com.remswork.project.alice.model.Department) DepartmentException(com.remswork.project.alice.exception.DepartmentException) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) SectionDaoException(com.remswork.project.alice.dao.exception.SectionDaoException) Session(org.hibernate.Session)

Example 27 with SectionException

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

the class SectionDaoImpl method addSection.

@Override
public Section addSection(Section section, long departmentId) throws SectionException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (section == null)
            throw new SectionDaoException("You tried to add section with a null value");
        if (section.getName() == null)
            throw new SectionDaoException("Section's first name is required");
        if (section.getName().trim().equals(""))
            throw new SectionDaoException("Section can't have an empty name");
        if (departmentId < 1)
            throw new SectionDaoException("Query param : departmentId is required");
        Department department = departmentDao.getDepartmentById(departmentId);
        section.setDepartment(department);
        section = (Section) session.merge(section);
        session.getTransaction().commit();
        session.close();
        return section;
    } catch (SectionDaoException | DepartmentException e) {
        session.close();
        throw new SectionException(e.getMessage());
    }
}
Also used : Department(com.remswork.project.alice.model.Department) DepartmentException(com.remswork.project.alice.exception.DepartmentException) SectionException(com.remswork.project.alice.exception.SectionException) SectionDaoException(com.remswork.project.alice.dao.exception.SectionDaoException) Session(org.hibernate.Session)

Example 28 with SectionException

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

the class StudentDaoImpl method updateStudentById.

@Override
public Student updateStudentById(long id, Student newStudent, long sectionId) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Student student = session.get(Student.class, id);
        if (student == null)
            throw new StudentDaoException("Student with id : " + id + " does not exist.");
        if (newStudent == null)
            newStudent = new Student();
        if (newStudent.getStudentNumber() < 1)
            student.setStudentNumber(newStudent.getStudentNumber());
        if (!(newStudent.getFirstName() != null ? newStudent.getFirstName() : "").trim().isEmpty())
            student.setFirstName(newStudent.getFirstName());
        if (!(newStudent.getLastName() != null ? newStudent.getLastName() : "").trim().isEmpty())
            student.setLastName(newStudent.getLastName());
        if (!(newStudent.getMiddleName() != null ? newStudent.getMiddleName() : "").trim().isEmpty())
            student.setMiddleName(newStudent.getMiddleName());
        if (!(newStudent.getGender() != null ? newStudent.getGender() : "").trim().isEmpty()) {
            if (!(newStudent.getGender().trim().equalsIgnoreCase("Male") || newStudent.getGender().trim().equalsIgnoreCase("Female")))
                throw new StudentDaoException("Student's gender is invalid");
            student.setGender(newStudent.getGender());
        }
        if (newStudent.getAge() != 0) {
            if (newStudent.getAge() <= 14 || newStudent.getAge() > 60)
                throw new StudentDaoException("Student's age is invalid. " + "The minimum age is 14 and the maximum age is 60");
            student.setAge(newStudent.getAge());
        }
        if (sectionId > 0) {
            Section section = sectionDao.getSectionById(sectionId);
            if (section.getId() == (student.getSection() != null ? student.getSection().getId() : 0))
                throw new StudentDaoException("Can't update student's section with same section");
            student.setSection(section);
            student = (Student) session.merge(student);
        }
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException | SectionException e) {
        session.close();
        throw new StudentException(e.getMessage());
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) StudentDaoException(com.remswork.project.alice.dao.exception.StudentDaoException) SectionException(com.remswork.project.alice.exception.SectionException) Session(org.hibernate.Session)

Aggregations

SectionException (com.remswork.project.alice.exception.SectionException)28 Section (com.remswork.project.alice.model.Section)20 Message (com.remswork.project.alice.model.support.Message)15 Session (org.hibernate.Session)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 AsyncTask (android.os.AsyncTask)5 Gson (com.google.gson.Gson)5 SectionDaoException (com.remswork.project.alice.dao.exception.SectionDaoException)5 DepartmentException (com.remswork.project.alice.exception.DepartmentException)5 StudentException (com.remswork.project.alice.exception.StudentException)5 Department (com.remswork.project.alice.model.Department)5 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)5 SectionResourceLinks (com.remswork.project.alice.resource.links.SectionResourceLinks)5 SectionServiceException (com.remswork.project.alice.web.service.exception.SectionServiceException)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 HttpURLConnection (java.net.HttpURLConnection)5 URL (java.net.URL)5 ExecutionException (java.util.concurrent.ExecutionException)5 Client (javax.ws.rs.client.Client)5