Search in sources :

Example 6 with TeacherException

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

the class TeacherDaoImpl method updateTeacherById.

@Override
public Teacher updateTeacherById(long id, Teacher newTeacher, long departmentId) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (newTeacher == null)
            newTeacher = new Teacher();
        Teacher teacher = session.get(Teacher.class, id);
        if (teacher == null)
            throw new TeacherDaoException("Teacher with id : " + id + " does not exist.");
        if (!(newTeacher.getFirstName() != null ? newTeacher.getFirstName() : "").trim().isEmpty())
            teacher.setFirstName(newTeacher.getFirstName());
        if (!(newTeacher.getLastName() != null ? newTeacher.getLastName() : "").trim().isEmpty())
            teacher.setLastName(newTeacher.getLastName());
        if (!(newTeacher.getEmail() != null ? newTeacher.getEmail() : "").trim().isEmpty())
            teacher.setEmail(newTeacher.getEmail());
        if (!(newTeacher.getMiddleName() != null ? newTeacher.getMiddleName() : "").trim().isEmpty())
            teacher.setMiddleName(newTeacher.getMiddleName());
        if (departmentId > 0) {
            Department department = departmentDao.getDepartmentById(departmentId);
            if (department.getId() == (teacher.getDepartment() != null ? teacher.getDepartment().getId() : 0))
                throw new TeacherDaoException("Can't update teacher's department with same department");
            teacher.setDepartment(department);
            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) Session(org.hibernate.Session)

Example 7 with TeacherException

use of com.remswork.project.alice.exception.TeacherException 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 8 with TeacherException

use of com.remswork.project.alice.exception.TeacherException 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 9 with TeacherException

use of com.remswork.project.alice.exception.TeacherException 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)

Example 10 with TeacherException

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

the class TeacherServiceImpl method getTeacherById.

@Override
public Teacher getTeacherById(long id) 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());
        Response response = target.request().get();
        if (response.getStatus() == 200) {
            return (Teacher) response.readEntity(Teacher.class);
        } else if (response.getStatus() == 404) {
            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) Teacher(com.remswork.project.alice.model.Teacher) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

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