Search in sources :

Example 6 with SectionException

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

the class SectionDaoImpl method getSectionList.

@Override
public List<Section> getSectionList() throws SectionException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        List<Section> sectionList = new ArrayList<>();
        Query query = session.createQuery("from Section");
        for (Object sectionObj : query.list()) sectionList.add((Section) sectionObj);
        session.getTransaction().commit();
        session.close();
        return sectionList;
    } catch (SectionDaoException e) {
        session.close();
        throw new SectionException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) ArrayList(java.util.ArrayList) 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 7 with SectionException

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

the class SectionDaoImpl method deleteSectionById.

@Override
public Section deleteSectionById(long id) 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");
        // to avoid the constraints restriction we meed to remove section from the student that having the section
        Query studentQuery = session.createQuery("from Student");
        for (Object studentObj : studentQuery.list()) {
            Student student = (Student) studentObj;
            if (student.getSection() == null)
                continue;
            if (student.getSection().equals(section) || (student.getSection() != null ? student.getSection().getId() : 0) == section.getId()) {
                student.setSection(null);
            }
        }
        // to avoid the constraints restriction we meed to remove section from the class that having the section
        Query classQuery = session.createQuery("from Class");
        for (Object classObj : classQuery.list()) {
            Class _class = (Class) classObj;
            if (_class.getSection() == null)
                continue;
            if (_class.getSection().equals(section) || (_class.getSection() != null ? _class.getSection().getId() : 0) == section.getId()) {
                _class.setSection(null);
            }
        }
        section.setDepartment(null);
        session.delete(section);
        session.getTransaction().commit();
        session.close();
        return section;
    } catch (SectionDaoException e) {
        session.close();
        throw new SectionException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) Class(com.remswork.project.alice.model.Class) Student(com.remswork.project.alice.model.Student) 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 8 with SectionException

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

the class StudentDaoImpl method addStudent.

@Override
public Student addStudent(Student student, long sectionId) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (student == null)
            throw new StudentDaoException("You tried to add student with a null value");
        if (student.getStudentNumber() == 0)
            throw new StudentDaoException("Student's studentnumber is required");
        if (student.getFirstName() == null)
            throw new StudentDaoException("Student's first name is required");
        if (student.getFirstName().trim().equals(""))
            throw new StudentDaoException("Student can't have an empty first name");
        if (student.getLastName() == null)
            throw new StudentDaoException("Student's last name is required");
        if (student.getLastName().trim().equals(""))
            throw new StudentDaoException("Student can't have an empty last name");
        if (student.getMiddleName() == null)
            throw new StudentDaoException("Student's middle name is required");
        if (student.getMiddleName().trim().equals(""))
            throw new StudentDaoException("Student can't have an empty middle name");
        if (student.getGender() == null)
            throw new StudentDaoException("Student's gender is required");
        if (student.getGender().trim().equals(""))
            throw new StudentDaoException("Student can't have an empty gender");
        if (!(student.getGender().trim().equalsIgnoreCase("Male") || student.getGender().trim().equalsIgnoreCase("Female")))
            throw new StudentDaoException("Student's gender is invalid");
        if (student.getAge() == 0)
            throw new StudentDaoException("Student's age is required");
        if (student.getAge() <= 14 || student.getAge() > 60)
            throw new StudentDaoException("Student's age is invalid. " + "The minimum age is 14 and the maximum age is 60");
        if (sectionId > 0) {
            Section section = sectionDao.getSectionById(sectionId);
            student.setSection(section);
        } else
            student.setSection(null);
        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)

Example 9 with SectionException

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

the class SectionController method getSection.

@RequestMapping(value = "get", method = RequestMethod.POST)
public String getSection(@RequestParam("query") String query, ModelMap modelMap) {
    try {
        List<Department> departmentList = departmentService.getDepartmentList();
        List<Section> sectionList = new ArrayList<>();
        Section section = null;
        if (!query.trim().isEmpty()) {
            try {
                long id = Long.parseLong(query.trim());
                section = sectionService.getSectionById(id);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                section = null;
            } catch (SectionException e) {
                e.printStackTrace();
                section = null;
            }
            if (section != null)
                sectionList.add(section);
            else {
                for (Section t : sectionService.getSectionList()) {
                    if (t.getName().equals(query.trim())) {
                        sectionList.add(t);
                        continue;
                    }
                }
            }
        }
        if (sectionList.size() < 1) {
            sectionList = sectionService.getSectionList();
            modelMap.put("responseMessage", "No result found.");
        } else {
            modelMap.put("responseMessage", sectionList.size() + " Result found.");
        }
        modelMap.put("sectionList", sectionList);
        modelMap.put("departmentList", departmentList);
        return "section-table";
    } catch (SectionException | DepartmentException e) {
        e.printStackTrace();
        return "error";
    }
}
Also used : Department(com.remswork.project.alice.model.Department) DepartmentException(com.remswork.project.alice.exception.DepartmentException) ArrayList(java.util.ArrayList) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with SectionException

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

the class StudentController method updateStudentById.

@RequestMapping(value = "update", method = RequestMethod.POST)
public String updateStudentById(@RequestParam("id") long id, @RequestParam("studentNumber") long studentNumber, @RequestParam("firstName") String firstName, @RequestParam("middleName") String middleName, @RequestParam("lastName") String lastName, @RequestParam("gender") String gender, @RequestParam("age") int age, @RequestParam("sectionId") long sectionId, ModelMap modelMap) {
    try {
        Student newStudent = new Student(studentNumber, firstName, lastName, middleName, gender, age, "");
        try {
            studentService.updateStudentById(id, newStudent, sectionId);
        } catch (StudentException e) {
            try {
                e.printStackTrace();
                sectionId = 0;
                studentService.updateStudentById(id, newStudent, sectionId);
            } catch (StudentException e2) {
                e2.printStackTrace();
            }
        }
        List<Student> studentList = studentService.getStudentList();
        List<Section> sectionList = sectionService.getSectionList();
        modelMap.put("studentList", studentList);
        modelMap.put("sectionList", sectionList);
        return "student";
    } catch (StudentException | SectionException e) {
        e.printStackTrace();
        return "error";
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) Student(com.remswork.project.alice.model.Student) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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