Search in sources :

Example 26 with StudentException

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

the class StudentServiceImpl method addStudent.

@Override
public Student addStudent(final Student student, final long sectionId) throws StudentException {
    try {
        return new AsyncTask<String, Student, Student>() {

            @Override
            protected Student doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("?sectionId=").concat(String.valueOf(sectionId));
                    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(student));
                    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, Student.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 : Student");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new StudentException("Server Error");
                } catch (StudentException 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) StudentException(com.remswork.project.alice.exception.StudentException) OutputStreamWriter(java.io.OutputStreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 27 with StudentException

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

the class StudentDaoImpl method updateStudentById.

@Override
public Student updateStudentById(long id, Student newStudent, long sectionId) throws StudentException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Student student = session.get(Student.class, id);
        if (student == null)
            throw new StudentDaoException("Student with id : " + id + " does not exist.");
        if (newStudent == null)
            newStudent = new Student();
        if (newStudent.getStudentNumber() < 1)
            student.setStudentNumber(newStudent.getStudentNumber());
        if (!(newStudent.getFirstName() != null ? newStudent.getFirstName() : "").trim().isEmpty())
            student.setFirstName(newStudent.getFirstName());
        if (!(newStudent.getLastName() != null ? newStudent.getLastName() : "").trim().isEmpty())
            student.setLastName(newStudent.getLastName());
        if (!(newStudent.getMiddleName() != null ? newStudent.getMiddleName() : "").trim().isEmpty())
            student.setMiddleName(newStudent.getMiddleName());
        if (!(newStudent.getGender() != null ? newStudent.getGender() : "").trim().isEmpty()) {
            if (!(newStudent.getGender().trim().equalsIgnoreCase("Male") || newStudent.getGender().trim().equalsIgnoreCase("Female")))
                throw new StudentDaoException("Student's gender is invalid");
            student.setGender(newStudent.getGender());
        }
        if (newStudent.getAge() != 0) {
            if (newStudent.getAge() <= 14 || newStudent.getAge() > 60)
                throw new StudentDaoException("Student's age is invalid. " + "The minimum age is 14 and the maximum age is 60");
            student.setAge(newStudent.getAge());
        }
        if (sectionId > 0) {
            Section section = sectionDao.getSectionById(sectionId);
            if (section.getId() == (student.getSection() != null ? student.getSection().getId() : 0))
                throw new StudentDaoException("Can't update student's section with same section");
            student.setSection(section);
            student = (Student) session.merge(student);
        }
        session.getTransaction().commit();
        session.close();
        return student;
    } catch (StudentDaoException | SectionException e) {
        session.close();
        throw new StudentException(e.getMessage());
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) StudentDaoException(com.remswork.project.alice.dao.exception.StudentDaoException) SectionException(com.remswork.project.alice.exception.SectionException) Session(org.hibernate.Session)

Aggregations

StudentException (com.remswork.project.alice.exception.StudentException)27 Message (com.remswork.project.alice.model.support.Message)17 Student (com.remswork.project.alice.model.Student)15 Section (com.remswork.project.alice.model.Section)8 AsyncTask (android.os.AsyncTask)6 Gson (com.google.gson.Gson)6 StudentDaoException (com.remswork.project.alice.dao.exception.StudentDaoException)6 StudentServiceException (com.remswork.project.alice.web.service.exception.StudentServiceException)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 HttpURLConnection (java.net.HttpURLConnection)6 URL (java.net.URL)6 ExecutionException (java.util.concurrent.ExecutionException)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 SectionException (com.remswork.project.alice.exception.SectionException)5 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)5 SectionResourceLinks (com.remswork.project.alice.resource.links.SectionResourceLinks)5