Search in sources :

Example 6 with Section

use of com.remswork.project.alice.model.Section 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 Section

use of com.remswork.project.alice.model.Section 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 Section

use of com.remswork.project.alice.model.Section 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 9 with Section

use of com.remswork.project.alice.model.Section 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)

Example 10 with Section

use of com.remswork.project.alice.model.Section in project classify-system by anverliedoit.

the class SectionServiceImpl method deleteSectionById.

@Override
public Section deleteSectionById(long id) throws SectionException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        uri.append("/");
        uri.append(id);
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Builder builder = target.request();
        builder.accept("application/json");
        Response response = builder.delete();
        if (response.getStatus() == 200) {
            return (Section) response.readEntity(Section.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new SectionServiceException(message.getMessage());
        } else
            throw new SectionServiceException("The request might invalid or server is down");
    } catch (SectionServiceException e) {
        throw new SectionException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) Message(com.remswork.project.alice.model.support.Message) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) SectionServiceException(com.remswork.project.alice.web.service.exception.SectionServiceException)

Aggregations

Section (com.remswork.project.alice.model.Section)27 SectionException (com.remswork.project.alice.exception.SectionException)20 Message (com.remswork.project.alice.model.support.Message)15 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)9 SectionResourceLinks (com.remswork.project.alice.resource.links.SectionResourceLinks)9 StudentException (com.remswork.project.alice.exception.StudentException)8 Student (com.remswork.project.alice.model.Student)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 DepartmentException (com.remswork.project.alice.exception.DepartmentException)5 Department (com.remswork.project.alice.model.Department)5 StudentResourceLinks (com.remswork.project.alice.resource.links.StudentResourceLinks)5 SectionServiceException (com.remswork.project.alice.web.service.exception.SectionServiceException)5 Client (javax.ws.rs.client.Client)5 WebTarget (javax.ws.rs.client.WebTarget)5 Response (javax.ws.rs.core.Response)5 Session (org.hibernate.Session)5 SectionDaoException (com.remswork.project.alice.dao.exception.SectionDaoException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 ClientBuilder (javax.ws.rs.client.ClientBuilder)3