Search in sources :

Example 16 with TeacherException

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

the class TeacherResource method updateTeacherById.

@PUT
@Path("{teacherId}")
public Response updateTeacherById(@PathParam("teacherId") long id, Teacher newTeacher) {
    try {
        TeacherResourceLinks resourceLink = new TeacherResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        Teacher teacher;
        if (departmentId > 0)
            teacher = teacherService.updateTeacherById(id, newTeacher, departmentId);
        else
            teacher = teacherService.updateTeacherById(id, newTeacher);
        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 17 with TeacherException

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

the class TeacherResource method addTeacher.

@POST
public Response addTeacher(Teacher teacher) {
    try {
        TeacherResourceLinks resourceLink = new TeacherResourceLinks(uriInfo);
        DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
        if (departmentId > 0)
            teacher = teacherService.addTeacher(teacher, departmentId);
        else
            teacher = teacherService.addTeacher(teacher);
        teacher.addLink(resourceLink.self(teacher.getId()));
        if (teacher.getDepartment() != null)
            teacher.getDepartment().addLink(departmentResourceLinks.self(teacher.getDepartment().getId()));
        return Response.status(Response.Status.CREATED).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) DepartmentResourceLinks(com.remswork.project.alice.resource.links.DepartmentResourceLinks) TeacherResourceLinks(com.remswork.project.alice.resource.links.TeacherResourceLinks)

Example 18 with TeacherException

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

the class TeacherServiceImpl method addTeacher.

@Deprecated
@Override
public Teacher addTeacher(final Teacher teacher, final long departmentId) throws TeacherException {
    try {
        return new AsyncTask<Teacher, Teacher, Teacher>() {

            @Override
            protected Teacher doInBackground(Teacher... params) {
                try {
                    Gson gson = new Gson();
                    URL url = new URL(baseUri);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setDoOutput(true);
                    Uri.Builder builder = new Uri.Builder().appendQueryParameter("departmentId", String.valueOf(departmentId));
                    String query = builder.build().getEncodedQuery();
                    OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
                    writer.write(query);
                    writer.write(gson.toJson(teacher).toString());
                    writer.flush();
                    writer.close();
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 201) {
                        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() == 400) {
                        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(teacher).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) AsyncTask(android.os.AsyncTask) Teacher(com.remswork.project.alice.model.Teacher) Gson(com.google.gson.Gson) IOException(java.io.IOException) Uri(android.net.Uri) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) TeacherException(com.remswork.project.alice.exception.TeacherException) OutputStreamWriter(java.io.OutputStreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 19 with TeacherException

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

the class TeacherServiceImpl method addTeacher.

@Deprecated
@Override
public Teacher addTeacher(final Teacher teacher) throws TeacherException {
    try {
        if (teacher.getDepartment() != null) {
            teacher.getDepartment().setId(0);
        }
        return (Teacher) new AsyncTask<Teacher, Teacher, Teacher>() {

            @Override
            protected Teacher doInBackground(Teacher... params) {
                try {
                    Gson gson = new Gson();
                    URL url = new URL(baseUri);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setDoOutput(true);
                    OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
                    writer.write(gson.toJson(teacher).toString());
                    writer.flush();
                    writer.close();
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 201) {
                        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() == 400) {
                        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(teacher).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) OutputStreamWriter(java.io.OutputStreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 20 with TeacherException

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

the class TeacherDaoImpl method getTeacherById.

@Override
public Teacher getTeacherById(long id) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Teacher teacher = session.get(Teacher.class, id);
        if (teacher == null)
            throw new TeacherDaoException("Teacher with id : " + id + " does not exist");
        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) 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