Search in sources :

Example 1 with Teacher

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

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

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

the class TeacherServiceImpl method getTeacherById.

@Override
public Teacher getTeacherById(final long id) throws TeacherException {
    try {
        return (Teacher) new AsyncTask<Long, Teacher, Teacher>() {

            @Override
            protected Teacher doInBackground(Long... params) {
                try {
                    URL url = new URL(baseUri + "/" + params[0]);
                    Gson gson = new Gson();
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String stringData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            stringData += (char) data;
                        }
                        return gson.fromJson(stringData, Teacher.class);
                    } else if (httpURLConnection.getResponseCode() == 404) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String stringData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            stringData += (char) data;
                        }
                        Message message = gson.fromJson(stringData, Message.class);
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new TeacherException("Server Error");
                } catch (TeacherException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }.execute(id).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) Teacher(com.remswork.project.alice.model.Teacher) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) TeacherException(com.remswork.project.alice.exception.TeacherException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with Teacher

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

the class TeacherServiceImpl method getTeacherList.

@Override
public List<Teacher> getTeacherList() throws TeacherException {
    try {
        final List<Teacher> teacherList = new ArrayList<>();
        return new AsyncTask<String, List<Teacher>, List<Teacher>>() {

            @Override
            protected List<Teacher> doInBackground(String... params) {
                try {
                    Gson gson = new Gson();
                    URL url = new URL(baseUri);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    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;
                        }
                        JSONArray jsonArray = new JSONArray(jsonData);
                        for (int ctr = 0; ctr < jsonArray.length(); ctr++) {
                            teacherList.add(gson.fromJson(jsonArray.get(ctr).toString(), Teacher.class));
                        }
                        return teacherList;
                    } else if (httpURLConnection.getResponseCode() == 404) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        JSONArray jsonArray = new JSONArray(jsonData);
                        for (int ctr = 0; ctr < jsonArray.length(); ctr++) {
                            teacherList.add(gson.fromJson(jsonArray.get(ctr).toString(), Teacher.class));
                        }
                        Message message = gson.fromJson(jsonData, Message.class);
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return teacherList;
                    } else
                        throw new TeacherException("Server Error");
                } catch (IOException e1) {
                    e1.printStackTrace();
                    return teacherList;
                } catch (JSONException e1) {
                    e1.printStackTrace();
                    return teacherList;
                } catch (TeacherException e1) {
                    e1.printStackTrace();
                    return teacherList;
                }
            }
        }.execute("").get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return new ArrayList<Teacher>();
    } catch (ExecutionException e) {
        e.printStackTrace();
        return new ArrayList<Teacher>();
    }
}
Also used : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) Teacher(com.remswork.project.alice.model.Teacher) ArrayList(java.util.ArrayList) AsyncTask(android.os.AsyncTask) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) JSONException(org.json.JSONException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) TeacherException(com.remswork.project.alice.exception.TeacherException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with Teacher

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

the class TeacherServiceImpl method updateTeacherById.

@Override
public Teacher updateTeacherById(long id, Teacher newTeacher, long departmentId) throws TeacherException {
    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.queryParam("departmentId", departmentId).request();
        builder.accept("application/json");
        Response response = builder.put(Entity.json(newTeacher));
        if (response.getStatus() == 200) {
            return (Teacher) response.readEntity(Teacher.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new TeacherServiceException(message.getMessage());
        } else
            throw new TeacherServiceException("The request might invalid or server is down");
    } catch (TeacherServiceException e) {
        throw new TeacherException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) TeacherServiceException(com.remswork.project.alice.web.service.exception.TeacherServiceException) TeacherException(com.remswork.project.alice.exception.TeacherException) Message(com.remswork.project.alice.model.support.Message) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) Teacher(com.remswork.project.alice.model.Teacher) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Aggregations

Teacher (com.remswork.project.alice.model.Teacher)29 TeacherException (com.remswork.project.alice.exception.TeacherException)14 Message (com.remswork.project.alice.model.support.Message)14 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 Subject (com.remswork.project.alice.model.Subject)5 Gson (com.google.gson.Gson)4 TeacherHelper (com.lieverandiver.thesisproject.helper.TeacherHelper)4 GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)4 Department (com.remswork.project.alice.model.Department)4 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)4 TeacherResourceLinks (com.remswork.project.alice.resource.links.TeacherResourceLinks)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