Search in sources :

Example 21 with StudentException

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

the class StudentResource method addStudent.

@POST
public Response addStudent(Student student) {
    try {
        StudentResourceLinks resourceLinks = new StudentResourceLinks(uriInfo);
        SectionResourceLinks sectionResourceLinks = new SectionResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        student = studentService.addStudent(student, sectionId);
        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()));
        }
        return Response.status(Response.Status.CREATED).entity(student).build();
    } catch (StudentException e) {
        e.printStackTrace();
        Message message = new Message(400, "Bad Request", e.getMessage());
        return Response.status(Response.Status.BAD_REQUEST).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) Section(com.remswork.project.alice.model.Section) SectionResourceLinks(com.remswork.project.alice.resource.links.SectionResourceLinks)

Example 22 with StudentException

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

the class StudentResource method updateStudentById.

@PUT
@Path("{studentId}")
public Response updateStudentById(@PathParam("studentId") long id, Student newStudent) {
    try {
        StudentResourceLinks resourceLinks = new StudentResourceLinks(uriInfo);
        SectionResourceLinks sectionResourceLinks = new SectionResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        Student student = studentService.updateStudentById(id, newStudent, sectionId);
        student.addLink(resourceLinks.self(id));
        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(400, "Bad Request", e.getMessage());
        return Response.status(Response.Status.BAD_REQUEST).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) Student(com.remswork.project.alice.model.Student) Section(com.remswork.project.alice.model.Section) SectionResourceLinks(com.remswork.project.alice.resource.links.SectionResourceLinks)

Example 23 with StudentException

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

the class StudentResource method getStudentById.

@GET
@Path("{studentId}")
public Response getStudentById(@PathParam("studentId") long id) {
    try {
        StudentResourceLinks resourceLinks = new StudentResourceLinks(uriInfo);
        SectionResourceLinks sectionResourceLinks = new SectionResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        Student student = studentService.getStudentById(id);
        student.addLink(resourceLinks.self(id));
        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) Student(com.remswork.project.alice.model.Student) Section(com.remswork.project.alice.model.Section) SectionResourceLinks(com.remswork.project.alice.resource.links.SectionResourceLinks)

Example 24 with StudentException

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

the class StudentResource method deleteStudentById.

@DELETE
@Path("{studentId}")
public Response deleteStudentById(@PathParam("studentId") long id) {
    try {
        StudentResourceLinks resourceLinks = new StudentResourceLinks(uriInfo);
        SectionResourceLinks sectionResourceLinks = new SectionResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        Student student = studentService.deleteStudentById(id);
        student.addLink(resourceLinks.self(id));
        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(400, "Bad Request", e.getMessage());
        return Response.status(Response.Status.BAD_REQUEST).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) Student(com.remswork.project.alice.model.Student) Section(com.remswork.project.alice.model.Section) SectionResourceLinks(com.remswork.project.alice.resource.links.SectionResourceLinks)

Example 25 with StudentException

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

the class StudentServiceImpl method deleteStudentById.

@Override
public Student deleteStudentById(final long id) throws StudentException {
    try {
        return new AsyncTask<String, Student, Student>() {

            @Override
            protected Student doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("/").concat(String.valueOf(id));
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("DELETE");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        return gson.fromJson(jsonData, Student.class);
                    } else if (httpURLConnection.getResponseCode() == 400) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        Message message = gson.fromJson(jsonData, Message.class);
                        Log.i("ServiceTAG", "Service : Student");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new StudentException("Server Error");
                } catch (StudentException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }.execute((String) null).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StudentException(com.remswork.project.alice.exception.StudentException) Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL)

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