Search in sources :

Example 16 with SectionException

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

the class SectionServiceImpl method getSectionById.

@Override
public Section getSectionById(final long id) throws SectionException {
    try {
        return new AsyncTask<String, Section, Section>() {

            @Override
            protected Section 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, Section.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 : Section");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new SectionException("Server Error");
                } catch (SectionException 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) InputStream(java.io.InputStream) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) IOException(java.io.IOException) SectionException(com.remswork.project.alice.exception.SectionException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL)

Example 17 with SectionException

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

the class StudentController method addStudent.

@RequestMapping(value = "add", method = RequestMethod.POST)
public String addStudent(@RequestParam("studentNumber") long studentNumber, @RequestParam("firstName") String firstName, @RequestParam("middleName") String middleName, @RequestParam("lastName") String lastName, @RequestParam("gender") String gender, @RequestParam("age") int age, @RequestParam("sectionId") long sectionId, ModelMap modelMap) {
    try {
        Student student = new Student(studentNumber, firstName, lastName, middleName, gender, age, "");
        studentService.addStudent(student, sectionId);
        List<Student> studentList = studentService.getStudentList();
        List<Section> sectionList = sectionService.getSectionList();
        modelMap.put("studentList", studentList);
        modelMap.put("sectionList", sectionList);
        return "student";
    } catch (StudentException | SectionException e) {
        e.printStackTrace();
        return "error";
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) Student(com.remswork.project.alice.model.Student) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with SectionException

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

the class StudentController method getStudent.

@RequestMapping(value = "get", method = RequestMethod.POST)
public String getStudent(@RequestParam("query") String query, ModelMap modelMap) {
    try {
        List<Section> sectionList = sectionService.getSectionList();
        List<Student> studentList = new ArrayList<>();
        Student student = null;
        if (!query.trim().isEmpty()) {
            try {
                long id = Long.parseLong(query.trim());
                student = studentService.getStudentById(id);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                student = null;
            } catch (StudentException e) {
                e.printStackTrace();
                student = null;
            }
            if (student != null)
                studentList.add(student);
            else {
                for (Student s : studentService.getStudentList()) {
                    if (s.getFirstName().equals(query.trim())) {
                        studentList.add(s);
                        continue;
                    }
                    if (s.getMiddleName().equals(query.trim())) {
                        studentList.add(s);
                        continue;
                    }
                    if (s.getLastName().equals(query.trim())) {
                        studentList.add(s);
                        continue;
                    }
                    try {
                        if (s.getStudentNumber() == Integer.parseInt(query)) {
                            studentList.add(s);
                            continue;
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (studentList.size() < 1) {
            studentList = studentService.getStudentList();
            modelMap.put("responseMessage", "No result found.");
        } else {
            modelMap.put("responseMessage", studentList.size() + " Result found.");
        }
        modelMap.put("studentList", studentList);
        modelMap.put("sectionList", sectionList);
        return "student-table";
    } catch (StudentException | SectionException e) {
        e.printStackTrace();
        return "error";
    }
}
Also used : StudentException(com.remswork.project.alice.exception.StudentException) ArrayList(java.util.ArrayList) Student(com.remswork.project.alice.model.Student) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with SectionException

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

the class SectionServiceImpl method updateSectionById.

@Override
public Section updateSectionById(long id, Section newSection, long departmentId) throws SectionException {
    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(newSection));
        if (response.getStatus() == 200) {
            return (Section) response.readEntity(Section.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new SectionServiceException(message.getMessage());
        } else
            throw new SectionServiceException("The request might invalid or server is down");
    } catch (SectionServiceException e) {
        throw new SectionException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) Message(com.remswork.project.alice.model.support.Message) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) SectionServiceException(com.remswork.project.alice.web.service.exception.SectionServiceException)

Example 20 with SectionException

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

the class SectionServiceImpl method addSection.

@Override
public Section addSection(Section Section, long departmentId) throws SectionException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Builder builder = target.queryParam("departmentId", departmentId).request();
        builder.accept("application/json");
        Response response = builder.post(Entity.json(Section));
        if (response.getStatus() == 201) {
            return (Section) response.readEntity(Section.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new SectionServiceException(message.getMessage());
        } else
            throw new SectionServiceException("The request might invalid or server is down");
    } catch (SectionServiceException e) {
        throw new SectionException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) Message(com.remswork.project.alice.model.support.Message) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) SectionServiceException(com.remswork.project.alice.web.service.exception.SectionServiceException)

Aggregations

SectionException (com.remswork.project.alice.exception.SectionException)28 Section (com.remswork.project.alice.model.Section)20 Message (com.remswork.project.alice.model.support.Message)15 Session (org.hibernate.Session)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 AsyncTask (android.os.AsyncTask)5 Gson (com.google.gson.Gson)5 SectionDaoException (com.remswork.project.alice.dao.exception.SectionDaoException)5 DepartmentException (com.remswork.project.alice.exception.DepartmentException)5 StudentException (com.remswork.project.alice.exception.StudentException)5 Department (com.remswork.project.alice.model.Department)5 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)5 SectionResourceLinks (com.remswork.project.alice.resource.links.SectionResourceLinks)5 SectionServiceException (com.remswork.project.alice.web.service.exception.SectionServiceException)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