Search in sources :

Example 21 with DepartmentException

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

the class DepartmentDaoImpl method deleteDepartmentById.

@Override
public Department deleteDepartmentById(long id) throws DepartmentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Department department = session.get(Department.class, id);
        if (department == null)
            throw new DepartmentDaoException("Department with id : " + id + " does not exist.");
        // to avoid the constraints restriction we meed to remove department from the teacher
        // that having the department
        Query teacherQuery = session.createQuery("from Teacher");
        for (Object teacherObj : teacherQuery.list()) {
            Teacher teacher = (Teacher) teacherObj;
            if (teacher.getDepartment() == null)
                continue;
            if (teacher.getDepartment().equals(department) || (teacher.getDepartment() != null ? teacher.getDepartment().getId() : 0) == department.getId()) {
                teacher.setDepartment(null);
            }
        }
        // to avoid the constraints restriction we meed to delete the section that having the department
        Query sectionQuery = session.createQuery("from Section");
        for (Object sectionObj : sectionQuery.list()) {
            Section section = (Section) sectionObj;
            if (section.getDepartment() == null)
                continue;
            if (section.getDepartment().equals(department) || (section.getDepartment() != null ? section.getDepartment().getId() : 0) == department.getId()) {
                session.delete(section);
            }
        }
        // to avoid the constraints restriction we meed to remove department from the class's teacher and section
        // that having the department
        Query classQuery = session.createQuery("from Class");
        for (Object classObj : classQuery.list()) {
            Class _class = (Class) classObj;
            Teacher teacher = _class.getTeacher();
            Section section = _class.getSection();
            if (teacher != null) {
                if (teacher.getDepartment() != null) {
                    if (teacher.getDepartment().equals(department) || (teacher.getDepartment() != null ? teacher.getDepartment().getId() : 0) == department.getId()) {
                        teacher.setDepartment(null);
                    }
                }
            }
            if (section != null) {
                if (section.getDepartment() != null) {
                    if (section.getDepartment().equals(department) || (section.getDepartment() != null ? section.getDepartment().getId() : 0) == department.getId()) {
                        _class.setSection(null);
                    }
                }
            }
        }
        session.delete(department);
        session.getTransaction().commit();
        session.close();
        return department;
    } catch (DepartmentDaoException e) {
        session.close();
        throw new DepartmentException(e.getMessage());
    }
}
Also used : Department(com.remswork.project.alice.model.Department) Query(org.hibernate.Query) DepartmentException(com.remswork.project.alice.exception.DepartmentException) Teacher(com.remswork.project.alice.model.Teacher) Class(com.remswork.project.alice.model.Class) DepartmentDaoException(com.remswork.project.alice.dao.exception.DepartmentDaoException) Section(com.remswork.project.alice.model.Section) Session(org.hibernate.Session)

Example 22 with DepartmentException

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

the class DepartmentServiceImpl method addDepartment.

@Override
public Department addDepartment(final Department department) throws DepartmentException {
    try {
        return new AsyncTask<String, Department, Department>() {

            @Override
            protected Department doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload);
                    Gson gson = new Gson();
                    URL url = new URL(link);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream os = httpURLConnection.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                    writer.write(gson.toJson(department));
                    writer.flush();
                    writer.close();
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 201) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        return gson.fromJson(jsonData, Department.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 : Department");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new DepartmentException("Server Error");
                } catch (DepartmentException 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 : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) HttpURLConnection(java.net.HttpURLConnection) DepartmentException(com.remswork.project.alice.exception.DepartmentException) OutputStreamWriter(java.io.OutputStreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with DepartmentException

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

the class DepartmentServiceImpl method updateDepartmentById.

@Override
public Department updateDepartmentById(final long id, final Department newDepartment) throws DepartmentException {
    try {
        return new AsyncTask<String, Department, Department>() {

            @Override
            protected Department doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("/").concat(String.valueOf(id)).concat("?departmentId=");
                    Gson gson = new Gson();
                    URL url = new URL(link);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("PUT");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream os = httpURLConnection.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                    writer.write(gson.toJson(newDepartment));
                    writer.flush();
                    writer.close();
                    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, Department.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 : Department");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new DepartmentException("Server Error");
                } catch (DepartmentException 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 : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) HttpURLConnection(java.net.HttpURLConnection) DepartmentException(com.remswork.project.alice.exception.DepartmentException) OutputStreamWriter(java.io.OutputStreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 24 with DepartmentException

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

the class DepartmentServiceImpl method getDepartmentList.

@Override
public List<Department> getDepartmentList() throws DepartmentException {
    final List<Department> departmentList = new ArrayList<>();
    try {
        return new AsyncTask<String, List<Department>, List<Department>>() {

            @Override
            protected List<Department> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload);
                    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++) {
                            departmentList.add(gson.fromJson(jsonArray.get(ctr).toString(), Department.class));
                        }
                        return departmentList;
                    } 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 : Department");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return departmentList;
                    } else
                        throw new DepartmentException("Server Error");
                } catch (DepartmentException 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) JSONException(org.json.JSONException) IOException(java.io.IOException) URL(java.net.URL) Department(com.remswork.project.alice.model.Department) HttpURLConnection(java.net.HttpURLConnection) DepartmentException(com.remswork.project.alice.exception.DepartmentException) ExecutionException(java.util.concurrent.ExecutionException)

Example 25 with DepartmentException

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

the class DepartmentServiceImpl method getDepartmentById.

@Override
public Department getDepartmentById(final long id) throws DepartmentException {
    try {
        return new AsyncTask<String, Department, Department>() {

            @Override
            protected Department 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("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;
                        }
                        return gson.fromJson(jsonData, Department.class);
                    } 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 : Department");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new DepartmentException("Server Error");
                } catch (DepartmentException 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) Message(com.remswork.project.alice.model.support.Message) DepartmentException(com.remswork.project.alice.exception.DepartmentException) 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

DepartmentException (com.remswork.project.alice.exception.DepartmentException)30 Department (com.remswork.project.alice.model.Department)22 Message (com.remswork.project.alice.model.support.Message)15 Session (org.hibernate.Session)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 AsyncTask (android.os.AsyncTask)5 Gson (com.google.gson.Gson)5 DepartmentDaoException (com.remswork.project.alice.dao.exception.DepartmentDaoException)5 SectionException (com.remswork.project.alice.exception.SectionException)5 Section (com.remswork.project.alice.model.Section)5 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)5 DepartmentServiceException (com.remswork.project.alice.web.service.exception.DepartmentServiceException)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 HttpURLConnection (java.net.HttpURLConnection)5 URL (java.net.URL)5 ExecutionException (java.util.concurrent.ExecutionException)5 Client (javax.ws.rs.client.Client)5 WebTarget (javax.ws.rs.client.WebTarget)5 Response (javax.ws.rs.core.Response)5