Search in sources :

Example 21 with Subject

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

the class SubjectResource method getSubjectById.

@GET
@Path("{subjectId}")
public Response getSubjectById(@PathParam("subjectId") long id) {
    try {
        SubjectResourceLinks resourceLinks = new SubjectResourceLinks(uriInfo);
        Subject subject = subjectService.getSubjectById(id);
        subject.addLink(resourceLinks.self(id));
        return Response.status(Response.Status.OK).entity(subject).build();
    } catch (SubjectException e) {
        e.printStackTrace();
        Message message = new Message(404, "Not Found", e.getMessage());
        return Response.status(Response.Status.NOT_FOUND).entity(message).build();
    }
}
Also used : SubjectResourceLinks(com.remswork.project.alice.resource.links.SubjectResourceLinks) Message(com.remswork.project.alice.model.support.Message) SubjectException(com.remswork.project.alice.exception.SubjectException) Subject(com.remswork.project.alice.model.Subject)

Example 22 with Subject

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

the class SubjectResource method updateSubjectById.

@PUT
@Path("{subjectId}")
public Response updateSubjectById(@PathParam("subjectId") long id, Subject newSubject) {
    try {
        SubjectResourceLinks resourceLinks = new SubjectResourceLinks(uriInfo);
        Subject subject = subjectService.updateSubjectById(id, newSubject);
        subject.addLink(resourceLinks.self(id));
        return Response.status(Response.Status.OK).entity(subject).build();
    } catch (SubjectException e) {
        e.printStackTrace();
        Message message = new Message(400, "Bad Request", e.getMessage());
        return Response.status(Response.Status.BAD_REQUEST).entity(message).build();
    }
}
Also used : SubjectResourceLinks(com.remswork.project.alice.resource.links.SubjectResourceLinks) Message(com.remswork.project.alice.model.support.Message) SubjectException(com.remswork.project.alice.exception.SubjectException) Subject(com.remswork.project.alice.model.Subject)

Example 23 with Subject

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

the class SubjectResource method getSubjectList.

@GET
public Response getSubjectList(@QueryParam("teacherId") long teacherId, @QueryParam("studentId") long studentId) {
    try {
        List<Subject> subjectList;
        SubjectResourceLinks resourceLinks = new SubjectResourceLinks(uriInfo);
        if (teacherId != 0)
            subjectList = subjectService.getSubjectListByTeacherId(teacherId);
        else if (studentId != 0)
            subjectList = subjectService.getSubjectListByStudentId(studentId);
        else
            subjectList = subjectService.getSubjectList();
        for (Subject subject : subjectList) subject.addLink(resourceLinks.self(subject.getId()));
        GenericEntity<List<Subject>> entity = new GenericEntity<List<Subject>>(subjectList) {
        };
        return Response.status(Response.Status.OK).entity(entity).build();
    } catch (SubjectException e) {
        e.printStackTrace();
        Message message = new Message(404, "Not Found", e.getMessage());
        return Response.status(Response.Status.NOT_FOUND).entity(message).build();
    }
}
Also used : SubjectResourceLinks(com.remswork.project.alice.resource.links.SubjectResourceLinks) Message(com.remswork.project.alice.model.support.Message) SubjectException(com.remswork.project.alice.exception.SubjectException) List(java.util.List) Subject(com.remswork.project.alice.model.Subject)

Example 24 with Subject

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

the class SubjectServiceImpl method getSubjectListByTeacherId.

@Override
public List<Subject> getSubjectListByTeacherId(final long teacherId) throws SubjectException {
    final List<Subject> subjectList = new ArrayList<>();
    try {
        return new AsyncTask<String, List<Subject>, List<Subject>>() {

            @Override
            protected List<Subject> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("/").concat("1").concat("?teacherId=").concat(String.valueOf(teacherId));
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    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++) {
                            subjectList.add(gson.fromJson(jsonArray.get(ctr).toString(), Subject.class));
                        }
                        return subjectList;
                    } else if (httpURLConnection.getResponseCode() == 404) {
                        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 : Subject");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return subjectList;
                    } else
                        throw new SubjectException("Server Error");
                } catch (SubjectException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } catch (JSONException 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 : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AsyncTask(android.os.AsyncTask) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) SubjectException(com.remswork.project.alice.exception.SubjectException) JSONException(org.json.JSONException) IOException(java.io.IOException) Subject(com.remswork.project.alice.model.Subject) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) ExecutionException(java.util.concurrent.ExecutionException)

Example 25 with Subject

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

the class SubjectServiceImpl method getSubjectListByTeacherIdUnique.

public List<Subject> getSubjectListByTeacherIdUnique(final long teacherId) throws SubjectException {
    final List<Subject> subjectList = new ArrayList<>();
    try {
        return new AsyncTask<String, List<Subject>, List<Subject>>() {

            @Override
            protected List<Subject> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("/").concat("1/unique").concat("?teacherId=").concat(String.valueOf(teacherId));
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    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++) {
                            subjectList.add(gson.fromJson(jsonArray.get(ctr).toString(), Subject.class));
                        }
                        return subjectList;
                    } else if (httpURLConnection.getResponseCode() == 404) {
                        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 : Subject");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return subjectList;
                    } else
                        throw new SubjectException("Server Error");
                } catch (SubjectException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } catch (JSONException 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 : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AsyncTask(android.os.AsyncTask) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) SubjectException(com.remswork.project.alice.exception.SubjectException) JSONException(org.json.JSONException) IOException(java.io.IOException) Subject(com.remswork.project.alice.model.Subject) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Subject (com.remswork.project.alice.model.Subject)29 SubjectException (com.remswork.project.alice.exception.SubjectException)23 Message (com.remswork.project.alice.model.support.Message)20 SubjectServiceException (com.remswork.project.alice.web.service.exception.SubjectServiceException)9 Client (javax.ws.rs.client.Client)9 WebTarget (javax.ws.rs.client.WebTarget)9 Response (javax.ws.rs.core.Response)9 SubjectResourceLinks (com.remswork.project.alice.resource.links.SubjectResourceLinks)7 List (java.util.List)6 Teacher (com.remswork.project.alice.model.Teacher)5 ArrayList (java.util.ArrayList)5 AsyncTask (android.os.AsyncTask)4 Gson (com.google.gson.Gson)4 GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)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 JSONArray (org.json.JSONArray)4