Search in sources :

Example 1 with StudentException

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

the class StudentResource method getStudentList.

@GET
public Response getStudentList() {
    try {
        StudentResourceLinks resourceLinks = new StudentResourceLinks(uriInfo);
        SectionResourceLinks sectionResourceLinks = new SectionResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        List<Student> studentList = studentService.getStudentList();
        if (sn == null) {
            for (Student student : studentList) {
                student.addLink(resourceLinks.self(student.getId()));
                if (student.getSection() != null) {
                    Section section = student.getSection();
                    section.addLink(sectionResourceLinks.self(section.getId()));
                    if (section.getDepartment() != null)
                        section.getDepartment().addLink(departmentResourceLinks.self(section.getDepartment().getId()));
                }
            }
            GenericEntity<List<Student>> entity = new GenericEntity<List<Student>>(studentList) {
            };
            return Response.status(Response.Status.OK).entity(entity).build();
        } else {
            Student student = studentService.getStudentBySN(sn);
            if (student.getSection() != null) {
                Section section = student.getSection();
                section.addLink(sectionResourceLinks.self(section.getId()));
                if (section.getDepartment() != null) {
                    section.getDepartment().addLink(departmentResourceLinks.self(section.getDepartment().getId()));
                }
            }
            return Response.status(Response.Status.OK).entity(student).build();
        }
    } catch (StudentException e) {
        e.printStackTrace();
        Message message = new Message(404, "Not Found", e.getMessage());
        return Response.status(Response.Status.NOT_FOUND).entity(message).build();
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) Message(com.remswork.project.alice.model.support.Message) StudentResourceLinks(com.remswork.project.alice.resource.links.StudentResourceLinks) DepartmentResourceLinks(com.remswork.project.alice.resource.links.DepartmentResourceLinks) List(java.util.List) Student(com.remswork.project.alice.model.Student) Section(com.remswork.project.alice.model.Section) SectionResourceLinks(com.remswork.project.alice.resource.links.SectionResourceLinks)

Example 2 with StudentException

use of com.remswork.project.alice.exception.StudentException 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 3 with StudentException

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

the class StudentDaoImpl method deleteStudentById.

@Override
public Student deleteStudentById(long id) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Student student = session.get(Student.class, id);
        String[] table = new String[8];
        table[0] = ActivityResult.class.getSimpleName();
        table[1] = AssignmentResult.class.getSimpleName();
        table[2] = AttendanceResult.class.getSimpleName();
        table[3] = ExamResult.class.getSimpleName();
        table[4] = ProjectResult.class.getSimpleName();
        table[5] = QuizResult.class.getSimpleName();
        table[6] = RecitationResult.class.getSimpleName();
        table[7] = Grade.class.getSimpleName();
        if (student == null)
            throw new StudentDaoException("Student with id : " + id + " does not exist.");
        for (String cell : table) {
            String hql = "delete from ".concat(cell).concat(" where student.id = :studentId");
            Query query = session.createQuery(hql);
            query.setParameter("studentId", id);
            query.executeUpdate();
        }
        Query classQuery = session.createQuery("from Class");
        for (Object classObj : classQuery.list()) {
            Class _class = (Class) classObj;
            _class = session.get(Class.class, _class.getId());
            for (Student s : _class.getStudentList()) {
                if (s == null)
                    continue;
                if (s.getId() == id) {
                    _class.getStudentList().remove(s);
                    break;
                }
            }
        }
        String hql = "";
        student.setSection(null);
        session.delete(student);
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException e) {
        session.close();
        throw new StudentException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) StudentException(com.remswork.project.alice.exception.StudentException) Class(com.remswork.project.alice.model.Class) StudentDaoException(com.remswork.project.alice.dao.exception.StudentDaoException) Session(org.hibernate.Session)

Example 4 with StudentException

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

the class StudentDaoImpl method getStudentBySN.

public Student getStudentBySN(long sn) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Query query = session.createQuery("from Student as s where s.studentNumber=:sn");
        query.setParameter("sn", sn);
        Student student = null;
        if (query.list().size() > 0) {
            student = (Student) query.list().get(0);
        }
        if (student == null)
            throw new StudentDaoException("Student with sn : " + sn + " does not exist");
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException e) {
        session.close();
        throw new StudentException(e.getMessage());
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) Query(org.hibernate.Query) StudentDaoException(com.remswork.project.alice.dao.exception.StudentDaoException) Session(org.hibernate.Session)

Example 5 with StudentException

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

the class StudentDaoImpl method getStudentById.

@Override
public Student getStudentById(long id) 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");
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException 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) Session(org.hibernate.Session)

Aggregations

StudentException (com.remswork.project.alice.exception.StudentException)27 Message (com.remswork.project.alice.model.support.Message)17 Student (com.remswork.project.alice.model.Student)15 Section (com.remswork.project.alice.model.Section)8 AsyncTask (android.os.AsyncTask)6 Gson (com.google.gson.Gson)6 StudentDaoException (com.remswork.project.alice.dao.exception.StudentDaoException)6 StudentServiceException (com.remswork.project.alice.web.service.exception.StudentServiceException)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 HttpURLConnection (java.net.HttpURLConnection)6 URL (java.net.URL)6 ExecutionException (java.util.concurrent.ExecutionException)6 Client (javax.ws.rs.client.Client)6 WebTarget (javax.ws.rs.client.WebTarget)6 Response (javax.ws.rs.core.Response)6 Session (org.hibernate.Session)6 SectionException (com.remswork.project.alice.exception.SectionException)5 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)5 SectionResourceLinks (com.remswork.project.alice.resource.links.SectionResourceLinks)5