Search in sources :

Example 1 with TeacherException

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

the class TeacherResource method getAll.

@GET
public Response getAll() {
    try {
        TeacherResourceLinks resourceLink = new TeacherResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        List<Teacher> teacherList = teacherService.getTeacherList();
        for (Teacher teacher : teacherList) {
            teacher.addLink(resourceLink.self(teacher.getId()));
            if (teacher.getDepartment() != null)
                teacher.getDepartment().addLink(departmentResourceLinks.self(teacher.getDepartment().getId()));
        }
        GenericEntity<List<Teacher>> entity = new GenericEntity<List<Teacher>>(teacherList) {
        };
        return Response.status(Response.Status.OK).entity(entity).build();
    } catch (TeacherException e) {
        e.printStackTrace();
        Message message = new Message(404, "Not Found", e.getMessage());
        return Response.status(Response.Status.NOT_FOUND).entity(message).build();
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) Message(com.remswork.project.alice.model.support.Message) Teacher(com.remswork.project.alice.model.Teacher) DepartmentResourceLinks(com.remswork.project.alice.resource.links.DepartmentResourceLinks) TeacherResourceLinks(com.remswork.project.alice.resource.links.TeacherResourceLinks) List(java.util.List)

Example 2 with TeacherException

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

the class TeacherResource method deleteTeacherById.

@DELETE
@Path("{teacherId}")
public Response deleteTeacherById(@PathParam("teacherId") long id) {
    try {
        TeacherResourceLinks resourceLink = new TeacherResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        Teacher teacher = teacherService.deleteTeacherById(id);
        teacher.addLink(resourceLink.self(id));
        if (teacher.getDepartment() != null)
            teacher.getDepartment().addLink(departmentResourceLinks.self(teacher.getDepartment().getId()));
        return Response.status(Response.Status.OK).entity(teacher).build();
    } catch (TeacherException e) {
        e.printStackTrace();
        Message message = new Message(400, "Bad Request", e.getMessage());
        return Response.status(Response.Status.BAD_REQUEST).entity(message).build();
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) Message(com.remswork.project.alice.model.support.Message) Teacher(com.remswork.project.alice.model.Teacher) DepartmentResourceLinks(com.remswork.project.alice.resource.links.DepartmentResourceLinks) TeacherResourceLinks(com.remswork.project.alice.resource.links.TeacherResourceLinks)

Example 3 with TeacherException

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

the class TeacherDaoImpl method getTeacherList.

@Override
public List<Teacher> getTeacherList() throws TeacherException {
    final List<Teacher> teacherList = new ArrayList<>();
    Query query = null;
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        query = session.createQuery("from Teacher");
        for (Object teacherObj : query.list()) teacherList.add((Teacher) teacherObj);
        session.getTransaction().commit();
        session.close();
        return teacherList;
    } catch (TeacherDaoException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) Query(org.hibernate.Query) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) ArrayList(java.util.ArrayList) Session(org.hibernate.Session)

Example 4 with TeacherException

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

the class TeacherDaoImpl method addTeacher.

@Override
public Teacher addTeacher(Teacher teacher, long departmentId) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (teacher == null)
            throw new TeacherDaoException("You tried to add teacher with a null value");
        if (teacher.getFirstName() == null)
            throw new TeacherDaoException("Teacher's first name is required");
        if (teacher.getFirstName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty first name");
        if (teacher.getLastName() == null)
            throw new TeacherDaoException("Teacher's last name is required");
        if (teacher.getLastName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty last name");
        if (teacher.getMiddleName() == null)
            throw new TeacherDaoException("Teacher's middle name is required");
        if (teacher.getMiddleName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty middle name");
        if (teacher.getEmail() == null)
            throw new TeacherDaoException("Teacher's email is required");
        if (teacher.getEmail().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty email");
        if (departmentId > 0) {
            Department department = departmentDao.getDepartmentById(departmentId);
            teacher.setDepartment(department);
        }
        UserDetail userDetail = new UserDetail();
        userDetail.setIsEnabled(true);
        userDetail.setRegistrationDate(new Date().now().toString());
        userDetail.setUsername(teacher.getEmail());
        userDetail.setPassword((teacher.getFirstName() + teacher.getLastName() + "123").toLowerCase());
        userDetail.setUserType(UserDetail.USER_TEACHER);
        teacher.setUserDetail(userDetail);
        teacher = (Teacher) session.merge(teacher);
        session.getTransaction().commit();
        session.close();
        return teacher;
    } catch (TeacherDaoException | DepartmentException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) DepartmentException(com.remswork.project.alice.exception.DepartmentException) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) Date(com.remswork.project.alice.model.support.Date) Session(org.hibernate.Session)

Example 5 with TeacherException

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

the class TeacherDaoImpl method addTeacher.

@Override
public Teacher addTeacher(Teacher teacher) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (teacher == null)
            throw new TeacherDaoException("You tried to add teacher with a null value");
        if (teacher.getFirstName() == null)
            throw new TeacherDaoException("Teacher's first name is required");
        if (teacher.getFirstName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty first name");
        if (teacher.getLastName() == null)
            throw new TeacherDaoException("Teacher's last name is required");
        if (teacher.getLastName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty last name");
        if (teacher.getMiddleName() == null)
            throw new TeacherDaoException("Teacher's middle name is required");
        if (teacher.getMiddleName().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty middle name");
        if (teacher.getEmail() == null)
            throw new TeacherDaoException("Teacher's email is required");
        if (teacher.getEmail().trim().equals(""))
            throw new TeacherDaoException("Teacher can't have an empty email");
        if (teacher.getDepartment() != null) {
            Department department = teacher.getDepartment();
            if (department.getName() == null)
                throw new TeacherDaoException("Teacher's department name is required");
            if (department.getName().trim().equals(""))
                throw new TeacherDaoException("Teacher's department can't have an empty name");
            if (department.getDescription() == null)
                throw new TeacherDaoException("Teacher's department description is required");
            if (department.getDescription().trim().equals(""))
                throw new TeacherDaoException("Teacher's department can't have an empty description");
        }
        UserDetail userDetail = new UserDetail();
        userDetail.setIsEnabled(true);
        userDetail.setRegistrationDate(new Date().now().toString());
        userDetail.setUsername(teacher.getEmail());
        userDetail.setPassword((teacher.getFirstName() + teacher.getLastName() + "123").toLowerCase());
        userDetail.setUserType(UserDetail.USER_TEACHER);
        teacher.setUserDetail(userDetail);
        session.persist(teacher);
        session.getTransaction().commit();
        session.close();
        return teacher;
    } catch (TeacherDaoException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) Date(com.remswork.project.alice.model.support.Date) Session(org.hibernate.Session)

Aggregations

TeacherException (com.remswork.project.alice.exception.TeacherException)21 Message (com.remswork.project.alice.model.support.Message)15 Teacher (com.remswork.project.alice.model.Teacher)14 TeacherDaoException (com.remswork.project.alice.dao.exception.TeacherDaoException)6 TeacherServiceException (com.remswork.project.alice.web.service.exception.TeacherServiceException)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 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)5 TeacherResourceLinks (com.remswork.project.alice.resource.links.TeacherResourceLinks)5 Gson (com.google.gson.Gson)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 ExecutionException (java.util.concurrent.ExecutionException)4 ClientBuilder (javax.ws.rs.client.ClientBuilder)4 Builder (javax.ws.rs.client.Invocation.Builder)4 AsyncTask (android.os.AsyncTask)2